Skip to content

Commit

Permalink
Use seperate tree cache for each hostname
Browse files Browse the repository at this point in the history
This allows us to populate navigation trees on dedicated preview servers with host names like "preview.webfactory.de" under different visibility rules.
  • Loading branch information
MalteWunsch committed Jan 8, 2024
1 parent 5e0c76d commit b0dd93d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/Build/TreeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\ConfigCacheFactoryInterface;
use Symfony\Component\Config\ConfigCacheInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Stopwatch\StopwatchEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
Expand All @@ -24,6 +25,7 @@ final class TreeFactory implements ServiceSubscriberInterface
/** @var ConfigCacheFactoryInterface */
private $configCacheFactory;

/** @var string */
private $cacheFile;

/** @var LoggerInterface */
Expand All @@ -50,14 +52,15 @@ public static function getSubscribedServices(): array

public function __construct(
ConfigCacheFactoryInterface $configCacheFactory,
$cacheFile,
string $kernelCacheDirectory,
RequestStack $requestStack,
ContainerInterface $container,
EventDispatcherInterface $eventDispatcher = null,
LoggerInterface $logger = null,
Stopwatch $stopwatch = null
) {
$this->configCacheFactory = $configCacheFactory;
$this->cacheFile = $cacheFile;
$this->cacheFile = $this->getCacheFile($kernelCacheDirectory, $requestStack);
$this->container = $container;
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger;
Expand Down Expand Up @@ -125,4 +128,16 @@ public function buildTreeCache(ConfigCacheInterface $cache): void
$cache->write("<?php return unserialize(<<<EOD\n".serialize($this->_tree)."\nEOD\n);",
$dispatcher->getResources());
}

private function getCacheFile(string $kernelCacheDirectory, RequestStack $requestStack): string
{
$cacheDirectory = $kernelCacheDirectory.'/webfactory_navigation_tree/';

$mainRequest = $requestStack->getMainRequest();
if (null !== $mainRequest) {
$cacheDirectory .= $mainRequest->getHttpHost().'/';
}

return $cacheDirectory . 'tree.php';
}
}
3 changes: 2 additions & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

<service id="Webfactory\Bundle\NavigationBundle\Build\TreeFactory" lazy="true">
<argument type="service" id="config_cache_factory" />
<argument>%kernel.cache_dir%/webfactory_navigation/tree.php</argument>
<argument>%kernel.cache_dir%</argument>
<argument type="service" id="Symfony\Component\HttpFoundation\RequestStack"/>
<argument type="service" id="Psr\Container\ContainerInterface"/>
<argument type="service" id="event_dispatcher"/>
<argument type="service" id="logger" on-invalid="null"/>
Expand Down

0 comments on commit b0dd93d

Please sign in to comment.