v1.0.0
This commit is contained in:
parent
0fcf42b47b
commit
443dceb8ca
3 changed files with 566609 additions and 230 deletions
61
vendor/eduardovillao/wp-since/src/Resolver/IgnoreRulesResolver.php
vendored
Normal file
61
vendor/eduardovillao/wp-since/src/Resolver/IgnoreRulesResolver.php
vendored
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
namespace WP_Since\Resolver;
|
||||
|
||||
class IgnoreRulesResolver
|
||||
{
|
||||
public static function getIgnoredPaths(string $pluginPath): array
|
||||
{
|
||||
$ignorePatterns = [];
|
||||
|
||||
$distignore = $pluginPath . '/.distignore';
|
||||
if (file_exists($distignore)) {
|
||||
$ignorePatterns = array_merge($ignorePatterns, self::parseIgnoreFile($distignore));
|
||||
}
|
||||
|
||||
$gitattributes = $pluginPath . '/.gitattributes';
|
||||
if (file_exists($gitattributes)) {
|
||||
$ignorePatterns = array_merge($ignorePatterns, self::parseGitAttributes($gitattributes));
|
||||
}
|
||||
|
||||
return array_map(function ($pattern) {
|
||||
return ltrim(rtrim($pattern, '/'), '/');
|
||||
}, $ignorePatterns);
|
||||
}
|
||||
|
||||
private static function parseIgnoreFile(string $file): array
|
||||
{
|
||||
return array_filter(array_map('trim', file($file)), fn($line) => $line !== '' && $line[0] !== '#');
|
||||
}
|
||||
|
||||
private static function parseGitAttributes(string $file): array
|
||||
{
|
||||
$lines = file($file);
|
||||
$ignores = [];
|
||||
|
||||
foreach ($lines as $line) {
|
||||
if (strpos($line, 'export-ignore') !== false) {
|
||||
$parts = preg_split('/\s+/', trim($line));
|
||||
if (!empty($parts[0])) {
|
||||
$ignores[] = $parts[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $ignores;
|
||||
}
|
||||
|
||||
public static function shouldIgnore(string $relativePath, array $ignorePaths): bool
|
||||
{
|
||||
foreach ($ignorePaths as $ignored) {
|
||||
$normalized = ltrim($ignored, '/');
|
||||
if (
|
||||
$relativePath === $normalized ||
|
||||
str_starts_with($relativePath, rtrim($normalized, '/') . '/')
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
26
vendor/eduardovillao/wp-since/src/Resolver/InlineIgnoreResolver.php
vendored
Normal file
26
vendor/eduardovillao/wp-since/src/Resolver/InlineIgnoreResolver.php
vendored
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace WP_Since\Resolver;
|
||||
|
||||
use PhpParser\Lexer\Emulative;
|
||||
|
||||
class InlineIgnoreResolver
|
||||
{
|
||||
public static function extractIgnoredLines(string $code): array
|
||||
{
|
||||
$lexer = new Emulative(['usedAttributes' => ['startLine']]);
|
||||
$lexer->startLexing($code);
|
||||
|
||||
$ignoredLines = [];
|
||||
|
||||
foreach ($lexer->getTokens() as $token) {
|
||||
if (is_array($token) && in_array($token[0], [T_COMMENT, T_DOC_COMMENT], true)) {
|
||||
if (strpos($token[1], '@wp-since ignore') !== false) {
|
||||
$ignoredLines[] = $token[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $ignoredLines;
|
||||
}
|
||||
}
|
||||
77
vendor/eduardovillao/wp-since/src/Resolver/VersionResolver.php
vendored
Normal file
77
vendor/eduardovillao/wp-since/src/Resolver/VersionResolver.php
vendored
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace WP_Since\Resolver;
|
||||
|
||||
class VersionResolver
|
||||
{
|
||||
public static function resolve(string $pluginPath): ?array
|
||||
{
|
||||
$pluginFile = self::findMainPluginFile($pluginPath);
|
||||
|
||||
if ($pluginFile) {
|
||||
$version = self::extractVersionFromPluginHeader($pluginFile);
|
||||
if ($version) {
|
||||
return ['version' => $version, 'source' => 'main plugin file header'];
|
||||
}
|
||||
}
|
||||
|
||||
$readme = self::findReadmeFile($pluginPath);
|
||||
if ($readme) {
|
||||
$version = self::extractVersionFromReadme($readme);
|
||||
if ($version) {
|
||||
return ['version' => $version, 'source' => 'readme'];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function findMainPluginFile(string $pluginPath): ?string
|
||||
{
|
||||
$basename = basename($pluginPath);
|
||||
$candidate = "{$pluginPath}/{$basename}.php";
|
||||
if (file_exists($candidate)) {
|
||||
return $candidate;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function extractVersionFromPluginHeader(string $file): ?string
|
||||
{
|
||||
$contents = file_get_contents($file);
|
||||
if (!$contents) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (preg_match('/^\s*\*\s+Requires at least:\s*([0-9.]+)/mi', $contents, $matches)) {
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function findReadmeFile(string $pluginPath): ?string
|
||||
{
|
||||
foreach (['readme.txt', 'README.txt'] as $filename) {
|
||||
$full = "{$pluginPath}/{$filename}";
|
||||
if (file_exists($full)) {
|
||||
return $full;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function extractVersionFromReadme(string $file): ?string
|
||||
{
|
||||
$lines = file($file);
|
||||
foreach ($lines as $line) {
|
||||
if (stripos($line, 'Requires at least:') === 0) {
|
||||
if (preg_match('/Requires at least:\s*([0-9.]+)/i', $line, $matches)) {
|
||||
return $matches[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue