Skip to content
32 changes: 28 additions & 4 deletions src/Behat/Gherkin/Loader/CucumberGherkinLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Behat\Gherkin\Loader;

use Behat\Gherkin\Cache\CacheInterface;
use Behat\Gherkin\Cucumber\BackgroundNodeMapper;
use Behat\Gherkin\Cucumber\ExampleTableNodeMapper;
use Behat\Gherkin\Cucumber\FeatureNodeMapper;
Expand All @@ -27,6 +28,11 @@ final class CucumberGherkinLoader extends AbstractFileLoader
*/
private $parser;

/**
* @var ?CacheInterface
*/
protected $cache;

public function __construct()
{
$tagMapper = new TagMapper();
Expand Down Expand Up @@ -74,6 +80,16 @@ public static function isAvailable() : bool
return class_exists(GherkinParser::class);
}

/**
* Sets cache layer.
*
* @param CacheInterface $cache Cache layer
*/
public function setCache(CacheInterface $cache)
{
$this->cache = $cache;
}

/**
* Loads features from provided resource.
*
Expand All @@ -83,18 +99,26 @@ public static function isAvailable() : bool
*/
public function load($resource)
{
$features = [];
$path = $this->findAbsolutePath($resource);

if ($this->cache && $this->cache->isFresh($path, filemtime($path))) {
return [$this->cache->read($path)];
}

$envelopes = $this->parser->parseString($this->findAbsolutePath($resource), file_get_contents($resource));
$envelopes = $this->parser->parseString($path, file_get_contents($path));
foreach ($envelopes as $envelope) {
if ($envelope->gherkinDocument) {
if ($feature = $this->mapper->map($envelope->gherkinDocument)) {
$features[] = $feature;
break;
}
}
}

return $features;
if ($this->cache) {
$this->cache->write($path, $feature);
}

return [$feature];
}

}