Skip to content

feat: namespace route config #16

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

return [
'namespaces' => [
// 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'
];
```
41 changes: 33 additions & 8 deletions src/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand All @@ -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;
});
}
Expand Down
8 changes: 6 additions & 2 deletions src/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down