Skip to content

Commit

Permalink
Merge pull request #153 from mikoweb/fix/twig_bundle
Browse files Browse the repository at this point in the history
TwigBundle fix. Incorrect paths for views @Twig/Exception/%s.%s.twig.
  • Loading branch information
lsmith77 committed Oct 5, 2016
2 parents ce79859 + be7f2bd commit b1797a8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Locator/FileLocator.php
Expand Up @@ -134,6 +134,7 @@ public function locate($name, $dir = null, $first = true)
if ('@' === $name[0]) {
return $this->locateBundleResource($name, $this->path, $first);
}

if (0 === strpos($name, 'views/')) {
if ($res = $this->locateAppResource($name, $this->path, $first)) {
return $res;
Expand Down Expand Up @@ -169,6 +170,13 @@ protected function locateBundleResource($name, $dir = null, $first = true)
list($bundleName, $path) = explode('/', $bundleName, 2);
}

if (!preg_match('/(Bundle)$/i', $bundleName)) {
$bundleName .= 'Bundle';
if (0 !== strpos($path, 'Resources')) {
$path = 'Resources/views/'.$path;
}
}

if (0 !== strpos($path, 'Resources')) {
throw new \RuntimeException('Template files have to be in Resources.');
}
Expand Down
23 changes: 23 additions & 0 deletions Tests/Locator/FileLocatorTest.php
Expand Up @@ -155,6 +155,7 @@ public function testLocate()

$file = $fileLocator->locate('@ThemeBundle/Resources/views/template', $this->getFixturePath(), true);
$this->assertEquals($this->getFixturePath().'/Resources/themes/foo/template', $file);
$this->assertResourcesEquals($fileLocator, '@ThemeBundle/Resources/views/template', '@Theme/template');
}

/**
Expand All @@ -176,6 +177,7 @@ public function testLocateWithOverriddenPathPattern()

$file = $fileLocator->locate('@ThemeBundle/Resources/views/template', $this->getFixturePath(), true);
$this->assertEquals($this->getFixturePath().'/Resources/views/themes/foo/template', $file);
$this->assertResourcesEquals($fileLocator, '@ThemeBundle/Resources/views/template', '@Theme/template');

// Fall through user-configured cascade order - /Resources/views/themes/bar will not be found.
$activeTheme = new ActiveTheme('bar', array('foo', 'bar', 'foobar'));
Expand All @@ -184,6 +186,7 @@ public function testLocateWithOverriddenPathPattern()

$file = $fileLocator->locate('@ThemeBundle/Resources/views/template', $this->getFixturePath(), true);
$this->assertEquals($this->getFixturePath().'/Resources/views/themes/fallback/template', $file);
$this->assertResourcesEquals($fileLocator, '@ThemeBundle/Resources/views/template', '@Theme/template');
}

/**
Expand All @@ -198,6 +201,7 @@ public function testLocateAppThemeOverridesAll()

$file = $fileLocator->locate('@ThemeBundle/Resources/views/foo', $this->getFixturePath(), true);
$this->assertEquals($this->getFixturePath().'/rootdir/Resources/themes/foo/LiipMockLocateAppThemeOverridesAll/foo', $file);
$this->assertResourcesEquals($fileLocator, '@ThemeBundle/Resources/views/foo', '@Theme/foo');
}

/**
Expand Down Expand Up @@ -239,6 +243,7 @@ public function testLocateViewWithMobileDevice()

$file = $fileLocator->locate('@ThemeBundle/Resources/views/defaultTemplate', $this->getFixturePath(), true);
$this->assertEquals($this->getFixturePath().'/Resources/views/phone/defaultTemplate', $file);
$this->assertResourcesEquals($fileLocator, '@ThemeBundle/Resources/views/defaultTemplate', '@Theme/defaultTemplate');
}

/**
Expand Down Expand Up @@ -285,6 +290,7 @@ public function testLocateViewFallback()

$file = $fileLocator->locate('@ThemeBundle/Resources/views/defaultTemplate', $this->getFixturePath(), true);
$this->assertEquals($this->getFixturePath().'/Resources/views/defaultTemplate', $file);
$this->assertResourcesEquals($fileLocator, '@ThemeBundle/Resources/views/defaultTemplate', '@Theme/defaultTemplate');
}

/**
Expand All @@ -304,6 +310,7 @@ public function testLocateAllFiles()

$files = $fileLocator->locate('@ThemeBundle/Resources/views/template', $this->getFixturePath(), false);
$this->assertEquals($expectedFiles, $files);
$this->assertResourcesEquals($fileLocator, '@ThemeBundle/Resources/views/template', '@Theme/template');
}

/**
Expand Down Expand Up @@ -350,6 +357,7 @@ public function testLocateRootDirectory()

$file = $fileLocator->locate('@ThemeBundle/Resources/views/rootTemplate', $this->getFixturePath(), true);
$this->assertEquals($this->getFixturePath().'/rootdir/Resources/themes/foo/LiipMockLocateRootDirectory/rootTemplate', $file);
$this->assertResourcesEquals($fileLocator, '@ThemeBundle/Resources/views/rootTemplate', '@Theme/rootTemplate');
}

/**
Expand All @@ -364,6 +372,7 @@ public function testLocateOverrideDirectory()

$file = $fileLocator->locate('@ThemeBundle/Resources/views/override', $this->getFixturePath(), true);
$this->assertEquals($this->getFixturePath().'/rootdir/Resources/LiipMockLocateOverrideDirectory/views/override', $file);
$this->assertResourcesEquals($fileLocator, '@ThemeBundle/Resources/views/override', '@Theme/override');
}

/**
Expand Down Expand Up @@ -440,4 +449,18 @@ public function testLocateBundleInheritance()

$file = $fileLocator->locate('@ThemeBundle/Resources/nonExistant', $this->getFixturePath(), true);
}

/**
* Asserts that two resources are equal paths.
*
* @param FileLocator $fileLocator
* @param string $expected
* @param string $actual
* @param string $message
*/
private function assertResourcesEquals(FileLocator $fileLocator, $expected, $actual, $message = '')
{
$this->assertEquals($fileLocator->locate($expected, $this->getFixturePath(), true),
$fileLocator->locate($actual, $this->getFixturePath(), true), $message);
}
}
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -26,6 +26,7 @@
"phpunit/php-code-coverage": "~2.2@stable",
"symfony/console": "~2.3|~3.0",
"symfony/expression-language": "~2.6|~3.0",
"symfony/templating": "~2.3|~3.0",
"kriswallsmith/assetic": "~1.1",
"twig/twig": "~1.4|~2.0@dev"
},
Expand Down

0 comments on commit b1797a8

Please sign in to comment.