Skip to content

Commit

Permalink
FRW-2465 Fixed performance problem related to ZED/Merchant portal nav…
Browse files Browse the repository at this point in the history
…igation. (#11007)

FRW-2465 Fixed performance problem related to ZED/Merchant portal navigation.
  • Loading branch information
kraal-spryker authored Aug 20, 2024
1 parent 1d2e709 commit db71db1
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 25 deletions.
100 changes: 98 additions & 2 deletions src/Spryker/Zed/Router/Business/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,35 @@

namespace Spryker\Zed\Router\Business\Router;

use Spryker\Zed\Router\Business\Route\Route;
use Spryker\Zed\Router\RouterConfig;
use Spryker\Zed\RouterExtension\Dependency\Plugin\RouterEnhancerAwareInterface;
use Symfony\Component\Config\ConfigCacheFactory;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Router as SymfonyRouter;

class Router extends SymfonyRouter implements RouterInterface, WarmableInterface
{
/**
* @var string
*/
protected const ROUTE_CACHE_PATH = '/url_generating_routes.php';

/**
* @var string
*/
protected const CACHE_DIR_PATH_PLACEHOLDER = '%s%s';

/**
* @var string
*/
protected const OPTION_CACHE_DIR = 'cache_dir';

/**
* @var array<\Spryker\Zed\RouterExtension\Dependency\Plugin\RouterEnhancerPluginInterface>
*/
Expand All @@ -28,16 +46,33 @@ class Router extends SymfonyRouter implements RouterInterface, WarmableInterface
*/
protected $configCacheFactory;

/**
* @var \Spryker\Zed\Router\RouterConfig
*/
protected RouterConfig $routerConfig;

/**
* @var array<string, mixed>|null
*/
protected static ?array $cache = [];

/**
* @param \Symfony\Component\Config\Loader\LoaderInterface $loader
* @param mixed $resource
* @param \Spryker\Zed\Router\RouterConfig $routerConfig
* @param array<\Spryker\Zed\RouterExtension\Dependency\Plugin\RouterEnhancerPluginInterface> $routerEnhancerPlugins
* @param array<string, mixed> $options
*/
public function __construct(LoaderInterface $loader, $resource, array $routerEnhancerPlugins, array $options = [])
{
public function __construct(
LoaderInterface $loader,
$resource,
RouterConfig $routerConfig,
array $routerEnhancerPlugins,
array $options = []
) {
parent::__construct($loader, $resource, $options);

$this->routerConfig = $routerConfig;
$this->routerEnhancerPlugins = $routerEnhancerPlugins;
}

Expand All @@ -57,6 +92,67 @@ public function getMatcher(): UrlMatcherInterface|RequestMatcherInterface
return $this->matcher;
}

/**
* @return \Symfony\Component\Routing\RouteCollection
*/
public function getRouteCollection(): RouteCollection
{
$cachePath = sprintf(static::CACHE_DIR_PATH_PLACEHOLDER, $this->options[static::OPTION_CACHE_DIR], static::ROUTE_CACHE_PATH);
if (!$this->routerConfig->isRoutingCacheEnabled() || !file_exists($cachePath)) {
return parent::getRouteCollection();
}

$routes = static::getSymfonyCompiledRoutes($cachePath);

return $this->mapRouteDataArrayCollectionToRouteCollection($routes);
}

/**
* @param array<mixed> $route
*
* @return \Spryker\Zed\Router\Business\Route\Route
*/
protected function mapRouteDataArrayToRouteObject(array $route): Route
{
[, $defaults, , $pathProperties] = $route;
[, $path] = array_pop($pathProperties);
$routeObject = new Route($path);
$routeObject->addDefaults($defaults);

return $routeObject;
}

/**
* @param array<mixed> $routes
*
* @return \Symfony\Component\Routing\RouteCollection
*/
protected function mapRouteDataArrayCollectionToRouteCollection(array $routes): RouteCollection
{
$routeCollection = new RouteCollection();

foreach ($routes as $name => $route) {
$routeObject = $this->mapRouteDataArrayToRouteObject($route);
$routeCollection->add($name, $routeObject);
}

return $routeCollection;
}

/**
* @param string $path
*
* @return array<mixed>
*/
public static function getSymfonyCompiledRoutes(string $path): array
{
if (!isset(static::$cache[$path])) {
static::$cache[$path] = require $path;
}

return static::$cache[$path] ??= require $path;
}

/**
* @param \Symfony\Component\Routing\Matcher\UrlMatcherInterface $matcher
*
Expand Down
5 changes: 5 additions & 0 deletions src/Spryker/Zed/Router/Business/RouterBusinessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function createBackofficeRouter(): RouterInterface
return new Router(
$this->createClosureLoader(),
$this->createBackofficeRouterResource(),
$this->getConfig(),
$this->getBackofficeRouterEnhancerPlugins(),
$this->getConfig()->getBackofficeRouterConfiguration(),
);
Expand All @@ -84,6 +85,7 @@ public function createMerchantPortalRouter(): RouterInterface
return new Router(
$this->createClosureLoader(),
$this->createMerchantPortalRouterResource(),
$this->getConfig(),
$this->getMerchantPortalRouterEnhancerPlugins(),
$this->getConfig()->getMerchantPortalRouterConfiguration(),
);
Expand Down Expand Up @@ -153,6 +155,7 @@ public function createBackendGatewayRouter(): RouterInterface
return new Router(
$this->createClosureLoader(),
$this->createBackendGatewayRouterResource(),
$this->getConfig(),
$this->getBackendGatewayRouterEnhancerPlugins(),
$this->getConfig()->getBackendGatewayRouterConfiguration(),
);
Expand Down Expand Up @@ -256,6 +259,7 @@ public function createZedRouter(): RouterInterface
return new Router(
$this->createClosureLoader(),
$this->createResource(),
$this->getConfig(),
$this->getRouterEnhancerPlugins(),
$this->getConfig()->getRouterConfiguration(),
);
Expand All @@ -277,6 +281,7 @@ public function createZedDevelopmentRouter(): RouterInterface
return new Router(
$this->createClosureLoader(),
$this->createResource(),
$this->getConfig(),
$this->getRouterEnhancerPlugins(),
$this->getConfig()->getDevelopmentRouterConfiguration(),
);
Expand Down
40 changes: 18 additions & 22 deletions src/Spryker/Zed/Router/Business/RouterFacadeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
interface RouterFacadeInterface
{
/**
* Specification:
* - Returns a ChainRouter which is added to the Application.
* - Executes {@link \Spryker\Zed\RouterExtension\Dependency\Plugin\RouterPluginInterface} plugin stack to add Router to the ChainRouter.
*
* @api
*
* @internal
*
* Specification:
* - Returns a ChainRouter which is added to the Application.
* - Uses RouterExtensionPluginInterfaces to add Router to the ChainRouter.
*
* @return \Spryker\Zed\Router\Business\Router\ChainRouter
*/
public function getBackofficeChainRouter(): ChainRouter;
Expand Down Expand Up @@ -53,14 +53,14 @@ public function getMerchantPortalChainRouter(): ChainRouter;
public function getBackofficeRouter(): RouterInterface;

/**
* Specification:
* - Returns a ChainRouter which is added to the BackendGateway Application.
* - Executes {@link \Spryker\Zed\RouterExtension\Dependency\Plugin\RouterPluginInterface} plugin stack to add Router to the ChainRouter.
*
* @api
*
* @internal
*
* Specification:
* - Returns a ChainRouter which is added to the BackendGateway Application.
* - Uses RouterExtensionPluginInterfaces to add Router to the ChainRouter.
*
* @return \Spryker\Zed\Router\Business\Router\ChainRouter
*/
public function getBackendGatewayChainRouter(): ChainRouter;
Expand All @@ -78,10 +78,6 @@ public function getBackendGatewayChainRouter(): ChainRouter;
public function getMerchantPortalRouter(): RouterInterface;

/**
* @api
*
* @internal
*
* Specification:
* - Returns Router which handles BackendGateway routes.
*
Expand All @@ -96,7 +92,7 @@ public function getBackendGatewayRouter(): RouterInterface;
/**
* Specification:
* - Returns a ChainRouter which is added to the BackendApi Application.
* - Uses RouterExtensionPluginInterfaces to add Router to the ChainRouter.
* - Executes {@link \Spryker\Zed\RouterExtension\Dependency\Plugin\RouterPluginInterface} plugin stack to add Router to the ChainRouter.
*
* @api
*
Expand Down Expand Up @@ -137,26 +133,26 @@ public function warmUpMerchantPortalRouterCache(): void;
public function warmUpBackendGatewayRouterCache(): void;

/**
* Specification:
* - Returns Router which handles Zed routes.
*
* @api
*
* @internal
*
* Specification:
* - Returns Router which handles Zed routes.
*
* @return \Spryker\Zed\Router\Business\Router\RouterInterface
*/
public function getZedRouter(): RouterInterface;

/**
* Specification:
* - Returns a ChainRouter which is added to the Application.
* - Executes {@link \Spryker\Zed\RouterExtension\Dependency\Plugin\RouterPluginInterface} plugin stack to add Router to the ChainRouter.
*
* @api
*
* @internal
*
* Specification:
* - Returns a ChainRouter which is added to the Application.
* - Uses RouterExtensionPluginInterfaces to add Router to the ChainRouter.
*
* @return \Spryker\Zed\Router\Business\Router\ChainRouter
*/
public function getRouter(): ChainRouter;
Expand All @@ -174,11 +170,11 @@ public function getRouter(): ChainRouter;
public function getZedFallbackRouter(): RouterInterface;

/**
* @api
*
* Specification:
* - Builds the cache for the Router.
*
* @api
*
* @return void
*/
public function cacheWarmUp(): void;
Expand Down
20 changes: 20 additions & 0 deletions src/Spryker/Zed/Router/RouterConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class RouterConfig extends AbstractBundleConfig
*/
protected const DIRECTORY_NAME_MERCHANT_PORTAL_APPLICATION = 'MerchantPortalApplication';

/**
* @var bool
*/
protected const IS_ROUTING_CACHE_ENABLED = false;

/**
* Specification:
* - Returns a Router configuration which makes use of a Router cache.
Expand Down Expand Up @@ -308,4 +313,19 @@ public function getNotAllowedBackofficeControllerDirectories(): array
static::DIRECTORY_NAME_MERCHANT_PORTAL_APPLICATION,
];
}

/**
* Specification:
* - Returns whether the Router cache is enabled.
*
* @api
*
* @deprecated Cache will be enabled by default in next major.
*
* @return bool
*/
public function isRoutingCacheEnabled(): bool
{
return static::IS_ROUTING_CACHE_ENABLED;
}
}
Loading

0 comments on commit db71db1

Please sign in to comment.