4 * This file is part of the Behat Testwork.
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\Testwork\Call\ServiceContainer;
13 use Behat\Testwork\ServiceContainer\Extension;
14 use Behat\Testwork\ServiceContainer\ExtensionManager;
15 use Behat\Testwork\ServiceContainer\ServiceProcessor;
16 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 use Symfony\Component\DependencyInjection\Definition;
21 * Provides call services for testwork.
23 * @author Konstantin Kudryashov <ever.zet@gmail.com>
25 final class CallExtension implements Extension
30 const CALL_CENTER_ID = 'call.center';
33 * Available extension points
35 const CALL_FILTER_TAG = 'call.call_filter';
36 const CALL_HANDLER_TAG = 'call.call_handler';
37 const RESULT_FILTER_TAG = 'call.result_filter';
38 const EXCEPTION_HANDLER_TAG = 'call.exception_handler';
41 * @var ServiceProcessor
46 * Initializes extension.
48 * @param null|ServiceProcessor $processor
50 public function __construct(ServiceProcessor $processor = null)
52 $this->processor = $processor ? : new ServiceProcessor();
58 public function getConfigKey()
66 public function initialize(ExtensionManager $extensionManager)
73 public function configure(ArrayNodeDefinition $builder)
76 ->addDefaultsIfNotSet()
78 ->scalarNode('error_reporting')
79 ->info('Call executor will catch exceptions matching this level')
80 ->defaultValue(E_ALL | E_STRICT)
89 public function load(ContainerBuilder $container, array $config)
91 $this->loadCallCenter($container);
92 $this->loadCallHandlers($container, $config['error_reporting']);
98 public function process(ContainerBuilder $container)
100 $this->processCallFilters($container);
101 $this->processCallHandlers($container);
102 $this->processResultFilters($container);
103 $this->processExceptionHandlers($container);
107 * Loads call center service.
109 * @param ContainerBuilder $container
111 protected function loadCallCenter(ContainerBuilder $container)
113 $definition = new Definition('Behat\Testwork\Call\CallCenter');
114 $container->setDefinition(self::CALL_CENTER_ID, $definition);
118 * Loads prebuilt call handlers.
120 * @param ContainerBuilder $container
121 * @param integer $errorReporting
123 protected function loadCallHandlers(ContainerBuilder $container, $errorReporting)
125 $definition = new Definition('Behat\Testwork\Call\Handler\RuntimeCallHandler', array($errorReporting));
126 $definition->addTag(self::CALL_HANDLER_TAG, array('priority' => 50));
127 $container->setDefinition(self::CALL_HANDLER_TAG . '.runtime', $definition);
131 * Registers all call filters to the CallCenter.
133 * @param ContainerBuilder $container
135 protected function processCallFilters(ContainerBuilder $container)
137 $references = $this->processor->findAndSortTaggedServices($container, CallExtension::CALL_FILTER_TAG);
138 $definition = $container->getDefinition(CallExtension::CALL_CENTER_ID);
140 foreach ($references as $reference) {
141 $definition->addMethodCall('registerCallFilter', array($reference));
146 * Registers all call handlers to the CallCenter.
148 * @param ContainerBuilder $container
150 protected function processCallHandlers(ContainerBuilder $container)
152 $references = $this->processor->findAndSortTaggedServices($container, CallExtension::CALL_HANDLER_TAG);
153 $definition = $container->getDefinition(CallExtension::CALL_CENTER_ID);
155 foreach ($references as $reference) {
156 $definition->addMethodCall('registerCallHandler', array($reference));
161 * Registers all call result filters to the CallCenter.
163 * @param ContainerBuilder $container
165 protected function processResultFilters(ContainerBuilder $container)
167 $references = $this->processor->findAndSortTaggedServices($container, CallExtension::RESULT_FILTER_TAG);
168 $definition = $container->getDefinition(CallExtension::CALL_CENTER_ID);
170 foreach ($references as $reference) {
171 $definition->addMethodCall('registerResultFilter', array($reference));
176 * Registers all exception handlers to the CallCenter.
178 * @param ContainerBuilder $container
180 private function processExceptionHandlers(ContainerBuilder $container)
182 $references = $this->processor->findAndSortTaggedServices($container, CallExtension::EXCEPTION_HANDLER_TAG);
183 $definition = $container->getDefinition(CallExtension::CALL_CENTER_ID);
185 foreach ($references as $reference) {
186 $definition->addMethodCall('registerExceptionHandler', array($reference));