plank / laravel-model-resolver
Retrieve all defined Models available in the Application or Vendor namespaces
Fund package maintenance!
Plank
Requires
- php: ^8.2
- illuminate/contracts: ^11.0
- spatie/laravel-package-tools: ^1.19
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1
- orchestra/testbench: ^9.0.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
README
Laravel Model Resolver
Resolve all defined Models from the application or dependencies.
Table of Contents
- Installation
- Quick Start
- Configuration
- Usage
- Contributing
- Credits
- License
- Security Vulnerabilities
- About Plank
Installation
You can install the package via composer:
composer require plank/laravel-model-resolver
You can publish the config file with:
php artisan laravel-model-resolver:install
Quick Start
- Install the package
- Run
composer dump-autoload --optimize
<?php namespace App\Listeners; use App\Contracts\Content; use Plank\LaravelModelResolver\Facades\Models; class PruneContent { public function handle() { Models::implements(Content::class) ->each(fn (Content $content) => $content->prune()); } }
Configuration
The configuration file allows you to customize:
- The repository implementation which resolves the defined Models
- Namespaces to ignore from being scanned for Models
use Plank\LaravelModelResolver\Repository\ModelRepository; return [ 'repository' => ModelRepository::class, 'ignore' => [ 'DeepCopy\\', 'Doctrine\\', 'Illuminate\\', 'Mockery\\', 'PHPStan\\', 'PHPUnit\\', 'Prophecy\\', 'Psr\\', 'Psy\\', 'Sebastian\\', 'Symfony\\', ], ];
Usage
It is crucial to note that this package relies on the existence of the file vendor/composer/autoload_classmap.php
which is created by running composer dump-autoload --optimize
. If this file does not exist, the package will fail to resolve Models and throw an error.
all
This method returns the class strings of all Models defined in the application and vendor namespaces.
public function modelInstances() { Models::all() ->map(function (string $class) => new $class); }
fromTable
This method returns a class string of the Model which defines that table name, if one exists.
public function handle(TableCreated $event) { $model = Models::fromTable($event->table); // ... }
implements
This method returns the class strings of Models which implement the given interface.
public function handle(Version $from, Version $to) { Models::implements(Versioned::class) ->each(fn (string $class) => $class::copyData($from, $to)); }
implementsAll
This method returns the class strings of the Models which implement all the given interfaces.
public function handle() { Models::implementsAll([Loggable::class, Titleable::class]) ->each(function (string $class) { $models = $class::query() ->whereCondition() ->cursor() ->each(fn (Loggable&Titleable $model) => $model->log($model->title().' has met some condition')); }); }
implementsAny
This method returns the class strings of the Models which match any of the given interfaces. This is method should not exist as common interfaces should be extracted, but this method allows shortcuts where appropriate.
public function handle() { Models::implementsAny([Expires::class, QueuesForDeletion::class]) ->each(function (string $class) { $models = $class::query() ->when( is_a($class, QueuesForDeletion::class, true), fn ($query) => $query->where('should_delete', true) ) ->when( is_a($class, Expires::class, true), fn ($query) => $query->where('expires_at', '>=', now()) ) ->cursor() ->each(fn (Expires|QueuesForDeletion $model) => $model->delete()); }); }
uses
This method returns the class strings of all Models that use the given trait.
public function purge() { Models::uses(SoftDeletes::class) ->each(function (string $class) { $models = $class::query() ->whereNotNull('deleted_at') ->cursor() ->each(fn (Model $model) => $model->forceDelete()); }); }
usesAll
This method returns the class strings of Models that use all the given traits.
public function handle() { Models::usesAll([GetsLogged::class, HasTitle::class]) ->each(function (string $class) { $models = $class::query() ->whereCondition() ->cursor() ->each(fn (Model $model) => $model->log($model->title().' has met some condition')); }); }
usesAny
This method returns the class strings of Models that use all the given traits.
public function handle() { Models::implementsAny([GetsExpired::class, IsQueuedForDeletion::class]) ->each(function (string $class) { $uses = class_uses_recursive($class); $models = $class::query() ->when( in_array(GetsExpired::class, $uses), fn ($query) => $query->where('should_delete', true) ) ->when( in_array(IsQueuedForDeletion::class, $uses), fn ($query) => $query->where('expires_at', '>=', now()) ) ->cursor() ->each(fn (Model $model) => $model->delete()); }); }
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
Security Vulnerabilities
If you discover a security vulnerability within siren, please send an e-mail to security@plank.co. All security vulnerabilities will be promptly addressed.
Check Us Out!
Plank focuses on impactful solutions that deliver engaging experiences to our clients and their users. We're committed to innovation, inclusivity, and sustainability in the digital space. Learn more about our mission to improve the web.