Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
<?php
/*
* This file is part of the StateMachine package.
*
* (c) Alexandre Bacco
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SM\Factory;
use SM\SMException;
use SM\StateMachine\StateMachineInterface;
abstract class AbstractFactory implements ClearableFactoryInterface
{
/**
* @var array
*/
protected $configs;
/**
* @var array
*/
protected $stateMachines = array();
/**
* @param array $configs Array of configs for the available state machines
*/
public function __construct(array $configs)
{
foreach ($configs as $graph => $config) {
$this->addConfig($config, $graph);
}
}
/**
* {@inheritDoc}
*/
public function get($object, $graph = 'default')
{
$hash = spl_object_hash($object);
if (isset($this->stateMachines[$hash][$graph])) {
return $this->stateMachines[$hash][$graph];
}
foreach ($this->configs as $config) {
if ($config['graph'] === $graph && $object instanceof $config['class']) {
return $this->stateMachines[$hash][$graph] = $this->createStateMachine($object, $config);
}
}
throw new SMException(sprintf(
'Cannot create a state machine because the configuration for object "%s" with graph "%s" does not exist.',
get_class($object),
$graph
));
}
/**
* {@inheritDoc}
*/
public function clear()
{
$this->stateMachines = array();
}
/**
* Adds a new config
*
* @param array $config
* @param string $graph
*
* @throws SMException If the index "class" is not configured
*/
public function addConfig(array $config, $graph = 'default')
{
if (!isset($config['graph'])) {
$config['graph'] = $graph;
}
if (!isset($config['class'])) {
throw new SMException(sprintf(
'Index "class" needed for the state machine configuration of graph "%s"',
$config['graph']
));
}
$this->configs[] = $config;
}
/**
* Create a state machine for the given object and config
*
* @param $object
* @param array $config
*
* @return StateMachineInterface
*/
abstract protected function createStateMachine($object, array $config);
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* This file is part of the StateMachine package.
*
* (c) Alexandre Bacco
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SM\Factory;
interface ClearableFactoryInterface extends FactoryInterface
{
/**
* Clears all state machines from the factory
*/
public function clear();
}

View File

@@ -0,0 +1,59 @@
<?php
/*
* This file is part of the StateMachine package.
*
* (c) Alexandre Bacco
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SM\Factory;
use SM\Callback\CallbackFactoryInterface;
use SM\SMException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class Factory extends AbstractFactory
{
/**
* @var EventDispatcherInterface
*/
protected $dispatcher;
/**
* @var CallbackFactoryInterface
*/
protected $callbackFactory;
public function __construct(
array $configs,
EventDispatcherInterface $dispatcher = null,
CallbackFactoryInterface $callbackFactory = null
) {
parent::__construct($configs);
$this->dispatcher = $dispatcher;
$this->callbackFactory = $callbackFactory;
}
/**
* {@inheritDoc}
*/
protected function createStateMachine($object, array $config)
{
if (!isset($config['state_machine_class'])) {
$class = 'SM\\StateMachine\\StateMachine';
} elseif (class_exists($config['state_machine_class'])) {
$class = $config['state_machine_class'];
} else {
throw new SMException(sprintf(
'Class "%s" for creating a new state machine does not exist.',
$config['state_machine_class']
));
}
return new $class($object, $config, $this->dispatcher, $this->callbackFactory);
}
}

View File

@@ -0,0 +1,27 @@
<?php
/*
* This file is part of the StateMachine package.
*
* (c) Alexandre Bacco
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SM\Factory;
use SM\StateMachine\StateMachineInterface;
interface FactoryInterface
{
/**
* Returns the state machine match the couple object/graph
*
* @param object $object
* @param string $graph
*
* @return StateMachineInterface
*/
public function get($object, $graph = 'default');
}