diff --git a/src/RouteServiceProvider.php b/src/RouteServiceProvider.php index 860a0dd..761f05f 100644 --- a/src/RouteServiceProvider.php +++ b/src/RouteServiceProvider.php @@ -13,29 +13,41 @@ class RouteServiceProvider extends ServiceProvider * @param \Illuminate\Routing\Router $router * @return void */ - public function boot(Router $router) + public function boot() { $this->loadConfig(); - parent::boot($router); + parent::boot(); } + /** + * Register + */ public function register() { $this->registerAssets(); } + /** + * Register assets + */ private function registerAssets() { $this->publishes([ - __DIR__.'/../config/optimus.components.php' => config_path('optimus.components.php') + __DIR__ . '/config/optimus.components.php' => config_path('optimus.components.php'), ]); } + /** + * Load configuration + */ private function loadConfig() { - if ($this->app['config']->get('optimus.components') === null) { - $this->app['config']->set('optimus.components', require __DIR__.'/config/optimus.components.php'); + /** @var \Illuminate\Config\Repository $config */ + $config = $this->app['config']; + + if ($config->get('optimus.components') === null) { + $config->set('optimus.components', require __DIR__ . '/config/optimus.components.php'); } } @@ -50,32 +62,40 @@ public function map(Router $router) $config = $this->app['config']['optimus.components']; $middleware = $config['protection_middleware']; + $highLevelParts = array_map(function ($namespace) { return glob(sprintf('%s%s*', $namespace, DIRECTORY_SEPARATOR), GLOB_ONLYDIR); }, $config['namespaces']); foreach ($highLevelParts as $part => $partComponents) { foreach ($partComponents as $componentRoot) { - $component = substr($componentRoot, strrpos($componentRoot, '/')+1); - $namespace = $part . '\\' . $component . '\\Controllers'; + $component = substr($componentRoot, strrpos($componentRoot, '/') + 1); + + $namespace = sprintf( + '%s\\%s\\Controllers', + $part, + $component + ); $fileNames = [ 'routes' => true, 'routes_protected' => true, - 'routes_public' => false + 'routes_public' => false, ]; foreach ($fileNames as $fileName => $protected) { $path = sprintf('%s/%s.php', $componentRoot, $fileName); - if (file_exists($path)) { - $router->group([ - 'middleware' => $protected ? $middleware : [], - 'namespace' => $namespace - ], function ($router) use ($path) { - require $path; - }); + if (!file_exists($path)) { + continue; } + + $router->group([ + 'middleware' => $protected ? $middleware : [], + 'namespace' => $namespace, + ], function ($router) use ($path) { + require $path; + }); } } }