Skip to content

Commit

Permalink
Add rule loader (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
smortexa committed Aug 19, 2022
1 parent fa5e05c commit 2c4fe61
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
32 changes: 32 additions & 0 deletions src/RuleLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Mortexa\LaravelArkitect;

use Illuminate\Support\Arr;

class RuleLoader
{
public static function user(): array
{
$rules = [];

foreach (glob(base_path('tests/Architecture/*.php')) as $file) {
$class = basename($file, '.php');
$rules[] = 'Tests\\Architecture\\'.$class;
}

return $rules;
}

public static function package(): array
{
$package_config = CreateApplication::app()->make('config')['arkitect'];

return Arr::flatten($package_config['rules']);
}

public static function all(): array
{
return array_merge(static::package(), static::user());
}
}
16 changes: 2 additions & 14 deletions src/phparkitect.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,10 @@
declare(strict_types=1);

use Arkitect\CLI\Config;
use Illuminate\Support\Arr;
use Mortexa\LaravelArkitect\CreateApplication;
use Mortexa\LaravelArkitect\RuleLoader;

return static function (Config $config): void {
$app = CreateApplication::app();

$package_config = $app->make('config')['arkitect'];

$rules = [];

$rules = array_merge($rules, Arr::flatten($package_config['rules']));

foreach (glob($app->basePath('tests/Architecture/*.php')) as $file) {
$class = basename($file, '.php');
$rules[] = 'Tests\\Architecture\\'.$class;
}
$rules = RuleLoader::all();

foreach ($rules as $rule) {
if ($rule::directoryExists()) {
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/RuleLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Mortexa\LaravelArkitect\RuleLoader;
use function PHPUnit\Framework\assertEqualsCanonicalizing;

it('loads user\'s rules', function () {
$fooClass = base_path('tests/Architecture/Foo.php');
$barClass = base_path('tests/Architecture/Bar.php');

if (File::exists($fooClass)) {
unlink($fooClass);
}

if (File::exists($barClass)) {
unlink($barClass);
}

Artisan::call('make:arkitect Foo');
Artisan::call('make:arkitect Bar');

$rules = RuleLoader::user();

assertEqualsCanonicalizing(['Tests\\Architecture\\Foo', 'Tests\\Architecture\\Bar'], $rules);
});

0 comments on commit 2c4fe61

Please sign in to comment.