Skip to content
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

remove service class parameters #4519

Merged
merged 1 commit into from
Dec 7, 2014
Merged
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
5 changes: 1 addition & 4 deletions best_practices/business-logic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,9 @@ the class namespace as a parameter:
# app/config/services.yml

# service definition with class namespace as parameter
parameters:
slugger.class: AppBundle\Utils\Slugger

services:
app.slugger:
class: "%slugger.class%"
class: AppBundle\Utils\Slugger

This practice is cumbersome and completely unnecessary for your own services:

Expand Down
89 changes: 22 additions & 67 deletions book/service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,11 @@ straightforward. Parameters make defining services more organized and flexible:

# app/config/config.yml
parameters:
my_mailer.class: Acme\HelloBundle\Mailer
my_mailer.transport: sendmail

services:
my_mailer:
class: "%my_mailer.class%"
class: Acme\HelloBundle\Mailer
arguments: ["%my_mailer.transport%"]

.. code-block:: xml
Expand All @@ -220,12 +219,11 @@ straightforward. Parameters make defining services more organized and flexible:
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="my_mailer.class">Acme\HelloBundle\Mailer</parameter>
<parameter key="my_mailer.transport">sendmail</parameter>
</parameters>

<services>
<service id="my_mailer" class="%my_mailer.class%">
<service id="my_mailer" class="Acme\HelloBundle\Mailer">
<argument>%my_mailer.transport%</argument>
</service>
</services>
Expand All @@ -236,19 +234,18 @@ straightforward. Parameters make defining services more organized and flexible:
// app/config/config.php
use Symfony\Component\DependencyInjection\Definition;

$container->setParameter('my_mailer.class', 'Acme\HelloBundle\Mailer');
$container->setParameter('my_mailer.transport', 'sendmail');

$container->setDefinition('my_mailer', new Definition(
'%my_mailer.class%',
'Acme\HelloBundle\Mailer',
array('%my_mailer.transport%')
));

The end result is exactly the same as before - the difference is only in
*how* you defined the service. By surrounding the ``my_mailer.class`` and
``my_mailer.transport`` strings in percent (``%``) signs, the container knows
to look for parameters with those names. When the container is built, it
looks up the value of each parameter and uses it in the service definition.
*how* you defined the service. By surrounding the ``my_mailer.transport``
string in percent (``%``) signs, the container knows to look for a parameter
with that name. When the container is built, it looks up the value of each
parameter and uses it in the service definition.

.. note::

Expand Down Expand Up @@ -348,12 +345,11 @@ directories don't exist, create them.

# src/Acme/HelloBundle/Resources/config/services.yml
parameters:
my_mailer.class: Acme\HelloBundle\Mailer
my_mailer.transport: sendmail

services:
my_mailer:
class: "%my_mailer.class%"
class: Acme\HelloBundle\Mailer
arguments: ["%my_mailer.transport%"]

.. code-block:: xml
Expand All @@ -365,12 +361,11 @@ directories don't exist, create them.
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="my_mailer.class">Acme\HelloBundle\Mailer</parameter>
<parameter key="my_mailer.transport">sendmail</parameter>
</parameters>

<services>
<service id="my_mailer" class="%my_mailer.class%">
<service id="my_mailer" class="Acme\HelloBundle\Mailer">
<argument>%my_mailer.transport%</argument>
</service>
</services>
Expand All @@ -381,11 +376,10 @@ directories don't exist, create them.
// src/Acme/HelloBundle/Resources/config/services.php
use Symfony\Component\DependencyInjection\Definition;

$container->setParameter('my_mailer.class', 'Acme\HelloBundle\Mailer');
$container->setParameter('my_mailer.transport', 'sendmail');

$container->setDefinition('my_mailer', new Definition(
'%my_mailer.class%',
'Acme\HelloBundle\Mailer',
array('%my_mailer.transport%')
));

Expand Down Expand Up @@ -608,15 +602,11 @@ the service container gives you a much more appealing option:
.. code-block:: yaml

# src/Acme/HelloBundle/Resources/config/services.yml
parameters:
# ...
newsletter_manager.class: Acme\HelloBundle\Newsletter\NewsletterManager

services:
my_mailer:
# ...
newsletter_manager:
class: "%newsletter_manager.class%"
class: Acme\HelloBundle\Newsletter\NewsletterManager
arguments: ["@my_mailer"]

.. code-block:: xml
Expand All @@ -627,16 +617,11 @@ the service container gives you a much more appealing option:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<!-- ... -->
<parameter key="newsletter_manager.class">Acme\HelloBundle\Newsletter\NewsletterManager</parameter>
</parameters>

