Skip to content

Commit 5c5f058

Browse files
authored
minor #853 [Docs] Inject factories (autowiring) (loic425)
This PR was merged into the 1.11 branch. Discussion ---------- | Q | A | --------------- | ----- | Bug fix? | no | New feature? | yes (on docs) | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT Commits ------- 4a3032f [Docs] Inject factories
2 parents 86ab9e3 + 4a3032f commit 5c5f058

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

docs/resource_factories.md

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
Resource factories are used on Create operations to instantiate your resource.
44

55
<!-- TOC -->
6-
* [Default factory for you resource](#default-factory-for-your-resource)
7-
* [Define your custom factory](#define-your-custom-factory)
8-
* [Use your custom method](#use-your-custom-method)
9-
* [Pass arguments to your method](#pass-arguments-to-your-method)
10-
* [Use a factory without declaring it](#use-a-factory-without-declaring-it)
11-
* [Use a callable for your custom factory](#use-a-callable-for-your-custom-factory)
6+
* [Resource factories](#resource-factories)
7+
* [Default factory for your resource](#default-factory-for-your-resource)
8+
* [Inject the factory in your service](#inject-the-factory-in-your-service)
9+
* [Define your custom factory](#define-your-custom-factory)
10+
* [Use your custom method](#use-your-custom-method)
11+
* [Pass arguments to your method](#pass-arguments-to-your-method)
12+
* [Use a factory without declaring it](#use-a-factory-without-declaring-it-)
13+
* [Use a callable for your custom factory](#use-a-callable-for-your-custom-factory)
1214
<!-- TOC -->
1315

1416

@@ -18,6 +20,31 @@ By default, a resource factory is defined to your resource `Sylius\Component\Res
1820

1921
It has a `createNew` method with no arguments.
2022

23+
## Inject the factory in your service
24+
25+
If you are using Symfony autowiring, you can inject the resource factory using the right variable name.
26+
27+
```php
28+
namespace App;
29+
30+
use Sylius\Resource\Factory\FactoryInterface;
31+
32+
final class MyService
33+
{
34+
public function __construct(
35+
private FactoryInterface $bookFactory,
36+
) {}
37+
}
38+
```
39+
40+
In this example, the `app.factory.book` will be injected in your `$bookFactory`
41+
42+
You can find the variable name using this debug command:
43+
44+
```shell
45+
$ bin/console debug:autowiring app.factory.book
46+
```
47+
2148
## Define your custom factory
2249

2350
```php

0 commit comments

Comments
 (0)