Skip to content

Commit

Permalink
Merge pull request #43 from winzou/sf4
Browse files Browse the repository at this point in the history
Fix sf4 compatibility
  • Loading branch information
winzou authored Aug 6, 2018
2 parents 8e61db2 + e62f37e commit 6f5c1b5
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 69 deletions.
18 changes: 0 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ dist: xenial
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- hhvm
- 7.0
Expand All @@ -22,22 +19,7 @@ env:

matrix:
fast_finish: true
include:
- php: 5.3
dist: precise
allow_failures:
- php: 5.3
dist: xenial
- php: 5.3
env: 'SYMFONY_VERSION=~3.0'
- php: 5.4
env: 'SYMFONY_VERSION=~3.0'
- php: 5.3
env: 'SYMFONY_VERSION=~4.0'
- php: 5.4
env: 'SYMFONY_VERSION=~4.0'
- php: 5.5
env: 'SYMFONY_VERSION=~4.0'
- php: 5.6
env: 'SYMFONY_VERSION=~4.0'
- php: 7.0
Expand Down
21 changes: 14 additions & 7 deletions Callback/ContainerAwareCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ public function __construct(array $specs, $callable, ContainerInterface $contain
public function call(TransitionEvent $event)
{
// Load the services only now (when the callback is actually called)
if (
is_array($this->callable)
&& is_string($this->callable[0])
&& 0 === strpos($this->callable[0], '@')
) {

$serviceId = substr($this->callable[0], 1);
if (is_array($this->callable) && is_string($this->callable[0])) {
if (0 === strpos($this->callable[0], '@')) {
// BC for sf < 4.0, we refer to the service via its '@name'
$serviceId = substr($this->callable[0], 1);
} else {
$serviceId = $this->callable[0];

// We allow static calls so no Exception thrown if callable is not a service
if (!$this->container->has($serviceId)) {
return parent::call($event);
}
}

// Callback services have to be public
$this->callable[0] = $this->container->get($serviceId);
}

Expand Down
16 changes: 11 additions & 5 deletions Command/winzouStateMachineDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

class winzouStateMachineDebugCommand extends ContainerAwareCommand
{
protected static $defaultName = 'debug:winzou:state-machine';

/**
* @var array
*/
Expand All @@ -22,9 +24,10 @@ class winzouStateMachineDebugCommand extends ContainerAwareCommand
*/
public function configure()
{
$this->addArgument('key', InputArgument::REQUIRED, 'A state machine key');

$this->setName('debug:winzou:state-machine');
$this
->setName(self::$defaultName) // BC with 2.7
->addArgument('key', InputArgument::REQUIRED, 'A state machine key')
;
}

/**
Expand All @@ -35,7 +38,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
$this->config = $this->getContainer()->getParameter('sm.configs');

if (empty($this->config)) {
throw new \RuntimeException('The is no state machines configured.');
throw new \RuntimeException('There is no state machine configured.');
}
}

Expand Down Expand Up @@ -82,7 +85,10 @@ public function execute(InputInterface $input, OutputInterface $output)

$this->printStates($config['states'], $output);
$this->printTransitions($config['transitions'], $output);
$this->printCallbacks($config['callbacks'], $output);

if (isset($config['callbacks'])) {
$this->printCallbacks($config['callbacks'], $output);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/winzouStateMachineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function load(array $configs, ContainerBuilder $container)
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('sm.configs', $this->parseConfig($config));

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}

/**
Expand Down
34 changes: 0 additions & 34 deletions Resources/config/services.xml

This file was deleted.

31 changes: 31 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
parameters:
sm.callback.class: "winzou\\Bundle\\StateMachineBundle\\Callback\\ContainerAwareCallback"

services:
_defaults:
autowire: true
autoconfigure: true
public: false
bind:
$configs: "%sm.configs%"

# aliases for interfaces
SM\Factory\FactoryInterface:
alias: "SM\\Factory\\Factory"
SM\Callback\CallbackFactoryInterface:
alias: "winzou\\Bundle\\StateMachineBundle\\Callback\\ContainerAwareCallbackFactory"

# services from the lib
SM\Callback\CascadeTransitionCallback:
public: true
SM\Extension\Twig\SMExtension:
SM\Factory\Factory:

# services from the bundle itself
winzou\Bundle\StateMachineBundle\Callback\ContainerAwareCallbackFactory:
arguments:
$class: "%sm.callback.class%"

winzou\Bundle\StateMachineBundle\Command\:
resource: "../../Command/*"
tags: ["console.command"]
5 changes: 5 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"bundles": {
"winzou\\Bundle\\StateMachineBundle\\winzouStateMachineBundle": ["all"]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,41 @@ function it_converts_servicename_to_class($container, TransitionEvent $event, Co
{
$this->beConstructedWith(array(), array('@my_service', 'dummy'), $container);

$container->has('my_service')->willReturn(true);
$container->get('my_service')->shouldBeCalled()->willReturn($service);

$service->dummy($event)->shouldBeCalled()->willReturn(true);

$this->call($event)->shouldReturn(true);
}

function it_throws_an_exception_when_relevant($container, TransitionEvent $event, ContainerAwareCallbackSpec $service)
function it_converts_classname_to_class($container, TransitionEvent $event, ContainerAwareCallbackSpec $service)
{
$this->beConstructedWith(array(), array('Existing\\Service', 'dummy'), $container);

$container->has('Existing\\Service')->willReturn(true);
$container->get('Existing\\Service')->shouldBeCalled()->willReturn($service);

$service->dummy($event)->shouldBeCalled()->willReturn(true);

$this->call($event)->shouldReturn(true);
}

function it_allows_static_calls($container, TransitionEvent $event)
{
$this->beConstructedWith(array(), array('spec\\winzou\\Bundle\\StateMachineBundle\\Callback\\ContainerAwareCallbackSpec', 'dummyStatic'), $container);

$container->has('spec\\winzou\\Bundle\\StateMachineBundle\\Callback\\ContainerAwareCallbackSpec')->willReturn(false);

$this->call($event)->shouldReturn(2);
}

function it_throws_an_exception_when_relevant($container, TransitionEvent $event)
{
$this->beConstructedWith(array(), array('@my_service', 'dummy'), $container);

$container->get('my_service')->shouldBeCalled()->willThrow('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException');
$container->has('my_service')->willReturn(false);
$container->get('my_service')->willThrow('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException');

$this->shouldThrow('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException')->during('call', array($event));
}
Expand All @@ -45,6 +68,11 @@ function dummy()
return true;
}

static function dummyStatic()
{
return 2;
}

function it_does_not_convert_object($container, TransitionEvent $event, ContainerAwareCallbackSpec $service)
{
$this->beConstructedWith(array(), array($service, 'dummy'), $container);
Expand Down
1 change: 0 additions & 1 deletion winzouStateMachineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace winzou\Bundle\StateMachineBundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class winzouStateMachineBundle extends Bundle
Expand Down

0 comments on commit 6f5c1b5

Please sign in to comment.