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\Loader;
13 use Behat\Gherkin\Cache\CacheInterface;
14 use Behat\Gherkin\Node\FeatureNode;
15 use Behat\Gherkin\Parser;
18 * Gherkin *.feature files loader.
20 * @author Konstantin Kudryashov <ever.zet@gmail.com>
22 class GherkinFileLoader extends AbstractFileLoader
30 * @param Parser $parser Parser
31 * @param CacheInterface $cache Cache layer
33 public function __construct(Parser $parser, CacheInterface $cache = null)
35 $this->parser = $parser;
36 $this->cache = $cache;
42 * @param CacheInterface $cache Cache layer
44 public function setCache(CacheInterface $cache)
46 $this->cache = $cache;
50 * Checks if current loader supports provided resource.
52 * @param mixed $path Resource to load
56 public function supports($path)
58 return is_string($path)
59 && is_file($absolute = $this->findAbsolutePath($path))
60 && 'feature' === pathinfo($absolute, PATHINFO_EXTENSION);
64 * Loads features from provided resource.
66 * @param string $path Resource to load
68 * @return FeatureNode[]
70 public function load($path)
72 $path = $this->findAbsolutePath($path);
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);
81 $feature = $this->parseFeature($path);
84 return null !== $feature ? array($feature) : array();
88 * Parses feature at provided absolute path.
90 * @param string $path Feature path
94 protected function parseFeature($path)
96 $filename = $this->findRelativePath($path);
97 $content = file_get_contents($path);
98 $feature = $this->parser->parse($content, $filename);