From 5f391b8520e5bd860a5ff9c8d7e37a71f640486a Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Wed, 9 Jan 2019 16:29:44 +0100 Subject: [PATCH 1/2] Allow overriding templates from plugins --- Locator/BundleResourceLocator.php | 12 ++++++++-- .../twigNamespacedBothThemesTemplate.txt.twig | 1 + ...cedVanillaOverriddenThemeTemplate.txt.twig | 1 + Tests/Functional/TemplatingTest.php | 23 +++++++++++++++++++ spec/Locator/BundleResourceLocatorSpec.php | 11 +++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 Tests/Fixtures/themes/FirstTestTheme/TestPlugin/views/Templating/twigNamespacedBothThemesTemplate.txt.twig create mode 100644 Tests/Fixtures/themes/FirstTestTheme/TestPlugin/views/Templating/twigNamespacedVanillaOverriddenThemeTemplate.txt.twig diff --git a/Locator/BundleResourceLocator.php b/Locator/BundleResourceLocator.php index 48802f14e896..cc3e70ca919a 100644 --- a/Locator/BundleResourceLocator.php +++ b/Locator/BundleResourceLocator.php @@ -85,10 +85,9 @@ 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; @@ -96,4 +95,13 @@ private function locateResourceBasedOnTwigNamespace(string $resourcePath, ThemeI throw new ResourceNotFoundException($resourcePath, $theme); } + + private function getBundleOrPluginName(string $twigNamespace): string + { + if (preg_match('/Plugin$/', $twigNamespace)) { + return $twigNamespace; + } + + return $twigNamespace . 'Bundle'; + } } diff --git a/Tests/Fixtures/themes/FirstTestTheme/TestPlugin/views/Templating/twigNamespacedBothThemesTemplate.txt.twig b/Tests/Fixtures/themes/FirstTestTheme/TestPlugin/views/Templating/twigNamespacedBothThemesTemplate.txt.twig new file mode 100644 index 000000000000..3bbeacd04ac6 --- /dev/null +++ b/Tests/Fixtures/themes/FirstTestTheme/TestPlugin/views/Templating/twigNamespacedBothThemesTemplate.txt.twig @@ -0,0 +1 @@ +@TestPlugin/Templating/twigNamespacedBothThemesTemplate.txt.twig|sylius/first-test-theme diff --git a/Tests/Fixtures/themes/FirstTestTheme/TestPlugin/views/Templating/twigNamespacedVanillaOverriddenThemeTemplate.txt.twig b/Tests/Fixtures/themes/FirstTestTheme/TestPlugin/views/Templating/twigNamespacedVanillaOverriddenThemeTemplate.txt.twig new file mode 100644 index 000000000000..73406287ee0d --- /dev/null +++ b/Tests/Fixtures/themes/FirstTestTheme/TestPlugin/views/Templating/twigNamespacedVanillaOverriddenThemeTemplate.txt.twig @@ -0,0 +1 @@ +@TestPlugin/Templating/twigNamespacedVanillaOverriddenThemeTemplate.txt.twig|sylius/first-test-theme diff --git a/Tests/Functional/TemplatingTest.php b/Tests/Functional/TemplatingTest.php index 9eb28897bf7d..4b5ab50c7fbe 100644 --- a/Tests/Functional/TemplatingTest.php +++ b/Tests/Functional/TemplatingTest.php @@ -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 diff --git a/spec/Locator/BundleResourceLocatorSpec.php b/spec/Locator/BundleResourceLocatorSpec.php index 520eb18bd4b4..0fdd46b0cdd7 100644 --- a/spec/Locator/BundleResourceLocatorSpec.php +++ b/spec/Locator/BundleResourceLocatorSpec.php @@ -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 From 455875b2f7a11c06978606e340e083fc53cf212e Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Thu, 10 Jan 2019 13:25:17 +0100 Subject: [PATCH 2/2] Replace regexp with plain string operation --- Locator/BundleResourceLocator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Locator/BundleResourceLocator.php b/Locator/BundleResourceLocator.php index cc3e70ca919a..82f60f1c3603 100644 --- a/Locator/BundleResourceLocator.php +++ b/Locator/BundleResourceLocator.php @@ -98,7 +98,7 @@ private function locateResourceBasedOnTwigNamespace(string $resourcePath, ThemeI private function getBundleOrPluginName(string $twigNamespace): string { - if (preg_match('/Plugin$/', $twigNamespace)) { + if (substr($twigNamespace, -6) === 'Plugin') { return $twigNamespace; }