Skip to content

Commit

Permalink
Merge pull request #54
Browse files Browse the repository at this point in the history
Fixed Initiaition issue of abstract classes
  • Loading branch information
tharindarodrigo authored Jul 15, 2023
2 parents 4734c9a + 84ff354 commit 4e7e1eb
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions src/Commands/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
namespace Althinect\FilamentSpatieRolesPermissions\Commands;

use Althinect\FilamentSpatieRolesPermissions\Commands\Concerns\ManipulateFiles;
use Filament\Facades\Filament;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionException;
use Spatie\Permission\Models\Permission as PermissionModel;
use Illuminate\Support\Facades\DB;

class Permission extends Command
{
Expand All @@ -37,6 +35,10 @@ public function __construct()
$this->config = config('filament-spatie-roles-permissions.generator');
}

/**
* @throws ReflectionException
* @throws FileNotFoundException
*/
public function handle(): void
{
$classes = $this->getAllModels();
Expand Down Expand Up @@ -70,6 +72,10 @@ public function deleteExistingPermissions(): void
}
}

/**
* @throws ReflectionException
* @throws FileNotFoundException
*/
public function prepareClassPermissionsAndPolicies($classes): void
{
$filesystem = new Filesystem();
Expand Down Expand Up @@ -100,7 +106,7 @@ public function prepareClassPermissionsAndPolicies($classes): void
$policyVariables = [
'class' => $modelName . 'Policy',
'namespacedModel' => $model->getName(),
'namespacedUserModel' => (new \ReflectionClass($this->config['user_model']))->getName(),
'namespacedUserModel' => (new ReflectionClass($this->config['user_model']))->getName(),
'namespace' => $this->config['policies_namespace'],
'user' => 'User',
'model' => $modelName,
Expand Down Expand Up @@ -142,6 +148,9 @@ public function prepareCustomPermissions(): void
}
}

/**
* @throws ReflectionException
*/
public function getModels(): array
{
$models = [];
Expand All @@ -151,21 +160,31 @@ public function getModels(): array

foreach ($resources as $resource) {
$resourceNameSpace = $this->extractNamespace($resource);
$reflection = new \ReflectionClass($resourceNameSpace . '\\' . $resource->getFilenameWithoutExtension());
if ($reflection->getParentClass()->getName() == 'Filament\Resources\Resource') {
$models[] = new \ReflectionClass(app($resourceNameSpace . '\\' . $resource->getFilenameWithoutExtension())->getModel());

$reflection = new ReflectionClass($resourceNameSpace . '\\' . $resource->getFilenameWithoutExtension());
if (
!$reflection->isAbstract() &&
$reflection->getParentClass()->getName() == 'Filament\Resources\Resource'
) {
$models[] = new ReflectionClass(app($resourceNameSpace . '\\' . $resource->getFilenameWithoutExtension())->getModel());
}
}
}

foreach ($this->config['model_directories'] as $modelDirectory) {
$models = array_merge($models, $this->getClassesInDirectory($modelDirectory));
$modelsInDirectory = [];
foreach ($this->config['models_directories'] as $directory) {

$modelsInDirectory[] = $this->getClassesInDirectory($directory);
}

return $models;
return array_merge($models, $modelsInDirectory);

}



/**
* @throws ReflectionException
*/
private function getClassesInDirectory($path): array
{
$files = File::files($path);
Expand All @@ -174,8 +193,11 @@ private function getClassesInDirectory($path): array
foreach ($files as $file) {
$namespace = $this->extractNamespace($file);
$class = new ($namespace . '\\' . $file->getFilenameWithoutExtension());
$model = new \ReflectionClass($class);
$models[] = $model;
$model = new ReflectionClass($class);
if (!$model->isAbstract()) {
$models[] = $model;
}

}

return $models;
Expand Down Expand Up @@ -239,4 +261,6 @@ public function getAllModels(): array

return array_merge($models, $customModels);
}


}

0 comments on commit 4e7e1eb

Please sign in to comment.