Skip to content

Commit

Permalink
Merge pull request #6 from ezsystems/fix/errorLoadingTemplateStandard…
Browse files Browse the repository at this point in the history
…Design

Fix Twig loading exception on first page load
  • Loading branch information
lolautruche authored Jun 22, 2017
2 parents d94ab62 + 4e12fb1 commit 3f43b41
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 23 deletions.
4 changes: 2 additions & 2 deletions bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ parameters:
services:
ezdesign.template_name_resolver:
class: EzSystems\EzPlatformDesignEngine\Templating\ThemeTemplateNameResolver
arguments: ["$design$"]
arguments: ["@ezpublish.config.resolver"]

ezdesign.template_path_registry:
class: EzSystems\EzPlatformDesignEngine\Templating\TemplatePathRegistry
Expand Down Expand Up @@ -53,4 +53,4 @@ services:
- "@ezdesign.asset_path_resolver"
- "@assets._default_package"
calls:
- [setCurrentDesign, ["$design$"]]
- [setConfigResolver, ["@ezpublish.config.resolver"]]
4 changes: 2 additions & 2 deletions lib/Asset/ThemePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public function __construct(AssetPathResolverInterface $pathResolver, PackageInt

public function getUrl($path)
{
return $this->innerPackage->getUrl($this->pathResolver->resolveAssetPath($path, $this->currentDesign));
return $this->innerPackage->getUrl($this->pathResolver->resolveAssetPath($path, $this->getCurrentDesign()));
}

public function getVersion($path)
{
return $this->innerPackage->getVersion($this->pathResolver->resolveAssetPath($path, $this->currentDesign));
return $this->innerPackage->getVersion($this->pathResolver->resolveAssetPath($path, $this->getCurrentDesign()));
}
}
6 changes: 3 additions & 3 deletions lib/DesignAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
interface DesignAwareInterface
{
/**
* Injects current design.
* Returns current design.
*
* @param string $currentDesign
* @return string
*/
public function setCurrentDesign($currentDesign);
public function getCurrentDesign();
}
19 changes: 13 additions & 6 deletions lib/DesignAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@

namespace EzSystems\EzPlatformDesignEngine;

use eZ\Publish\Core\MVC\ConfigResolverInterface;

trait DesignAwareTrait
{
/**
* @var string
* @var ConfigResolverInterface
*/
protected $currentDesign;
private $configResolver;

public function setConfigResolver(ConfigResolverInterface $configResolver)
{
$this->configResolver = $configResolver;
}

/**
* Injects the current design.
* Returns the current design.
*
* @param string $currentDesign
* @return string
*/
public function setCurrentDesign($currentDesign)
public function getCurrentDesign()
{
$this->currentDesign = $currentDesign;
return $this->configResolver->getParameter('design');
}
}
24 changes: 18 additions & 6 deletions lib/Templating/ThemeTemplateNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

namespace EzSystems\EzPlatformDesignEngine\Templating;

use eZ\Publish\Core\MVC\ConfigResolverInterface;

class ThemeTemplateNameResolver implements TemplateNameResolverInterface
{
/**
* @var string Name of the current design, in the current context (e.g. SiteAccess)
* @var ConfigResolverInterface
*/
private $currentDesign;
private $configResolver;

/**
* Collection of already resolved template names.
Expand All @@ -23,9 +25,19 @@ class ThemeTemplateNameResolver implements TemplateNameResolverInterface
*/
private $resolvedTemplateNames = [];

public function __construct($currentDesign)
public function __construct(ConfigResolverInterface $configResolver)
{
$this->configResolver = $configResolver;
}

/**
* Returns the name of the current design, in the current context (i.e. SiteAccess).
*
* @return string
*/
private function getCurrentDesign()
{
$this->currentDesign = $currentDesign;
return $this->configResolver->getParameter('design');
}

public function resolveTemplateName($name)
Expand All @@ -36,11 +48,11 @@ public function resolveTemplateName($name)
return $this->resolvedTemplateNames[$name];
}

return $this->resolvedTemplateNames[$name] = str_replace('@' . self::EZ_DESIGN_NAMESPACE, '@' . $this->currentDesign, $name);
return $this->resolvedTemplateNames[$name] = str_replace('@' . self::EZ_DESIGN_NAMESPACE, '@' . $this->getCurrentDesign(), $name);
}

