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

Seperate navigation tree cache for each host (Case 166581) #37

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/Build/TreeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,39 @@
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;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Webfactory\Bundle\NavigationBundle\Event\TreeInitializedEvent;
use Webfactory\Bundle\NavigationBundle\Tree\Tree;

/**
* @final (not actually declared final to allow it being used as a lazy service)
*/
class TreeFactory implements ServiceSubscriberInterface
{
/** @var ConfigCacheFactoryInterface */
private $configCacheFactory;

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

/** @var LoggerInterface */
protected $logger;
private $logger;

/** @var Tree */
protected $_tree;
private $_tree;

/** @var Stopwatch */
protected $stopwatch;
private $stopwatch;

/** @var EventDispatcherInterface */
protected $eventDispatcher;
private $eventDispatcher;

/** @var ContainerInterface */
protected $container;
private $container;

public static function getSubscribedServices(): array
{
Expand All @@ -50,14 +55,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 All @@ -71,7 +77,7 @@ public function debug(string $msg): void
}
}

protected function startTiming(string $sectionName): ?StopwatchEvent
private function startTiming(string $sectionName): ?StopwatchEvent
{
if ($this->stopwatch) {
return $this->stopwatch->start('webfactory/navigation-bundle: '.$sectionName);
Expand All @@ -80,7 +86,7 @@ protected function startTiming(string $sectionName): ?StopwatchEvent
return null;
}

protected function stopTiming(StopwatchEvent $watch = null): void
private function stopTiming(StopwatchEvent $watch = null): void
{
if ($watch) {
$watch->stop();
Expand Down Expand Up @@ -125,4 +131,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