From 6570d5cbe21af37a147cb966c12e45801230b2a9 Mon Sep 17 00:00:00 2001 From: FabienSalles Date: Fri, 8 Nov 2019 12:00:48 +0100 Subject: [PATCH 1/2] Fix error when we use VO for the marking property Fix Illegal offset type when we use ValueObject instead of string for the marking property #28203 #22031 --- .../MarkingStore/MethodMarkingStore.php | 2 +- .../MarkingStore/MethodMarkingStoreTest.php | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php b/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php index a2341aadbd..3835765265 100644 --- a/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php +++ b/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php @@ -61,7 +61,7 @@ final class MethodMarkingStore implements MarkingStoreInterface } if ($this->singleState) { - $marking = [$marking => 1]; + $marking = [(string) $marking => 1]; } return new Marking($marking); diff --git a/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php b/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php index 7b5c7ffa91..7393faa825 100644 --- a/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php +++ b/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php @@ -52,4 +52,35 @@ class MethodMarkingStoreTest extends TestCase $this->assertEquals($marking, $marking2); } + + public function testGetMarkingWithValueObject() + { + $subject = new Subject($this->createValueObject('first_place')); + + $markingStore = new MethodMarkingStore(true); + + $marking = $markingStore->getMarking($subject); + + $this->assertInstanceOf(Marking::class, $marking); + $this->assertCount(1, $marking->getPlaces()); + $this->assertSame('first_place', (string) $subject->getMarking()); + } + + private function createValueObject(string $markingValue) + { + return new class($markingValue) { + /** @var string */ + private $markingValue; + + public function __construct(string $markingValue) + { + $this->markingValue = $markingValue; + } + + public function __toString() + { + return $this->markingValue; + } + }; + } } From dad4344793ab59eb718e5c896dd359fbe2b343fe Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 8 Nov 2019 17:10:53 +0100 Subject: [PATCH 2/2] [DI] fix locators with numeric keys --- .../DependencyInjection/Compiler/ServiceLocatorTagPass.php | 7 ++++++- .../Tests/Compiler/ServiceLocatorTagPassTest.php | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ServiceLocatorTagPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ServiceLocatorTagPass.php index 0d77d7e483..a7427c5a5b 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ServiceLocatorTagPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ServiceLocatorTagPass.php @@ -41,6 +41,8 @@ final class ServiceLocatorTagPass extends AbstractRecursivePass throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set.', $this->currentId)); } + $i = 0; + foreach ($arguments[0] as $k => $v) { if ($v instanceof ServiceClosureArgument) { continue; @@ -49,10 +51,13 @@ final class ServiceLocatorTagPass extends AbstractRecursivePass throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set, "%s" found for key "%s".', $this->currentId, \is_object($v) ? \get_class($v) : \gettype($v), $k)); } - if (\is_int($k)) { + if ($i === $k) { unset($arguments[0][$k]); $k = (string) $v; + ++$i; + } elseif (\is_int($k)) { + $i = null; } $arguments[0][$k] = new ServiceClosureArgument($v); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php index 1de02d2577..66af69b543 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php @@ -114,6 +114,7 @@ class ServiceLocatorTagPassTest extends TestCase ->setArguments([[ 'bar' => new Reference('baz'), new Reference('bar'), + 16 => new Reference('baz'), ]]) ->addTag('container.service_locator') ; @@ -124,6 +125,7 @@ class ServiceLocatorTagPassTest extends TestCase $locator = $container->get('foo'); $this->assertSame(TestDefinition1::class, \get_class($locator('bar'))); + $this->assertSame(TestDefinition2::class, \get_class($locator(16))); } public function testBindingsAreCopied()