8000 Fixed usage of default configuration in PHP by HeahDude · Pull Request #8232 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Fixed usage of default configuration in PHP #8232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions service_container.rst
8000
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,19 @@ each time you ask for it.
.. code-block:: php

// app/config/services.php
// _defaults and loading entire directories is not possible with PHP configuration
// you need to define your services one-by-one
use AppBundle\Service\MessageGenerator;
use Symfony\Component\DependencyInjection\Definition;

$container->autowire(MessageGenerator::class)
// To use as default template
$definition = new Definition();

$definition
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false);
->setPublic(false)
;

// $this is a reference to the current loader
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');

Thanks to this configuration, you can automatically use any classes from the
``src/AppBundle`` directory as a service, without needing to manually configure
Expand Down Expand Up @@ -475,12 +481,21 @@ pass here. No problem! In your configuration, you can explicitly set this argume

// app/config/services.php
use AppBundle\Updates\SiteUpdateManager;
use Symfony\Component\DependencyInjection\Definition;

// _defaults and importing directories does not work in PHP
// but registering a service explicitly does
$container->autowire(SiteUpdateManager::class)
// Same as before
$definition = new Definition();

$definition
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false)
;

$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');

// Explicitly configure the service
$container->getDefinition(SiteUpdateManager::class)
->setArgument('$adminEmail', 'manager@example.com');

.. versionadded:: 3.3
Expand Down Expand Up @@ -860,6 +875,31 @@ key. For example, the default Symfony configuration contains this:
</services>
</container>

.. code-block:: php

// app/config/services.php
use Symfony\Component\DependencyInjection\Definition;

// To use as default template
$definition = new Definition();

$definition
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false)
;

$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');

// Changes default config
$definition
->setPublic(true)
->addTag('controller.service_arguments')
;

// $this is a reference to the current loader
$this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');

This can be used to quickly make many classes available as services and apply some
default configuration. The ``id`` of each service is its fully-qualified class name.
You can override any service that's imported by using its id (class name) below
Expand Down
53 changes: 38 additions & 15 deletions service_container/3.3-di-changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,37 @@ Symfony Standard Edition:
<prototype namespace="AppBundle\Controller\" resource="../../src/AppBundle/Controller" public="true">
<tag name="controller.service_arguments" />
</prototype>

<!-- add more services, or override services that need manual wiring -->
</services>
</container>

.. code-block:: php

// app/config/services.php
use Symfony\Component\DependencyInjection\Definition;

// _defaults and loading entire directories is not possible with PHP configuration
// you need to define your services one-by-one
use AppBundle\Controller\DefaultController;
// To use as default template
$definition = new Definition();

$container->autowire(DefaultController::class)
$definition
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false)
;

$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');

// Changes default config
$definition
->setPublic(true)
->addTag('controller.service_arguments')
->setPublic(true);
;

// $this is a reference to the current loader
$this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');

// add more services, or override services that need manual wiring

This small bit of configuration contains a paradigm shift of how services
are configured in Symfony.
Expand Down Expand Up @@ -147,9 +163,18 @@ thanks to the following config:
.. code-block:: php

// app/config/services.php
use Symfony\Component\DependencyInjection\Definition;

// To use as default template
$definition = new Definition();

// services cannot be automatically loaded with PHP configuration
// you need to define your services one-by-one
$definition
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false)
;

$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');

This means that every class in ``src/AppBundle/`` is *available* to be used as a
service. And thanks to the ``_defaults`` section at the top of the file, all of
Expand Down Expand Up @@ -348,14 +373,12 @@ The third big change is that, in a new Symfony 3.3 project, your controllers are

// app/config/services.php

// loading entire directories is not possible with PHP configuration
// you need to define your services one-by-one
use AppBundle\Controller\DefaultController;
// ...

// override default template
$definition->setPublic(true);

$container->autowire(DefaultController::class)
->setAutoconfigured(true)
->addTag('controller.service_arguments')
->setPublic(true);
$this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');

But, you might not even notice this. First, your controllers *can* still extend
the same base ``Controller`` class or a new :ref:`AbstractController <controller-abstract-versus-controller>`.
Expand Down Expand Up @@ -642,7 +665,7 @@ You're now ready to automatically register all services in ``src/AppBundle/``
+ AppBundle\:
+ resource: '../../src/AppBundle/*'
+ exclude: '../../src/AppBundle/{Entity,Repository}'
+
+
+ AppBundle\Controller\:
+ resource: '../../src/AppBundle/Controller'
+ public: true
Expand Down
16 changes: 9 additions & 7 deletions service_container/configurators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,22 @@ all the classes are already loaded as services. All you need to do is specify th
.. code-block:: php

// app/config/services.php
use AppBundle\Mail\EmailConfigurator;
use AppBundle\Mail\EmailFormatterManager;
use AppBundle\Mail\GreetingCardManager;
use AppBundle\Mail\NewsletterManager;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

// ...
$container->autowire(EmailFormatterManager::class);
$container->autowire(EmailConfigurator::class);
// Same as before
$definition = new Definition();

$definition->setAutowired(true);

$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*');

$container->autowire(NewsletterManager::class)
$container->getDefinition(NewsletterManager::class)
->setConfigurator(array(new Reference(EmailConfigurator::class), 'configure'));

$container->autowire(GreetingCardManager::class)
$container->getDefinition(GreetingCardManager::class)
->setConfigurator(array(new Reference(EmailConfigurator::class), 'configure'));


Expand Down
0