tarik02/laravel-mixin

Mixin library for Laravel

v0.1.0 2020-01-27 15:15 UTC

This package is auto-updated.

Last update: 2024-04-28 01:14:04 UTC


README

Travis CI Build Status Latest Stable Version Total Downloads Packagist License

Take class, some traits and interfaces and put them in one place. For example, add new methods, relations, etc. to existing model without touching it. Very useful for dividing project into packages without loosing ability to improve.

NOTE: This can only be used with special packages which are ready to use this package. See below for more information.

Installation

$ composer require tarik02/laravel-mixin
$ mkdir -p storage/framework/mixin
$ echo "*\n\!.gitignore" > storage/framework/mixin/.gitignore

Commands

The package provides the following commands:

$ php artisan mixin:cache        # Cache all mixins and use only cache
$ php artisan mixin:cache:clear  # Clear mixins cache
$ php artisan mixin:generate     # Generate all mixins

Cache should only be used in production and be regenerated after every code change.

Documentation

You can generate documentation using the Sami. Documentation for master branch is always available here.

Note

You should use all methods of this package only during application booting phase. Using them during another stages may lead to undefined behaviour.

Usage example

Create your model class (note, the class is abstract):

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Article
 *
 * @property int $id
 * @property string $title
 * @property string $description
 * @property string $text
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 */
abstract class Article extends Model
{
    //
}

Create App\Providers\MixinServiceProvider:

<?php

namespace App\Providers;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\ServiceProvider;

class MixinServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $mixin = $this->app->make('mixin');

        $mixin
            ->register(\Models\Article::class, \App\Models\Article::class)
            ->mixTrait(SoftDeletes::class)
        ;

        $mixin
            ->class(\Models\Article::class)
            ->mixInterface(MyInterface::class)
        ;
    }
}

Add MixinServiceProvider before AppServiceProvider to providers section in config/app.php:

+        App\Providers\MixinServiceProvider::class,
         App\Providers\AppServiceProvider::class,

Now you have to use \Models\Article class instead of \App\Models\Article (really, you can name it whatever you want).

If your trait has a constructor, you should pass true as second parameter to mixTrait function.

Complete list of building methods of MixedClass:

  • mixInterface(string $name) - add an interface to implements section.
  • mixTrait(string $name, bool $hasConstructor = false) - add a trait to use section.
  • useInsteadOf(string $method, string $trait, string $replacedTrait) - use method named $method from trait $trait instead of the one from $replacedTrait.
  • renameMethod(string $trait, string $oldName, string $newName) - rename method named $oldName of trait $trait to $newName.
  • beforeConstruct(string $code) - add $code before parent constructor call.
  • afterConstruct(string $code) - add $code after parent constructor call.
  • code(string $code) - add $code into class body (property or method).

Under the hood

Actually, the code above generates class which extends specified class, uses specified traits and implements specified interfaces. Classes are located at storage/framework/mixin and can be cached (see Caching section). Here's how the class looks like:

<?php

namespace Models;

use App\Models\Article as Base;

class Article
    extends Base
{
    use \Illuminate\Database\Eloquent\SoftDeletes;
}

License

The package is open-sourced software licensed under the MIT license.