Symfony(6)Service Container

Symfony(6)Service Container

A modern PHP application is full of objects. A service container will allow us to standardize and centralize the way objects are constructed in our application.

1. Basic
What is a Service
A Service is a PHP object that created for a specific purpose.

Service Container
A Service Container is simply a PHP object that manages the instantiation of services.

2. How to Configure
Creating/Configuring Services in the Container
#app/config/config.yml
services:
     my_mailer:
          class:    Acme\HelloBundle\Mailer
          arguments: [sendMail]

public function sendEmailAction(){
     $mailer = $this->get(‘my_mailer’); //match the name, get it
}

Service Parameters
#app/config/config.yml
parameters:
     my_mailer.class:     Acme\HelloBundle\Mailer
     my_mailer.transport: sendmail
services:
     my_mailer:
          class:          “%my_mailer.class%”
          arguments:     [“%my_mailer.transport%"]

Importing other Container Configuration Resources
imports:
     - { resource: “@AcmeHelloBundle/Resources/config/services.yml” }
     - { resource: “…snip..."}

Referencing Services
class NewsletterManager{
     protected $mailer;
     public function __construct(Mailer $mailer){
          $this->mailer = $mailer
     }
}

services:
     my_mailer:
          …snip…
     newsleter_manager:
          class: …snip…
          arguments: [“@my_mailer"]

Optional Dependencies: Setter Injection
Optional dependencies for a class.
class NewsletterManager{
     protected $mailer;
     public function setMailer(Mailer $mailer){
          $this->mailer = $mailer;
     }
}

services:
     my_mailer:
          ..snip…
     newsletter_manager:
          class:     …snip…
          calls:
               - [setMailer, [“@my_mailer"]]

Injecting the Request
class NewsletterManager{
     protected $requestStack;
     
     public function __construct(RequestStack $requestStack){
          $this->requestStack = $requestStack;
     }
     
     public function anyMethod(){
          $request = $this->requestStack->getCurrentRequest();
          ..snip...
     }
}

services:
     newsletter_manager:
          class: …snip…
          arguments: [“@request_stack”]

Making References Optional
services:
     newsletter_manager:
          class:      …snip…
          arguments: [“@?my_mailer"]

public function __construct(Mailer $mailer = null){ …snip… }

Array of Arguments
services:
     newsletter_manager:
          class: …snip…
          arguments: [“@mailer”, “@templating”]

Tags
services:
     foo.twig.extension:
          class:      …snip…
          tags:
               - { name: twig.extension }

3. Run and Debugging
>php app/console container:debug

Check one service
>php app/console container:debug request_stack
[container] Information for service request_stackService Id      request_stack Class            Symfony\Component\HttpFoundation\RequestStack Tags            - Scope            container Public          yes Synthetic        no Required File    -


References:
http://symfony.com/doc/2.4/book/service_container.html

http://twpug.net/docs/symblog/
http://symfony.com/blog/

http://getbootstrap.com/getting-started/#examples



你可能感兴趣的:(service)