From 963b01878a0bdd419a277510d304b4af5d809687 Mon Sep 17 00:00:00 2001 From: Patrick McCarren Date: Fri, 5 Jul 2019 20:49:03 -0400 Subject: [PATCH 1/2] feat: namespace route config --- src/RouteServiceProvider.php | 41 +++++++++++++++++++++++++++++------- src/Utilities.php | 8 +++++-- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/RouteServiceProvider.php b/src/RouteServiceProvider.php index 84ea63f..58fe573 100644 --- a/src/RouteServiceProvider.php +++ b/src/RouteServiceProvider.php @@ -61,19 +61,37 @@ public function map(Router $router) { $config = $this->app['config']['optimus.components']; - $middleware = $config['protection_middleware']; + $protectionMiddleware = $config['protection_middleware']; $highLevelParts = array_map(function ($namespace) { - return glob(sprintf('%s%s*', $namespace, DIRECTORY_SEPARATOR), GLOB_ONLYDIR); + if (! is_array($namespace)) { + $namespace = [ + 'path' => $namespace, + 'route' => [] + ]; + } + + if (! array_key_exists('route', $namespace)) { + $namespace['route'] = []; + } + + if (! array_key_exists('middleware', $namespace['route'])) { + $namespace['route']['middleware'] = []; + } + + return [ + 'components' => glob(sprintf('%s%s*', $namespace['path'], DIRECTORY_SEPARATOR), GLOB_ONLYDIR), + 'route' => $namespace['route'], + ]; }, $config['namespaces']); - foreach ($highLevelParts as $part => $partComponents) { - foreach ($partComponents as $componentRoot) { + foreach ($highLevelParts as $highPart => $part) { + foreach ($part['components'] as $componentRoot) { $component = substr($componentRoot, strrpos($componentRoot, DIRECTORY_SEPARATOR) + 1); $namespace = sprintf( '%s\\%s\\Controllers', - $part, + $highPart, $component ); @@ -90,10 +108,17 @@ public function map(Router $router) continue; } - $router->group([ - 'middleware' => $protected ? $middleware : [], + $middleware = $part['route']['middleware']; + if ($protected) { + $middleware = array_merge($protectionMiddleware, $middleware); + } + + $group = array_merge($part['route'], [ 'namespace' => $namespace, - ], function ($router) use ($path) { + 'middleware' => $middleware, + ]); + + $router->group($group, function ($router) use ($path) { require $path; }); } diff --git a/src/Utilities.php b/src/Utilities.php index 69911d1..8c0d758 100644 --- a/src/Utilities.php +++ b/src/Utilities.php @@ -6,8 +6,12 @@ class Utilities { public static function findNamespaceResources(array $namespaces, $resourceFolderName, $resourceNamespace) { - return array_reduce($namespaces, function ($carry, $namespacePath) use ($resourceNamespace, $resourceFolderName) { - $components = glob(sprintf('%s%s*', $namespacePath, DIRECTORY_SEPARATOR), GLOB_ONLYDIR); + return array_reduce($namespaces, function ($carry, $namespaceConfig) use ($resourceNamespace, $resourceFolderName) { + if (! is_array($namespaceConfig)) { + $namespaceConfig = ['path' => $namespaceConfig]; + } + + $components = glob(sprintf('%s%s*', $namespaceConfig['path'], DIRECTORY_SEPARATOR), GLOB_ONLYDIR); $paths = array_map(function ($component) use ($resourceNamespace, $resourceFolderName) { $path = [$component]; From b3cb1e9b6dcfaf6febfea60fdf0f809daca48e20 Mon Sep 17 00:00:00 2001 From: Patrick McCarren Date: Fri, 5 Jul 2019 20:55:54 -0400 Subject: [PATCH 2/2] chore: docs --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index cd07b94..a147dbf 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,38 @@ Some service providers to enable a Laravel project structure that is grouped by ```bash composer require optimus/distributed-laravel 0.1.* ``` + +## Usage +Define a `optimus.components.php` configuration file. For example: + +```php + [ + // Define a simple namespace mapping + 'Infrastructure' => base_path() . DIRECTORY_SEPARATOR . 'infrastructure', + + // Here we define a namespace mapping with route config + 'Api' => [ + 'path' => base_path() . DIRECTORY_SEPARATOR . 'api', + 'route' => [ + 'middleware' => [ + 'requestid' + ] + ] + ], + ], + + // middleware to be applied to all routes within routes.php or routes_protected.php. + 'protection_middleware' => [ + 'auth:api' + ], + + 'resource_namespace' => 'resources', + + 'language_folder_name' => 'lang', + + 'view_folder_name' => 'views' +]; +```