Browse Source

Starting to re-do theme assets handling

Jose Lorenzo Rodriguez 12 years ago
parent
commit
c264d4aa7c

+ 3 - 26
src/Core/App.php

@@ -35,11 +35,10 @@ use Cake\Utility\Inflector;
  * 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
  *
@@ -164,28 +163,6 @@ class App {
 	}
 
 /**
- * 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
  *
  * Usage:

+ 10 - 19
src/View/Helper.php

@@ -213,19 +213,19 @@ class Helper implements EventListener {
 
 		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 @@ class Helper implements EventListener {
 				//@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;

+ 2 - 2
src/View/View.php

@@ -42,10 +42,10 @@ use Cake\View\ViewVarsTrait;
  *
  * 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

+ 2 - 1
tests/TestCase/View/CellTest.php

@@ -38,7 +38,7 @@ class CellTest extends TestCase {
 		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 @@ class CellTest extends TestCase {
 	public function tearDown() {
 		parent::tearDown();
 		Plugin::unload('TestPlugin');
+		Plugin::unload('TestTheme');
 		unset($this->View);
 	}
 

+ 7 - 4
tests/TestCase/View/Helper/HtmlHelperTest.php

@@ -71,6 +71,8 @@ class HtmlHelperTest extends TestCase {
 		$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 @@ class HtmlHelperTest extends TestCase {
  */
 	public function tearDown() {
 		parent::tearDown();
+		Plugin::unload('TestTheme');
 		unset($this->Html, $this->View);
 	}
 
@@ -444,7 +447,7 @@ class HtmlHelperTest extends TestCase {
 		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 @@ class HtmlHelperTest extends TestCase {
 		$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 @@ class HtmlHelperTest extends TestCase {
 		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')

+ 4 - 1
tests/TestCase/View/HelperTest.php

@@ -180,6 +180,9 @@ class HelperTest extends TestCase {
 		$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 @@ class HelperTest extends TestCase {
 		$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';