承接 javoscript/laravel-macroable-models 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

javoscript/laravel-macroable-models

Composer 安装命令:

composer require javoscript/laravel-macroable-models

包简介

A package for adding methods to Laravel models on the fly

README 文档

README

A package for adding methods to Laravel models on the fly 🕊

The package offers developers an easy way of programmatically adding methods to Laravel Eloquent models. Behind the scenes, it makes use of Laravel's own macroable trait. For more details, check the post where I explain how I did it in my blog.

Installation

Just install the package with composer

composer require javoscript/laravel-macroable-models

(Only necessary for Laravel <5.5, or if you want to be explicit) - Add the Service Provider to the providers array in the config/app.php file

// config/app.php

$providers = [
    // ...
    \Javoscript\MacroableModels\MacroableModelsServiceProvider::class,
    // ...
];

Usage example

The package provides a Facade to facilitate access to it's functionality. Alternatively, you can access it through the app('macroable-models') helper.

For obvious reasons, macros should be added to the model before other parts of the system make use of it. Because of this, the boot method of Service Providers is a good place to start adding macros.

For example, adding a method to the \App\User model in AppServiceProvider:

// app/Providers/AppServiceProvider.php

// ...

use \Javoscript\MacroableModels\Facades\MacroableModels;
use \App\User;

// ...

public function boot()
{

    MacroableModels::addMacro(User::class, 'sayHi', function() {
        return 'Hi!';
    });

}

After adding the macro to the User model, now every instance of this Eloquent model will have the sayHi() method available. We can quickly verify this within artisan tinker:

php artisan tinker

>>> \App\User::first()->sayHi()
=> "Hi!"

In a dedicated MacrosServiceProvider file

If you want to keep multiple macro definitions together, then adding a Service Provider for this purpose might be a good idea.

You can generate a new Service Provider with artisan:

php artisan make:provider MacrosServiceProvider

Then, you should add it to the providers array in the config/app.php file.

// config/app.php

$providers = [
    // ...
    App\Providers\MacrosServiceProvider::class,
    // ...
];

Then, in the boot method of this new Service Provider, you can centralize macro definitions:

// app/Providers/MacrosServiceProvider.php

// ...

use \Javoscript\MacroableModels\Facades\MacroableModels;
use \App\User;

// ...

public function boot()
{
    
    MacroableModels::addMacro(User::class, 'sayHi', function() {
        return 'Hi!';
    });
    
    MacroableModels::addMacro(User::class, 'sayBye', function() {
        return 'Bye bye';
    });
    
}

Available methods

The following examples will use the \App\User model so that you can try the examples on a fresh Laravel application. Any class that extends the Illuminate\Database\Eloquent\Model class can be extended with these macros.

addMacro(Model::class, 'macroName', function() {}) : void

The most important method of this package, and the one you will most likely be using the most. Add a macro with the name macroName to the model Model::class.

After the macro has been added, you can call the method on the model as you normally would.

MacroableModels::addMacro(\App\User::class, 'sayHi', function() { return "Hi!"; });

\App\User::first()->sayHi();

With parameters

The defined macro function can receive any number and type of parameters.

MacroableModels::addMacro(\App\User::class, 'say', function(string $something) { return $something; });

$user = \App\User::first();
$user->say("Hello world!");

Context binding... the correct $this

On the macro function you have access to the $this object, which references the instance of the model that is executing the function.

MacroableModels::addMacro(\App\User::class, 'getId', function() { return $this->id; });

\App\User::first()->getId();
// 1

Adding relationships

You can define relationship functions too!

MacroableModels::addMacro(\App\User::class, 'posts', function() {
    return $this->hasMany(App\Post::class);
});

Beware! You won't be able to use Laravel's magic relationship attributes.

$user = App\User::first();

$user->posts;
// null
// This will always return null, as the posts attribute wasn't defined

$user->posts()->get()
// This will correctly return the posts Eloquent collection

Overriding existing macro

If you add a macro with the same name of an existing one, it replaces it.

MacroableModels::addMacro(\App\User::class, 'greet', function() { return "Hi!"; });
\App\User::first()->greet();
// "Hi!"

