Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Exception / ServiceContainer / ExceptionExtension.php
1 <?php
2
3 /*
4  * This file is part of the Behat Testwork.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Testwork\Exception\ServiceContainer;
12
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;
22
23 /**
24  * Provides exception handling services.
25  *
26  * @author Konstantin Kudryashov <ever.zet@gmail.com>
27  */
28 final class ExceptionExtension implements Extension
29 {
30     /*
31      * Available services
32      */
33     const PRESENTER_ID = 'exception.presenter';
34
35     /*
36      * Available extension points
37      */
38     const STRINGER_TAG = 'exception.stringer';
39
40     /**
41      * @var ServiceProcessor
42      */
43     private $processor;
44
45     /**
46      * Initializes extension.
47      *
48      * @param null|ServiceProcessor $processor
49      */
50     public function __construct(ServiceProcessor $processor = null)
51     {
52         $this->processor = $processor ? : new ServiceProcessor();
53     }
54
55     /**
56      * {@inheritdoc}
57      */
58     public function getConfigKey()
59     {
60         return 'exceptions';
61     }
62
63     /**
64      * {@inheritdoc}
65      */
66     public function initialize(ExtensionManager $extensionManager)
67     {
68     }
69
70     /**
71      * {@inheritdoc}
72      */
73     public function configure(ArrayNodeDefinition $builder)
74     {
75         $builder
76             ->addDefaultsIfNotSet()
77             ->children()
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
85                     ))
86                     ->defaultValue(OutputPrinter::VERBOSITY_NORMAL)
87                 ->end()
88             ->end()
89         ;
90     }
91
92     /**
93      * {@inheritdoc}
94      */
95     public function load(ContainerBuilder $container, array $config)
96     {
97         $this->loadPresenter($container, $config['verbosity']);
98         $this->loadDefaultStringers($container);
99         $this->loadVerbosityController($container);
100     }
101
102     /**
103      * {@inheritdoc}
104      */
105     public function process(ContainerBuilder $container)
106     {
107         $this->processStringers($container);
108     }
109
110     /**
111      * Loads exception presenter.
112      *
113      * @param ContainerBuilder $container
114      * @param integer          $verbosity
115      */
116     protected function loadPresenter(ContainerBuilder $container, $verbosity)
117     {
118         $definition = new Definition('Behat\Testwork\Exception\ExceptionPresenter', array(
119             '%paths.base%',
120             $verbosity
121         ));
122         $container->setDefinition(self::PRESENTER_ID, $definition);
123     }
124
125     /**
126      * Loads default stringer.
127      *
128      * @param ContainerBuilder $container
129      */
130     protected function loadDefaultStringers(ContainerBuilder $container)
131     {
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);
135
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);
139     }
140
141     /**
142      * Processes all available exception stringers.
143      *
144      * @param ContainerBuilder $container
145      */
146     protected function processStringers(ContainerBuilder $container)
147     {
148         $references = $this->processor->findAndSortTaggedServices($container, self::STRINGER_TAG);
149         $definition = $container->getDefinition(self::PRESENTER_ID);
150
151         foreach ($references as $reference) {
152             $definition->addMethodCall('registerExceptionStringer', array($reference));
153         }
154     }
155
156     /**
157      * Loads verbosity controller.
158      *
159      * @param ContainerBuilder $container
160      */
161     protected function loadVerbosityController($container)
162     {
163         $definition = new Definition('Behat\Testwork\Exception\Cli\VerbosityController', array(
164             new Reference(self::PRESENTER_ID)
165         ));
166         $definition->addTag(CliExtension::CONTROLLER_TAG, array('priority' => 9999));
167         $container->setDefinition(CliExtension::CONTROLLER_TAG . '.exception_verbosity', $definition);
168     }
169 }