Skip to content

Commit

Permalink
laravel 9
Browse files Browse the repository at this point in the history
  • Loading branch information
wfeller committed Apr 10, 2022
1 parent cac2389 commit 1aaf559
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
{"name": "William", "homepage": "https://github.com/wfeller"}
],
"require": {
"php": "^7.2|^8.0",
"illuminate/database": "^7.0|^8.0|^9.0",
"hanneskod/classtools": "~1.0"
"php": "^8.0",
"illuminate/database": "^9.0",
"gnugat/nomo-spaco": "~0.4"
},
"require-dev": {
"orchestra/testbench": "^4.0|^5.0|^6.0|^7.0",
"orchestra/testbench": "^6.0|^7.0",
"phpunit/phpunit": "^8.0|^9.0"
},
"autoload": {
Expand Down
37 changes: 20 additions & 17 deletions src/Commands/DiscoverChildren.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace WF\Parental\Commands;

use hanneskod\classtools\Iterator\ClassIterator;
use Gnugat\NomoSpaco\File\FileRepository;
use Gnugat\NomoSpaco\FqcnRepository;
use Gnugat\NomoSpaco\Token\ParserFactory;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Symfony\Component\Finder\Finder;
use WF\Parental\HasChildren;
use WF\Parental\HasParent;

Expand Down Expand Up @@ -47,25 +48,27 @@ private function path()

private function findChildren()
{
$finder = new Finder;
$iter = new ClassIterator($finder->in(config('parental.model_directories', [])));
$children = [];

foreach ($iter->getClassMap() as $class => $fileInfo) {
try {
if (! is_a($class, Model::class, true)) {
continue;
}
$traits = class_uses_recursive($class);
$repository = new FqcnRepository(new FileRepository, new ParserFactory);

foreach (config('parental.model_directories', []) as $directory) {
foreach ($repository->findIn($directory) as $class) {
try {
if (! is_a($class, Model::class, true)) {
continue;
}

$traits = class_uses_recursive($class);

if (in_array(HasParent::class, $traits) // It's a child
&& in_array(HasChildren::class, $traits) // and the parent has the parent trait
) {
$parent = get_parent_class($class);
$children[$parent][$class] = $class;
if (in_array(HasParent::class, $traits) && // It's a child
in_array(HasChildren::class, $traits) // and the parent has the parent trait
) {
$parent = get_parent_class($class);
$children[$parent][$class] = $class;
}
} catch (\Throwable) {
}
} catch (\Throwable $e) {
continue;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/HasParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected static function bootHasParent() : void
});
}

public static function addGlobalScope($scope, \Closure $implementation = null)
public static function addGlobalScope($scope, $implementation = null)
{
$implementation = parent::addGlobalScope($scope, $implementation);
$child = new static;
Expand Down
2 changes: 1 addition & 1 deletion tests/Features/ModelAliasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function it_throws_if_child_class_alias_not_defined_and_interface_not_imp
$this->assertNotInstanceOf(DefaultsMissingAliasToParentClass::class, new Trip);

$this->expectException(\Throwable::class);
$this->expectExceptionMessage("Class 'invalid-trip-type' not found");
$this->expectExceptionMessage('Class "invalid-trip-type" not found');

Trip::query()->create([(new Trip)->getInheritanceColumn() => 'invalid-trip-type']);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/HasParentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use WF\Parental\HasParent;
use WF\Parental\Tests\TestCase;

class HasParentTest extends TestCase
class HasParentModelTest extends TestCase
{
/** @test */
function child_model_has_table_name_of_parent_model()
Expand Down

0 comments on commit 1aaf559

Please sign in to comment.