定制 plank/laravel-model-resolver 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

plank/laravel-model-resolver

最新稳定版本:v12.1.2

Composer 安装命令:

composer require plank/laravel-model-resolver

包简介

Retrieve all defined Models available in the Application or Vendor namespaces

README 文档

README

PHP Version Support PHP Version Support GitHub Workflow Status

Laravel Model Resolver

Resolve all defined Models from the application or dependencies.

Table of Contents

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

  1. Install the package
  2. 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.

统计信息

  • 总下载量: 4.91k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-24