Skip to content

Commit 3ea6101

Browse files
committed
change: Deprecate Gherkin::VERSION and change how file caches are namespaced
The `Gherkin::VERSION` constant has not been updated for recent releases, and will not be part of our release process in the future. This constant was only being used internally to ensure that any pre-existing file caches are invalidated when a different version of the parser is installed. I have switched this to get a version from the composer runtime API, which should be at least as reliable as the previous hardcoded constant for this purpose.
1 parent b2d5a80 commit 3ea6101

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
],
1515

1616
"require": {
17-
"php": "8.1.* || 8.2.* || 8.3.* || 8.4.*"
17+
"php": "8.1.* || 8.2.* || 8.3.* || 8.4.*",
18+
"composer-runtime-api": "^2.2"
1819
},
1920

2021
"require-dev": {

src/Behat/Gherkin/Cache/FileCache.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
namespace Behat\Gherkin\Cache;
1212

1313
use Behat\Gherkin\Exception\CacheException;
14-
use Behat\Gherkin\Gherkin;
1514
use Behat\Gherkin\Node\FeatureNode;
15+
use Composer\InstalledVersions;
1616

1717
/**
1818
* File cache.
@@ -24,6 +24,17 @@ class FileCache implements CacheInterface
2424
{
2525
private $path;
2626

27+
/**
28+
* Used as part of the cache directory path to invalidate cache if the installed package version changes.
29+
*/
30+
private static function getGherkinVersionHash(): string
31+
{
32+
$version = InstalledVersions::getVersion('behat/gherkin');
33+
34+
// Composer version strings can contain arbitrary content so hash for filesystem safety
35+
return md5($version);
36+
}
37+
2738
/**
2839
* Initializes file cache.
2940
*
@@ -33,7 +44,7 @@ class FileCache implements CacheInterface
3344
*/
3445
public function __construct($path)
3546
{
36-
$this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'v' . Gherkin::VERSION;
47+
$this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . self::getGherkinVersionHash();
3748

3849
if (!is_dir($this->path)) {
3950
@mkdir($this->path, 0777, true);

src/Behat/Gherkin/Gherkin.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
*/
2424
class Gherkin
2525
{
26+
/**
27+
* @deprecated this constant will not be updated for releases after 4.8.0 and will be removed in the next major.
28+
* You can use composer's runtime API to get the behat version if you need it. Note that composer's versions will
29+
* not always be simple numeric values.
30+
*/
2631
public const VERSION = '4.8.0';
2732

2833
/**

0 commit comments

Comments
 (0)