Security update for permissions_by_term
[yaffs-website] / vendor / behat / gherkin / src / Behat / Gherkin / Gherkin.php
1 <?php
2
3 /*
4  * This file is part of the Behat Gherkin.
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\Gherkin;
12
13 use Behat\Gherkin\Filter\FeatureFilterInterface;
14 use Behat\Gherkin\Filter\LineFilter;
15 use Behat\Gherkin\Filter\LineRangeFilter;
16 use Behat\Gherkin\Loader\FileLoaderInterface;
17 use Behat\Gherkin\Loader\LoaderInterface;
18
19 /**
20  * Gherkin manager.
21  *
22  * @author Konstantin Kudryashov <ever.zet@gmail.com>
23  */
24 class Gherkin
25 {
26     const VERSION = '4.4-dev';
27
28     /**
29      * @var LoaderInterface[]
30      */
31     protected $loaders = array();
32     /**
33      * @var FeatureFilterInterface[]
34      */
35     protected $filters = array();
36
37     /**
38      * Adds loader to manager.
39      *
40      * @param LoaderInterface $loader Feature loader
41      */
42     public function addLoader(LoaderInterface $loader)
43     {
44         $this->loaders[] = $loader;
45     }
46
47     /**
48      * Adds filter to manager.
49      *
50      * @param FeatureFilterInterface $filter Feature filter
51      */
52     public function addFilter(FeatureFilterInterface $filter)
53     {
54         $this->filters[] = $filter;
55     }
56
57     /**
58      * Sets filters to the parser.
59      *
60      * @param FeatureFilterInterface[] $filters
61      */
62     public function setFilters(array $filters)
63     {
64         $this->filters = array();
65         array_map(array($this, 'addFilter'), $filters);
66     }
67
68     /**
69      * Sets base features path.
70      *
71      * @param string $path Loaders base path
72      */
73     public function setBasePath($path)
74     {
75         foreach ($this->loaders as $loader) {
76             if ($loader instanceof FileLoaderInterface) {
77                 $loader->setBasePath($path);
78             }
79         }
80     }
81
82     /**
83      * Loads & filters resource with added loaders.
84      *
85      * @param mixed                    $resource Resource to load
86      * @param FeatureFilterInterface[] $filters  Additional filters
87      *
88      * @return array
89      */
90     public function load($resource, array $filters = array())
91     {
92         $filters = array_merge($this->filters, $filters);
93
94         $matches = array();
95         if (preg_match('/^(.*)\:(\d+)-(\d+|\*)$/', $resource, $matches)) {
96             $resource = $matches[1];
97             $filters[] = new LineRangeFilter($matches[2], $matches[3]);
98         } elseif (preg_match('/^(.*)\:(\d+)$/', $resource, $matches)) {
99             $resource = $matches[1];
100             $filters[] = new LineFilter($matches[2]);
101         }
102
103         $loader = $this->resolveLoader($resource);
104
105         if (null === $loader) {
106             return array();
107         }
108
109         $features = array();
110         foreach ($loader->load($resource) as $feature) {
111             foreach ($filters as $filter) {
112                 $feature = $filter->filterFeature($feature);
113
114                 if (!$feature->hasScenarios() && !$filter->isFeatureMatch($feature)) {
115                     continue 2;
116                 }
117             }
118
119             $features[] = $feature;
120         }
121
122         return $features;
123     }
124
125     /**
126      * Resolves loader by resource.
127      *
128      * @param mixed $resource Resource to load
129      *
130      * @return LoaderInterface
131      */
132     public function resolveLoader($resource)
133     {
134         foreach ($this->loaders as $loader) {
135             if ($loader->supports($resource)) {
136                 return $loader;
137             }
138         }
139
140         return null;
141     }
142 }