Skip to content

Commit

Permalink
add matcher for Yii1 (fixes #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
untone-survive committed Oct 12, 2023
1 parent f67f579 commit 96c172c
Show file tree
Hide file tree
Showing 4 changed files with 950 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/Matchers/Yii.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,41 @@

class Yii implements MatcherInterface
{
private const VERSIONS = [
[
'file' => 'yii',
'versionFile' => '/vendor/yiisoft/yii2/BaseYii.php',
'versionRegexp' => '/public static function getVersion\(\)\s*\{\s*return \'([^\']+)\';\s*}/',
],
[
'file' => 'framework/yiic',
'versionFile' => '/framework/YiiBase.php',
'versionRegexp' => '/public static function getVersion\(\)\s*\{\s*return \'([^\']+)\';\s*}/',
],
];

public function match(Filesystem $fs, string $path): MatchResultInterface
{
$path = rtrim($path, '/');
if (!$fs->fileExists($path . '/yii')) {
return new EmptyMatchResult();

foreach (self::VERSIONS as $version) {
if (!$fs->fileExists($path . '/' . $version['file'])) {
continue;
}
return new MatchResult($path, $this->detectVersion($fs, $path, $version));
}

return new MatchResult($path, $this->detectVersion($fs, $path));
return new EmptyMatchResult();
}

private function detectVersion(Filesystem $fs, string $path): ?string
private function detectVersion(Filesystem $fs, string $path, array $versionInfo): ?string
{
$version = null;

$yii2VersionFile = $path . '/vendor/yiisoft/yii2/BaseYii.php';
$yii2VersionFile = $path . $versionInfo['versionFile'];
if ($fs->fileExists($yii2VersionFile)) {
// Use regular expression to match the getVersion method content
preg_match(
'/public static function getVersion\(\)\s*\{\s*return \'([^\']+)\';\s*}/',
$fs->read($yii2VersionFile),
$matches
);

// Check if the match is found and return the version
preg_match($versionInfo['versionRegexp'], $fs->read($yii2VersionFile), $matches);

if (isset($matches[1])) {
$version = $matches[1];
}
Expand Down
Loading

0 comments on commit 96c172c

Please sign in to comment.