Skip to content

Commit

Permalink
Merge 5903e16 into e2f557a
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed Apr 27, 2020
2 parents e2f557a + 5903e16 commit a6462c1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 29 deletions.
62 changes: 49 additions & 13 deletions src/Twig/FileLoader.php
Expand Up @@ -18,8 +18,8 @@

namespace Cake\TwigView\Twig;

use Cake\TwigView\View\TwigView;
use Cake\View\Exception\MissingTemplateException;
use Cake\Core\App;
use Cake\Core\Plugin;
use Twig\Error\LoaderError;
use Twig\Loader\LoaderInterface;
use Twig\Source;
Expand All @@ -32,16 +32,16 @@
class FileLoader implements LoaderInterface
{
/**
* @var \Cake\TwigView\View\TwigView
* @var string[]
*/
protected $twigView;
protected $extensions;

/**
* @param \Cake\TwigView\View\TwigView $twigView TwigView instance.
* @param string[] $extensions Template file extensions
*/
public function __construct(TwigView $twigView)
public function __construct(array $extensions)
{
$this->twigView = $twigView;
$this->extensions = $extensions;
}

/**
Expand Down Expand Up @@ -79,7 +79,7 @@ public function exists(string $name)
{
try {
$this->findTemplate($name);
} catch (MissingTemplateException $e) {
} catch (LoaderError $e) {
return false;
}

Expand All @@ -96,12 +96,48 @@ public function findTemplate(string $name): string
return $name;
}

try {
$path = $this->twigView->resolveTemplatePath($name);
} catch (MissingTemplateException $e) {
throw new LoaderError($e->getMessage());
[$plugin, $name] = pluginSplit($name);
$name = str_replace('/', DIRECTORY_SEPARATOR, $name);

if ($plugin !== null) {
$path = $this->checkExtensions(Plugin::templatePath($plugin) . $name);
if ($path !== null) {
return $path;
}

throw new LoaderError("Could not find template `{$name}` in plugin `{$plugin}`.");
}

foreach (App::path('templates') as $templatePath) {
$path = $this->checkExtensions($templatePath . $name);
if ($path !== null) {
return $path;
}
}

$error = "Could not find template `{$name}`.\nThese paths were searched:\n\n";
foreach (App::path('templates') as $templatePath) {
$error .= "- `{$templatePath}`\n";
}
throw new LoaderError($error);
}

/**
* Check partial path with all template file extensions to see
* which file exists.
*
* @param string $partial Template path excluding extension
* @return string|null
*/
public function checkExtensions(string $partial): ?string
{
foreach ($this->extensions as $extension) {
$path = $partial . $extension;
if (file_exists($path)) {
return $path;
}
}

return $path;
return null;
}
}
14 changes: 1 addition & 13 deletions src/View/TwigView.php
Expand Up @@ -150,26 +150,14 @@ public function getExtensions(): array
return $this->extensions;
}

/**
* Gets the full path for template.
*
* @param string $name Template name
* @return string
* @throws \Cake\View\Exception\MissingTemplateException When template is not found
*/
public function resolveTemplatePath(string $name): string
{
return $this->_getTemplateFileName($name);
}

/**
* Creates the Twig LoaderInterface instance.
*
* @return \Twig\Loader\LoaderInterface
*/
protected function createLoader(): LoaderInterface
{
return new FileLoader($this);
return new FileLoader($this->extensions);
}

/**
Expand Down
10 changes: 7 additions & 3 deletions tests/TestCase/Twig/FileLoaderTest.php
Expand Up @@ -20,7 +20,6 @@

use Cake\TestSuite\TestCase;
use Cake\TwigView\Twig\FileLoader;
use Cake\TwigView\View\TwigView;
use Twig\Error\LoaderError;

/**
Expand All @@ -39,7 +38,7 @@ public function setUp(): void

$this->loadPlugins(['TestTwigView']);

$this->loader = new FileLoader(new TwigView());
$this->loader = new FileLoader(['.twig']);
}

public function tearDown(): void
Expand Down Expand Up @@ -93,7 +92,12 @@ public function testIsFresh()
public function testIsFreshNonExistingFile()
{
$this->expectException(LoaderError::class);

$this->loader->isFresh(TMP . 'foobar' . time(), time());
}

public function testExistsNonExistingFile()
{
$exists = $this->loader->exists(TMP . 'foobar' . time(), time());
$this->assertSame(false, $exists);
}
}
12 changes: 12 additions & 0 deletions tests/TestCase/View/TwigViewTest.php
Expand Up @@ -169,6 +169,18 @@ public function testTwigInclude()
$this->removePlugins(['TestTwigView']);
}

/**
* Tests extends loads templates from root templates paths.
*
* @return void
*/
public function testTwigExtendsRootPath()
{
$view = new AppView(null, null, null, ['templatePath' => 'Blog']);
$output = $view->render('blog_with_extends');
$this->assertSame('base from subdir/base', $output);
}

/**
* Tests deprecated element and cell tags render.
*
Expand Down
1 change: 1 addition & 0 deletions tests/test_app/templates/Blog/blog_with_extends.twig
@@ -0,0 +1 @@
{% extends 'subdir_with_base/base' %}
1 change: 1 addition & 0 deletions tests/test_app/templates/subdir_with_base/base.twig
@@ -0,0 +1 @@
base from subdir/base

0 comments on commit a6462c1

Please sign in to comment.