Author: Johannes M. Schmitt (schmittjoh@gmail.com)
Inheritance: extends Definition
コード例 #1
0
ファイル: CachePoolPassTest.php プロジェクト: symfony/symfony
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage Invalid "cache.pool" tag for service "app.cache_pool": accepted attributes are
  */
 public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes()
 {
     $container = new ContainerBuilder();
     $container->setParameter('kernel.debug', false);
     $container->setParameter('kernel.name', 'app');
     $container->setParameter('kernel.environment', 'prod');
     $container->setParameter('kernel.root_dir', 'foo');
     $adapter = new Definition();
     $adapter->setAbstract(true);
     $adapter->addTag('cache.pool');
     $container->setDefinition('app.cache_adapter', $adapter);
     $cachePool = new ChildDefinition('app.cache_adapter');
     $cachePool->addTag('cache.pool', array('foobar' => 123));
     $container->setDefinition('app.cache_pool', $cachePool);
     $this->cachePoolPass->process($container);
 }
コード例 #2
0
ファイル: JsonLoginFactory.php プロジェクト: symfony/symfony
 /**
  * {@inheritdoc}
  */
 protected function createListener($container, $id, $config, $userProvider)
 {
     $listenerId = $this->getListenerId();
     $listener = new ChildDefinition($listenerId);
     $listener->replaceArgument(2, $id);
     $listener->replaceArgument(3, new Reference($this->createAuthenticationSuccessHandler($container, $id, $config)));
     $listener->replaceArgument(4, new Reference($this->createAuthenticationFailureHandler($container, $id, $config)));
     $listener->replaceArgument(5, array_intersect_key($config, $this->options));
     $listenerId .= '.' . $id;
     $container->setDefinition($listenerId, $listener);
     return $listenerId;
 }
コード例 #3
0
    private function doResolveDefinition(ContainerBuilder $container, ChildDefinition $definition)
    {
        if (!$container->has($parent = $definition->getParent())) {
            throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
        }

        $parentDef = $container->findDefinition($parent);
        if ($parentDef instanceof ChildDefinition) {
            $id = $this->currentId;
            $this->currentId = $parent;
            $parentDef = $this->resolveDefinition($container, $parentDef);
            $container->setDefinition($parent, $parentDef);
            $this->currentId = $id;
        }

        $this->compiler->addLogMessage($this->formatter->formatResolveInheritance($this, $this->currentId, $parent));
        $def = new Definition();

        // merge in parent definition
        // purposely ignored attributes: abstract, tags
        $def->setClass($parentDef->getClass());
        $def->setArguments($parentDef->getArguments());
        $def->setMethodCalls($parentDef->getMethodCalls());
        $def->setProperties($parentDef->getProperties());
        $def->setAutowiringTypes($parentDef->getAutowiringTypes());
        if ($parentDef->isDeprecated()) {
            $def->setDeprecated(true, $parentDef->getDeprecationMessage('%service_id%'));
        }
        $def->setFactory($parentDef->getFactory());
        $def->setConfigurator($parentDef->getConfigurator());
        $def->setFile($parentDef->getFile());
        $def->setPublic($parentDef->isPublic());
        $def->setLazy($parentDef->isLazy());
        $def->setAutowired($parentDef->isAutowired());

        // overwrite with values specified in the decorator
        $changes = $definition->getChanges();
        if (isset($changes['class'])) {
            $def->setClass($definition->getClass());
        }
        if (isset($changes['factory'])) {
            $def->setFactory($definition->getFactory());
        }
        if (isset($changes['configurator'])) {
            $def->setConfigurator($definition->getConfigurator());
        }
        if (isset($changes['file'])) {
            $def->setFile($definition->getFile());
        }
        if (isset($changes['public'])) {
            $def->setPublic($definition->isPublic());
        }
        if (isset($changes['lazy'])) {
            $def->setLazy($definition->isLazy());
        }
        if (isset($changes['deprecated'])) {
            $def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
        }
        if (isset($changes['autowire'])) {
            $def->setAutowired($definition->isAutowired());
        }
        if (isset($changes['decorated_service'])) {
            $decoratedService = $definition->getDecoratedService();
            if (null === $decoratedService) {
                $def->setDecoratedService($decoratedService);
            } else {
                $def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2]);
            }
        }

        // merge arguments
        foreach ($definition->getArguments() as $k => $v) {
            if (is_numeric($k)) {
                $def->addArgument($v);
                continue;
            }

            if (0 !== strpos($k, 'index_')) {
                throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
            }

            $index = (int) substr($k, strlen('index_'));
            $def->replaceArgument($index, $v);
        }

        // merge properties
        foreach ($definition->getProperties() as $k => $v) {
            $def->setProperty($k, $v);
        }

        // append method calls
        if (count($calls = $definition->getMethodCalls()) > 0) {
            $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
        }

        // merge autowiring types
        foreach ($definition->getAutowiringTypes() as $autowiringType) {
            $def->addAutowiringType($autowiringType);
        }

        // these attributes are always taken from the child
        $def->setAbstract($definition->isAbstract());
        $def->setShared($definition->isShared());
        $def->setTags($definition->getTags());

        return $def;
    }
