tarik02/laravel-mixin is a Laravel package for mixin library for laravel.
It currently has 0 GitHub stars and 2 downloads on Packagist (latest version v0.1.0).
Install it with composer require tarik02/laravel-mixin.
Discover more Laravel packages by tarik02
or browse all Laravel packages to compare alternatives.
Last updated
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.
$ composer require tarik02/laravel-mixin
$ mkdir -p storage/framework/mixin
$ echo "*\n\!.gitignore" > storage/framework/mixin/.gitignore
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.
You can generate documentation using the Sami. Documentation for master branch is always available here.
You should use all methods of this package only during application booting phase. Using them during another stages may lead to undefined behaviour.
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).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;
}
The package is open-sourced software licensed under the MIT license.