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\Exception\ServiceContainer;
13 use Behat\Testwork\Cli\ServiceContainer\CliExtension;
14 use Behat\Testwork\Output\Printer\OutputPrinter;
15 use Behat\Testwork\ServiceContainer\Extension;
16 use Behat\Testwork\ServiceContainer\ExtensionManager;
17 use Behat\Testwork\ServiceContainer\ServiceProcessor;
18 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
20 use Symfony\Component\DependencyInjection\Definition;
21 use Symfony\Component\DependencyInjection\Reference;
24 * Provides exception handling services.
26 * @author Konstantin Kudryashov <ever.zet@gmail.com>
28 final class ExceptionExtension implements Extension
33 const PRESENTER_ID = 'exception.presenter';
36 * Available extension points
38 const STRINGER_TAG = 'exception.stringer';
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('verbosity')
79 ->info('Output verbosity')
80 ->example(sprintf('%d, %d, %d, %d',
81 OutputPrinter::VERBOSITY_NORMAL,
82 OutputPrinter::VERBOSITY_VERBOSE,
83 OutputPrinter::VERBOSITY_VERY_VERBOSE,
84 OutputPrinter::VERBOSITY_DEBUG
86 ->defaultValue(OutputPrinter::VERBOSITY_NORMAL)
95 public function load(ContainerBuilder $container, array $config)
97 $this->loadPresenter($container, $config['verbosity']);
98 $this->loadDefaultStringers($container);
99 $this->loadVerbosityController($container);
105 public function process(ContainerBuilder $container)
107 $this->processStringers($container);
111 * Loads exception presenter.
113 * @param ContainerBuilder $container
114 * @param integer $verbosity
116 protected function loadPresenter(ContainerBuilder $container, $verbosity)
118 $definition = new Definition('Behat\Testwork\Exception\ExceptionPresenter', array(
122 $container->setDefinition(self::PRESENTER_ID, $definition);
126 * Loads default stringer.
128 * @param ContainerBuilder $container
130 protected function loadDefaultStringers(ContainerBuilder $container)
132 $definition = new Definition('Behat\Testwork\Exception\Stringer\PHPUnitExceptionStringer');
133 $definition->addTag(self::STRINGER_TAG, array('priority' => 50));
134 $container->setDefinition(self::STRINGER_TAG . '.phpunit', $definition);
136 $definition = new Definition('Behat\Testwork\Exception\Stringer\TestworkExceptionStringer');
137 $definition->addTag(self::STRINGER_TAG, array('priority' => 50));
138 $container->setDefinition(self::STRINGER_TAG . '.testwork', $definition);
142 * Processes all available exception stringers.
144 * @param ContainerBuilder $container
146 protected function processStringers(ContainerBuilder $container)
148 $references = $this->processor->findAndSortTaggedServices($container, self::STRINGER_TAG);
149 $definition = $container->getDefinition(self::PRESENTER_ID);
151 foreach ($references as $reference) {
152 $definition->addMethodCall('registerExceptionStringer', array($reference));
157 * Loads verbosity controller.
159 * @param ContainerBuilder $container
161 protected function loadVerbosityController($container)
163 $definition = new Definition('Behat\Testwork\Exception\Cli\VerbosityController', array(
164 new Reference(self::PRESENTER_ID)
166 $definition->addTag(CliExtension::CONTROLLER_TAG, array('priority' => 9999));
167 $container->setDefinition(CliExtension::CONTROLLER_TAG . '.exception_verbosity', $definition);