Skip to content

Commit

Permalink
fix bug where factory build methods weren't having setters applied
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Nelson committed Jun 2, 2020
1 parent cfec396 commit 9b60e63
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public function populateContainer(Container $container, CompiledConfig $compiled
$arguments = $this->resolveArray($container, $definition["arguments"] ?? []);

if ($isFactoryCreated) {
return call_user_func_array(
$service = call_user_func_array(
[
$definition["factoryClass"] ?? $container->offsetGet(mb_substr($definition["factoryService"], 1)),
$definition["factoryMethod"]
],
$arguments
);
} else {
$service = (new \ReflectionClass($definition["class"]))->newInstanceArgs($arguments);
}

$service = (new \ReflectionClass($definition["class"]))->newInstanceArgs($arguments);

foreach ($definition["calls"] ?? [] as $call) {
call_user_func_array(
[$service, $call["method"]],
Expand Down
1 change: 0 additions & 1 deletion src/FileConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class FileConfig
"factoryClass" => 1,
"factoryMethod" => 1,
"factoryService" => 1,
"factoryArguments" => 1,
"aliasOf" => 1,
"abstract" => 1,
"calls" => 1,
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/FactoryClass/ExampleClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ExampleClass
{
protected $serviceExample;
protected $tagCollection;
protected $customParameter;

public function __construct(ServiceExample $serviceExample, iterable $tagCollection)
{
Expand All @@ -24,4 +25,14 @@ public function getTagCollection()
{
return $this->tagCollection;
}

public function setCustomParameter(string $customParameter)
{
$this->customParameter = $customParameter;
}

public function getCustomParameter() : ?string
{
return $this->customParameter;
}
}
2 changes: 2 additions & 0 deletions tests/integration/FactoryClass/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function testFactoryService()
$exampleClass = $container->offsetGet("example.class");
self::assertInstanceOf(ExampleClass::class, $exampleClass);
self::assertInstanceOf(ServiceExample::class, $exampleClass->getServiceExample());
self::assertSame("custom_value", $exampleClass->getCustomParameter());
foreach ($exampleClass->getTagCollection() as $tag) {
self::assertInstanceOf(TestTagInterface::class, $tag);
}
Expand All @@ -41,6 +42,7 @@ public function testFactoryClass()
$exampleClass = $container->offsetGet("example.class.2");
self::assertInstanceOf(ExampleClass::class, $exampleClass);
self::assertInstanceOf(ServiceExample::class, $exampleClass->getServiceExample());
self::assertSame("custom_value", $exampleClass->getCustomParameter());
foreach ($exampleClass->getTagCollection() as $tag) {
self::assertInstanceOf(TestTagInterface::class, $tag);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/FactoryClass/file1.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
parameters:
my_custom_argument: "custom_value"

services:
factory.service:
class: Silktide\Syringe\IntegrationTests\FactoryClass\ExampleFactoryService
Expand All @@ -9,6 +12,10 @@ services:
class: Silktide\Syringe\IntegrationTests\FactoryClass\ExampleClass
factoryService: "@factory.service"
factoryMethod: "create"
calls:
- method: setCustomParameter
arguments:
- "%my_custom_argument%"

service.example:
class: Silktide\Syringe\IntegrationTests\FactoryClass\ServiceExample
Expand All @@ -20,6 +27,10 @@ services:
arguments:
- "@service.example"
- "#tagtag"
calls:
- method: setCustomParameter
arguments:
- "%my_custom_argument%"

TagClass1:
class: Silktide\Syringe\IntegrationTests\Examples\TagClass1
Expand Down

0 comments on commit 9b60e63

Please sign in to comment.