Skip to content

Use "$container" consistently #18299

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

Merged
merged 1 commit into from
May 19, 2023
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
4 changes: 2 additions & 2 deletions .doctor-rst.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ rules:
american_english: ~
argument_variable_must_match_type:
arguments:
- { type: 'ContainerBuilder', name: 'containerBuilder' }
- { type: 'ContainerConfigurator', name: 'containerConfigurator' }
- { type: 'ContainerBuilder', name: 'container' }
- { type: 'ContainerConfigurator', name: 'container' }
avoid_repetetive_words: ~
blank_line_after_anchor: ~
blank_line_after_directive: ~
Expand Down
4 changes: 2 additions & 2 deletions bundles/best_practices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ The end user can provide values in any configuration file:
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return static function (ContainerConfigurator $containerConfigurator) {
$containerConfigurator->parameters()
return static function (ContainerConfigurator $container) {
$container->parameters()
->set('acme_blog.author.email', 'fabien@example.com')
;
};
Expand Down
12 changes: 6 additions & 6 deletions bundles/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ force validation (e.g. if an additional option was passed, an exception will be
thrown)::

// src/Acme/SocialBundle/DependencyInjection/AcmeSocialExtension.php
public function load(array $configs, ContainerBuilder $containerBuilder)
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();

Expand Down Expand Up @@ -259,15 +259,15 @@ In your extension, you can load this and dynamically set its arguments::
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

public function load(array $configs, ContainerBuilder $containerBuilder)
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($containerBuilder, new FileLocator(dirname(__DIR__).'/Resources/config'));
$loader = new XmlFileLoader($container, new FileLocator(dirname(__DIR__).'/Resources/config'));
$loader->load('services.xml');

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$definition = $containerBuilder->getDefinition('acme.social.twitter_client');
$definition = $container->getDefinition('acme.social.twitter_client');
$definition->replaceArgument(0, $config['twitter']['client_id']);
$definition->replaceArgument(1, $config['twitter']['client_secret']);
}
Expand All @@ -288,7 +288,7 @@ In your extension, you can load this and dynamically set its arguments::
class AcmeHelloExtension extends ConfigurableExtension
{
// note that this method is called loadInternal and not load
protected function loadInternal(array $mergedConfig, ContainerBuilder $containerBuilder)
protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
{
// ...
}
Expand All @@ -304,7 +304,7 @@ In your extension, you can load this and dynamically set its arguments::
(e.g. by overriding configurations and using :phpfunction:`isset` to check
for the existence of a value). Be aware that it'll be very hard to support XML::

public function load(array $configs, ContainerBuilder $containerBuilder)
public function load(array $configs, ContainerBuilder $container)
{
$config = [];
// let resources override the previous set value
Expand Down
8 changes: 4 additions & 4 deletions bundles/extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This is how the extension of an AcmeHelloBundle should look like::

class AcmeHelloExtension extends Extension
{
public function load(array $configs, ContainerBuilder $containerBuilder)
public function load(array $configs, ContainerBuilder $container)
{
// ... you'll load the files here later
}
Expand Down Expand Up @@ -89,10 +89,10 @@ For instance, assume you have a file called ``services.xml`` in the
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

// ...
public function load(array $configs, ContainerBuilder $containerBuilder)
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader(
$containerBuilder,
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('services.xml');
Expand All @@ -115,7 +115,7 @@ they are compiled when generating the application cache to improve the overall
performance. Define the list of annotated classes to compile in the
``addAnnotatedClassesToCompile()`` method::

public function load(array $configs, ContainerBuilder $containerBuilder)
public function load(array $configs, ContainerBuilder $container)
{
// ...

Expand Down
20 changes: 10 additions & 10 deletions bundles/prepend_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ To give an Extension the power to do this, it needs to implement
{
// ...

public function prepend(ContainerBuilder $containerBuilder)
public function prepend(ContainerBuilder $container)
{
// ...
}
Expand All @@ -52,15 +52,15 @@ a configuration setting in multiple bundles as well as disable a flag in multipl
in case a specific other bundle is not registered::

// src/Acme/HelloBundle/DependencyInjection/AcmeHelloExtension.php
public function prepend(ContainerBuilder $containerBuilder)
public function prepend(ContainerBuilder $container)
{
// get all bundles
$bundles = $containerBuilder->getParameter('kernel.bundles');
$bundles = $container->getParameter('kernel.bundles');
// determine if AcmeGoodbyeBundle is registered
if (!isset($bundles['AcmeGoodbyeBundle'])) {
// disable AcmeGoodbyeBundle in bundles
$config = ['use_acme_goodbye' => false];
foreach ($containerBuilder->getExtensions() as $name => $extension) {
foreach ($container->getExtensions() as $name => $extension) {
switch ($name) {
case 'acme_something':
case 'acme_other':
Expand All @@ -70,21 +70,21 @@ in case a specific other bundle is not registered::
// note that if the user manually configured
// use_acme_goodbye to true in config/services.yaml
// then the setting would in the end be true and not false
$containerBuilder->prependExtensionConfig($name, $config);
$container->prependExtensionConfig($name, $config);
break;
}
}
}

// get the configuration of AcmeHelloExtension (it's a list of configuration)
$configs = $containerBuilder->getExtensionConfig($this->getAlias());
$configs = $container->getExtensionConfig($this->getAlias());

// iterate in reverse to preserve the original order after prepending the config
foreach (array_reverse($configs) as $config) {
// check if entity_manager_name is set in the "acme_hello" configuration
if (isset($config['entity_manager_name'])) {
// prepend the acme_something settings with the entity_manager_name
$containerBuilder->prependExtensionConfig('acme_something', [
$container->prependExtensionConfig('acme_something', [
'entity_manager_name' => $config['entity_manager_name'],
]);
}
Expand Down Expand Up @@ -141,13 +141,13 @@ registered and the ``entity_manager_name`` setting for ``acme_hello`` is set to
// config/packages/acme_something.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return static function (ContainerConfigurator $containerConfigurator) {
$containerConfigurator->extension('acme_something', [
return static function (ContainerConfigurator $container) {
$container->extension('acme_something', [
// ...
'use_acme_goodbye' => false,
'entity_manager_name' => 'non_default',
]);
$containerConfigurator->extension('acme_other', [
$container->extension('acme_other', [
// ...
'use_acme_goodbye' => false,
]);
Expand Down
8 changes: 4 additions & 4 deletions cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ with either :class:`Symfony\\Contracts\\Cache\\CacheInterface` or
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return function(ContainerConfigurator $containerConfigurator) {
$containerConfigurator->services()
return function(ContainerConfigurator $container) {
$container->services()
// ...

->set('app.cache.adapter.redis')
Expand Down Expand Up @@ -465,14 +465,14 @@ and use that when configuring the pool.
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Config\FrameworkConfig;

return static function (ContainerBuilder $containerBuilder, FrameworkConfig $framework) {
return static function (ContainerBuilder $container, FrameworkConfig $framework) {
$framework->cache()
->pool('cache.my_redis')
->adapters(['cache.adapter.redis'])
->provider('app.my_custom_redis_provider');


$containerBuilder->register('app.my_custom_redis_provider', \Redis::class)
$container->register('app.my_custom_redis_provider', \Redis::class)
->setFactory([RedisAdapter::class, 'createConnection'])
->addArgument('redis://localhost')
->addArgument([
Expand Down
52 changes: 26 additions & 26 deletions components/dependency_injection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ You can register this in the container as a service::

use Symfony\Component\DependencyInjection\ContainerBuilder;

$containerBuilder = new ContainerBuilder();
$containerBuilder->register('mailer', 'Mailer');
$container = new ContainerBuilder();
$container->register('mailer', 'Mailer');

An improvement to the class to make it more flexible would be to allow
the container to set the ``transport`` used. If you change the class
Expand All @@ -68,8 +68,8 @@ Then you can set the choice of transport in the container::

use Symfony\Component\DependencyInjection\ContainerBuilder;

$containerBuilder = new ContainerBuilder();
$containerBuilder
$container = new ContainerBuilder();
$container
->register('mailer', 'Mailer')
->addArgument('sendmail');

Expand All @@ -83,9 +83,9 @@ the ``Mailer`` service's constructor argument::

use Symfony\Component\DependencyInjection\ContainerBuilder;

$containerBuilder = new ContainerBuilder();
$containerBuilder->setParameter('mailer.transport', 'sendmail');
$containerBuilder
$container = new ContainerBuilder();
$container->setParameter('mailer.transport', 'sendmail');
$container
->register('mailer', 'Mailer')
->addArgument('%mailer.transport%');

Expand All @@ -112,14 +112,14 @@ not exist yet. Use the ``Reference`` class to tell the container to inject the
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

$containerBuilder = new ContainerBuilder();
$container = new ContainerBuilder();

$containerBuilder->setParameter('mailer.transport', 'sendmail');
$containerBuilder
$container->setParameter('mailer.transport', 'sendmail');
$container
->register('mailer', 'Mailer')
->addArgument('%mailer.transport%');

$containerBuilder
$container
->register('newsletter_manager', 'NewsletterManager')
->addArgument(new Reference('mailer'));

Expand All @@ -144,14 +144,14 @@ If you do want to though then the container can call the setter method::
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

$containerBuilder = new ContainerBuilder();
$container = new ContainerBuilder();

$containerBuilder->setParameter('mailer.transport', 'sendmail');
$containerBuilder
$container->setParameter('mailer.transport', 'sendmail');
$container
->register('mailer', 'Mailer')
->addArgument('%mailer.transport%');

$containerBuilder
$container
->register('newsletter_manager', 'NewsletterManager')
->addMethodCall('setMailer', [new Reference('mailer')]);

Expand All @@ -160,11 +160,11 @@ like this::

use Symfony\Component\DependencyInjection\ContainerBuilder;

$containerBuilder = new ContainerBuilder();
$container = new ContainerBuilder();

// ...

$newsletterManager = $containerBuilder->get('newsletter_manager');
$newsletterManager = $container->get('newsletter_manager');

Avoiding your Code Becoming Dependent on the Container
------------------------------------------------------
Expand Down Expand Up @@ -198,8 +198,8 @@ Loading an XML config file::
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

$containerBuilder = new ContainerBuilder();
$loader = new XmlFileLoader($containerBuilder, new FileLocator(__DIR__));
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.xml');

Loading a YAML config file::
Expand All @@ -208,8 +208,8 @@ Loading a YAML config file::
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

$containerBuilder = new ContainerBuilder();
$loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__));
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.yaml');

.. note::
Expand All @@ -233,8 +233,8 @@ into a separate config file and load it in a similar way::
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

$containerBuilder = new ContainerBuilder();
$loader = new PhpFileLoader($containerBuilder, new FileLocator(__DIR__));
$container = new ContainerBuilder();
$loader = new PhpFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.php');

You can now set up the ``newsletter_manager`` and ``mailer`` services using
Expand Down Expand Up @@ -287,13 +287,13 @@ config files:

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return static function (ContainerConfigurator $containerConfigurator) {
$containerConfigurator->parameters()
return static function (ContainerConfigurator $container) {
$container->parameters()
// ...
->set('mailer.transport', 'sendmail')
;

$services = $containerConfigurator->services();
$services = $container->services();
$services->set('mailer', 'Mailer')
->args(['%mailer.transport%'])
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return static function (ContainerConfigurator $containerConfigurator) {
$containerConfigurator->import('%kernel.project_dir%/somefile.yaml');
return static function (ContainerConfigurator $container) {
$container->import('%kernel.project_dir%/somefile.yaml');
};
Loading