Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: callbacks on states and transitions #125

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
language: php
sudo: false
# disabled old php build (http://php.net/eol.php)
php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7
- hhvm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Finite\Bundle\FiniteBundle\DependencyInjection\Compiler;

use Finite\Event\Callback\Callback;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -24,14 +25,14 @@ public function process(ContainerBuilder $container)
$definition = $container->getDefinition($id);
$config = $definition->getArgument(0);
if (isset($config['callbacks'])) {
foreach (array('before', 'after') as $position) {
foreach (array(Callback::CLAUSE_BEFORE, Callback::CLAUSE_AFTER) as $position) {
foreach ($config['callbacks'][$position] as &$callback) {
if (
is_array($callback['do'])
&& 0 === strpos($callback['do'][0], '@')
&& $container->hasDefinition(substr($callback['do'][0], 1))
is_array($callback[Callback::CLAUSE_DO])
&& 0 === strpos($callback[Callback::CLAUSE_DO][0], '@')
&& $container->hasDefinition(substr($callback[Callback::CLAUSE_DO][0], 1))
) {
$callback['do'][0] = new Reference(substr($callback['do'][0], 1));
$callback[Callback::CLAUSE_DO][0] = new Reference(substr($callback[Callback::CLAUSE_DO][0], 1));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Finite\Bundle\FiniteBundle\DependencyInjection;

use Finite\Event\Callback\Callback;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand Down Expand Up @@ -86,8 +87,8 @@ protected function addTransitionSection(NodeBuilder $rootProto)
protected function addCallbackSection(NodeBuilder $rootProto)
{
$callbacks = $rootProto->arrayNode('callbacks')->children();
$this->addSubCallbackSection($callbacks, 'before');
$this->addSubCallbackSection($callbacks, 'after');
$this->addSubCallbackSection($callbacks, Callback::CLAUSE_BEFORE);
$this->addSubCallbackSection($callbacks, Callback::CLAUSE_AFTER);
$callbacks->end()->end();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Finite\Bundle\FiniteBundle\DependencyInjection;

use Finite\Event\Callback\Callback;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
Expand Down Expand Up @@ -68,7 +69,7 @@ protected function removeDisabledCallbacks(array $config)
return $config;
}

foreach (array('before', 'after') as $position) {
foreach (array(Callback::CLAUSE_BEFORE, Callback::CLAUSE_AFTER) as $position) {
foreach ($config['callbacks'][$position] as $i => $callback) {
if ($callback['disabled']) {
unset($config['callbacks'][$position][$i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@
<tag name="twig.extension" />
</service>
</services>

</container>
27 changes: 25 additions & 2 deletions src/Finite/Event/Callback/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@
*/
class Callback implements CallbackInterface
{
const CLAUSE_AFTER = 'after';
const CLAUSE_BEFORE = 'before';
const CLAUSE_FROM = 'from';
const CLAUSE_TO = 'to';
const CLAUSE_ON = 'on';
const CLAUSE_DO = 'do';

/**
* @var CallbackSpecificationInterface
*/
private $specification;

/**
* @var callable
* @var array callable
*/
private $callable;

/**
* @param CallbackSpecificationInterface $callbackSpecification
* @param callable $callable
* @param $callable
*/
public function __construct(CallbackSpecificationInterface $callbackSpecification, $callable)
{
Expand All @@ -37,6 +44,22 @@ public function getSpecification()
return $this->specification;
}

/**
* @return array callable
*/
public function getCallbacks()
{
return $this->callable;
}

/**
* @return array
*/
public function getClauses()
{
return $this->specification->getClauses();
}

/**
* {@inheritdoc}
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Finite/Event/Callback/CallbackInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,19 @@ interface CallbackInterface
* @param TransitionEvent $event
*/
public function __invoke(TransitionEvent $event);

/**
* @return CallbackSpecificationInterface
*/
public function getSpecification();

/**
* @return array callable
*/
public function getCallbacks();

/**
* @return array
*/
public function getClauses();
}
16 changes: 12 additions & 4 deletions src/Finite/Event/Callback/CallbackSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(StateMachineInterface $sm, array $from, array $to, a
$isExclusion = function ($str) { return 0 === strpos($str, '-'); };
$removeDash = function ($str) { return substr($str, 1); };

foreach (array('from', 'to', 'on') as $clause) {
foreach (array(Callback::CLAUSE_FROM, Callback::CLAUSE_TO, Callback::CLAUSE_ON) as $clause) {
$excludedClause = 'excluded_'.$clause;

$this->specs[$excludedClause] = array_filter(${$clause}, $isExclusion);
Expand All @@ -58,9 +58,17 @@ public function isSatisfiedBy(TransitionEvent $event)
{
return
$event->getStateMachine() === $this->stateMachine &&
$this->supportsClause('from', $event->getInitialState()->getName()) &&
$this->supportsClause('to', $event->getTransition()->getState()) &&
$this->supportsClause('on', $event->getTransition()->getName());
$this->supportsClause(Callback::CLAUSE_FROM, $event->getInitialState()->getName()) &&
$this->supportsClause(Callback::CLAUSE_TO, $event->getTransition()->getState()) &&
$this->supportsClause(Callback::CLAUSE_ON, $event->getTransition()->getName());
}

/**
* @return array
*/
public function getClauses()
{
return $this->specs;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Finite/Event/Callback/CallbackSpecificationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ interface CallbackSpecificationInterface
* @return bool
*/
public function isSatisfiedBy(TransitionEvent $event);

/**
* @return array
*/
public function getClauses();
}
20 changes: 10 additions & 10 deletions src/Finite/Event/CallbackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ public function __construct(EventDispatcherInterface $dispatcher)
$this->specResolver = new OptionsResolver();
$this->specResolver->setDefaults(
array(
'on' => self::ALL,
'from' => self::ALL,
'to' => self::ALL,
Callback::CLAUSE_ON => self::ALL,
Callback::CLAUSE_FROM => self::ALL,
Callback::CLAUSE_TO => self::ALL,
)
);

$this->specResolver->setAllowedTypes('on', array('string', 'array'));
$this->specResolver->setAllowedTypes('from', array('string', 'array'));
$this->specResolver->setAllowedTypes('to', array('string', 'array'));
$this->specResolver->setAllowedTypes(Callback::CLAUSE_ON, array('string', 'array'));
$this->specResolver->setAllowedTypes(Callback::CLAUSE_FROM, array('string', 'array'));
$this->specResolver->setAllowedTypes(Callback::CLAUSE_TO, array('string', 'array'));

$toArrayNormalizer = function (Options $options, $value) {
return (array) $value;
};

$this->specResolver->setNormalizer('on', $toArrayNormalizer);
$this->specResolver->setNormalizer('from', $toArrayNormalizer);
$this->specResolver->setNormalizer('to', $toArrayNormalizer);
$this->specResolver->setNormalizer(Callback::CLAUSE_ON, $toArrayNormalizer);
$this->specResolver->setNormalizer(Callback::CLAUSE_FROM, $toArrayNormalizer);
$this->specResolver->setNormalizer(Callback::CLAUSE_TO, $toArrayNormalizer);
}

/**
Expand Down Expand Up @@ -109,7 +109,7 @@ protected function add($smOrCallback, $event, $callable = null, array $specs = a
);

$specs = $this->specResolver->resolve($specs);
$callback = CallbackBuilder::create($smOrCallback, $specs['from'], $specs['to'], $specs['on'], $callable)->getCallback();
$callback = CallbackBuilder::create($smOrCallback, $specs[Callback::CLAUSE_FROM], $specs[Callback::CLAUSE_TO], $specs[Callback::CLAUSE_ON], $callable)->getCallback();

$this->dispatcher->addListener($event, $callback);

Expand Down
Loading