diff --git a/README.md b/README.md index fa9ee80..9dcd50a 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ abstract class BaseModel extends Model 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: +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 performance reasons, you should cache the dynamically generated morph map by running the following command: ```bash php artisan morph-map:cache @@ -137,6 +137,8 @@ Removing a cached morph map can be done by running: php artisan morph-map:clear ``` +When using Laravel 11+, the morph map can also be cached by using Laravel's optimize commands: `optimize` and `optimize:clear` + ### Using a custom resolver You can also determine morph class values programmatically by using a custom resolver. For example, you could use the following to automatically derive the value based on the singular form of the model's table name: diff --git a/config/morph-map-generator.php b/config/morph-map-generator.php index 4fcd923..04d0bd5 100644 --- a/config/morph-map-generator.php +++ b/config/morph-map-generator.php @@ -8,6 +8,13 @@ 'autogenerate' => true, + /* + * When enabled, morph maps will be optimized alongside Laravel 11+. + */ + + 'optimize' => true, + + /** * When your models don't reside in the default App namespace you can set a * different base directory. diff --git a/src/MorphMapGeneratorServiceProvider.php b/src/MorphMapGeneratorServiceProvider.php index e01858c..d8c1ffe 100644 --- a/src/MorphMapGeneratorServiceProvider.php +++ b/src/MorphMapGeneratorServiceProvider.php @@ -29,6 +29,10 @@ public function boot(MorphMapCacheDriver $cache): void CacheMorphMapCommand::class, ClearMorphMapCommand::class, ]); + + if(method_exists($this, 'optimizes') && config('morph-map-generator.optimize', true)) { + $this->optimizes('morph-map:cache', 'morph-map:clear', 'morph-map'); + } } if ($cache->exists()) {