From 649ae7d350d0332ffa87c68e50f8433d085e41f1 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 1 Aug 2020 23:30:12 -0400 Subject: [PATCH] Fix template override paths The changes in #680 broke compatibility and changed the documented paths that bake would use. I'm reluctant to add another break and go back to what it was as I think the changes to use CakePHP's conventions were generally good ones. This change updates the documentation and removes the `Bake/bake` path from application overrides. Fixes #698 --- docs/en/development.rst | 2 +- src/View/BakeView.php | 11 +++++------ tests/TestCase/View/BakeViewTest.php | 11 +++++++++++ tests/test_app/templates/plugin/Bake/override.twig | 1 + 4 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 tests/test_app/templates/plugin/Bake/override.twig diff --git a/docs/en/development.rst b/docs/en/development.rst index 69a6b90d7..cccf6ddcb 100644 --- a/docs/en/development.rst +++ b/docs/en/development.rst @@ -194,7 +194,7 @@ If you wish to modify the default output produced by the "bake" command, you can create your own bake templates in your application. This way does not use the ``--theme`` option in the command line when baking. The best way to do this is: -#. Create a new directory **/templates/bake/**. +#. Create a new directory **/templates/plugin/Bake/**. #. Copy any templates you want to override from **vendor/cakephp/bake/templates/bake/** to matching files in your application. diff --git a/src/View/BakeView.php b/src/View/BakeView.php index a5a896b91..33e2a3758 100644 --- a/src/View/BakeView.php +++ b/src/View/BakeView.php @@ -88,11 +88,7 @@ public function initialize(): void public function render(?string $view = null, $layout = null): string { $viewFileName = $this->_getTemplateFileName($view); - $templateEventName = str_replace( - ['.twig', DS], - ['', '.'], - explode('templates' . DS . static::BAKE_TEMPLATE_FOLDER . DS, $viewFileName)[1] - ); + $templateEventName = str_replace(DS, '.', $view); $this->_currentType = static::TYPE_TEMPLATE; $this->dispatchEvent('View.beforeRender', [$viewFileName]); @@ -142,7 +138,10 @@ protected function _paths(?string $plugin = null, bool $cached = true): array { $paths = parent::_paths($plugin, false); foreach ($paths as &$path) { - $path .= static::BAKE_TEMPLATE_FOLDER . DS; + // Append 'bake' to all directories that aren't the application override directory. + if (strpos($path, 'plugin' . DS . 'Bake') === false) { + $path .= static::BAKE_TEMPLATE_FOLDER . DS; + } } return $paths; diff --git a/tests/TestCase/View/BakeViewTest.php b/tests/TestCase/View/BakeViewTest.php index 80daa72bc..2bf8ae291 100644 --- a/tests/TestCase/View/BakeViewTest.php +++ b/tests/TestCase/View/BakeViewTest.php @@ -170,4 +170,15 @@ public function testCustomRenderEvents() $result ); } + + /** + * Ensure that application override templates don't have a double path in them. + * + * @return void + */ + public function testApplicationOverride() + { + $result = $this->View->render('Bake.override'); + $this->assertSame("Application override.\n", $result); + } } diff --git a/tests/test_app/templates/plugin/Bake/override.twig b/tests/test_app/templates/plugin/Bake/override.twig new file mode 100644 index 000000000..da38af8e7 --- /dev/null +++ b/tests/test_app/templates/plugin/Bake/override.twig @@ -0,0 +1 @@ +Application override.