From c264d4aa7cff7fc643c1795c2ca3fe0af649c7a7 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Tue, 20 May 2014 11:23:38 +0200 Subject: [PATCH] Starting to re-do theme assets handling --- src/Core/App.php | 29 ++----------------- src/View/Helper.php | 29 +++++++------------ src/View/View.php | 4 +-- tests/TestCase/View/CellTest.php | 3 +- tests/TestCase/View/Helper/HtmlHelperTest.php | 11 ++++--- tests/TestCase/View/HelperTest.php | 5 +++- 6 files changed, 28 insertions(+), 53 deletions(-) diff --git a/src/Core/App.php b/src/Core/App.php index 05f20b7d9c7..f6bbb386468 100644 --- a/src/Core/App.php +++ b/src/Core/App.php @@ -35,11 +35,10 @@ * It is also possible to inspect paths for plugin classes, for instance, to get * the path to a plugin's helpers you would call `App::path('View/Helper', 'MyPlugin')` * - * ### Locating plugins and themes + * ### Locating plugins * - * Plugins and Themes can be located with App as well. Using App::pluginPath('DebugKit') for example, will - * give you the full path to the DebugKit plugin. App::themePath('purple'), would give the full path to the - * `purple` theme. + * Plugins can be located with App as well. Using App::pluginPath('DebugKit') for example, will + * give you the full path to the DebugKit plugin. * * ### Inspecting known objects * @@ -163,28 +162,6 @@ public static function pluginPath($plugin) { return Plugin::path($plugin); } -/** - * Finds the path that a theme is on. Searches through the defined theme paths. - * - * Usage: - * - * `App::themePath('MyTheme');` will return the full path to the 'MyTheme' theme. - * - * @param string $theme theme name to find the path of. - * @return string full path to the theme. - * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::themePath - */ - public static function themePath($theme) { - $themeDir = 'Themed' . DS . Inflector::camelize($theme); - $paths = static::path('Template'); - foreach ($paths as $path) { - if (is_dir($path . $themeDir)) { - return $path . $themeDir . DS; - } - } - return $paths[0] . $themeDir . DS; - } - /** * Returns the full path to a package inside the CakePHP core * diff --git a/src/View/Helper.php b/src/View/Helper.php index 2133e0f8504..8d1b047cef6 100644 --- a/src/View/Helper.php +++ b/src/View/Helper.php @@ -213,19 +213,19 @@ public function webroot($file) { if (!empty($this->theme)) { $file = trim($file, '/'); - $theme = $this->theme . '/'; + $theme = Inflector::underscore($this->theme) . '/'; if (DS === '\\') { $file = str_replace('/', '\\', $file); } - if (file_exists(Configure::read('App.www_root') . 'theme/' . $this->theme . DS . $file)) { - $webPath = "{$this->request->webroot}theme/" . $theme . $asset[0]; + if (file_exists(Configure::read('App.www_root') . $theme . DS . $file)) { + $webPath = "{$this->request->webroot}" . $theme . $asset[0]; } else { - $themePath = App::themePath($this->theme); + $themePath = Plugin::path($this->theme); $path = $themePath . 'webroot/' . $file; if (file_exists($path)) { - $webPath = "{$this->request->webroot}theme/" . $theme . $asset[0]; + $webPath = "{$this->request->webroot}" . $theme . $asset[0]; } } } @@ -319,22 +319,13 @@ public function assetTimestamp($path) { //@codingStandardsIgnoreEnd } $segments = explode('/', ltrim($filepath, '/')); - if ($segments[0] === 'theme') { - $theme = $segments[1]; - unset($segments[0], $segments[1]); - $themePath = App::themePath($theme) . 'webroot' . DS . implode(DS, $segments); + $plugin = Inflector::camelize($segments[0]); + if (Plugin::loaded($plugin)) { + unset($segments[0]); + $pluginPath = Plugin::path($plugin) . 'webroot' . DS . implode(DS, $segments); //@codingStandardsIgnoreStart - return $path . '?' . @filemtime($themePath); + return $path . '?' . @filemtime($pluginPath); //@codingStandardsIgnoreEnd - } else { - $plugin = Inflector::camelize($segments[0]); - if (Plugin::loaded($plugin)) { - unset($segments[0]); - $pluginPath = Plugin::path($plugin) . 'webroot' . DS . implode(DS, $segments); - //@codingStandardsIgnoreStart - return $path . '?' . @filemtime($pluginPath); - //@codingStandardsIgnoreEnd - } } } return $path; diff --git a/src/View/View.php b/src/View/View.php index 96f364178df..6544f374f52 100644 --- a/src/View/View.php +++ b/src/View/View.php @@ -42,10 +42,10 @@ * * Since 2.1, the base View class also includes support for themes by default. Theme views are regular * view files that can provide unique HTML and static assets. If theme views are not found for the - * current view the default app view files will be used. You can set `$this->theme = 'mytheme'` + * current view the default app view files will be used. You can set `$this->theme = 'Mytheme'` * in your Controller to use the Themes. * - * Example of theme path with `$this->theme = 'SuperHot';` Would be `app/Template/Themed/SuperHot/Posts` + * Example of theme path with `$this->theme = 'SuperHot';` Would be `Plugin/SuperHot/Template/Posts` * * @property \Cake\View\Helper\CacheHelper $Cache * @property \Cake\View\Helper\FormHelper $Form diff --git a/tests/TestCase/View/CellTest.php b/tests/TestCase/View/CellTest.php index a58358a524f..eb73bc0e627 100644 --- a/tests/TestCase/View/CellTest.php +++ b/tests/TestCase/View/CellTest.php @@ -38,7 +38,7 @@ public function setUp() { parent::setUp(); Configure::write('App.namespace', 'TestApp'); Configure::write('debug', 2); - Plugin::load('TestPlugin'); + Plugin::load(['TestPlugin', 'TestTheme']); $request = $this->getMock('Cake\Network\Request'); $response = $this->getMock('Cake\Network\Response'); $this->View = new \Cake\View\View($request, $response); @@ -52,6 +52,7 @@ public function setUp() { public function tearDown() { parent::tearDown(); Plugin::unload('TestPlugin'); + Plugin::unload('TestTheme'); unset($this->View); } diff --git a/tests/TestCase/View/Helper/HtmlHelperTest.php b/tests/TestCase/View/Helper/HtmlHelperTest.php index e77a361e151..1a5232d6996 100644 --- a/tests/TestCase/View/Helper/HtmlHelperTest.php +++ b/tests/TestCase/View/Helper/HtmlHelperTest.php @@ -71,6 +71,8 @@ public function setUp() { $this->Html->request = new Request(); $this->Html->request->webroot = ''; + Configure::write('App.namespace', 'TestApp'); + Plugin::load(['TestTheme']); Configure::write('Asset.timestamp', false); } @@ -81,6 +83,7 @@ public function setUp() { */ public function tearDown() { parent::tearDown(); + Plugin::unload('TestTheme'); unset($this->Html, $this->View); } @@ -444,7 +447,7 @@ public function testImageTagWithTheme() { Configure::write('debug', true); $this->Html->request->webroot = '/'; - $this->Html->theme = 'test_theme'; + $this->Html->theme = 'TestTheme'; $result = $this->Html->image('__cake_test_image.gif'); $this->assertTags($result, array( 'img' => array( @@ -471,14 +474,14 @@ public function testThemeAssetsInMainWebrootPath() { $webRoot = Configure::read('App.www_root'); Configure::write('App.www_root', TEST_APP . 'webroot/'); - $this->Html->theme = 'test_theme'; + $this->Html->theme = 'TestTheme'; $result = $this->Html->css('webroot_test'); $expected = array( 'link' => array('rel' => 'stylesheet', 'href' => 'preg:/.*theme\/test_theme\/css\/webroot_test\.css/') ); $this->assertTags($result, $expected); - $this->Html->theme = 'test_theme'; + $this->Html->theme = 'TestTheme'; $result = $this->Html->css('theme_webroot'); $expected = array( 'link' => array('rel' => 'stylesheet', 'href' => 'preg:/.*theme\/test_theme\/css\/theme_webroot\.css/') @@ -988,7 +991,7 @@ public function testScriptInTheme() { new File($testfile, true); $this->Html->request->webroot = '/'; - $this->Html->theme = 'test_theme'; + $this->Html->theme = 'TestTheme'; $result = $this->Html->script('__test_js.js'); $expected = array( 'script' => array('src' => '/theme/test_theme/js/__test_js.js') diff --git a/tests/TestCase/View/HelperTest.php b/tests/TestCase/View/HelperTest.php index 8166133fcaf..0a7e8088397 100644 --- a/tests/TestCase/View/HelperTest.php +++ b/tests/TestCase/View/HelperTest.php @@ -180,6 +180,9 @@ public function setUp() { $this->View = new View(); $this->Helper = new Helper($this->View); $this->Helper->request = new Request(); + + Configure::write('App.namespace', 'TestApp'); + Plugin::load(['TestTheme']); } /** @@ -406,7 +409,7 @@ public function testWebrootPaths() { $expected = '/img/cake.power.gif'; $this->assertEquals($expected, $result); - $this->Helper->theme = 'test_theme'; + $this->Helper->theme = 'TestTheme'; $result = $this->Helper->webroot('/img/cake.power.gif'); $expected = '/theme/test_theme/img/cake.power.gif';