-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Workflow] Introduce concept of SupportStrategyInterface #20751
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
Changes from all commits
6421c10
bfd3611
c0cbc85
fa448af
19fb676
faddc97
a115bdd
d22fe01
047a2e7
a34e4af
1e65b12
f4e0d62
2d11004
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,7 +274,6 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode) | |
->end() | ||
->end() | ||
->arrayNode('supports') | ||
->isRequired() | ||
->beforeNormalization() | ||
->ifString() | ||
->then(function ($v) { return array($v); }) | ||
|
@@ -287,6 +286,9 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode) | |
->end() | ||
->end() | ||
->end() | ||
->scalarNode('support_strategy') | ||
->cannotBeEmpty() | ||
->end() | ||
->scalarNode('initial_place')->defaultNull()->end() | ||
->arrayNode('places') | ||
->isRequired() | ||
|
@@ -325,6 +327,10 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode) | |
->end() | ||
->end() | ||
->end() | ||
->validate() | ||
->ifTrue(function ($v) { return isset($v['supports']) && isset($v['support_strategy']); }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
->thenInvalid('"supports" and "support_strategy" cannot be used together.') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we also need to make sure that one of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have not seen other examples for that yet, but of course I can add a second validation for both being !isset()? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. something like |
||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; | ||
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer; | ||
use Symfony\Component\Workflow; | ||
use Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
/** | ||
|
@@ -458,8 +459,14 @@ private function registerWorkflowConfiguration(array $workflows, ContainerBuilde | |
$container->setDefinition(sprintf('%s.definition', $workflowId), $definitionDefinition); | ||
|
||
// Add workflow to Registry | ||
foreach ($workflow['supports'] as $supportedClass) { | ||
$registryDefinition->addMethodCall('add', array(new Reference($workflowId), $supportedClass)); | ||
if (isset($workflow['supports'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is always set here. You should check for the array not being empty There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. having a test covering this combination would probably be good |
||
foreach ($workflow['supports'] as $supportedClassName) { | ||
$strategyDefinition = new Definition(ClassInstanceSupportStrategy::class, array($supportedClassName)); | ||
$strategyDefinition->setPublic(false); | ||
$registryDefinition->addMethodCall('add', array(new Reference($workflowId), $strategyDefinition)); | ||
} | ||
} elseif (isset($workflow['support_strategy'])) { | ||
$registryDefinition->addMethodCall('add', array(new Reference($workflowId), new Reference($workflow['support_strategy']))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a second option, "support_strategy" expects to be a reference to a registered service implementing the introduced SupportStrategyInterface. |
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,6 +170,10 @@ public function testWorkflows() | |
$markingStoreRef = $serviceMarkingStoreWorkflowDefinition->getArgument(1); | ||
$this->assertInstanceOf(Reference::class, $markingStoreRef); | ||
$this->assertEquals('workflow_service', (string) $markingStoreRef); | ||
|
||
$this->assertTrue($container->hasDefinition('workflow.registry', 'Workflow registry is registered as a service')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Until now, there was not coverage for registry at all. I did not want to introduce too much, but the following should we ok? |
||
$registryDefinition = $container->getDefinition('workflow.registry'); | ||
$this->assertGreaterThan(0, count($registryDefinition->getMethodCalls())); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\Workflow; | ||
|
||
use Symfony\Component\Workflow\Exception\InvalidArgumentException; | ||
use Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface; | ||
|
||
/** | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
|
@@ -22,12 +23,16 @@ class Registry | |
private $workflows = array(); | ||
|
||
/** | ||
* @param Workflow $workflow | ||
* @param string $className | ||
* @param Workflow $workflow | ||
* @param string|SupportStrategyInterface $supportStrategy | ||
*/ | ||
public function add(Workflow $workflow, $className) | ||
public function add(Workflow $workflow, $supportStrategy) | ||
{ | ||
$this->workflows[] = array($workflow, $className); | ||
if (!$supportStrategy instanceof SupportStrategyInterface) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in previous comments, to avoid BC break we are allowing string as a fallback, checking it and otherwise throw deprecation warning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No BC break anymore after doing suggested changes thanks to @iltar Though it seems triggering the deprecated error leads to CI integrations to fail... Any ideas how to fix this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks to @xabbuh I figured out the |
||
@trigger_error('Support of class name string was deprecated after version 3.2 and won\'t work anymore in 4.0.', E_USER_DEPRECATED); | ||
} | ||
|
||
$this->workflows[] = array($workflow, $supportStrategy); | ||
} | ||
|
||
/** | ||
|
@@ -40,8 +45,8 @@ public function get($subject, $workflowName = null) | |
{ | ||
$matched = null; | ||
|
||
foreach ($this->workflows as list($workflow, $className)) { | ||
if ($this->supports($workflow, $className, $subject, $workflowName)) { | ||
foreach ($this->workflows as list($workflow, $supportStrategy)) { | ||
if ($this->supports($workflow, $supportStrategy, $subject, $workflowName)) { | ||
if ($matched) { | ||
throw new InvalidArgumentException('At least two workflows match this subject. Set a different name on each and use the second (name) argument of this method.'); | ||
} | ||
|
@@ -56,16 +61,19 @@ public function get($subject, $workflowName = null) | |
return $matched; | ||
} | ||
|
||
private function supports(Workflow $workflow, $className, $subject, $name) | ||
private function supports(Workflow $workflow, $supportStrategy, $subject, $workflowName) | ||
{ | ||
if (!$subject instanceof $className) { | ||
if (is_string($supportStrategy) && !$subject instanceof $supportStrategy) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a comment before this line:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than dealing with BC here, I would convert the string to a SupportStrategy when setting it instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So @stof , you mean that inside the add() method I would create a ClassInstanceSupportStrategy in the deprecation case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I still add a BC comment somewhere? ping @lyrixx ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The two conditions could then be simplified to: if (!$supportStrategy->supports($workflow, $subject)) {
return false;
} |
||
return false; | ||
} | ||
if ($supportStrategy instanceof SupportStrategyInterface && !$supportStrategy->supports($workflow, $subject)) { | ||
return false; | ||
} | ||
|
||
if (null === $name) { | ||
if (null === $workflowName) { | ||
return true; | ||
} | ||
|
||
return $name === $workflow->getName(); | ||
return $workflowName === $workflow->getName(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All this method could be simplified: if (null !== $workflowName && $workflowName !== $workflow->getName()) {
return false;
}
// Keep BC. To be removed in 4.0
if (is_string($supportStrategy)) {
return $subject instanceof $supportStrategy;
}
return $supportStrategy->supports($workflow, $subject); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if @stof 's idea above will be implemented, this can even be more simplified, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed. (Please, ping me when you're ready. Thanks) |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Workflow\SupportStrategy; | ||
|
||
use Symfony\Component\Workflow\Workflow; | ||
|
||
class ClassInstanceSupportStrategy implements SupportStrategyInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should add a docblock and add yourself as an author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same for the interface There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and the class should become final |
||
{ | ||
private $className; | ||
|
||
public function __construct($className) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should add a docblock documenting the argument as a |
||
{ | ||
$this->className = $className; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Workflow $workflow, $subject) | ||
{ | ||
return $subject instanceof $this->className; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Workflow\SupportStrategy; | ||
|
||
use Symfony\Component\Workflow\Workflow; | ||
|
||
interface SupportStrategyInterface | ||
{ | ||
/** | ||
* @param Workflow $workflow | ||
* @param object $subject | ||
* | ||
* @return bool | ||
*/ | ||
public function supports(Workflow $workflow, $subject); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
use Symfony\Component\Workflow\Definition; | ||
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface; | ||
use Symfony\Component\Workflow\Registry; | ||
use Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface; | ||
use Symfony\Component\Workflow\Workflow; | ||
|
||
class RegistryTest extends \PHPUnit_Framework_TestCase | ||
|
@@ -14,13 +15,11 @@ class RegistryTest extends \PHPUnit_Framework_TestCase | |
|
||
protected function setUp() | ||
{ | ||
$workflows = array(); | ||
|
||
$this->registry = new Registry(); | ||
|
||
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), Subject1::class); | ||
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow2'), Subject2::class); | ||
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow3'), Subject2::class); | ||
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), $this->createSupportStrategy(Subject1::class)); | ||
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow2'), $this->createSupportStrategy(Subject2::class)); | ||
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow3'), $this->createSupportStrategy(Subject2::class)); | ||
} | ||
|
||
protected function tearDown() | ||
|
@@ -64,6 +63,40 @@ public function testGetWithNoMatch() | |
$this->assertInstanceOf(Workflow::class, $w1); | ||
$this->assertSame('workflow1', $w1->getName()); | ||
} | ||
|
||
/** | ||
* @group legacy | ||
*/ | ||
public function testGetWithSuccessLegacyStrategy() | ||
{ | ||
$registry = new Registry(); | ||
|
||
$registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), Subject1::class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to #20984 the current syntax should be kept |
||
$registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow2'), Subject2::class); | ||
|
||
$workflow = $registry->get(new Subject1()); | ||
$this->assertInstanceOf(Workflow::class, $workflow); | ||
$this->assertSame('workflow1', $workflow->getName()); | ||
|
||
$workflow = $registry->get(new Subject1(), 'workflow1'); | ||
$this->assertInstanceOf(Workflow::class, $workflow); | ||
$this->assertSame('workflow1', $workflow->getName()); | ||
|
||
$workflow = $registry->get(new Subject2(), 'workflow2'); | ||
$this->assertInstanceOf(Workflow::class, $workflow); | ||
$this->assertSame('workflow2', $workflow->getName()); | ||
} | ||
|
||
private function createSupportStrategy($supportedClassName) | ||
{ | ||
$strategy = $this->getMockBuilder(SupportStrategyInterface::class)->getMock(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to #20984 the current syntax should be kept |
||
$strategy->expects($this->any())->method('supports') | ||
->will($this->returnCallback(function ($workflow, $subject) use ($supportedClassName) { | ||
return $subject instanceof $supportedClassName; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you use a mock here when you can use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't know how this is handled in Symfony environment, but as I want to test the registry alone I personally prefer mocking all other collaborators. Should I change it? |
||
})); | ||
|
||
return $strategy; | ||
} | ||
} | ||
|
||
class Subject1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Workflow\Tests\SupportStrategy; | ||
|
||
use Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy; | ||
use Symfony\Component\Workflow\Workflow; | ||
|
||
class ClassInstanceSupportStrategyTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testSupportsIfClassInstance() | ||
{ | ||
$strategy = new ClassInstanceSupportStrategy('Symfony\Component\Workflow\Tests\SupportStrategy\Subject1'); | ||
|
||
$this->assertTrue($strategy->supports($this->getWorkflow(), new Subject1())); | ||
} | ||
|
||
public function testSupportsIfNotClassInstance() | ||
{ | ||
$strategy = new ClassInstanceSupportStrategy('Symfony\Component\Workflow\Tests\SupportStrategy\Subject2'); | ||
|
||
$this->assertFalse($strategy->supports($this->getWorkflow(), new Subject1())); | ||
} | ||
|
||
private function getWorkflow() | ||
{ | ||
return $this->getMockBuilder(Workflow::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
} | ||
} | ||
|
||
class Subject1 | ||
{ | ||
} | ||
class Subject2 | ||
{ | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this forbids resetting it to null in a later file to use
supports
againThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So should I simply remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping @stof