|
1 | 1 | DependencyInjection Component |
2 | 2 | ============================= |
3 | 3 |
|
4 | | -DependencyInjection manages your services via a robust and flexible Dependency |
5 | | -Injection Container. |
6 | | - |
7 | | -Here is a simple example that shows how to register services and parameters: |
8 | | - |
9 | | -```php |
10 | | -use Symfony\Component\DependencyInjection\ContainerBuilder; |
11 | | -use Symfony\Component\DependencyInjection\Reference; |
12 | | - |
13 | | -$sc = new ContainerBuilder(); |
14 | | -$sc |
15 | | - ->register('foo', '%foo.class%') |
16 | | - ->addArgument(new Reference('bar')) |
17 | | -; |
18 | | -$sc->setParameter('foo.class', 'Foo'); |
19 | | - |
20 | | -$sc->get('foo'); |
21 | | -``` |
22 | | - |
23 | | -Method Calls (Setter Injection): |
24 | | - |
25 | | -```php |
26 | | -$sc = new ContainerBuilder(); |
27 | | - |
28 | | -$sc |
29 | | - ->register('bar', '%bar.class%') |
30 | | - ->addMethodCall('setFoo', array(new Reference('foo'))) |
31 | | -; |
32 | | -$sc->setParameter('bar.class', 'Bar'); |
33 | | - |
34 | | -$sc->get('bar'); |
35 | | -``` |
36 | | - |
37 | | -Factory Class: |
38 | | - |
39 | | -If your service is retrieved by calling a static method: |
40 | | - |
41 | | -```php |
42 | | -$sc = new ContainerBuilder(); |
43 | | - |
44 | | -$sc |
45 | | - ->register('bar', '%bar.class%') |
46 | | - ->setFactoryClass('%bar.class%') |
47 | | - ->setFactoryMethod('getInstance') |
48 | | - ->addArgument('Aarrg!!!') |
49 | | -; |
50 | | -$sc->setParameter('bar.class', 'Bar'); |
51 | | - |
52 | | -$sc->get('bar'); |
53 | | -``` |
54 | | - |
55 | | -File Include: |
56 | | - |
57 | | -For some services, especially those that are difficult or impossible to |
58 | | -autoload, you may need the container to include a file before |
59 | | -instantiating your class. |
60 | | - |
61 | | -```php |
62 | | -$sc = new ContainerBuilder(); |
63 | | - |
64 | | -$sc |
65 | | - ->register('bar', '%bar.class%') |
66 | | - ->setFile('/path/to/file') |
67 | | - ->addArgument('Aarrg!!!') |
68 | | -; |
69 | | -$sc->setParameter('bar.class', 'Bar'); |
70 | | - |
71 | | -$sc->get('bar'); |
72 | | -``` |
| 4 | +The DependencyInjection component allows you to standardize and centralize the |
| 5 | +way objects are constructed in your application. |
73 | 6 |
|
74 | 7 | Resources |
75 | 8 | --------- |
76 | 9 |
|
77 | | -You can run the unit tests with the following command: |
78 | | - |
79 | | - $ cd path/to/Symfony/Component/DependencyInjection/ |
80 | | - $ composer install |
81 | | - $ phpunit |
| 10 | + * [Documentation](https://symfony.com/doc/current/components/dependency_injection/index.html) |
| 11 | + * [Contributing](https://symfony.com/doc/current/contributing/index.html) |
| 12 | + * [Report issues](https://github.com/symfony/symfony/issues) and |
| 13 | + [send Pull Requests](https://github.com/symfony/symfony/pulls) |
| 14 | + in the [main Symfony repository](https://github.com/symfony/symfony) |
0 commit comments