コード例 #4
0
    /**
     * @expectedException \OutOfBoundsException
     */
    public function testGetArgumentShouldCheckBounds()
    {
        $def = new ChildDefinition('foo');

        $def->setArguments(array(0 => 'foo'));
        $def->replaceArgument(0, 'foo');

        $def->getArgument(1);
    }
コード例 #5
0
 private function registerCacheConfiguration(array $config, ContainerBuilder $container)
 {
     $version = substr(str_replace('/', '-', base64_encode(hash('sha256', uniqid(mt_rand(), true), true))), 0, 22);
     $container->getDefinition('cache.annotations')->replaceArgument(2, $version);
     $container->getDefinition('cache.adapter.apcu')->replaceArgument(2, $version);
     $container->getDefinition('cache.adapter.system')->replaceArgument(2, $version);
     $container->getDefinition('cache.adapter.filesystem')->replaceArgument(2, $config['directory']);
     if (isset($config['prefix_seed'])) {
         $container->setParameter('cache.prefix.seed', $config['prefix_seed']);
     }
     if ($container->hasParameter('cache.prefix.seed')) {
         // Inline any env vars referenced in the parameter
         $container->setParameter('cache.prefix.seed', $container->resolveEnvPlaceholders($container->getParameter('cache.prefix.seed'), true));
     }
     foreach (array('doctrine', 'psr6', 'redis') as $name) {
         if (isset($config[$name = 'default_' . $name . '_provider'])) {
             $container->setAlias('cache.' . $name, new Alias(Compiler\CachePoolPass::getServiceProvider($container, $config[$name]), false));
         }
     }
     foreach (array('app', 'system') as $name) {
         $config['pools']['cache.' . $name] = array('adapter' => $config[$name], 'public' => true);
     }
     foreach ($config['pools'] as $name => $pool) {
         $definition = new ChildDefinition($pool['adapter']);
         $definition->setPublic($pool['public']);
         unset($pool['adapter'], $pool['public']);
         $definition->addTag('cache.pool', $pool);
         $container->setDefinition($name, $definition);
     }
     if (method_exists(PropertyAccessor::class, 'createCache')) {
         $propertyAccessDefinition = $container->register('cache.property_access', AdapterInterface::class);
         $propertyAccessDefinition->setPublic(false);
         $propertyAccessDefinition->setFactory(array(PropertyAccessor::class, 'createCache'));
         $propertyAccessDefinition->setArguments(array(null, null, $version, new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
         $propertyAccessDefinition->addTag('cache.pool', array('clearer' => 'cache.default_clearer'));
         $propertyAccessDefinition->addTag('monolog.logger', array('channel' => 'cache'));
     }
     $this->addClassesToCompile(array('Symfony\\Component\\Cache\\Adapter\\ApcuAdapter', 'Symfony\\Component\\Cache\\Adapter\\FilesystemAdapter', 'Symfony\\Component\\Cache\\CacheItem'));
 }
コード例 #6
0
 private function assertUrlPackage(ContainerBuilder $container, ChildDefinition $package, $baseUrls, $version, $format)
 {
     $this->assertEquals('assets.url_package', $package->getParent());
     $this->assertEquals($baseUrls, $package->getArgument(0));
     $this->assertVersionStrategy($container, $package->getArgument(1), $version, $format);
 }