<services>
<service id="my_mailer">
<!-- ... -->
</service>
<service id="newsletter_manager" class="%newsletter_manager.class%">
<service id="newsletter_manager" class="Acme\HelloBundle\Newsletter\NewsletterManager">
<argument type="service" id="my_mailer"/>
</service>
</services>
Expand All @@ -648,15 +633,9 @@ the service container gives you a much more appealing option:
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

// ...
$container->setParameter(
'newsletter_manager.class',
'Acme\HelloBundle\Newsletter\NewsletterManager'
);

$container->setDefinition('my_mailer', ...);
$container->setDefinition('newsletter_manager', new Definition(
'%newsletter_manager.class%',
'Acme\HelloBundle\Newsletter\NewsletterManager',
array(new Reference('my_mailer'))
));

Expand Down Expand Up @@ -704,15 +683,11 @@ Injecting the dependency by the setter method just needs a change of syntax:
.. code-block:: yaml

# src/Acme/HelloBundle/Resources/config/services.yml
parameters:
# ...
newsletter_manager.class: Acme\HelloBundle\Newsletter\NewsletterManager

services:
my_mailer:
# ...
newsletter_manager:
class: "%newsletter_manager.class%"
class: Acme\HelloBundle\Newsletter\NewsletterManager
calls:
- [setMailer, ["@my_mailer"]]

Expand All @@ -724,16 +699,11 @@ Injecting the dependency by the setter method just needs a change of syntax:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<!-- ... -->
<parameter key="newsletter_manager.class">Acme\HelloBundle\Newsletter\NewsletterManager</parameter>
</parameters>

<services>
<service id="my_mailer">
<!-- ... -->
</service>
<service id="newsletter_manager" class="%newsletter_manager.class%">
<service id="newsletter_manager" class="Acme\HelloBundle\Newsletter\NewsletterManager">
<call method="setMailer">
<argument type="service" id="my_mailer" />
</call>
Expand All @@ -747,15 +717,9 @@ Injecting the dependency by the setter method just needs a change of syntax:
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

// ...
$container->setParameter(
'newsletter_manager.class',
'Acme\HelloBundle\Newsletter\NewsletterManager'
);

$container->setDefinition('my_mailer', ...);
$container->setDefinition('newsletter_manager', new Definition(
'%newsletter_manager.class%'
'Acme\HelloBundle\Newsletter\NewsletterManager'
))->addMethodCall('setMailer', array(
new Reference('my_mailer'),
));
Expand All @@ -781,12 +745,9 @@ it exists and do nothing if it doesn't:
.. code-block:: yaml

# src/Acme/HelloBundle/Resources/config/services.yml
parameters:
# ...

services:
newsletter_manager:
class: "%newsletter_manager.class%"
class: Acme\HelloBundle\Newsletter\NewsletterManager
arguments: ["@?my_mailer"]

.. code-block:: xml
Expand All @@ -801,7 +762,7 @@ it exists and do nothing if it doesn't:
<service id="my_mailer">
<!-- ... -->
</service>
<service id="newsletter_manager" class="%newsletter_manager.class%">
<service id="newsletter_manager" class="Acme\HelloBundle\Newsletter\NewsletterManager">
<argument type="service" id="my_mailer" on-invalid="ignore" />
</service>
</services>
Expand All @@ -814,15 +775,9 @@ it exists and do nothing if it doesn't:
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerInterface;

// ...
$container->setParameter(
'newsletter_manager.class',
'Acme\HelloBundle\Newsletter\NewsletterManager'
);

$container->setDefinition('my_mailer', ...);
$container->setDefinition('newsletter_manager', new Definition(
'%newsletter_manager.class%',
'Acme\HelloBundle\Newsletter\NewsletterManager',
array(
new Reference(
'my_mailer',
Expand Down Expand Up @@ -899,7 +854,7 @@ Configuring the service container is easy:
# src/Acme/HelloBundle/Resources/config/services.yml
services:
newsletter_manager:
class: "%newsletter_manager.class%"
class: Acme\HelloBundle\Newsletter\NewsletterManager
arguments: ["@mailer", "@templating"]

.. code-block:: xml
Expand All @@ -910,7 +865,7 @@ Configuring the service container is easy:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<service id="newsletter_manager" class="%newsletter_manager.class%">
<service id="newsletter_manager" class="Acme\HelloBundle\Newsletter\NewsletterManager">
<argument type="service" id="mailer"/>
<argument type="service" id="templating"/>
</service>
Expand All @@ -920,7 +875,7 @@ Configuring the service container is easy:

// src/Acme/HelloBundle/Resources/config/services.php
$container->setDefinition('newsletter_manager', new Definition(
'%newsletter_manager.class%',
'Acme\HelloBundle\Newsletter\NewsletterManager',
array(
new Reference('mailer'),
new Reference('templating'),
Expand Down
Loading