Skip to content

Commit

Permalink
bug Sylius#10083 [Theme] Allow overriding templates from plugins (^1.…
Browse files Browse the repository at this point in the history
…3) (Zales0123)

This PR was merged into the 1.3 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.3
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | fixes (kinda) Sylius#9654
| License         | MIT

This is, of course, a quick fix and quite straight-forward, but it works 😄 And it is still required to use `@SyliusTestPlugin` notation (maybe it's worth to be documented somehow?). This PR fixes the functionality for Sylius `^1.3`, for working fix on `1.2` take a look here: Sylius#10082.

Regarding testing it better (as I've also written in fix for `1.2`):

> 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 :)

Commits
-------

a43d08b Allow overriding templates from plugins
60b0284 Replace regexp with plain string operation
  • Loading branch information
pamil authored Jan 10, 2019
2 parents 3381542 + 455875b commit 33d4bf2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
12 changes: 10 additions & 2 deletions 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';
}
}
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 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
11 changes: 11 additions & 0 deletions spec/Locator/BundleResourceLocatorSpec.php
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

0 comments on commit 33d4bf2

Please sign in to comment.