Skip to content

Commit

Permalink
Merge branch '1.3'
Browse files Browse the repository at this point in the history
* 1.3:
  Replace regexp with plain string operation
  Replace regexp with plain string operation
  Fix plugin templates resolving on 1.2
  Allow overriding templates from plugins
  • Loading branch information
pamil committed Jan 10, 2019
2 parents 2e28729 + 995db97 commit d0e9edd
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/Sylius/Bundle/ThemeBundle/Locator/BundleResourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,23 @@ private function locateResourceBasedOnBundleNotation(string $resourcePath, Theme
private function locateResourceBasedOnTwigNamespace(string $resourcePath, ThemeInterface $theme): string
{
$twigNamespace = substr($resourcePath, 1, strpos($resourcePath, '/') - 1);
$bundleName = $twigNamespace . 'Bundle';
$resourceName = substr($resourcePath, strpos($resourcePath, '/') + 1);

$path = sprintf('%s/%s/views/%s', $theme->getPath(), $bundleName, $resourceName);
$path = sprintf('%s/%s/views/%s', $theme->getPath(), $this->getBundleOrPluginName($twigNamespace), $resourceName);

if ($this->filesystem->exists($path)) {
return $path;
}

throw new ResourceNotFoundException($resourcePath, $theme);
}

private function getBundleOrPluginName(string $twigNamespace): string
{
if (substr($twigNamespace, -6) === 'Plugin') {
return $twigNamespace;
}

return $twigNamespace . 'Bundle';
}
}
11 changes: 10 additions & 1 deletion src/Sylius/Bundle/ThemeBundle/Templating/TemplateNameParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function parse($name): TemplateReferenceInterface
}

$template = new TemplateReference(
$matches[1] ? $matches[1] . 'Bundle' : '',
$this->getBundleOrPluginName($matches[1]),
$matches[2],
$matches[3],
$matches[4],
Expand All @@ -74,4 +74,13 @@ public function parse($name): TemplateReferenceInterface

return $this->cache[$name] = $template;
}

private function getBundleOrPluginName(string $name): string
{
if (substr($name, -6) === 'Plugin') {
return $name;
}

return $name ? $name . 'Bundle' : '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@TestPlugin/Templating/twigNamespacedBothThemesTemplate.txt.twig|sylius/first-test-theme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@TestPlugin/Templating/twigNamespacedVanillaOverriddenThemeTemplate.txt.twig|sylius/first-test-theme
23 changes: 23 additions & 0 deletions src/Sylius/Bundle/ThemeBundle/Tests/Functional/TemplatingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ public function getBundleTemplatesUsingNamespacedPaths()
];
}

/**
* @test
* @dataProvider getPluginTemplatesUsingNamespacedPaths
*/
public function it_renders_sylius_plugin_templates_using_namespaced_paths(string $templateName, string $contents): void
{
$client = self::createClient();

$crawler = $client->request('GET', '/template/' . $templateName);
$this->assertEquals($contents, trim($crawler->text()));
}

/**
* @return array
*/
public function getPluginTemplatesUsingNamespacedPaths(): array
{
return [
['@TestPlugin/Templating/twigNamespacedVanillaOverriddenThemeTemplate.txt.twig', '@TestPlugin/Templating/twigNamespacedVanillaOverriddenThemeTemplate.txt.twig|sylius/first-test-theme'],
['@TestPlugin/Templating/twigNamespacedBothThemesTemplate.txt.twig', '@TestPlugin/Templating/twigNamespacedBothThemesTemplate.txt.twig|sylius/first-test-theme'],
];
}

/**
* @test
* @dataProvider getAppTemplates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ function it_locates_bundle_resource_using_path_derived_from_twig_namespaces(
$this->locateResource('@Just/Directory/index.html.twig', $theme)->shouldReturn('/theme/path/JustBundle/views/Directory/index.html.twig');
}

function it_locates_plugin_resource_using_path_derived_from_twig_namespaces(
Filesystem $filesystem,
ThemeInterface $theme
): void {
$theme->getPath()->willReturn('/theme/path');

$filesystem->exists('/theme/path/JustPlugin/views/Directory/index.html.twig')->shouldBeCalled()->willReturn(true);

$this->locateResource('@JustPlugin/Directory/index.html.twig', $theme)->shouldReturn('/theme/path/JustPlugin/views/Directory/index.html.twig');
}

function it_throws_an_exception_if_resource_can_not_be_located_using_path_derived_from_twig_namespaces(
Filesystem $filesystem,
ThemeInterface $theme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ function it_generates_template_references_from_namespaced_paths(KernelInterface
$this->parse('@Acme/Nested/Directory/app.html.twig')->shouldBeLike(new TemplateReference('AcmeBundle', 'Nested/Directory', 'app', 'html', 'twig'));
}

function it_generates_plugin_template_references_from_namespaced_paths(KernelInterface $kernel): void
{
$kernel->getBundle('AcmePlugin')->willReturn(null); // just do not throw an exception

$this->parse('@AcmePlugin/app.html.twig')->shouldBeLike(new TemplateReference('AcmePlugin', '', 'app', 'html', 'twig'));
$this->parse('@AcmePlugin/Directory/app.html.twig')->shouldBeLike(new TemplateReference('AcmePlugin', 'Directory', 'app', 'html', 'twig'));
$this->parse('@AcmePlugin/Directory.WithDot/app.html.twig')->shouldBeLike(new TemplateReference('AcmePlugin', 'Directory.WithDot', 'app', 'html', 'twig'));
$this->parse('@AcmePlugin/Directory/app.with.dots.html.twig')->shouldBeLike(new TemplateReference('AcmePlugin', 'Directory', 'app.with.dots', 'html', 'twig'));
$this->parse('@AcmePlugin/Nested/Directory/app.html.twig')->shouldBeLike(new TemplateReference('AcmePlugin', 'Nested/Directory', 'app', 'html', 'twig'));
}

function it_delegates_custom_namespace_to_decorated_parser(
KernelInterface $kernel,
TemplateNameParserInterface $decoratedParser,
Expand Down

0 comments on commit d0e9edd

Please sign in to comment.