MacroableModels::addMacro(\App\User::class, 'greet', function() { return "Hello human"; });
\App\User::first()->greet();
// "Hello human"

Model's methods precedence

If you add a macro with the same name of an existing method from the model, the latter will take precedence. You won't be able to override it with this package.

class Dog extends Illuminate\Database\Eloquent\Model
{
    public function bark()
    {
        return "Woof!";
    }
}

MacroableModels::addMacro(Dog::class, 'bark', function() { return "Miauuu!"; });

$dog = new Dog;
$dog->bark();
// "Woof!"

removeMacro(Model::class, 'macroName') : boolean

The opposite of addMacro, this method removes a previously added macro from the specified model. It returns true if a macro with that name was previously registered on the model and it removed it correctly - and false otherwise.

MacroableModels::removeMacro(\App\User::class, 'salute');
// false

MacroableModels::addMacro(\App\User::class, 'salute', function() { return "Hello!"; });

MacroableModels::removeMacro(\App\User::class, 'salute');
// true

Additional goodies

Because, why not? 🤷‍♂

getAllMacros() : Array

Returns all registered macros, grouped by name.

MacroableModels::addMacro(\App\User::class, 'hi', function() { return "Hi!"; })

MacroableModels::addMacro(\App\Dog::class, 'hi', function() { return "Woof!"; })

MacroableModels::addMacro(\App\User::class, 'bye', function() { return "Bye bye"; })

MacroableModels::getAllMacros()

/*
   [
     "hi" => [
       "App\User" => Closure() {#3362 …2},
       "App\Dog" => Closure() {#3376 …2},
     ],
     "bye" => [
       "App\User" => Closure() {#3366 …2},
     ],
   ]
*/

modelHasMacro(Model::class, 'macroName') : boolean

Simple: if the model has the macro, it returns true - else, it returns false.

MacroableModels::modelHasMacro(\App\User::class, 'salute');
// false

MacroableModels::addMacro(\App\User::class, 'salute', function() { return "Hi!"; });
MacroableModels::modelHasMacro(\App\User::class, 'salute');
// true

modelsThatImplement('macroName') : Array

Given a macro name, it returns an array with the classes of the models to which it was added.

MacroableModels::addMacro(\App\User::class, 'hi', function() { return "Hi!"; });
MacroableModels::addMacro(\App\Dog::class, 'hi', function() { return "Woof!"; });

MacroableModels::modelsThatImplement('hi');
/*
   [
      "App\User",
      "App\Dog",
   ]
*/

macrosForModel(Model::class) : Array

Given the model class, it returns an array with all the macros that were added to it, detailing the defined parameters for each.

MacroableModels::addMacro(\App\User::class, 'say', function(String $something) { return $something; });
MacroableModels::addMacro(\App\User::class, 'sum', function(Integer $a, Integer $b) { return $a + $b; });

MacroableModels::macrosForModel(\App\User::class);
/*
   [
     "say" => [
       "name" => "say",
       "parameters" => [
         ReflectionParameter {#3385
           +name: "something",
           position: 0,
           typeHint: "string",
         },
       ],
     ],
     "sum" => [
       "name" => "sum",
       "parameters" => [
         ReflectionParameter {#3357
           +name: "a",
           position: 0,
           typeHint: "Integer",
         },
         ReflectionParameter {#3360
           +name: "b",
           position: 1,
           typeHint: "Integer",
         },
       ],
     ],
   ]
*/

Related packages

There are some related packages out there, from which some inspiration was taken.

  • imanghafoori1/eloquent-relativity: github
  • spatie/laravel-collection-macros: github
  • spatie/macroable: github

javoscript/laravel-macroable-models 适用场景与选型建议

javoscript/laravel-macroable-models 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 795.67k 次下载、GitHub Stars 达 71, 最近一次更新时间为 2020 年 02 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 javoscript/laravel-macroable-models 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 javoscript/laravel-macroable-models 我们能提供哪些服务?
定制开发 / 二次开发

基于 javoscript/laravel-macroable-models 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 795.67k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 71
  • 点击次数: 11
  • 依赖项目数: 14
  • 推荐数: 0

GitHub 信息

  • Stars: 71
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-29