Skip to content

Commit

Permalink
Starting to re-do theme assets handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 20, 2014
1 parent 5f60225 commit c264d4a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 53 deletions.
29 changes: 3 additions & 26 deletions src/Core/App.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
29 changes: 10 additions & 19 deletions src/View/Helper.php
Expand Up @@ -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];
}
}
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/View/View.php
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/View/CellTest.php
Expand Up @@ -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);
Expand All @@ -52,6 +52,7 @@ public function setUp() {
public function tearDown() {
parent::tearDown();
Plugin::unload('TestPlugin');
Plugin::unload('TestTheme');
unset($this->View);
}

Expand Down
11 changes: 7 additions & 4 deletions tests/TestCase/View/Helper/HtmlHelperTest.php
Expand Up @@ -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);
}

Expand All @@ -81,6 +83,7 @@ public function setUp() {
*/
public function tearDown() {
parent::tearDown();
Plugin::unload('TestTheme');
unset($this->Html, $this->View);
}

Expand Down Expand Up @@ -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(
Expand All @@ -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/')
Expand Down Expand Up @@ -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')
Expand Down
5 changes: 4 additions & 1 deletion tests/TestCase/View/HelperTest.php
Expand Up @@ -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']);
}

/**
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit c264d4a

Please sign in to comment.