phparchitect-2019-08

(Rick Simeone) #1
10 \ August 2019 \ http://www.phparch.com

Symfony 4: A New Way to Develop Applications

This was the minimum requirement for service. These files
can often be found within Symfony bundles, and are nothing
more than a (long) list of services and their corresponding
classes. Once Symfony collected all these services, it compiled
a service container with all services which would then be
used within your Symfony project. By default, all services
were public, which is slower than having private services.
Well, not that much slower but still slower. When calling a
service, Symfony tries to find the corresponding name in this
service container, instantiate the corresponding class if not
done so already, and returns it for you to use. Simple right?
Also, no magic.


The same process is happening with autowiring. The first
thing is that Symfony creates services from classes automat-
ically without specifying this inside your services.yml file.
Symfony cannot know what you like to call those services (like
renderer or mailer), but instead, it registers each class it finds
as the fully qualified class name (FQCN). Our mailer service
would automatically be registered as the service AppBundle\
Service\MailerService. Since we use a type hint to an object,
PHP expects you to pass a \AppBundle\Service\RenderService
instance to this constructor. During autowiring, Symfony
looks in the service container to see if there is such a service
defined. If so, it automatically injects that specific service
into the constructor, even though you haven’t configured this
service and added any arguments to it. This is what Symfony’s
autowiring functionality is all about: Symfony automatically

wires the dependencies based on their fully qualified class
name.
If we check the Symfony config file in config/services.yaml
shown in Listing 5, we see that autowiring is enabled by default.
You can turn this off if you want, but there is no reason to do
this configuration manually. In the end, it is the same thing,
and you save a lot of your precious time using autowiring. In
the configuration, we can see that all classes are available to
be used as services except classes defined under:

../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}

Also, we can see all the classes in the ./Controller folder are
registered as services and tagged as controllers. This is very
important as this allows us to use autowiring and injections
inside controller classes. Autoconfiguration is closely related
to autowiring. Where autowiring automatically connects any
dependencies to services, autoconfiguration configures those
services automatically. This isn’t always needed, but you often
see autoconfiguration used for automatically adding tags to
services, like in default configuration file.

Listing 5



  1. Symfony default config file




  2. This file is the entry point to configure your own services.




  3. Files in the packages/ subdirectory configure your dependencies.






  4. Put parameters here that don't need to change on each machine where the app is deployed.




  5. https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration



  6. parameters:



  7. services:


  8. default configuration for services in this file



  9. _defaults:

  10. autowire: true # Automatically injects dependencies in your services.

  11. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.




  12. makes classes in src/ available to be used as services




  13. this creates a service per class whose id is the fully-qualified class name



  14. App\:

  15. resource: '../src/*'

  16. exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'




  17. controllers are imported separately to make sure services can be injected




  18. as action arguments even if you don't extend any base controller class



  19. App\Controller\:

  20. resource: '../src/Controller'

  21. tags: ['controller.service_arguments']




  22. add more service definitions when explicit configuration is needed




  23. please note that last definitions always replace previous ones



Free download pdf