Skip to content

Commit

Permalink
DE-48590 - Add to OnlyNecessaryRoutesProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbrauner committed May 17, 2023
1 parent 0b257f8 commit 75a2c65
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
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
}
}
}
}

0 comments on commit 75a2c65

Please sign in to comment.