Skip to content

Commit

Permalink
Register optimize commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Riley Aven committed Oct 28, 2024
1 parent 600d8b1 commit 401bdb9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions config/morph-map-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions src/MorphMapGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit 401bdb9

Please sign in to comment.