Resolve all defined Models from the application or dependencies.
- Installation
- Quick Start
- Configuration
- Usage
- Contributing
- Credits
- License
- Security Vulnerabilities
- About Plank
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
- 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());
}
}
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\\',
],
];
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.
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);
}
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);
// ...
}
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));
}
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'));
});
}
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());
});
}
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());
});
}
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'));
});
}
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());
});
}
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
If you discover a security vulnerability within siren, please send an e-mail to [email protected]. All security vulnerabilities will be promptly addressed.
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.