Skip to content

Commit

Permalink
change: Deprecate Gherkin::VERSION and change how file caches are n…
Browse files Browse the repository at this point in the history
…amespaced

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.
  • Loading branch information
acoulton committed Dec 24, 2024
1 parent a539cab commit 969444e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
],

"require": {
"php": "8.1.* || 8.2.* || 8.3.* || 8.4.*"
"php": "8.1.* || 8.2.* || 8.3.* || 8.4.*",
"composer-runtime-api": "^2.2"
},

"require-dev": {
Expand Down
14 changes: 12 additions & 2 deletions src/Behat/Gherkin/Cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
namespace Behat\Gherkin\Cache;

use Behat\Gherkin\Exception\CacheException;
use Behat\Gherkin\Gherkin;
use Behat\Gherkin\Node\FeatureNode;
use Composer\InstalledVersions;

/**
* File cache.
Expand All @@ -24,6 +24,16 @@ class FileCache implements CacheInterface
{
private $path;

/**
* Used as part of the cache directory path to invalidate cache if the installed package version changes.
*/
private static function getGherkinVersionHash(): string
{
$version = InstalledVersions::getVersion('behat/gherkin');
// Composer version strings can contain arbitrary content so hash for filesystem safety
return md5($version);
}

/**
* Initializes file cache.
*
Expand All @@ -33,7 +43,7 @@ class FileCache implements CacheInterface
*/
public function __construct($path)
{
$this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'v' . Gherkin::VERSION;
$this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . self::getGherkinVersionHash();

if (!is_dir($this->path)) {
@mkdir($this->path, 0777, true);
Expand Down
5 changes: 5 additions & 0 deletions src/Behat/Gherkin/Gherkin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
*/
class Gherkin
{
/**
* @deprecated this constant will not be updated for releases after 4.8.0 and will be removed in the next major.
* You can use composer's runtime API to get the behat version if you need it. Note that composer's versions will
* not always be simple numeric values.
*/
public const VERSION = '4.8.0';

/**
Expand Down

0 comments on commit 969444e

Please sign in to comment.