예제 #1
0
 /**
  * Add the on demand rebuild route provider service.
  *
  * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
  */
 public static function addRouteProvider(ContainerBuilder $container)
 {
     foreach (['router.route_provider' => 'RouteProvider'] as $original_id => $class) {
         // While $container->get() does a recursive resolve, getDefinition() does
         // not, so do it ourselves.
         for ($id = $original_id; $container->hasAlias($id); $id = (string) $container->getAlias($id)) {
         }
         $definition = $container->getDefinition($id);
         $definition->clearTag('needs_destruction');
         $container->setDefinition("simpletest.{$original_id}", $definition);
         $container->setDefinition($id, new Definition('Drupal\\simpletest\\' . $class));
     }
 }
 /**
  * @covers ::process
  */
 public function testEncoders()
 {
     $container = new ContainerBuilder();
     $container->setDefinition('serializer', new Definition(Serializer::class, [[], []]));
     $definition = new Definition('TestClass');
     $definition->addTag('authentication_provider', ['provider_id' => 'bunny_auth']);
     $definition->addTag('_provider', ['provider' => 'test_provider_a']);
     $container->setDefinition('test_provider_a.authentication.bunny_auth', $definition);
     $definition = new Definition('TestClass');
     $definition->addTag('authentication_provider', ['provider_id' => 'llama_auth', 'priority' => 100]);
     $definition->addTag('_provider', ['provider' => 'test_provider_a']);
     $container->setDefinition('test_provider_a.authentication.llama_auth', $definition);
     $definition = new Definition('TestClass');
     $definition->addTag('authentication_provider', ['provider_id' => 'camel_auth', 'priority' => -100]);
     $definition->addTag('_provider', ['provider' => 'test_provider_b']);
     $container->setDefinition('test_provider_b.authentication.camel_auth', $definition);
     $compiler_pass = new AuthenticationProviderPass();
     $compiler_pass->process($container);
     $this->assertEquals(['bunny_auth' => 'test_provider_a', 'llama_auth' => 'test_provider_a', 'camel_auth' => 'test_provider_b'], $container->getParameter('authentication_providers'));
 }
 /**
  * {@inheritdoc}
  */
 public function alter(ContainerBuilder $container)
 {
     $modules = $container->getParameter('container.modules');
     // Check for installed Replicate module.
     if (isset($modules['replicate'])) {
         // Add a Replicate field event subscriber.
         $service_definition = new Definition('Drupal\\paragraphs\\EventSubscriber\\ReplicateFieldSubscriber', [new Reference('replicate.replicator')]);
         $service_definition->addTag('event_subscriber');
         $container->setDefinition('replicate.event_subscriber.paragraphs', $service_definition);
     }
 }
 /**
  * @covers ::process
  */
 public function testEncoders()
 {
     $container = new ContainerBuilder();
     $container->setDefinition('serializer', new Definition(Serializer::class, [[], []]));
     $definition = new Definition('TestClass');
     $definition->addTag('encoder', ['format' => 'xml']);
     $definition->addTag('_provider', ['provider' => 'test_provider_a']);
     $container->setDefinition('encoder_1', $definition);
     $definition = new Definition('TestClass');
     $definition->addTag('encoder', ['format' => 'json']);
     $definition->addTag('_provider', ['provider' => 'test_provider_a']);
     $container->setDefinition('encoder_2', $definition);
     $definition = new Definition('TestClass');
     $definition->addTag('encoder', ['format' => 'hal_json']);
     $definition->addTag('_provider', ['provider' => 'test_provider_b']);
     $container->setDefinition('encoder_3', $definition);
     $compiler_pass = new RegisterSerializationClassesCompilerPass();
     $compiler_pass->process($container);
     $this->assertEquals(['xml', 'json', 'hal_json'], $container->getParameter('serializer.formats'));
     $this->assertEquals(['xml' => 'test_provider_a', 'json' => 'test_provider_a', 'hal_json' => 'test_provider_b'], $container->getParameter('serializer.format_providers'));
 }
 /**
  * {@inheritdoc}
  */
 public function alter(ContainerBuilder $container)
 {
     $modules = $container->getParameter('container.modules');
     if (isset($modules['hal'])) {
         // Hal module is enabled, add our new normalizer for entity reference
         // revision items.
         $service_definition = new Definition('Drupal\\entity_reference_revisions\\Normalizer\\EntityReferenceRevisionItemNormalizer', array(new Reference('rest.link_manager'), new Reference('serializer.entity_resolver')));
         // The priority must be higher than that of
         // serializer.normalizer.entity_reference_item.hal in
         // hal.services.yml.
         $service_definition->addTag('normalizer', array('priority' => 20));
         $container->setDefinition('serializer.normalizer.entity_reference_revision_item', $service_definition);
     }
 }
