You are here

public function ServiceContainerServiceProvider::alterContainerDefinition in Service Container 7

Same name in this branch
  1. 7 src/ServiceContainer/ServiceProvider/ServiceContainerServiceProvider.php \Drupal\service_container\ServiceContainer\ServiceProvider\ServiceContainerServiceProvider::alterContainerDefinition()
  2. 7 tests/modules/service_container_test_ctools/src/ServiceContainer/ServiceProvider/ServiceContainerServiceProvider.php \Drupal\service_container_test_ctools\ServiceContainer\ServiceProvider\ServiceContainerServiceProvider::alterContainerDefinition()
  3. 7 tests/modules/service_container_test/src/ServiceContainer/ServiceProvider/ServiceContainerServiceProvider.php \Drupal\service_container_test\ServiceContainer\ServiceProvider\ServiceContainerServiceProvider::alterContainerDefinition()
Same name and namespace in other branches
  1. 7.2 src/ServiceContainer/ServiceProvider/ServiceContainerServiceProvider.php \Drupal\service_container\ServiceContainer\ServiceProvider\ServiceContainerServiceProvider::alterContainerDefinition()

Allows to alter the container definition.

Parameters

array $container_definition: An associative array with the following keys:

  • parameters: Simple key-value store of container parameters.
  • services: Services like defined in services.yml
  • tags: Associative array keyed by tag names with array('service_name' => $tag_args) as values.

Overrides ServiceProviderInterface::alterContainerDefinition

See also

ServiceProviderInterface::getContainerDefinition()

File

src/ServiceContainer/ServiceProvider/ServiceContainerServiceProvider.php, line 182
Contains \Drupal\service_container\ServiceContainer\ServiceProvider\ServiceContainerServiceProvider

Class

ServiceContainerServiceProvider
Provides render cache service definitions.

Namespace

Drupal\service_container\ServiceContainer\ServiceProvider

Code

public function alterContainerDefinition(&$container_definition) {
  foreach (array(
    'annotated_plugins_auto_discovery',
    'ctools_plugins_auto_discovery',
  ) as $prefix) {
    $container_definition['parameters'][$prefix] = array();
    foreach ($container_definition['parameters'] as $parameter => $value) {
      if (strpos($parameter, $prefix) === 0) {
        $container_definition['parameters'][$prefix] = array_merge($container_definition['parameters'][$prefix], $value);
      }
    }
  }
  if (!empty($container_definition['parameters']['ctools_plugins_auto_discovery']) && $this
    ->moduleExists('ctools')) {
    $ctools_types = $this
      ->cToolsGetTypes();
    $filtered_types = array_intersect_key($ctools_types, array_flip($container_definition['parameters']['ctools_plugins_auto_discovery']));
    $this
      ->registerCToolsPluginTypes($container_definition, $filtered_types);
  }
  if (!empty($container_definition['parameters']['annotated_plugins_auto_discovery']) && $this
    ->moduleExists('service_container_annotation_discovery')) {
    $this
      ->registerAnnotatedPluginTypes($container_definition, $container_definition['parameters']['annotated_plugins_auto_discovery']);
  }

  // Set empty value when its not set.
  if (empty($container_definition['tags']['plugin_manager'])) {
    $container_definition['tags']['plugin_manager'] = array();
  }

  // Process plugin managers of different types.
  $plugin_manager_types = $container_definition['parameters']['service_container.plugin_manager_types'];
  $all_plugin_managers = $container_definition['parameters']['service_container.plugin_managers'];
  foreach ($all_plugin_managers as $plugin_manager_type => $plugin_managers) {
    if (empty($plugin_manager_types[$plugin_manager_type])) {
      continue;
    }
    $discovery_class = $plugin_manager_types[$plugin_manager_type];
    $this
      ->processPluginManagers($container_definition, $discovery_class, $plugin_managers);
  }

  // Register plugin manager plugins as private services in the container.
  foreach ($container_definition['tags']['plugin_manager'] as $service => $tags) {
    foreach ($tags as $tag) {
      $discovery_class = $tag['discovery_class'];
      $discovery = new $discovery_class($tag['plugin_manager_definition']);
      $definitions = $discovery
        ->getDefinitions();
      foreach ($definitions as $key => $definition) {

        // CTools uses 'file' and 'path', while the service container uses only 'file'.
        // To make this compatible, combine 'file' and 'path' and remove 'path'.
        if (isset($definition['file']) && isset($definition['path'])) {
          $definition['file'] = $definition['path'] . '/' . $definition['file'];
        }
        $container_definition['services'][$tag['prefix'] . $key] = $definition + array(
          'public' => FALSE,
        );
      }
    }
  }
}