Skip to content

Automatically generate morph maps in your Laravel application

License

Notifications You must be signed in to change notification settings

angeljqv/laravel-morph-map-generator

 
 

Repository files navigation

Automatically generate morph maps in your Laravel application

Latest Version on Packagist Tests Psalm Check & fix styling Total Downloads

With this package, you shouldn't worry about forgetting to add models to your application's morph map. Each model will autoregister itself in the morph map. The only thing you should do is implementing the getMorphClass method on your models like this:

class Post extends Model
{
    public function getMorphClass(): string {
        return 'post';
    }
}

From now on, the Post model will be represented as post within your morph map.

Support us

We invest many resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-morph-map-generator

You can publish the config file with:

php artisan vendor:publish --provider="Spatie\LaravelMorphMapGenerator\MorphMapGeneratorServiceProvider" --tag="config"

This is the contents of the published config file:

return [
    /*
    * When enabled, morph maps will be automatically generated when the
    * application is booted.
    */

    'autogenerate' => true,

    /**
     * Change the base directory if the models don't reside in the default App namespace.
     *
     * For example, the base directory would become 'src' if:
     * - Application is in src/App
     * - Models are in src/Domain
     */

    'base_directory' => '/',

    /*
    * Within these paths, the package will search for models to be included
    * in the generated morph map.
    */

    'paths' => [
        app_path(),
    ],

    /*
    * Only models that extend from one of the base models defined here will
    * be included in the generated morph map.
    */

    'base_models' => [
        Illuminate\Database\Eloquent\Model::class,
    ],

    /*
    * When generating the morph map, these models will not be included.
    */

    'ignored_models' => [],


    /*
    * Morph maps can be cached, there's a `FilesystemMorphMapCacheDriver` which
    * stores the morph map as a file in a directory or you can also use the
    * Laravel built-in cache by using `LaravelMorphMapCacheDriver`.
    *
    * Both drivers have their own config:
    * - `FilesystemMorphMapCacheDriver` requires a `path` to store the file
    * - `LaravelMorphMapCacheDriver` requires a `key` for storage
    */

    'cache' => [
        'type' => Spatie\LaravelMorphMapGenerator\Cache\FilesystemMorphMapCacheDriver::class,
        'path' => storage_path('app/morph-map-generator'),
    ],
];

Usage

First, you have to implement getMorphClass for the models you want to include in your morph map. We suggest you create a new base model class in your application from which all your models extend. So you could throw an exception when getMorphClass was not yet implemented:

use Illuminate\Database\Eloquent\Model;

abstract class BaseModel extends Model
{
    public function getMorphClass()
    {
        throw new Exception('The model should implement `getMorphClass`');
    }
}

When a model is not implementing getMorphClass, it will throw an exception when building the generated morph map, making it possible to quickly find models that do not have a morph map entry.

When autogenerate is enabled in the morph-map-generator config file, the morph map in your application will be dynamically generated each time the application boots. This is great in development environments since each time your application boots, the morph map is regenerated. For performanc reasons, you should cache the dynamically generated morph map by running the following command:

php artisan morph-map:cache

Removing a cached morph map can be done by running:

php artisan morph-map:clear

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Automatically generate morph maps in your Laravel application

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%