本文整理汇总了PHP中Drupal\Core\DependencyInjection\ContainerBuilder::setDefinition方法的典型用法代码示例。如果您正苦于以下问题:PHP ContainerBuilder::setDefinition方法的具体用法?PHP ContainerBuilder::setDefinition怎么用?PHP ContainerBuilder::setDefinition使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\DependencyInjection\ContainerBuilder
的用法示例。
在下文中一共展示了ContainerBuilder::setDefinition方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addRouteProvider
/**
* 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));
}
}
示例2: testEncoders
/**
* @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'));
}
示例3: alter
/**
* {@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);
}
}
示例4: testEncoders
/**
* @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'));
}
示例5: alter
/**
* {@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: register
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container)
{
$definition = new Definition('Drupal\\Core\\Cache\\NullBackend', ['null']);
$container->setDefinition('cache.null', $definition);
}
示例7: parseDefinition
//.........这里部分代码省略.........
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: alter
/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container)
{
$container->setDefinition('password_original', $container->getDefinition('password'));
$container->setDefinition('password', $container->getDefinition('password_migrate'));
}
示例9: runApp
<?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: parseDefinition
/**
* 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);
}