Buy

Configuring Specific (Named) Arguments

We saw earlier that sometimes you only need to pass one argument... and the rest can be autowired. For MarkdownTransformer, we only need to configure the second argument:

45 lines app/config/services.yml
... lines 1 - 5
services:
... lines 7 - 30
AppBundle\Service\MarkdownTransformer:
arguments: ['', '@doctrine_cache.providers.my_markdown_cache']
... lines 33 - 45

To allow the first to be continue to be autowired, we set it to an empty string. That works... but it's just weird. So, in Symfony 3.3, there's a better way.

In MarkdownTransformer, the argument names are $markdownParser and $cache:

35 lines src/AppBundle/Service/MarkdownTransformer.php
... lines 1 - 7
class MarkdownTransformer
{
... lines 10 - 12
public function __construct(MarkdownParserInterface $markdownParser, Cache $cache)
{
... lines 15 - 16
}
... lines 18 - 33
}

The $cache argument is the one we need to configure. Back in services.yml, copy the cache service id, clear out the arguments, and add $cache: '@doctrine_cache.providers.my_markdown_cache':

46 lines app/config/services.yml
... lines 1 - 5
services:
... lines 7 - 30
AppBundle\Service\MarkdownTransformer:
arguments:
$cache: '@doctrine_cache.providers.my_markdown_cache'
... lines 34 - 46

The dollar sign is the important part: it tells Symfony that we're actually configuring an argument by its name. $cache here must match $cache here.

And it works beautifully - refresh now! Everything is happy!

Named Arguments are Validated

But wait! Isn't this dangerous!? I mean, normally, if I were coding in MarkdownTransformer, I could safely rename this variable to $cacheDriver:

35 lines src/AppBundle/Service/MarkdownTransformer.php
... lines 1 - 7
class MarkdownTransformer
{
... lines 10 - 12
public function __construct(MarkdownParserInterface $markdownParser, Cache $cacheDriver)
{
... line 15
$this->cache = $cacheDriver;
}
... lines 18 - 33
}

That change shouldn't make any difference to any code outside of this class.

But suddenly now... it does make a difference! The $cache in services.yml no longer matches the argument name. Isn't this a huge problem!? Actually, no. Yes, you will get an error - you can see it when you refresh. But this error will happen if you try to refresh any page across your entire system. Symfony validates all of your services and configuration. And as soon as it saw $cache in services.yml with no corresponding argument, it screamed.

So yes, changing the argument in your service class will break your app. But the error is unignorable: nothing works until you fix it. Update the configuration to $cacheDriver:

46 lines app/config/services.yml
... lines 1 - 5
services:
... lines 7 - 30
AppBundle\Service\MarkdownTransformer:
arguments:
$cacheDriver: '@doctrine_cache.providers.my_markdown_cache'
... lines 34 - 46

And refresh again.

Back working! This is what makes Symfony's autowiring special. If there is any question or problem wiring the arguments to any service, Symfony throws an exception at compile team... meaning, it throws an exception when you try to refresh any page. This means no surprises: it's not possible to have an autowiring error on only one page and not notice it.

Leave a comment!

  • 2017-10-04 Bruno Lima

    Great! Thank you.

  • 2017-10-04 weaverryan

    Yo Bruno Lima!

    Wow, nice find! They're both identical actually! But in all of the 3.3 changes, I didn't even know this was one of them! So yea, use whichever you want. The shorter syntax is pretty cool looking :). The only downside is if you ever need to add more config (like tags), you'll then need to put the arguments under an arguments key. But, that's not too common.

    Cheers!

  • 2017-10-04 Bruno Lima

    Hi Ryan,

    I was wondering if there are any differences using this:


    AppBundle\Service\MarkdownTransformer:
    $cacheDriver: '@doctrine_cache.providers.my_markdown_cache'

    instead of this:


    AppBundle\Service\MarkdownTransformer:
    arguments:
    $cacheDriver: '@doctrine_cache.providers.my_markdown_cache'

    since it looks like both works.