Security update for permissions_by_term
[yaffs-website] / vendor / behat / gherkin / src / Behat / Gherkin / Loader / GherkinFileLoader.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\Loader;
12
13 use Behat\Gherkin\Cache\CacheInterface;
14 use Behat\Gherkin\Node\FeatureNode;
15 use Behat\Gherkin\Parser;
16
17 /**
18  * Gherkin *.feature files loader.
19  *
20  * @author Konstantin Kudryashov <ever.zet@gmail.com>
21  */
22 class GherkinFileLoader extends AbstractFileLoader
23 {
24     protected $parser;
25     protected $cache;
26
27     /**
28      * Initializes loader.
29      *
30      * @param Parser         $parser Parser
31      * @param CacheInterface $cache  Cache layer
32      */
33     public function __construct(Parser $parser, CacheInterface $cache = null)
34     {
35         $this->parser = $parser;
36         $this->cache = $cache;
37     }
38
39     /**
40      * Sets cache layer.
41      *
42      * @param CacheInterface $cache Cache layer
43      */
44     public function setCache(CacheInterface $cache)
45     {
46         $this->cache = $cache;
47     }
48
49     /**
50      * Checks if current loader supports provided resource.
51      *
52      * @param mixed $path Resource to load
53      *
54      * @return Boolean
55      */
56     public function supports($path)
57     {
58         return is_string($path)
59         && is_file($absolute = $this->findAbsolutePath($path))
60         && 'feature' === pathinfo($absolute, PATHINFO_EXTENSION);
61     }
62
63     /**
64      * Loads features from provided resource.
65      *
66      * @param string $path Resource to load
67      *
68      * @return FeatureNode[]
69      */
70     public function load($path)
71     {
72         $path = $this->findAbsolutePath($path);
73
74         if ($this->cache) {
75             if ($this->cache->isFresh($path, filemtime($path))) {
76                 $feature = $this->cache->read($path);
77             } elseif (null !== $feature = $this->parseFeature($path)) {
78                 $this->cache->write($path, $feature);
79             }
80         } else {
81             $feature = $this->parseFeature($path);
82         }
83
84         return null !== $feature ? array($feature) : array();
85     }
86
87     /**
88      * Parses feature at provided absolute path.
89      *
90      * @param string $path Feature path
91      *
92      * @return FeatureNode
93      */
94     protected function parseFeature($path)
95     {
96         $filename = $this->findRelativePath($path);
97         $content = file_get_contents($path);
98         $feature = $this->parser->parse($content, $filename);
99
100         return $feature;
101     }
102 }