@@ -492,9 +492,9 @@ Start by adding a ``_defaults`` section with ``autowire`` and ``autoconfigure``.
492
492
493
493
# app/config/services.yml
494
494
services:
495
- + _defaults:
496
- + autowire: true
497
- + autoconfigure: true
495
+ + _defaults:
496
+ + autowire: true
497
+ + autoconfigure: true
498
498
499
499
# ...
500
500
@@ -519,15 +519,15 @@ Start by updating the service ids to class names:
519
519
services:
520
520
# ...
521
521
522
- - app.github_notifier:
523
- - class: AppBundle\Service\GitHubNotifier
524
- + AppBundle\Service\GitHubNotifier:
522
+ - app.github_notifier:
523
+ - class: AppBundle\Service\GitHubNotifier
524
+ + AppBundle\Service\GitHubNotifier:
525
525
arguments:
526
526
- '@app.api_client_github'
527
527
528
- - markdown_transformer:
529
- - class: AppBundle\Service\MarkdownTransformer
530
- + AppBundle\Service\MarkdownTransformer: ~
528
+ - markdown_transformer:
529
+ - class: AppBundle\Service\MarkdownTransformer
530
+ + AppBundle\Service\MarkdownTransformer: ~
531
531
532
532
# keep these ids because there are multiple instances per class
533
533
app.api_client_github:
@@ -557,8 +557,8 @@ Then import this at the top of ``services.yml``:
557
557
.. code-block :: diff
558
558
559
559
# app/config/services.yml
560
- +imports:
561
- + - { resource: legacy_aliases.yml }
560
+ + imports:
561
+ + - { resource: legacy_aliases.yml }
562
562
563
563
# ...
564
564
@@ -579,7 +579,7 @@ Now you're ready to default all services to be private:
579
579
_defaults:
580
580
autowire: true
581
581
autoconfigure: true
582
- + public: false
582
+ + public: false
583
583
584
584
Thanks to this, any services created in this file cannot be fetched directly from
585
585
the container. But, since the old service id's are aliases in a separate file (``legacy_aliases.yml ``),
@@ -596,16 +596,16 @@ instances of the same class), you may need to make those public:
596
596
services:
597
597
# ...
598
598
599
- app.api_client_github:
600
- # ...
599
+ app.api_client_github:
600
+ # ...
601
601
602
- + # remove this if/when you are not fetching this
603
- + # directly from the container via $container->get()
604
- + public: true
602
+ + # remove this if/when you are not fetching this
603
+ + # directly from the container via $container->get()
604
+ + public: true
605
605
606
- app.api_client_sl_connect:
607
- # ...
608
- + public: true
606
+ app.api_client_sl_connect:
607
+ # ...
608
+ + public: true
609
609
610
610
This is to guarantee that the application doesn't break. If you're not fetching
611
611
these services directly from the container, this isn't needed. In a minute, you'll
@@ -625,14 +625,14 @@ You're now ready to automatically register all services in ``src/AppBundle/``
625
625
_defaults:
626
626
# ...
627
627
628
- + AppBundle\:
629
- + resource: '../../src/AppBundle/*'
630
- + exclude: '../../src/AppBundle/{Entity,Repository}'
631
- +
632
- + AppBundle\Controller\:
633
- + resource: '../../src/AppBundle/Controller'
634
- + public: true
635
- + tags: ['controller.service_arguments']
628
+ + AppBundle\:
629
+ + resource: '../../src/AppBundle/*'
630
+ + exclude: '../../src/AppBundle/{Entity,Repository}'
631
+ +
632
+ + AppBundle\Controller\:
633
+ + resource: '../../src/AppBundle/Controller'
634
+ + public: true
635
+ + tags: ['controller.service_arguments']
636
636
637
637
# ...
638
638
@@ -650,14 +650,14 @@ same class:
650
650
services:
651
651
# ...
652
652
653
- + # alias ApiClient to one of our services below
654
- + # app.api_client_github will be used to autowire ApiClient type-hints
655
- + AppBundle\Service\ApiClient: '@app.api_client_github'
653
+ + # alias ApiClient to one of our services below
654
+ + # app.api_client_github will be used to autowire ApiClient type-hints
655
+ + AppBundle\Service\ApiClient: '@app.api_client_github'
656
656
657
- app.api_client_github:
658
- # ...
659
- app.api_client_sl_connect:
660
- # ...
657
+ app.api_client_github:
658
+ # ...
659
+ app.api_client_sl_connect:
660
+ # ...
661
661
662
662
This guarantees that if you try to autowire an ``ApiClient `` instance, the ``app.api_client_github ``
663
663
will be used. If you *don't * have this, the auto-registration feature will try to
@@ -671,17 +671,19 @@ To make sure your application didn't break, you did some extra work. Now it's ti
671
671
to clean things up! First, update your application to *not * use the old service id's (the
672
672
ones in ``legacy_aliases.yml ``). This means updating any service arguments (e.g.
673
673
``@app.github_notifier `` to ``@AppBundle\Service\GitHubNotifier ``) and updating your
674
- code to not fetch this service directly from the container. For example::
674
+ code to not fetch this service directly from the container. For example:
675
675
676
- - public function indexAction()
677
- + public function indexAction(GitHubNotifier $gitHubNotifier, MarkdownTransformer $markdownTransformer)
678
- {
679
- - // the old way of fetching services
680
- - $githubNotifier = $this->container->get('app.github_notifier');
681
- - $markdownTransformer = $this->container->get('markdown_transformer');
676
+ .. code-block :: diff
677
+
678
+ - public function indexAction()
679
+ + public function indexAction(GitHubNotifier $gitHubNotifier, MarkdownTransformer $markdownTransformer)
680
+ {
681
+ - // the old way of fetching services
682
+ - $githubNotifier = $this->container->get('app.github_notifier');
683
+ - $markdownTransformer = $this->container->get('markdown_transformer');
682
684
683
685
// ...
684
- }
686
+ }
685
687
686
688
As soon as you do this, you can delete ``legacy_aliases.yml `` and remove its import.
687
689
You should do the same thing for any services that you made public, like
@@ -694,13 +696,13 @@ these directly from the container, you can remove the ``public: true`` flag:
694
696
services:
695
697
# ...
696
698
697
- app.api_client_github:
698
- # ...
699
- - public: true
699
+ app.api_client_github:
700
+ # ...
701
+ - public: true
700
702
701
- app.api_client_sl_connect:
702
- # ...
703
- - public: true
703
+ app.api_client_sl_connect:
704
+ # ...
705
+ - public: true
704
706
705
707
Finally, you can optionally remove any services from ``services.yml `` whose arguments
706
708
can be autowired. The final configuration looks like this:
0 commit comments