From 6fef885e537dcc472ced8d15d5727cdf09940b5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammed=20Sar=C4=B1?= Date: Sat, 28 Nov 2020 17:32:52 +0100 Subject: [PATCH] Support disjointed app and model namespaces --- README.md | 10 ++++++++++ config/morph-map-generator.php | 10 ++++++++++ src/Commands/CacheMorphMapCommand.php | 2 ++ src/MorphMapGeneratorServiceProvider.php | 1 + 4 files changed, 23 insertions(+) diff --git a/README.md b/README.md index 650efd0..26e48bb 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,16 @@ return [ '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. diff --git a/config/morph-map-generator.php b/config/morph-map-generator.php index eec13fa..83a2e26 100644 --- a/config/morph-map-generator.php +++ b/config/morph-map-generator.php @@ -8,6 +8,16 @@ '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. diff --git a/src/Commands/CacheMorphMapCommand.php b/src/Commands/CacheMorphMapCommand.php index 631c439..72dd840 100644 --- a/src/Commands/CacheMorphMapCommand.php +++ b/src/Commands/CacheMorphMapCommand.php @@ -16,6 +16,7 @@ class CacheMorphMapCommand extends Command public function handle(MorphMapCacheDriver $cache): void { [ + 'base_directory' => $baseDir, 'paths' => $paths, 'base_models' => $baseModels, 'ignored_models' => $ignoredModels, @@ -23,6 +24,7 @@ public function handle(MorphMapCacheDriver $cache): void $discoveredModels = DiscoverModels::create() ->ignoreModels($ignoredModels) + ->withBasePath(base_path($baseDir)) ->withPaths($paths) ->withBaseModels($baseModels) ->discover(); diff --git a/src/MorphMapGeneratorServiceProvider.php b/src/MorphMapGeneratorServiceProvider.php index 867376c..8c63453 100644 --- a/src/MorphMapGeneratorServiceProvider.php +++ b/src/MorphMapGeneratorServiceProvider.php @@ -27,6 +27,7 @@ public function register(): void if (config('morph-map-generator.autogenerate')) { $discoveredModels = DiscoverModels::create() + ->withBasePath(base_path(config('morph-map.generator.base_directory'))) ->withPaths(config('morph-map-generator.paths')) ->withBaseModels(config('morph-map-generator.base_models')) ->ignoreModels(config('morph-map-generator.ignored_models'))