Skip to content

Commit d0fde2c

Browse files
authored
Merge pull request #36 from judgedim/master
Support for synthetic services
2 parents 5587cfc + 6daa6ba commit d0fde2c

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,27 @@ services:
488488
app.phpmailer:
489489
class: AppBundle\Mail\PhpMailer
490490
shared: false
491+
``````
492+
493+
#### Synthetic services
494+
495+
Services can be injected [at runtime](http://symfony.com/doc/current/service_container/synthetic_services.html). You can inject a class instance as service, instead of configuring the container to create a new instance using the `synthetic()` function helper:
496+
497+
```php
498+
return [
499+
'app.phpmailer' => create(PhpMailer::class)
500+
->synthetic(),
501+
];
502+
```
503+
504+
This is the same as:
505+
506+
```yaml
507+
508+
services:
509+
app.phpmailer:
510+
class: AppBundle\Mail\PhpMailer
511+
synthetic: true
491512
```
492513

493514
## Factories

src/DefinitionHelper/CreateDefinitionHelper.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ public function unshared() : self
127127
return $this;
128128
}
129129

130+
/**
131+
* Marks the definition as synthetic
132+
*/
133+
public function synthetic() : self
134+
{
135+
$this->definition->setSynthetic(true);
136+
137+
return $this;
138+
}
139+
130140
/**
131141
* Marks the definition as private
132142
*/

src/DefinitionHelper/FactoryDefinitionHelper.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ public function arguments(...$arguments) : self
5050

5151
return $this;
5252
}
53+
54+
/**
55+
* Marks the definition as synthetic
56+
*/
57+
public function synthetic() : self
58+
{
59+
$this->definition->setSynthetic(true);
60+
61+
return $this;
62+
}
5363
}

tests/CreateTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,19 @@ public function services_can_be_marked_as_unshared()
225225
self::assertFalse($container->get('bar') === $container->get('bar'));
226226
}
227227

228+
/** @test */
229+
public function services_can_be_marked_as_synthetic()
230+
{
231+
$container = $this->createContainerWithConfig([
232+
'bar' => create()
233+
->synthetic()
234+
]);
235+
236+
$foo = new \stdClass;
237+
$container->set('bar', $foo);
238+
self::assertInstanceOf('stdClass', $container->get('bar' ));
239+
}
240+
228241
/** @test */
229242
public function services_can_be_deprecated()
230243
{

tests/FactoryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ public function parameters_can_be_injected_in_arguments()
6262
self::assertEquals('def', $container->get('foo')->arg1);
6363
}
6464

65+
/** @test */
66+
public function services_can_be_marked_as_synthetic()
67+
{
68+
$container = $this->createContainerWithConfig([
69+
'bar' => factory([self::class, 'foo'])
70+
->synthetic()
71+
]);
72+
$foo = new class() {
73+
public $foo;
74+
};
75+
$container->set('bar', $foo);
76+
self::assertTrue(is_object($container->get('bar' )));
77+
self::assertNotInstanceOf('stdClass', $container->get('bar' ));
78+
}
79+
6580
public static function foo()
6681
{
6782
return new \stdClass();

0 commit comments

Comments
 (0)