Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add matcher for Yii1 (fixes #14) #27

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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