public function isTemplateDesignNamespaced($name)
{
return (strpos($name, '@' . self::EZ_DESIGN_NAMESPACE) !== false) || (strpos($name, '@' . $this->currentDesign) !== false);
return (strpos($name, '@' . self::EZ_DESIGN_NAMESPACE) !== false) || (strpos($name, '@' . $this->getCurrentDesign()) !== false);
}
}
19 changes: 17 additions & 2 deletions tests/lib/Asset/ThemePackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace EzSystems\EzPlatformDesignEngine\Tests\Asset;

use eZ\Publish\Core\MVC\ConfigResolverInterface;
use EzSystems\EzPlatformDesignEngine\Asset\AssetPathResolverInterface;
use EzSystems\EzPlatformDesignEngine\Asset\ThemePackage;
use PHPUnit\Framework\TestCase;
Expand All @@ -26,12 +27,18 @@ class ThemePackageTest extends TestCase
*/
private $innerPackage;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\MVC\ConfigResolverInterface
*/
private $configResolver;

protected function setUp()
{
parent::setUp();

$this->assetPathResolver = $this->createMock(AssetPathResolverInterface::class);
$this->innerPackage = $this->createMock(PackageInterface::class);
$this->configResolver = $this->createMock(ConfigResolverInterface::class);
}

public function testGetUrl()
Expand All @@ -50,9 +57,13 @@ public function testGetUrl()
->method('getUrl')
->with($fullAssetPath)
->willReturn("/$fullAssetPath");
$this->configResolver
->method('getParameter')
->with('design')
->willReturn($currentDesign);

$package = new ThemePackage($this->assetPathResolver, $this->innerPackage);
$package->setCurrentDesign($currentDesign);
$package->setConfigResolver($this->configResolver);
self::assertSame("/$fullAssetPath", $package->getUrl($assetPath));
}

Expand All @@ -73,9 +84,13 @@ public function testGetVersion()
->method('getVersion')
->with($fullAssetPath)
->willReturn($version);
$this->configResolver
->method('getParameter')
->with('design')
->willReturn($currentDesign);

$package = new ThemePackage($this->assetPathResolver, $this->innerPackage);
$package->setCurrentDesign($currentDesign);
$package->setConfigResolver($this->configResolver);
self::assertSame($version, $package->getVersion($assetPath));
}
}
25 changes: 23 additions & 2 deletions tests/lib/Templating/ThemeTemplateNameResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,24 @@

namespace EzSystems\EzPlatformDesignEngine\Tests\Templating;

use eZ\Publish\Core\MVC\ConfigResolverInterface;
use EzSystems\EzPlatformDesignEngine\Templating\ThemeTemplateNameResolver;
use PHPUnit\Framework\TestCase;

class ThemeTemplateNameResolverTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject|ConfigResolverInterface
*/
private $configResolver;

public function setUp()
{
parent::setUp();

$this->configResolver = $this->createMock(ConfigResolverInterface::class);
}

public function templateNameProvider()
{
return [
Expand All @@ -28,7 +41,11 @@ public function templateNameProvider()
*/
public function testResolveTemplateName($currentDesign, $templateName, $expectedTemplateName)
{
$resolver = new ThemeTemplateNameResolver($currentDesign);
$this->configResolver
->method('getParameter')
->with('design')
->willReturn($currentDesign);
$resolver = new ThemeTemplateNameResolver($this->configResolver);
self::assertSame($expectedTemplateName, $resolver->resolveTemplateName($templateName));
}

Expand All @@ -47,7 +64,11 @@ public function isTemplateDesignNamespacedProvider()
*/
public function testIsTemplateDesignNamespaced($currentDesign, $templateName, $expected)
{
$resolver = new ThemeTemplateNameResolver($currentDesign);
$this->configResolver
->method('getParameter')
->with('design')
->willReturn($currentDesign);
$resolver = new ThemeTemplateNameResolver($this->configResolver);
self::assertSame($expected, $resolver->isTemplateDesignNamespaced($templateName));
}
}

0 comments on commit 3f43b41

Please sign in to comment.