Skip to content

Fixing Twig extension registering #7

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

Merged
Merged
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
5 changes: 3 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#parameters:
# ignoreErrors:
parameters:
ignoreErrors:
- "#Call to an undefined method Twig_ExtensionInterface#"

includes:
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
28 changes: 20 additions & 8 deletions src/Theme/Extensions/ThemeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,28 @@
class ThemeExtension extends \Twig_Extension
{
/**
* @var string
* A list of theme URLs (the active one is the last one)
*
* @var string[]
*/
private $themeUrl;
private $themeUrls = [];

public function __construct(string $themeUrl)
public function pushThemeUrl(?string $themeUrl): void
{
$this->themeUrls[] = rtrim($themeUrl, '/').'/';
}

$this->themeUrl = rtrim($themeUrl, '/').'/';
public function popThemeUrl(): ?string
{
return array_pop($this->themeUrls);
}

private function getThemeUrl(): ?string
{
return $this->themeUrls[count($this->themeUrls)-1];
}


/**
* Returns a list of functions to add to the existing list.
*
Expand All @@ -25,12 +37,12 @@ public function __construct(string $themeUrl)
public function getFunctions()
{
return [
new \Twig_Function('theme', [$this, 'getThemeUrl']),
new \Twig_Function('theme', [$this, 'getResourceUrl']),
];
}

public function getThemeUrl(string $resource): string
public function getResourceUrl(string $resource): string
{
return $this->themeUrl.$resource;
return $this->getThemeUrl().$resource;
}
}
}
21 changes: 19 additions & 2 deletions src/Theme/TwigTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use TheCodingMachine\CMS\Block\BlockRendererInterface;
use TheCodingMachine\CMS\CMSException;
use TheCodingMachine\CMS\RenderableInterface;
use TheCodingMachine\CMS\Theme\Extensions\ThemeExtension;
use Zend\Diactoros\Stream;

class TwigTheme implements RenderableInterface
Expand All @@ -25,12 +26,17 @@ class TwigTheme implements RenderableInterface
* @var BlockRendererInterface
*/
private $blockRenderer;
/**
* @var string
*/
private $themeUrl;

public function __construct(\Twig_Environment $twig, string $template, BlockRendererInterface $blockRenderer)
public function __construct(\Twig_Environment $twig, string $template, BlockRendererInterface $blockRenderer, string $themeUrl = null)
{
$this->twig = $twig;
$this->template = $template;
$this->blockRenderer = $blockRenderer;
$this->themeUrl = $themeUrl;
}


Expand Down Expand Up @@ -58,7 +64,18 @@ public function render(array $context): StreamInterface
$context['page'] = $page;
}

$text = $this->twig->render($this->template, $context);
if (!$this->twig->hasExtension(ThemeExtension::class)) {
$this->twig->addExtension(new ThemeExtension());
}
$themeExtension = $this->twig->getExtension(ThemeExtension::class);
/* @var $themeExtension ThemeExtension */

$themeExtension->pushThemeUrl($this->themeUrl);
try {
$text = $this->twig->render($this->template, $context);
} finally {
$themeExtension->popThemeUrl();
}

$stream = new Stream('php://temp', 'wb+');
$stream->write($text);
Expand Down
5 changes: 3 additions & 2 deletions src/Theme/TwigThemeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ public function createTheme(ThemeDescriptorInterface $descriptor): RenderableInt
$config = $descriptor->getConfig();
if (isset($config['theme'])) {
$twig = clone $this->twig;
$twig->addExtension(new ThemeExtension($this->themesUrl.ltrim($config['theme'], '/')));
$twig->setLoader(new \Twig_Loader_Filesystem($this->themesPath.ltrim($config['theme'], '/')));
$themeUrl = $this->themesUrl.ltrim($config['theme'], '/');
} else {
$twig = $this->twig;
$themeUrl = null;
}

return new TwigTheme($twig, $descriptor->getTemplate(), $this->blockRenderer);
return new TwigTheme($twig, $descriptor->getTemplate(), $this->blockRenderer, $themeUrl);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Theme/SubThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SubThemeTest extends AbstractThemeTestCase
{
public function testSubTheme()
{
$twigTheme = new TwigTheme($this->createTwigEnvironment(), 'index.html', $this->createBlockRenderer());
$twigTheme = new TwigTheme($this->createTwigEnvironment(), 'index.html', $this->createBlockRenderer(), 'theme');

$subTheme = new SubTheme($twigTheme, ['name' => 'Foo']);

Expand Down
7 changes: 7 additions & 0 deletions tests/Theme/TwigThemeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,12 @@ public function testThemeManagement()
$result = (string) $twigTheme->render([]);

$this->assertSame('/root_url/foo/bar.js', $result);

// Let's do it twice (to check if Twig clone works correctly)
$twigTheme = $twigThemeFactory->createTheme($descriptor);
$result = (string) $twigTheme->render([]);

$this->assertSame('/root_url/foo/bar.js', $result);

}
}
6 changes: 3 additions & 3 deletions tests/Theme/TwigThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TwigThemeTest extends AbstractThemeTestCase
{
public function testTwigTheme()
{
$twigTheme = new TwigTheme($this->createTwigEnvironment(), 'index.html', $this->createBlockRenderer());
$twigTheme = new TwigTheme($this->createTwigEnvironment(), 'index.html', $this->createBlockRenderer(), 'theme_path');

$stream = $twigTheme->render(['name' => 'Foo']);

Expand All @@ -19,7 +19,7 @@ public function testTwigTheme()

public function testSubBlocks()
{
$twigTheme = new TwigTheme($this->createTwigEnvironment(), 'index.html', $this->createBlockRenderer());
$twigTheme = new TwigTheme($this->createTwigEnvironment(), 'index.html', $this->createBlockRenderer(), 'theme_path');

$header = new Block(
new TwigThemeDescriptor('header.html', []),
Expand All @@ -41,7 +41,7 @@ public function testSubBlocks()

public function testException()
{
$twigTheme = new TwigTheme($this->createTwigEnvironment(), 'index.html', $this->createBlockRenderer());
$twigTheme = new TwigTheme($this->createTwigEnvironment(), 'index.html', $this->createBlockRenderer(), 'theme_path');

$header = new Block(
new TwigThemeDescriptor('header.html', []),
Expand Down