4 * This file is part of the Behat Gherkin.
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\Gherkin;
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;
22 * @author Konstantin Kudryashov <ever.zet@gmail.com>
26 const VERSION = '4.4-dev';
29 * @var LoaderInterface[]
31 protected $loaders = array();
33 * @var FeatureFilterInterface[]
35 protected $filters = array();
38 * Adds loader to manager.
40 * @param LoaderInterface $loader Feature loader
42 public function addLoader(LoaderInterface $loader)
44 $this->loaders[] = $loader;
48 * Adds filter to manager.
50 * @param FeatureFilterInterface $filter Feature filter
52 public function addFilter(FeatureFilterInterface $filter)
54 $this->filters[] = $filter;
58 * Sets filters to the parser.
60 * @param FeatureFilterInterface[] $filters
62 public function setFilters(array $filters)
64 $this->filters = array();
65 array_map(array($this, 'addFilter'), $filters);
69 * Sets base features path.
71 * @param string $path Loaders base path
73 public function setBasePath($path)
75 foreach ($this->loaders as $loader) {
76 if ($loader instanceof FileLoaderInterface) {
77 $loader->setBasePath($path);
83 * Loads & filters resource with added loaders.
85 * @param mixed $resource Resource to load
86 * @param FeatureFilterInterface[] $filters Additional filters
90 public function load($resource, array $filters = array())
92 $filters = array_merge($this->filters, $filters);
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]);
103 $loader = $this->resolveLoader($resource);
105 if (null === $loader) {
110 foreach ($loader->load($resource) as $feature) {
111 foreach ($filters as $filter) {
112 $feature = $filter->filterFeature($feature);
114 if (!$feature->hasScenarios() && !$filter->isFeatureMatch($feature)) {
119 $features[] = $feature;
126 * Resolves loader by resource.
128 * @param mixed $resource Resource to load
130 * @return LoaderInterface
132 public function resolveLoader($resource)
134 foreach ($this->loaders as $loader) {
135 if ($loader->supports($resource)) {