예제 #6
0
 /**
  * {@inheritdoc}
  */
 public function register(ContainerBuilder $container)
 {
     $definition = new Definition('Drupal\\Core\\Cache\\NullBackend', ['null']);
     $container->setDefinition('cache.null', $definition);
 }
예제 #7
0
 /**
  * Parses a definition.
  *
  * @param string $id
  * @param array  $service
  * @param string $file
  *
  * @throws InvalidArgumentException When tags are invalid
  */
 private function parseDefinition($id, $service, $file)
 {
     if (is_string($service) && 0 === strpos($service, '@')) {
         $this->container->setAlias($id, substr($service, 1));
         return;
     }
     if (!is_array($service)) {
         throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but %s found for service "%s" in %s. Check your YAML syntax.', gettype($service), $id, $file));
     }
     if (isset($service['alias'])) {
         $public = !array_key_exists('public', $service) || (bool) $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['synchronized'])) {
         $definition->setSynchronized($service['synchronized'], 'request' !== $id);
     }
     if (isset($service['lazy'])) {
         $definition->setLazy($service['lazy']);
     }
     if (isset($service['public'])) {
         $definition->setPublic($service['public']);
     }
     if (isset($service['abstract'])) {
         $definition->setAbstract($service['abstract']);
     }
     if (isset($service['factory'])) {
         if (is_string($service['factory'])) {
             if (strpos($service['factory'], ':') !== false && strpos($service['factory'], '::') === false) {
                 $parts = explode(':', $service['factory']);
                 $definition->setFactory(array($this->resolveServices('@' . $parts[0]), $parts[1]));
             } else {
                 $definition->setFactory($service['factory']);
             }
         } else {
             $definition->setFactory(array($this->resolveServices($service['factory'][0]), $service['factory'][1]));
         }
     }
     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'])) {
         if (!is_array($service['calls'])) {
             throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
         }
         foreach ($service['calls'] as $call) {
             if (isset($call['method'])) {
                 $method = $call['method'];
                 $args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : array();
             } else {
                 $method = $call[0];
                 $args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
             }
             $definition->addMethodCall($method, $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. Check your YAML syntax.', $id, $file));
         }
         foreach ($service['tags'] as $tag) {
             if (!is_array($tag)) {
                 throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
             }
             if (!isset($tag['name'])) {
                 throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
             }
             $name = $tag['name'];
             unset($tag['name']);
             foreach ($tag as $attribute => $value) {
                 if (!is_scalar($value) && null !== $value) {
                     throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file));
                 }
             }
             $definition->addTag($name, $tag);
         }
     }
     if (isset($service['decorates'])) {
         $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null;
         $definition->setDecoratedService($service['decorates'], $renameId);
     }
     $this->container->setDefinition($id, $definition);
 }
예제 #8
0
 /**
  * {@inheritdoc}
  */
 public function alter(ContainerBuilder $container)
 {
     $container->setDefinition('password_original', $container->getDefinition('password'));
     $container->setDefinition('password', $container->getDefinition('password_migrate'));
 }
<?php

/*
 * Example 2: Explain the concept of Definition.
 */
namespace Acquia\D8\Workshop;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Stopwatch\Stopwatch;
require __DIR__ . '../../../vendor/autoload.php';
$container = new ContainerBuilder();
// Simple example without any parameters.
$loggerDefinition = new Definition('Symfony\\Component\\Stopwatch\\Stopwatch');
$container->setDefinition('stopwatch', $loggerDefinition);
// At this point the container knows 'how' to create the StopWatch service.
runApp($container);
function runApp(ContainerBuilder $container)
{
    $container->get('stopwatch')->start('Acquia D8 Workshop');
    // Calculate something.
    $factorial = 1;
    for ($x = 100; $x >= 1; $x--) {
        $factorial = $factorial * $x;
    }
    dump($factorial);
    $stop = $container->get('stopwatch')->stop('Acquia D8 Workshop');
    print_r($stop->getMemory() . "\n");
}
예제 #10
0
 /**
  * Parses a definition.
  *
  * @param string $id
  * @param array  $service
  * @param string $file
  *
  * @throws InvalidArgumentException When tags are invalid
  */
 private function parseDefinition($id, $service, $file)
 {
     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) || (bool) $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['synchronized'])) {
         $definition->setSynchronized($service['synchronized']);
     }
     if (isset($service['lazy'])) {
         $definition->setLazy($service['lazy']);
     }
     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, $file));
         }
         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, $file));
             }
             $name = $tag['name'];
             unset($tag['name']);
             foreach ($tag as $attribute => $value) {
                 if (!is_scalar($value) && null !== $value) {
                     throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s.', $id, $name, $attribute, $file));
                 }
             }
             $definition->addTag($name, $tag);
         }
     }
     $this->container->setDefinition($id, $definition);
 }