Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Various bug fixes related to template caching
  • Loading branch information
ehough committed Jul 7, 2015
1 parent 47e2159 commit 34335de
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 47 deletions.
Expand Up @@ -42,7 +42,7 @@ class tubepress_app_template_impl_ThemeTemplateLocator
/**
* @var array
*/
private $_templateNameToThemeInstanceCache = array();
private $_currentThemeNameToTemplateNameToThemeInstanceCache = array();

public function __construct(tubepress_platform_api_log_LoggerInterface $logger,
tubepress_app_api_options_ContextInterface $context,
Expand Down Expand Up @@ -140,66 +140,79 @@ public function getCacheKey($name)
*/
private function _findThemeForTemplate($templateName)
{
if (isset($this->_templateNameToThemeInstanceCache[$templateName])) {
$activeTheme = $this->_currentThemeService->getCurrentTheme();
$activeThemeName = $activeTheme->getName();

$cachedValue = $this->_templateNameToThemeInstanceCache[$templateName];

if ($this->_shouldLog) {
if (strpos($templateName, '::') !== false) {

if ($cachedValue) {
$exploded = explode('::', $templateName);

$this->_logger->debug(sprintf('Theme for template <code>%s</code> was found in the cache', $templateName));
if (count($exploded) === 2 && $this->_themeRegistry->getInstanceByName($exploded[0]) !== null) {

}
$activeTheme = $this->_themeRegistry->getInstanceByName($exploded[0]);
$activeThemeName = $activeTheme->getName();
$templateName = $exploded[1];
}

return $cachedValue ? $cachedValue : null;
}

if ($this->_shouldLog) {

$this->_logger->debug(sprintf('Seeing if able to find source of template <code>%s</code> from theme hierarchy', $templateName));
$this->_logger->debug(sprintf('Seeing if we can find <code>%s</code> in the theme hierarchy. %s.',
$templateName, $this->_loggerPostfix($activeTheme)));
}

$currentTheme = null;
if (isset($this->_currentThemeNameToTemplateNameToThemeInstanceCache[$activeThemeName]) &&
isset($this->_currentThemeNameToTemplateNameToThemeInstanceCache[$activeThemeName][$templateName])) {

if (strpos($templateName, '::') !== false) {
$cachedValue = $this->_currentThemeNameToTemplateNameToThemeInstanceCache[$activeThemeName][$templateName];

$exploded = explode('::', $templateName);
if ($this->_shouldLog) {

if (count($exploded) === 2 && $this->_themeRegistry->getInstanceByName($exploded[0]) !== null) {
if ($cachedValue) {

$this->_logger->debug(sprintf('Theme for template <code>%s</code> was found in the cache to be contained in theme <code>%s</code> version <code>%s</code>. %s.',
$templateName, $cachedValue->getName(), $cachedValue->getVersion(), $this->_loggerPostfix($activeTheme)));

$currentTheme = $this->_themeRegistry->getInstanceByName($exploded[0]);
$templateName = $exploded[1];
} else {

$this->_logger->debug(sprintf('We already tried to find a theme that contains <code>%s</code> in the theme hierarchy but didn\'t find it anywhere. %s.',
$templateName, $this->_loggerPostfix($activeTheme)));
}
}
}

if (!$currentTheme) {
return $cachedValue ? $cachedValue : null;

$currentTheme = $this->_currentThemeService->getCurrentTheme();
}
} else {

if ($this->_shouldLog) {
if ($this->_shouldLog) {

$this->_logger->debug(sprintf('Current theme is <code>%s</code> version <code>%s</code>', $currentTheme->getName(), $currentTheme->getVersion()));
$this->_logger->debug(sprintf('Looks like this is the first time searching for a theme that contains <code>%s</code>. %s.', $templateName, $this->_loggerPostfix($activeTheme)));
}
}

do {

if ($currentTheme->hasTemplateSource($templateName)) {
$activeThemeName = $activeTheme->getName();

if ($activeTheme->hasTemplateSource($templateName)) {

if (!isset($this->_currentThemeNameToTemplateNameToThemeInstanceCache[$activeThemeName])) {

$this->_templateNameToThemeInstanceCache[$templateName] = $currentTheme;
$this->_currentThemeNameToTemplateNameToThemeInstanceCache[$activeThemeName] = array();
}

$this->_currentThemeNameToTemplateNameToThemeInstanceCache[$activeThemeName][$templateName] = $activeTheme;

if ($this->_shouldLog) {

$this->_logger->debug(sprintf('Template source for <code>%s</code> was found in theme <code>%s</code>',
$templateName, $currentTheme->getName()));
$this->_logger->debug(sprintf('Template source for <code>%s</code> was found in theme <code>%s</code> version <code>%s</code>. %s.',
$templateName, $activeThemeName, $activeTheme->getVersion(), $this->_loggerPostfix($activeTheme)));
}

return $currentTheme;
return $activeTheme;
}

$nextThemeNameToCheck = $currentTheme->getParentThemeName();
$nextThemeNameToCheck = $activeTheme->getParentThemeName();

if ($nextThemeNameToCheck === null) {

Expand All @@ -209,12 +222,12 @@ private function _findThemeForTemplate($templateName)
if ($this->_shouldLog) {

$this->_logger->debug(sprintf('Template source for <code>%s</code> was not found in theme <code>%s</code>. Now trying its parent theme: <code>%s</code>.',
$templateName, $currentTheme->getName(), $nextThemeNameToCheck));
$templateName, $activeTheme->getName(), $nextThemeNameToCheck));
}

try {

$currentTheme = $this->_themeRegistry->getInstanceByName($nextThemeNameToCheck);
$activeTheme = $this->_themeRegistry->getInstanceByName($nextThemeNameToCheck);

} catch (InvalidArgumentException $e) {

Expand All @@ -226,15 +239,25 @@ private function _findThemeForTemplate($templateName)
break;
}

} while ($currentTheme !== null);
} while ($activeTheme !== null);

if (!isset($this->_currentThemeNameToTemplateNameToThemeInstanceCache[$activeThemeName])) {

$this->_currentThemeNameToTemplateNameToThemeInstanceCache[$activeThemeName] = array();
}

$this->_templateNameToThemeInstanceCache[$templateName] = false;
$this->_currentThemeNameToTemplateNameToThemeInstanceCache[$activeThemeName][$templateName] = false;

if ($this->_shouldLog) {

$this->_logger->debug(sprintf('Unable to find source of template <code>%s</code> from theme hierarchy. Falling back to registered path providers.', $templateName));
$this->_logger->debug(sprintf('Unable to find source of template <code>%s</code> from theme hierarchy.', $templateName));
}

return null;
}

private function _loggerPostfix(tubepress_app_api_theme_ThemeInterface $theme)
{
return sprintf('Theme <code>%s</code> version <code>%s</code>', $theme->getName(), $theme->getVersion());
}
}
@@ -0,0 +1,35 @@
<?php
/**
* Copyright 2006 - 2015 TubePress LLC (http://tubepress.com)
*
* This file is part of TubePress (http://tubepress.com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

class tubepress_app_template_impl_php_PhpEngine extends ehough_templating_PhpEngine
{
/**
* Loads the given template.
*
* @param string|ehough_templating_TemplateReferenceInterface $name A template name or a ehough_templating_TemplateReferenceInterface instance
*
* @return ehough_templating_storage_Storage A Storage instance
*
* @throws InvalidArgumentException if the template cannot be found
*/
protected function load($name)
{
$template = $this->parser->parse($name);
$storage = $this->loader->load($template);

if (false === $storage) {

throw new InvalidArgumentException(sprintf('The template "%s" does not exist.', $template));
}

return $storage;
}
}
Expand Up @@ -194,16 +194,16 @@ private function _registerTemplatingService(tubepress_platform_api_ioc_Container
* Register the PHP templating engine
*/
$containerBuilder->register(
'ehough_templating_PhpEngine',
'ehough_templating_PhpEngine'
'tubepress_app_template_impl_php_PhpEngine',
'tubepress_app_template_impl_php_PhpEngine'
)->addArgument(new tubepress_platform_api_ioc_Reference('tubepress_app_template_impl_php_Support'))
->addArgument(new tubepress_platform_api_ioc_Reference('tubepress_app_template_impl_php_Support'));

/**
* Public templating engine
*/
$engineReferences = array(
new tubepress_platform_api_ioc_Reference('ehough_templating_PhpEngine'),
new tubepress_platform_api_ioc_Reference('tubepress_app_template_impl_php_PhpEngine'),
new tubepress_platform_api_ioc_Reference('tubepress_app_template_impl_twig_Engine')
);
$containerBuilder->register(
Expand Down
1 change: 1 addition & 0 deletions src/add-ons/template/manifest.json
Expand Up @@ -26,6 +26,7 @@
"tubepress_app_template_ioc_TemplateExtension" : "classes/tubepress/app/template/ioc/TemplateExtension.php",
"tubepress_app_template_impl_options_ui_FieldProvider" : "classes/tubepress/app/template/impl/options/ui/FieldProvider.php",
"tubepress_app_template_impl_DelegatingEngine" : "classes/tubepress/app/template/impl/DelegatingEngine.php",
"tubepress_app_template_impl_php_PhpEngine" : "classes/tubepress/app/template/impl/php/PhpEngine.php",
"tubepress_app_template_impl_php_Support" : "classes/tubepress/app/template/impl/php/Support.php",
"tubepress_app_template_impl_TemplatingService" : "classes/tubepress/app/template/impl/TemplatingService.php",
"tubepress_app_template_impl_ThemeTemplateLocator" : "classes/tubepress/app/template/impl/ThemeTemplateLocator.php",
Expand Down
1 change: 1 addition & 0 deletions src/platform/scripts/classloading/classmap.php
Expand Up @@ -571,6 +571,7 @@
'tubepress_app_template_impl_TemplatingService' => TUBEPRESS_ROOT . '/src/add-ons/template/classes/tubepress/app/template/impl/TemplatingService.php',
'tubepress_app_template_impl_ThemeTemplateLocator' => TUBEPRESS_ROOT . '/src/add-ons/template/classes/tubepress/app/template/impl/ThemeTemplateLocator.php',
'tubepress_app_template_impl_options_ui_FieldProvider' => TUBEPRESS_ROOT . '/src/add-ons/template/classes/tubepress/app/template/impl/options/ui/FieldProvider.php',
'tubepress_app_template_impl_php_PhpEngine' => TUBEPRESS_ROOT . '/src/add-ons/template/classes/tubepress/app/template/impl/php/PhpEngine.php',
'tubepress_app_template_impl_php_Support' => TUBEPRESS_ROOT . '/src/add-ons/template/classes/tubepress/app/template/impl/php/Support.php',
'tubepress_app_template_impl_twig_Engine' => TUBEPRESS_ROOT . '/src/add-ons/template/classes/tubepress/app/template/impl/twig/Engine.php',
'tubepress_app_template_impl_twig_EnvironmentBuilder' => TUBEPRESS_ROOT . '/src/add-ons/template/classes/tubepress/app/template/impl/twig/EnvironmentBuilder.php',
Expand Down
Expand Up @@ -150,19 +150,22 @@ public function testGetTemplateSource($exists)
*/
public function testExistInParent($exists)
{
$this->_mockCurrentThemeService->shouldReceive('getCurrentTheme')->once()->andReturn($this->_mockChildTheme);
$this->_mockChildTheme->shouldReceive('hasTemplateSource')->once()->with('template-name')->andReturn(false);
$this->_mockChildTheme->shouldReceive('getParentThemeName')->once()->andReturn('xyz');
$this->_mockChildTheme->shouldReceive('getName')->twice()->andReturn('abc');
$this->_mockCurrentThemeService->shouldReceive('getCurrentTheme')->twice()->andReturn($this->_mockChildTheme);
$this->_mockChildTheme->shouldReceive('hasTemplateSource')->twice()->with('template-name')->andReturn(false);
$this->_mockChildTheme->shouldReceive('getParentThemeName')->twice()->andReturn('xyz');
$this->_mockChildTheme->shouldReceive('getName')->atLeast(1)->andReturn('abc');
$this->_mockChildTheme->shouldReceive('getVersion')->twice()->andReturn('1.2.3');
$this->_mockThemeRegistry->shouldReceive('getInstanceByName')->with('xyz')->andReturn($this->_mockParentTheme);
$this->_mockParentTheme->shouldReceive('hasTemplateSource')->once()->with('template-name')->andReturn($exists);
$this->_mockParentTheme->shouldReceive('hasTemplateSource')->twice()->with('template-name')->andReturn($exists);
$this->_mockChildTheme->shouldReceive('getVersion')->atLeast(1)->andReturn(tubepress_platform_api_version_Version::parse('1.2.3'));

if (!$exists) {

$this->_mockParentTheme->shouldReceive('getParentThemeName')->once()->andReturnNull();
$this->_mockParentTheme->shouldReceive('getParentThemeName')->twice()->andReturnNull();
$this->_mockParentTheme->shouldReceive('getName')->twice()->andReturn('parent-theme-name');
} else {
$this->_mockParentTheme->shouldReceive('getName')->times(1)->andReturn('xyz');
$this->_mockParentTheme->shouldReceive('getName')->atLeast(1)->andReturn('xyz');
$this->_mockParentTheme->shouldReceive('getVersion')->atLeast(1)->andReturn('3.2.1');
}

$this->assertTrue($this->_sut->exists('template-name') === $exists);
Expand Down
Expand Up @@ -187,16 +187,16 @@ private function _expectTemplateService()
* Register the PHP templating engine
*/
$this->expectRegistration(
'ehough_templating_PhpEngine',
'ehough_templating_PhpEngine'
'tubepress_app_template_impl_php_PhpEngine',
'tubepress_app_template_impl_php_PhpEngine'
)->withArgument(new tubepress_platform_api_ioc_Reference('tubepress_app_template_impl_php_Support'))
->withArgument(new tubepress_platform_api_ioc_Reference('tubepress_app_template_impl_php_Support'));

/**
* Public templating engine
*/
$engineReferences = array(
new tubepress_platform_api_ioc_Reference('ehough_templating_PhpEngine'),
new tubepress_platform_api_ioc_Reference('tubepress_app_template_impl_php_PhpEngine'),
new tubepress_platform_api_ioc_Reference('tubepress_app_template_impl_twig_Engine')
);
$this->expectRegistration(
Expand Down

0 comments on commit 34335de

Please sign in to comment.