protected function createAndCache($what) { if (null !== $this->{$what}) { return $this->{$what}; } $cacheClass = $this->options["{$what}_cache_class"]; if (null === $this->options['cache_dir'] || null === $cacheClass) { return $this->{$what} = new $this->options["{$what}_class"]($this->getRouteCollection(), $this->context); } $cache = new ExpirableConfigCache($this->options['cache_dir'] . '/' . $cacheClass . '.php', $this->options['debug'], $this->metaQuery->getLastTouched()); if (!$cache->isFresh()) { $cs = new CriticalSection(); $cs->setLogger($this->logger); $self = $this; $cs->execute(__FILE__, function () use($what, $self, $cache) { if (!$cache->isFresh()) { $self->dumpIntoCache($cache, $what); } else { $self->debug("Cache has been refreshed while we were waiting."); } }); } if (!class_exists($cacheClass)) { require $cache->getPath(); } return $this->{$what} = new $cacheClass($this->context); }
public function getTree() { if (!$this->_tree) { $cache = new ExpirableConfigCache($this->cacheFile, $this->debug, $this->metaQuery->getLastTouched()); $_watch = $this->startTiming('Checking whether the cache is fresh'); $fresh = $cache->isFresh(); $this->stopTiming($_watch); if (!$fresh) { $cs = new \Webfactory\Bundle\WfdMetaBundle\Util\CriticalSection(); $cs->setLogger($this->logger); $_watch = $this->startTiming('Critical section'); $self = $this; $cs->execute(__FILE__, function () use($self, $cache) { if (!$cache->isFresh()) { $self->debug("Building the tree"); $self->buildTreeCache($cache); $self->debug("Finished building the tree"); } else { $self->debug("Had to wait for the cache to be initialized by another process"); } }); $this->stopTiming($_watch); } if (!$this->_tree) { $this->debug("Loading the cached tree"); $_watch = $this->startTiming('Loading a cached tree'); $this->_tree = (require $cache); $this->stopTiming($_watch); $this->debug("Finished loading the cached tree"); } if ($this->eventDispatcher) { $this->eventDispatcher->dispatch('webfactory_navigation.tree_initialized', new TreeInitializedEvent($this->_tree)); } } return $this->_tree; }