Skip to content

Commit

Permalink
bug #10082 [Theme] Allow overriding templates from plugins (1.2.*) (Z…
Browse files Browse the repository at this point in the history
…ales0123, pamil)

This PR was merged into the 1.2 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.2
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | fixes #9654
| License         | MIT

It's hard to test this change with some functional test in a current tests structure, as it would require to use `SyliusPluginTrait` in test bundle/plugin, but it's in a core bundle :/ I have some idea have to test it properly with a Behat scenario, but I need to spend a few more minutes on that :)

It would be nice if someone could incorporate these changes into their application and see does it work as expected (cc @jacquesbh :)). I've of course done that, but _better safe than sorry_ 🐃 

This change only applies to `1.2` branch, for Sylius `^1.3` take a look here: #10083.

Commits
-------

2add023 Fix plugin templates resolving on 1.2
b4084c4 Replace regexp with plain string operation
  • Loading branch information
pamil committed Jan 10, 2019
2 parents 17dd2be + b4084c4 commit 2401264
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Sylius/Bundle/ThemeBundle/Templating/TemplateNameParser.php
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' : '';
}
}
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 2401264

Please sign in to comment.