Commit 34e63095 authored by alexpott's avatar alexpott

Issue #1939660 by chx | catch: Use YAML as the primary means for service registration.

parent 31256993
This diff is collapsed.
......@@ -62,20 +62,4 @@ public function get($bin) {
return $this->container->get($service_name)->get($bin);
}
/**
* Helper to register a cache bin to the container.
*
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
* The container to register the cache bin on.
* @param $bin
* The cache bin to add. Do not add the cache_ prefix.
*/
public static function registerBin(ContainerBuilder $container, $bin) {
$container
->register("cache.$bin", 'Drupal\Core\Cache\CacheBackendInterface')
->setFactoryService('cache_factory')
->setFactoryMethod('get')
->addArgument($bin)
->addTag('cache.bin');
}
}
<?php
/**
* Contains \Drupal\Core\Config\FileStorageFactory.
*/
namespace Drupal\Core\Config;
/**
* Provides a factory for creating config file storage objects.
*/
class FileStorageFactory {
/**
* Returns a FileStorage object working with the active config directory.
*
* @return \Drupal\Core\Config\FileStorage FileStorage
*/
static function getActive() {
return new FileStorage(config_get_config_directory(CONFIG_ACTIVE_DIRECTORY));
}
/**
* Returns a FileStorage object working with the staging config directory.
*
* @return \Drupal\Core\Config\FileStorage FileStorage
*/
static function getStaging() {
return new FileStorage(config_get_config_directory(CONFIG_STAGING_DIRECTORY));
}
}
This diff is collapsed.
<?php
/**
* @file
* Contains \Drupal\Core\DependencyInjection\YamlFileLoader.
*/
namespace Drupal\Core\DependencyInjection;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Yaml\Parser;
/**
* YamlFileLoader loads YAML files service definitions.
*
* Drupal does not use Symfony's Config component, so this is a partial copy
* of \Symfony\Component\DependencyInjection\Loader\YamlFileLoader class not
* depending on the Config component.
*/
class YamlFileLoader {
/**
* @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
*/
protected $container;
public function __construct(ContainerBuilder $container) {
$this->container = $container;
}
/**
* Load a YAML file containing service definitions and kernel parameters.
*
* string $filename
* The name of the file to load.
*/
public function load($filename) {
$content = $this->loadFile($filename);
$content += array('parameters' => array(), 'services' => array());
// parameters
foreach ($content['parameters'] as $key => $value) {
$this->container->setParameter($key, $this->resolveServices($value));
}
// services
foreach ($content['services'] as $id => $service) {
$this->parseDefinition($id, $service, $filename);
}
}
/**
* Parses a definition.
*
* Copied from \Symfony\Component\DependencyInjection\Loader\YamlFileLoader::parseDefinition().
*
* @param string $id
* The id of the service.
* @param string|array $service
* Either a string starting with a @ meaning this service is an alias or
* the array defining the service.
* @param string $filename
* The name of the file, only used in error messages.
*
* @throws \InvalidArgumentException When tags are invalid
*/
protected function parseDefinition($id, $service, $filename) {
if (is_string($service) && 0 === strpos($service, '@')) {
$this->container->setAlias($id, substr($service, 1));
return;
}
elseif (isset($service['alias'])) {
$public = !array_key_exists('public', $service) || (Boolean) $service['public'];
$this->container->setAlias($id, new Alias($service['alias'], $public));
return;
}
if (isset($service['parent'])) {
$definition = new DefinitionDecorator($service['parent']);
}
else {
$definition = new Definition();
}
if (isset($service['class'])) {
$definition->setClass($service['class']);
}
if (isset($service['scope'])) {
$definition->setScope($service['scope']);
}
if (isset($service['synthetic'])) {
$definition->setSynthetic($service['synthetic']);
}
if (isset($service['public'])) {
$definition->setPublic($service['public']);
}
if (isset($service['abstract'])) {
$definition->setAbstract($service['abstract']);
}
if (isset($service['factory_class'])) {
$definition->setFactoryClass($service['factory_class']);
}
if (isset($service['factory_method'])) {
$definition->setFactoryMethod($service['factory_method']);
}
if (isset($service['factory_service'])) {
$definition->setFactoryService($service['factory_service']);
}
if (isset($service['file'])) {
$definition->setFile($service['file']);
}
if (isset($service['arguments'])) {
$definition->setArguments($this->resolveServices($service['arguments']));
}
if (isset($service['properties'])) {
$definition->setProperties($this->resolveServices($service['properties']));
}
if (isset($service['configurator'])) {
if (is_string($service['configurator'])) {
$definition->setConfigurator($service['configurator']);
}
else {
$definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
}
}
if (isset($service['calls'])) {
foreach ($service['calls'] as $call) {
$args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
$definition->addMethodCall($call[0], $args);
}
}
if (isset($service['tags'])) {
if (!is_array($service['tags'])) {
throw new \InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s.', $id, $filename));
}
foreach ($service['tags'] as $tag) {
if (!isset($tag['name'])) {
throw new \InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $filename));
}
$name = $tag['name'];
unset($tag['name']);
foreach ($tag as $value) {
if (!is_scalar($value)) {
throw new \InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s" in %s.', $id, $name, $filename));
}
}
$definition->addTag($name, $tag);
}
}
$this->container->setDefinition($id, $definition);
}
/**
* Loads a YAML file.
*
* @param string $filename
*
* @return array
* The file content.
*/
protected function loadFile($filename) {
$parser = new Parser();
return $this->validate($parser->parse(file_get_contents($filename)), $filename);
}
/**
* Validates a YAML file.
*
* @param mixed $content
* The parsed YAML file.
* @param string $filename
* The name of the file, only used for error messages.
*
* @return array
* The $content unchanged returned to allow for chaining this method.
*
* @throws \InvalidArgumentException When service file is not valid
*/
protected function validate($content, $filename) {
if (NULL === $content) {
return $content;
}
if (!is_array($content)) {
throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid: it is not an array.', $filename));
}
if ($keys = array_diff_key($content, array('parameters' => TRUE, 'services' => TRUE))) {
$invalid_keys = htmlspecialchars(implode(', ', $keys), ENT_QUOTES, 'UTF-8');
throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid: it contains invalid keys %s.', $filename, $invalid_keys));
}
return $content;
}
/**
* Resolves services.
*
* Copied from \Symfony\Component\DependencyInjection\Loader\YamlFileLoader::parseDefinition().
*
* @param mixed $value
* If a string, then it is either a plain string (for example a class
* name) or a reference to a service. If it's an array then it's a list of
* such strings.
*
* @return string|\Symfony\Component\DependencyInjection\Reference
* Either the string unchanged or the Reference object.
*/
protected function resolveServices($value) {
if (is_array($value)) {
$value = array_map(array($this, 'resolveServices'), $value);
}
elseif (is_string($value) && 0 === strpos($value, '@')) {
if (0 === strpos($value, '@?')) {
$value = substr($value, 2);
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
}
else {
$value = substr($value, 1);
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
}
if ('=' === substr($value, -1)) {
$value = substr($value, 0, -1);
$strict = FALSE;
}
else {
$strict = TRUE;
}
$value = new Reference($value, $invalidBehavior, $strict);
}
return $value;
}
}
......@@ -11,6 +11,7 @@
use Drupal\Core\Config\BootstrapConfigStorageFactory;
use Drupal\Core\CoreBundle;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\YamlFileLoader;
use Symfony\Component\ClassLoader\ClassLoader;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
......@@ -99,6 +100,13 @@ class DrupalKernel extends Kernel implements DrupalKernelInterface {
*/
protected $containerNeedsDumping;
/**
* Holds the list of YAML files containing service definitions.
*
* @var array
*/
protected $serviceYamls;
/**
* Constructs a DrupalKernel object.
*
......@@ -160,6 +168,9 @@ public function registerBundles() {
$bundles = array(
new CoreBundle(),
);
$this->serviceYamls = array(
'core/core.services.yml'
);
$this->bundleClasses = array('Drupal\Core\CoreBundle');
// Ensure we know what modules are enabled and that their namespaces are
......@@ -168,7 +179,8 @@ public function registerBundles() {
$module_list = $this->configStorage->read('system.module');
$this->moduleList = isset($module_list['enabled']) ? $module_list['enabled'] : array();
}
$this->registerNamespaces($this->getModuleNamespaces($this->getModuleFileNames()));
$module_filenames = $this->getModuleFileNames();
$this->registerNamespaces($this->getModuleNamespaces($module_filenames));
// Load each module's bundle class.
foreach ($this->moduleList as $module => $weight) {
......@@ -178,6 +190,10 @@ public function registerBundles() {
$bundles[] = new $class();
$this->bundleClasses[] = $class;
}
$filename = dirname($module_filenames[$module]) . "/$module.services.yml";
if (file_exists($filename)) {
$this->serviceYamls[] = $filename;
}
}
// Add site specific or test bundles.
......@@ -187,6 +203,10 @@ public function registerBundles() {
$this->bundleClasses[] = $class;
}
}
// Add site specific or test YAMLs.
if (!empty($GLOBALS['conf']['container_yamls'])) {
$this->serviceYamls = array_merge($this->serviceYamls, $GLOBALS['conf']['container_yamls']);
}
return $bundles;
}
......@@ -379,6 +399,10 @@ protected function buildContainer() {
$container->register('class_loader', 'Symfony\Component\ClassLoader\ClassLoader')->setSynthetic(TRUE);
$container->register('kernel', 'Symfony\Component\HttpKernel\KernelInterface')->setSynthetic(TRUE);
$container->register('service_container', 'Symfony\Component\DependencyInjection\ContainerInterface')->setSynthetic(TRUE);
$yaml_loader = new YamlFileLoader($container);
foreach ($this->serviceYamls as $filename) {
$yaml_loader->load($filename);
}
foreach ($this->bundles as $bundle) {
$bundle->build($container);
}
......
services:
plugin.manager.aggregator.fetcher:
class: Drupal\aggregator\Plugin\AggregatorPluginManager
arguments: [fetcher, '%container.namespaces%']
plugin.manager.aggregator.parser:
class: Drupal\aggregator\Plugin\AggregatorPluginManager
arguments: [parser, '%container.namespaces%']
plugin.manager.aggregator.processor:
class: Drupal\aggregator\Plugin\AggregatorPluginManager
arguments: [processor, '%container.namespaces%']
<?php
/**
* @file
* Contains \Drupal\aggregator\AggregatorBundle.
*/
namespace Drupal\aggregator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Registers aggregator module's services to the container.
*/
class AggregatorBundle extends Bundle {
/**
* Overrides Bundle::build().
*/
public function build(ContainerBuilder $container) {
foreach (array('fetcher', 'parser', 'processor') as $type) {
$container->register("plugin.manager.aggregator.$type", 'Drupal\aggregator\Plugin\AggregatorPluginManager')
->addArgument($type)
->addArgument('%container.namespaces%');
}
}
}
services:
ban.ip_manager:
class: Drupal\ban\BanIpManager
arguments: ['@database']
ban.subscriber:
class: Drupal\ban\EventSubscriber\BanSubscriber
tags:
- { name: event_subscriber }
arguments: ['@ban.ip_manager']
<?php
/**
* @file
* Definition of Drupal\ban\BanBundle.
*/
namespace Drupal\ban;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Defines the Ban bundle.
*/
class BanBundle extends Bundle {
/**
* Overrides Symfony\Component\HttpKernel\Bundle\Bundle::build().
*/
public function build(ContainerBuilder $container) {
$container->register('ban.ip_manager', 'Drupal\ban\BanIpManager')
->addArgument(new Reference('database'));
$container->register('ban.subscriber', 'Drupal\ban\EventSubscriber\BanSubscriber')
->addArgument(new Reference('ban.ip_manager'))
->addTag('event_subscriber');
}
}
services:
plugin.manager.block:
class: Drupal\block\Plugin\Type\BlockManager
arguments: ['%container.namespaces%']
cache.block:
class: Drupal\Core\Cache\CacheBackendInterface
tags:
- { name: cache.bin }
factory_method: get
factory_service: cache_factory
arguments: [block]
<?php
/**
* @file
* Contains \Drupal\block\BlockBundle.
*/
namespace Drupal\Block;
use Drupal\Core\Cache\CacheFactory;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Provides the block dependency injection container.
*/
class BlockBundle extends Bundle {
/**
* Overrides \Symfony\Component\HttpKernel\Bundle\Bundle::build().
*/
public function build(ContainerBuilder $container) {
// Register the BlockManager class with the dependency injection container.
$container->register('plugin.manager.block', 'Drupal\block\Plugin\Type\BlockManager')
->addArgument('%container.namespaces%');
CacheFactory::registerBin($container, 'block');
}
}
services:
plugin.manager.ckeditor.plugin:
class: Drupal\ckeditor\CKEditorPluginManager
arguments: ['%container.namespaces%']
<?php
/**
* @file
* Contains \Drupal\ckeditor\CKEditorBundle.
*/
namespace Drupal\ckeditor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* CKEditor dependency injection container.
*/
class CKEditorBundle extends Bundle {
/**
* Overrides Symfony\Component\HttpKernel\Bundle\Bundle::build().
*/
public function build(ContainerBuilder $container) {
$container->register('plugin.manager.ckeditor.plugin', 'Drupal\ckeditor\CKEditorPluginManager')
->addArgument('%container.namespaces%');
}
}
services:
plugin.manager.edit.editor:
class: Drupal\edit\Plugin\EditorManager
arguments: ['%container.namespaces%']
access_check.edit.entity_field:
class: Drupal\edit\Access\EditEntityFieldAccessCheck
tags:
- { name: access_check }
edit.editor.selector:
class: Drupal\edit\EditorSelector
arguments: ['@plugin.manager.edit.editor']
edit.metadata.generator:
class: Drupal\edit\MetadataGenerator
arguments: ['@access_check.edit.entity_field', '@edit.editor.selector', '@plugin.manager.edit.editor']
<?php
/**
* @file
* Contains \Drupal\edit\EditBundle.
*/
namespace Drupal\edit;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Edit dependency injection container.
*/
class EditBundle extends Bundle {
/**
* Overrides Symfony\Component\HttpKernel\Bundle\Bundle::build().
*/
public function build(ContainerBuilder $container) {
$container->register('plugin.manager.edit.editor', 'Drupal\edit\Plugin\EditorManager')
->addArgument('%container.namespaces%');
$container->register('access_check.edit.entity_field', 'Drupal\edit\Access\EditEntityFieldAccessCheck')
->addTag('access_check');
$container->register('edit.editor.selector', 'Drupal\edit\EditorSelector')
->addArgument(new Reference('plugin.manager.edit.editor'));
$container->register('edit.metadata.generator', 'Drupal\edit\MetadataGenerator')
->addArgument(new Reference('access_check.edit.entity_field'))
->addArgument(new Reference('edit.editor.selector'))
->addArgument(new Reference('plugin.manager.edit.editor'));
}
}
services:
plugin.manager.editor:
class: Drupal\editor\Plugin\EditorManager
arguments: ['%container.namespaces%']
<?php
/**
* @file
* Contains \Drupal\editor\EditorBundle.
*/
namespace Drupal\editor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Editor dependency injection container.
*/
class EditorBundle extends Bundle {
/**
* Overrides Symfony\Component\HttpKernel\Bundle\Bundle::build().
*/
public function build(ContainerBuilder $container) {
// Register the plugin manager for our plugin type with the dependency
// injection container.
$container->register('plugin.manager.editor', 'Drupal\editor\Plugin\EditorManager')
->addArgument('%container.namespaces%');
}
}
services:
plugin.manager.entity_reference.selection:
class: Drupal\entity_reference\Plugin\Type\SelectionPluginManager
arguments: ['%container.namespaces%']
entity_reference.autocomplete:
class: Drupal\entity_reference\EntityReferenceAutocomplete
arguments: ['@plugin.manager.entity']
<?php
/**
* @file
* Contains \Drupal\entity_reference\EntityReferenceBundle.
*/
namespace Drupal\entity_reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Entity Reference dependency injection container.
*/
class EntityReferenceBundle extends Bundle {
/**
* Overrides Symfony\Component\HttpKernel\Bundle\Bundle::build().
*/
public function build(ContainerBuilder $container) {
// Register the SelectionPluginManager class and the autocomplete helper
// with the dependency injection container.
$container->register('plugin.manager.entity_reference.selection', 'Drupal\entity_reference\Plugin\Type\SelectionPluginManager')
->addArgument('%container.namespaces%');