Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Suite / SuiteRegistry.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\Suite;
12
13 use Behat\Testwork\Suite\Exception\SuiteConfigurationException;
14 use Behat\Testwork\Suite\Exception\SuiteGenerationException;
15 use Behat\Testwork\Suite\Generator\SuiteGenerator;
16
17 /**
18  * Acts like a suite repository by auto-generating suites for registered suite configurations using registered
19  * generators.
20  *
21  * @author Konstantin Kudryashov <ever.zet@gmail.com>
22  */
23 final class SuiteRegistry implements SuiteRepository
24 {
25     /**
26      * @var Boolean
27      */
28     private $suitesGenerated = false;
29     /**
30      * @var SuiteGenerator[]
31      */
32     private $generators = array();
33     /**
34      * @var array
35      */
36     private $suiteConfigurations = array();
37     /**
38      * @var Suite[]
39      */
40     private $suites = array();
41
42     /**
43      * Registers suite generator.
44      *
45      * @param SuiteGenerator $generator
46      */
47     public function registerSuiteGenerator(SuiteGenerator $generator)
48     {
49         $this->generators[] = $generator;
50         $this->suitesGenerated = false;
51     }
52
53     /**
54      * Registers suite using provided name, type & parameters.
55      *
56      * @param string $name
57      * @param string $type
58      * @param array  $settings
59      *
60      * @throws SuiteConfigurationException
61      */
62     public function registerSuiteConfiguration($name, $type, array $settings)
63     {
64         if (isset($this->suiteConfigurations[$name])) {
65             throw new SuiteConfigurationException(sprintf(
66                 'Suite configuration for a suite "%s" is already registered.',
67                 $name
68             ), $name);
69         }
70
71         $this->suiteConfigurations[$name] = array($type, $settings);
72         $this->suitesGenerated = false;
73     }
74
75     /**
76      * Returns all available suites.
77      *
78      * @return Suite[]
79      */
80     public function getSuites()
81     {
82         if ($this->suitesGenerated) {
83             return $this->suites;
84         }
85
86         $this->suites = array();
87         foreach ($this->suiteConfigurations as $name => $configuration) {
88             list($type, $settings) = $configuration;
89
90             $this->suites[] = $this->generateSuite($name, $type, $settings);
91         }
92
93         $this->suitesGenerated = true;
94
95         return $this->suites;
96     }
97
98     /**
99      * Generates suite using registered generators.
100      *
101      * @param string $name
102      * @param string $type
103      * @param array  $settings
104      *
105      * @return Suite
106      *
107      * @throws SuiteGenerationException If no appropriate generator found
108      */
109     private function generateSuite($name, $type, array $settings)
110     {
111         foreach ($this->generators as $generator) {
112             if (!$generator->supportsTypeAndSettings($type, $settings)) {
113                 continue;
114             }
115
116             return $generator->generateSuite($name, $settings);
117         }
118
119         throw new SuiteGenerationException(sprintf(
120             'Can not find suite generator for a suite `%s` of type `%s`.',
121             $name,
122             $type
123         ), $name);
124     }
125 }