Skip to content
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

DE-48590 - Add to OnlyNecessaryRoutesProvider #56

Merged
merged 1 commit into from May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Route/OnlyNecessaryRoutesProvider.php
Expand Up @@ -5,6 +5,7 @@
use function apcu_exists;
use function apcu_fetch;
use function apcu_store;
use function in_array;
use function sprintf;
use function strpos;
use function strtok;
Expand All @@ -16,11 +17,16 @@ class OnlyNecessaryRoutesProvider
{
/**
* @param mixed[] $allRoutes
* @param string[] $apiNamesAlwaysInclude
*
* @return mixed[]
*/
public function getRoutes(?string $url, array $allRoutes, bool $useApcuCache): array
{
public function getRoutes(
?string $url,
array $allRoutes,
bool $useApcuCache,
array $apiNamesAlwaysInclude = []
): array {
if ($url === null) {
return $allRoutes;
}
Expand All @@ -29,7 +35,7 @@ public function getRoutes(?string $url, array $allRoutes, bool $useApcuCache): a
$filteredRoutes = [];

foreach ($allRoutes as $apiName => $apiRoutes) {
if (strpos($url, '/' . $apiName) === false) {
if (strpos($url, '/' . $apiName) === false && !in_array($apiName, $apiNamesAlwaysInclude, true)) {
continue;
}

Expand Down
8 changes: 7 additions & 1 deletion src/SlimApplicationFactory.php
Expand Up @@ -112,6 +112,11 @@ public function create(): SlimApp
false
);

$routeApiNamesAlwaysInclude = (array)$this->getSlimSettings(
SlimSettings::ROUTE_API_NAMES_ALWAYS_INCLUDE,
[]
);

if ($useApcuCache && !apcu_enabled()) {
throw new LogicException('APCU cache is not enabled');
}
Expand Down Expand Up @@ -140,7 +145,8 @@ public function create(): SlimApp
$routesToRegister = $this->onlyNecessaryRoutesProvider->getRoutes(
$requestUri,
$routesToRegister,
$useApcuCache
$useApcuCache,
$routeApiNamesAlwaysInclude
);
}

Expand Down
1 change: 1 addition & 0 deletions src/SlimSettings.php
Expand Up @@ -11,4 +11,5 @@ class SlimSettings
public const REGISTER_ONLY_NECESSARY_ROUTES = 'registerOnlyNecessaryRoutes';
public const USE_APCU_CACHE = 'useApcuCache';
public const DISABLE_USING_SLIM_CONTAINER = 'disableUsingSlimContainer';
public const ROUTE_API_NAMES_ALWAYS_INCLUDE = 'routeApiNamesAlwaysInclude';
}
17 changes: 17 additions & 0 deletions tests/Route/OnlyNecessaryRoutesProviderTest.php
Expand Up @@ -27,6 +27,23 @@ public function testGetRoutesReturnsFilteredRoutes(): void
}


public function testGetRoutesReturnsFilteredRoutesWithAlwaysIncludeApp(): void
{
$sampleRequestUri = '/chat/1.0/brand/1000/channel/chat_f00189ec-6a4b-4c76-b504-03c7ebcadb9c?parameter=12345';

$sampleRoutes = FileLoader::loadArrayFromJsonFile(__DIR__ . '/__fixtures__/all_routes.json');
$expectedRoutes = FileLoader::loadArrayFromJsonFile(
__DIR__ . '/__fixtures__/expected_routes_with_custom_components.json'
);

$provider = new OnlyNecessaryRoutesProvider();

$routes = $provider->getRoutes($sampleRequestUri, $sampleRoutes, false, ['custom-components']);

Assert::assertSame($expectedRoutes, $routes);
}


public function testGetRoutesReturnsAllRoutesWhenRequestUriIsNull(): void
{
$sampleRoutes = FileLoader::loadArrayFromJsonFile(__DIR__ . '/__fixtures__/all_routes.json');
Expand Down
@@ -0,0 +1,45 @@
{
"custom-components": {
"endpoints": {
"get": {
"service": "api.route.endpoints",
"middlewares": [],
"middlewareGroups": [],
"ignoreVersionMiddlewareGroup": false,
"name": null
}
}
},
"1.0": {
"endpoints": {
"get": {
"service": "api.route.endpoints",
"middlewares": [],
"middlewareGroups": [],
"ignoreVersionMiddlewareGroup": false,
"name": null
}
},
"brand\/{brandId}\/channel\/{channelPlatformId}": {
"get": {
"service": "channelInfo.route",
"middlewares": [
"channelInfo.cache.cachedChannelInfoMiddleware",
"api.middleware.corsHeadersResolver"
],
"middlewareGroups": [],
"ignoreVersionMiddlewareGroup": false,
"name": null
},
"options": {
"service": "api.route.preflightRequest",
"middlewares": [
"api.middleware.corsHeadersResolver"
],
"middlewareGroups": [],
"ignoreVersionMiddlewareGroup": false,
"name": null
}
}
}
}