Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register optimize commands #17

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading