diff --git a/src/Config/module.php b/src/Config/module.php index 603299b..a6cd876 100644 --- a/src/Config/module.php +++ b/src/Config/module.php @@ -177,19 +177,16 @@ /* |-------------------------------------------------------------------------- - | Choose what module will register as custom namespaces. - | Setting one to false will require you to register that part - | in your own Service Provider class. + | Autoload |-------------------------------------------------------------------------- + | + | Here is the load classes config. + | */ - 'register' => [ - 'translations' => true, - /** - * load files on boot or register method - * - * @example boot|register - */ - 'files' => 'register', + 'autoload' => [ + 'translations' => true, + 'files' => 'register', + 'composer' => 'register', ], ]; diff --git a/src/Laravel/LaravelFileRepository.php b/src/Laravel/LaravelFileRepository.php index 0965def..d4ceb7d 100644 --- a/src/Laravel/LaravelFileRepository.php +++ b/src/Laravel/LaravelFileRepository.php @@ -3,6 +3,7 @@ namespace Akaunting\Module\Laravel; use Akaunting\Module\FileRepository; +use Akaunting\Module\Laravel\Module; class LaravelFileRepository extends FileRepository { diff --git a/src/Laravel/Module.php b/src/Laravel/Module.php index 623a932..936ad68 100644 --- a/src/Laravel/Module.php +++ b/src/Laravel/Module.php @@ -2,11 +2,11 @@ namespace Akaunting\Module\Laravel; +use Akaunting\Module\Module as BaseModule; use Illuminate\Filesystem\Filesystem; use Illuminate\Foundation\AliasLoader; use Illuminate\Foundation\ProviderRepository; use Illuminate\Support\Str; -use Akaunting\Module\Module as BaseModule; class Module extends BaseModule { diff --git a/src/Lumen/LumenFileRepository.php b/src/Lumen/LumenFileRepository.php index faff504..f0b1b44 100644 --- a/src/Lumen/LumenFileRepository.php +++ b/src/Lumen/LumenFileRepository.php @@ -3,6 +3,7 @@ namespace Akaunting\Module\Lumen; use Akaunting\Module\FileRepository; +use Akaunting\Module\Lumen\Module; class LumenFileRepository extends FileRepository { diff --git a/src/Module.php b/src/Module.php index aa73125..f13694b 100644 --- a/src/Module.php +++ b/src/Module.php @@ -189,27 +189,48 @@ public function setPath($path) } /** - * Bootstrap the application events. + * Register the module. + */ + public function register() + { + $this->autoload('register'); + + $this->registerAliases(); + + $this->registerProviders(); + + $this->fireEvent('register'); + } + + /** + * Boot the module. */ public function boot() { - if (config('module.register.translations', true) === true) { - $this->registerTranslation(); + if (config('module.autoload.translations') === true) { + $this->loadTranslations(); } - if (config('module.register.files') == 'boot') { - $this->registerFiles(); - } + $this->autoload('boot'); $this->fireEvent('boot'); } + public function autoload(string $stage): void + { + if (config('module.autoload.files') == $stage) { + $this->loadFiles(); + } + + if (config('module.autoload.composer') == $stage) { + $this->loadComposer(); + } + } + /** - * Register module's translation. - * - * @return void + * Load the translations of this module. */ - protected function registerTranslation() + protected function loadTranslations(): void { $name = $this->getAlias(); @@ -220,6 +241,30 @@ protected function registerTranslation() } } + /** + * Load the files from this module. + */ + protected function loadFiles(): void + { + foreach ($this->get('files', []) as $file) { + include $this->getPath() . '/' . $file; + } + } + + /** + * Load the composer of this module. + */ + protected function loadComposer(): void + { + $autoload = $this->getPath() . '/vendor/autoload.php'; + + if (! is_file($autoload)) { + return; + } + + include $autoload; + } + /** * Get json contents from the cache, setting as needed. * @@ -264,22 +309,6 @@ public function getComposerAttr($key, $default = null) return $this->json('composer.json')->get($key, $default); } - /** - * Register the module. - */ - public function register() - { - $this->registerAliases(); - - $this->registerProviders(); - - if (config('module.register.files') == 'register') { - $this->registerFiles(); - } - - $this->fireEvent('register'); - } - /** * Register the module event. * @@ -306,16 +335,6 @@ abstract public function registerProviders(); */ abstract public function getCachedServicesPath(); - /** - * Register the files from this module. - */ - protected function registerFiles() - { - foreach ($this->get('files', []) as $file) { - include $this->path . '/' . $file; - } - } - /** * Handle call __toString. *