Skip to content

Commit 4a3032f

Browse files
committed
[Docs] Inject factories
1 parent 91a5e74 commit 4a3032f

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"require": {
2828
"php": "^8.0",
2929
"babdev/pagerfanta-bundle": "^3.7 || ^4.0",
30+
"doctrine/annotations": "^2.0",
3031
"doctrine/collections": "^1.8 || ^2.0",
3132
"doctrine/doctrine-bundle": "^2.0",
3233
"doctrine/event-manager": "^1.1",

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)