update project
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Support\Architecture;
|
||||
|
||||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
|
||||
final class ArchitectureFileFinder
|
||||
{
|
||||
/** @return string[] */
|
||||
public function phpFiles(array $paths): array
|
||||
{
|
||||
$files = [];
|
||||
foreach ($paths as $path) {
|
||||
$absolute = base_path($path);
|
||||
if (is_file($absolute) && str_ends_with($absolute, '.php')) {
|
||||
$files[] = $absolute;
|
||||
continue;
|
||||
}
|
||||
if (! is_dir($absolute)) {
|
||||
continue;
|
||||
}
|
||||
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($absolute)) as $file) {
|
||||
if ($file->isFile() && $file->getExtension() === 'php') {
|
||||
$files[] = $file->getPathname();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort($files);
|
||||
return array_values(array_unique($files));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Support\Architecture;
|
||||
|
||||
final readonly class ArchitectureRule
|
||||
{
|
||||
/**
|
||||
* @param string[] $paths
|
||||
* @param string[] $patterns
|
||||
* @param string[] $excludePaths
|
||||
*/
|
||||
public function __construct(
|
||||
public string $id,
|
||||
public string $description,
|
||||
public string $severity,
|
||||
public array $paths,
|
||||
public array $patterns,
|
||||
public string $message,
|
||||
public ?string $suggestedFix = null,
|
||||
public array $excludePaths = [],
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Support\Architecture;
|
||||
|
||||
final class ArchitectureRuleRegistry
|
||||
{
|
||||
/** @return ArchitectureRule[] */
|
||||
public function rules(): array
|
||||
{
|
||||
$rules = [];
|
||||
foreach (config('architecture_governance.rules', []) as $rule) {
|
||||
$rules[] = new ArchitectureRule(
|
||||
id: $rule['id'],
|
||||
description: $rule['description'],
|
||||
severity: $rule['severity'] ?? 'error',
|
||||
paths: $rule['paths'] ?? [],
|
||||
patterns: $rule['patterns'] ?? [],
|
||||
message: $rule['message'],
|
||||
suggestedFix: $rule['suggested_fix'] ?? null,
|
||||
excludePaths: $rule['exclude_paths'] ?? [],
|
||||
);
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Support\Architecture;
|
||||
|
||||
final readonly class ArchitectureScanner
|
||||
{
|
||||
public function __construct(
|
||||
private ArchitectureRuleRegistry $registry,
|
||||
private ArchitectureFileFinder $finder,
|
||||
) {}
|
||||
|
||||
/** @return ArchitectureViolation[] */
|
||||
public function scan(): array
|
||||
{
|
||||
$violations = [];
|
||||
foreach ($this->registry->rules() as $rule) {
|
||||
foreach ($this->finder->phpFiles($rule->paths) as $file) {
|
||||
$relative = str_replace(base_path().'/', '', $file);
|
||||
if ($this->isExcluded($relative, $rule->excludePaths)) {
|
||||
continue;
|
||||
}
|
||||
$lines = file($file, FILE_IGNORE_NEW_LINES) ?: [];
|
||||
foreach ($lines as $index => $line) {
|
||||
foreach ($rule->patterns as $pattern) {
|
||||
if (@preg_match($pattern, '') === false) {
|
||||
if (! str_contains($line, $pattern)) {
|
||||
continue;
|
||||
}
|
||||
} elseif (! preg_match($pattern, $line)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$violations[] = new ArchitectureViolation(
|
||||
ruleId: $rule->id,
|
||||
severity: $rule->severity,
|
||||
file: $relative,
|
||||
line: $index + 1,
|
||||
message: $rule->message,
|
||||
suggestedFix: $rule->suggestedFix,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $violations;
|
||||
}
|
||||
|
||||
/** @param string[] $excludePaths */
|
||||
private function isExcluded(string $relativePath, array $excludePaths): bool
|
||||
{
|
||||
foreach ($excludePaths as $exclude) {
|
||||
if (str_contains($relativePath, $exclude)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Support\Architecture;
|
||||
|
||||
final readonly class ArchitectureViolation
|
||||
{
|
||||
public function __construct(
|
||||
public string $ruleId,
|
||||
public string $severity,
|
||||
public string $file,
|
||||
public int $line,
|
||||
public string $message,
|
||||
public ?string $suggestedFix = null,
|
||||
) {}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'rule_id' => $this->ruleId,
|
||||
'severity' => $this->severity,
|
||||
'file' => $this->file,
|
||||
'line' => $this->line,
|
||||
'message' => $this->message,
|
||||
'suggested_fix' => $this->suggestedFix,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Support\Architecture;
|
||||
|
||||
final class DependencyMapGenerator
|
||||
{
|
||||
public function generate(): array
|
||||
{
|
||||
$finder = new ArchitectureFileFinder();
|
||||
$files = $finder->phpFiles(['app/Domain', 'app/Http/Controllers', 'app/Providers']);
|
||||
$map = [];
|
||||
|
||||
foreach ($files as $file) {
|
||||
$relative = str_replace(base_path().'/', '', $file);
|
||||
$contents = file_get_contents($file) ?: '';
|
||||
preg_match_all('/^use\s+([^;]+);/m', $contents, $matches);
|
||||
$map[$relative] = array_values(array_unique($matches[1] ?? []));
|
||||
}
|
||||
|
||||
return [
|
||||
'generated_at' => now()->toISOString(),
|
||||
'files' => $map,
|
||||
'forbidden_dependency_examples' => [
|
||||
'SchoolCore -> IslamicSundaySchool',
|
||||
'Domain services -> Illuminate\\Http\\Request',
|
||||
'Controllers -> provider SDKs',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user