-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wip): make routing configurable
- Loading branch information
1 parent
e30f028
commit d5daef0
Showing
16 changed files
with
231 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true"> | ||
<testsuites> | ||
<testsuite name="Functional"> | ||
<directory>tests/Functional</directory> | ||
</testsuite> | ||
<testsuite name="Unit"> | ||
<directory>tests/Unit</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="DB_CONNECTION" value="testing"/> | ||
</php> | ||
<source> | ||
<include> | ||
<directory>src</directory> | ||
</include> | ||
</source> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" | ||
colors="true"> | ||
<testsuites> | ||
<testsuite name="Functional"> | ||
<directory>tests/Functional</directory> | ||
</testsuite> | ||
<testsuite name="Unit"> | ||
<directory>tests/Unit</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="DB_CONNECTION" value="testing"/> | ||
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/> | ||
</php> | ||
<source> | ||
<include> | ||
<directory>src</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Apiato\Foundation\Configuration; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Facades\Route as LaravelRoute; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
|
||
final class Routing | ||
{ | ||
private array $apiRouteDirs = []; | ||
private array $webRouteDirs = []; | ||
|
||
public function loadApiRoutesFrom(string ...$path): self | ||
{ | ||
$this->apiRouteDirs = $path; | ||
|
||
return $this; | ||
} | ||
|
||
public function loadWebRoutesFrom(string ...$path): self | ||
{ | ||
$this->webRouteDirs = $path; | ||
|
||
return $this; | ||
} | ||
|
||
public function registerApiRoutes(): void | ||
{ | ||
collect($this->apiRouteDirs) | ||
->map(fn ($path) => $this->getFilesSortedByName($path)) | ||
->flatten() | ||
->each(fn (SplFileInfo $file) => $this->loadApiRoute($file)); | ||
} | ||
|
||
/** | ||
* @return SplFileInfo[] | ||
*/ | ||
private function getFilesSortedByName(string $apiRoutesPath): array | ||
{ | ||
$files = File::allFiles($apiRoutesPath); | ||
|
||
return Arr::sort($files, static fn ($file) => $file->getFilename()); | ||
} | ||
|
||
private function loadApiRoute(SplFileInfo $file): void | ||
{ | ||
$routeGroupArray = $this->getApiRouteGroup($file); | ||
|
||
LaravelRoute::group($routeGroupArray, static function () use ($file): void { | ||
require $file->getPathname(); | ||
}); | ||
} | ||
|
||
public function getApiRouteGroup(SplFileInfo|string $endpointFileOrPrefixString): array | ||
{ | ||
return [ | ||
'middleware' => $this->getMiddlewares(), | ||
'domain' => config('apiato.api.url'), | ||
// If $endpointFileOrPrefixString is a string, use that string as prefix | ||
// else, if it is a file then get the version name from the file name, and use it as prefix | ||
'prefix' => is_string($endpointFileOrPrefixString) ? $endpointFileOrPrefixString : $this->getApiVersionPrefix($endpointFileOrPrefixString), | ||
]; | ||
} | ||
|
||
private function getMiddlewares(): array | ||
{ | ||
$middlewares = ['api']; | ||
if (config('apiato.api.rate-limiter.enabled')) { | ||
$middlewares[] = 'throttle:' . config('apiato.api.rate-limiter.name'); | ||
} | ||
|
||
return $middlewares; | ||
} | ||
|
||
private function getApiVersionPrefix(SplFileInfo $file): string | ||
{ | ||
return config('apiato.api.prefix') . (config('apiato.api.enable_version_prefix') ? $this->getRouteFileVersionFromFileName($file) : ''); | ||
} | ||
|
||
private function getRouteFileVersionFromFileName(SplFileInfo $file): string|bool | ||
{ | ||
$fileNameWithoutExtension = pathinfo($file->getFilename(), PATHINFO_FILENAME); | ||
$fileNameWithoutExtensionExploded = explode('.', $fileNameWithoutExtension); | ||
|
||
end($fileNameWithoutExtensionExploded); | ||
|
||
return prev($fileNameWithoutExtensionExploded); | ||
} | ||
|
||
public function webRoutes(): array | ||
{ | ||
$files = []; | ||
foreach ($this->webRouteDirs as $path) { | ||
foreach (glob($path . '/*.php') as $file) { | ||
$files[] = $file; | ||
} | ||
} | ||
usort($files, 'strcmp'); | ||
|
||
return $files; | ||
} | ||
} |
Oops, something went wrong.