4 * This file is part of the Behat.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Behat\HelperContainer\ServiceContainer;
13 use Behat\Behat\Context\ServiceContainer\ContextExtension;
14 use Behat\Behat\HelperContainer\Exception\WrongServicesConfigurationException;
15 use Behat\Testwork\Call\ServiceContainer\CallExtension;
16 use Behat\Testwork\ServiceContainer\Extension;
17 use Behat\Testwork\ServiceContainer\ExtensionManager;
18 use Behat\Testwork\ServiceContainer\ServiceProcessor;
19 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
20 use Symfony\Component\DependencyInjection\ContainerBuilder;
21 use Symfony\Component\DependencyInjection\Definition;
24 * Behat helper container extension.
26 * Extends Behat with helper containers support.
28 * @author Konstantin Kudryashov <ever.zet@gmail.com>
30 final class HelperContainerExtension implements Extension
33 * Available extension points
35 const HELPER_CONTAINER_TAG = 'helper_container.container';
38 * @var ServiceProcessor
43 * Initializes compiler pass.
45 * @param null|ServiceProcessor $processor
47 public function __construct(ServiceProcessor $processor = null)
49 $this->processor = $processor ? : new ServiceProcessor();
55 public function getConfigKey()
57 return 'helper_container';
63 public function initialize(ExtensionManager $extensionManager)
70 public function configure(ArrayNodeDefinition $builder)
77 public function load(ContainerBuilder $container, array $config)
79 $definition = new Definition('Behat\Behat\HelperContainer\Argument\ServicesResolverFactory', array($container));
80 $definition->addTag(ContextExtension::SUITE_SCOPED_RESOLVER_FACTORY_TAG, array('priority' => 0));
81 $container->setDefinition(ContextExtension::SUITE_SCOPED_RESOLVER_FACTORY_TAG . '.helper_container', $definition);
83 $definition = new Definition('Behat\Behat\HelperContainer\Call\Filter\ServicesResolver');
84 $definition->addTag(CallExtension::CALL_FILTER_TAG, array('priority' => 0));
85 $container->setDefinition(CallExtension::CALL_FILTER_TAG . '.helper_container', $definition);
91 public function process(ContainerBuilder $container)
93 $references = $this->processor->findAndSortTaggedServices($container, self::HELPER_CONTAINER_TAG);
95 foreach ($references as $reference) {
96 if ($this->isDefinitionShared($container->getDefinition((string) $reference))) {
97 throw new WrongServicesConfigurationException(sprintf(
98 'Container services must not be configured as shared, but `@%s` is.', $reference
105 * Checks if provided definition is shared.
107 * @param Definition $definition
111 * @todo Remove after upgrading to Symfony 2.8+
113 private function isDefinitionShared(Definition $definition)
115 if (method_exists($definition, 'isShared')) {
116 return $definition->isShared();
117 } else if (method_exists($definition, 'getScope')) {
118 return $definition->getScope() !== ContainerBuilder::SCOPE_PROTOTYPE;