protected function _getFullAssetPath($path) { $filepath = preg_replace('/^' . preg_quote($this->Helper->request->webroot, '/') . '/', '', urldecode($path)); $webrootPath = WWW_ROOT . str_replace('/', DS, $filepath); if (file_exists($webrootPath)) { //@codingStandardsIgnoreStart return $webrootPath; //@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); //@codingStandardsIgnoreStart return $themePath; //@codingStandardsIgnoreEnd } else { $plugin = Inflector::camelize($segments[0]); if (CakePlugin::loaded($plugin)) { unset($segments[0]); $pluginPath = CakePlugin::path($plugin) . 'webroot' . DS . implode(DS, $segments); //@codingStandardsIgnoreStart return $pluginPath; //@codingStandardsIgnoreEnd } } return false; }
/** * Builds asset file path based off url * * @param string $url * @return string Absolute path for asset file */ static function getAssetFile($url) { $parts = explode('/', $url); if ($parts[0] === 'theme') { $file = ''; $fileFragments = explode(',', $url); $fileNumber = count($fileFragments); foreach ($fileFragments as $k => $fileFragment) { $fileParts = explode('/', $fileFragment); unset($fileParts[0], $fileParts[1]); if ($fileNumber == $k + 1) { $file .= urldecode(implode(DS, $fileParts)); } else { $file .= urldecode(implode(DS, $fileParts)) . ','; } } $themeName = $parts[1]; $path = Configure::read('App.www_root') . 'theme' . DS . $themeName; if (!file_exists($path)) { $path = App::themePath($themeName) . 'webroot'; } return array($path, $file); } $plugin = Inflector::camelize($parts[0]); if (CakePlugin::loaded($plugin)) { unset($parts[0]); $fileFragment = urldecode(implode(DS, $parts)); $pluginWebroot = CakePlugin::path($plugin) . 'webroot'; return array($pluginWebroot, $fileFragment); } else { return array(WWW_ROOT, $_GET['f']); } }
/** * @param array $scripts to minify * @param array $options theme */ public function css($scripts, $options = array()) { if (Configure::read('debug') || Configure::read('Minify.minify') === false) { return $this->Html->css($scripts); } $options = Set::merge(array('theme' => $this->_View->theme, 'plugin' => false, 'subdir' => false), $options); extract($options); $path = APP; if (!empty($theme)) { $path = App::themePath($theme); } elseif (!empty($plugin)) { $path = CakePlugin::pluginPath($plugin); } $targetDirectory = $path . DS . 'webroot' . DS . 'css' . DS; $outputfile = $targetDirectory . $subdir . DS . 'minified-' . sha1(join(':', $scripts)) . '.css'; if (file_exists($outputfile)) { $outputfile = str_replace($targetDirectory, '', $outputfile); return $this->Html->css($outputfile); } $contents = ''; foreach ($scripts as $script) { $file = $targetDirectory . $script; if (!preg_match('/\\.css$/', $file)) { $file .= '.css'; } $contents .= file_get_contents($file); } $contents = Minify_CSS_Compressor::process($contents); file_put_contents($outputfile, $contents); return $this->Html->css($scripts); }
/** * theme list (array) * * @param Controller $controller controller class object * @return array */ public function getList(Controller $controller) { $themeList = array(); //テーマの情報を格納 $dir = realpath(App::themePath('')) . '/'; //フォルダの中を取得 $dirList = scandir($dir, 1); //フォルダ名だけのリストをつくる $package['snapshot'] = ''; foreach ($dirList as $d) { if (is_dir($dir . $d) && ($d != '.' && $d != '..')) { if (is_file($dir . $d . '/theme.json')) { $file = file_get_contents($dir . $d . '/theme.json'); $package = json_decode($file, true); if (is_file(App::themePath($d) . 'webroot/snapshot.jpg')) { $package['snapshot'] = '/theme/' . $d . '/snapshot.jpg'; } elseif (is_file(App::themePath($d) . 'webroot/snapshot.png')) { $package['snapshot'] = '/theme/' . $d . '/snapshot.png'; } $package['key'] = $d; $themeList[$d] = $package; $package['snapshot'] = ''; } } } ksort($themeList); $this->ThemeList = $themeList; return $this->ThemeList; }
/** * cssの存在チェック * * @param Controller $controller controller object * @return bool */ public function isThemeBootstrapMinCss(Controller $controller) { $filePath = App::themePath($controller->theme) . 'webroot/css/bootstrap.min.css'; if (is_file(realpath($filePath))) { return true; } return false; }
protected function _theme($asset) { $parts = explode('/', $asset); if ($parts[0] !== 'theme') { return false; } $themeName = $parts[1]; unset($parts[0], $parts[1]); $fileFragment = implode(DS, $parts); $path = App::themePath($themeName) . 'webroot' . DS; return $this->_exists($path . $fileFragment); }
/** * testRenderOverridenAdminFormWithTheme */ public function testRenderOverridenAdminFormWithTheme() { $theme = 'Mytheme'; $this->controller->theme = $theme; $filePath = App::themePath($theme) . 'TestApp' . DS . 'admin_edit.ctp'; $expected = '<h1>I should be displayed</h1>'; $File = new File($filePath, true, 0777); $File->write($expected); $File->close(); $result = $this->testAction('/admin/test_app/edit', array('return' => 'contents')); $File->delete(); $this->assertContains($expected, trim($result)); }
public function test() { pr(App::themePath('purple')); pr(App::path('Model')); debug(App::paths()); pr(App::path('Component', 'DebugKit')); debug(env('document_root')); debug(h('fengjie ; fengjie ')); LogError('fengjie'); pr(array('fengjie', 'fengjie')); debug(h('fengjie ; fengjie ')); pr(YEAR); pr(HOUR); }
public function getThemeLayouts() { $themes = $this->Croogo->getThemes(); $available = array(); foreach ($themes as $theme) { if ($theme == 'default') { continue; } $path = App::themePath($theme); $layouts = $this->_getLayouts($path); $available[$theme] = compact('path', 'layouts'); } return $available; }
public function getThemeLayouts() { $themes = $this->Croogo->getThemes(); $available = array(); /* Get only active Theme & filter it by Content Type */ if (Configure::read('Switcher.filterByContentType')) { $themes = [Configure::read('Site.theme')]; } foreach ($themes as $theme) { if ($theme == 'default') { continue; } $path = App::themePath($theme); $layouts = $this->_getLayouts($path); $available[$theme] = compact('path', 'layouts'); } return $available; }
/** * Checks if a requested asset exists and sends it to the browser * * @param CakeEvent $event * containing the request and response object * @return CakeResponse if the client is requesting a recognized asset, null otherwise */ public function beforeDispatch($event) { $url = $event->data['request']->url; $response = $event->data['response']; if (strpos($url, '..') !== false || strpos($url, '.') === false) { return; } if ($result = $this->_filterAsset($event)) { $event->stopPropagation(); return $result; } $pathSegments = explode('.', $url); $ext = array_pop($pathSegments); $parts = explode('/', $url); $assetFile = null; if ($parts[0] === 'theme') { $themeName = $parts[1]; unset($parts[0], $parts[1]); $fileFragment = urldecode(implode(DS, $parts)); $path = App::themePath($themeName) . 'webroot' . DS; if (file_exists($path . $fileFragment)) { $assetFile = $path . $fileFragment; } } else { $plugin = Inflector::camelize($parts[0]); if (CakePlugin::loaded($plugin)) { unset($parts[0]); $fileFragment = urldecode(implode(DS, $parts)); $pluginWebroot = CakePlugin::path($plugin) . 'webroot' . DS; if (file_exists($pluginWebroot . $fileFragment)) { $assetFile = $pluginWebroot . $fileFragment; } } } if ($assetFile !== null) { $event->stopPropagation(); $response->modified(filemtime($assetFile)); if (!$response->checkNotModified($event->data['request'])) { $this->_deliverAsset($response, $assetFile, $ext); } return $response; } }
/** * Builds asset file path based off url * * @param string $url * @return string Absolute path for asset file */ protected function _getAssetFile($url) { $parts = explode('/', $url); // start zuha addition $path = App::themePath($parts[1]) . 'webroot' . DS; $fileFragment = urldecode(implode(DS, $parts)); if (file_exists($path . $fileFragment)) { return $path . $fileFragment; } // copied directly from Cake/Routing/Filter/AssetDispatcher.php $parts = explode('/', $url); if ($parts[0] === 'theme') { $themeName = $parts[1]; unset($parts[0], $parts[1]); $fileFragment = implode(DS, $parts); $path = App::themePath($themeName) . 'webroot' . DS; return str_replace('//', '/', $path . $fileFragment); // and added this one fix } return parent::_getAssetFile($url); }
protected function _setupViewPaths(Controller $controller) { $defaultViewPaths = App::path('View'); $pos = array_search(APP . 'View' . DS, $defaultViewPaths); if ($pos !== false) { $viewPaths = array_splice($defaultViewPaths, 0, $pos + 1); } else { $viewPaths = $defaultViewPaths; } if ($controller->theme) { $themePath = App::themePath($controller->theme); $viewPaths[] = $themePath; if ($controller->plugin) { $viewPaths[] = $themePath . 'Plugin' . DS . $controller->plugin . DS; } } if ($controller->plugin) { $viewPaths = array_merge($viewPaths, App::path('View', $controller->plugin)); } $viewPaths = array_merge($viewPaths, $defaultViewPaths); return $viewPaths; }
public function testFromAsset() { $Asset = new CssAsset('css/default.css', $this->file, $this->path); $result = AssetFactory::fromAsset($Asset, 'bundle'); $this->assertInstanceOf('Asset', $result); $this->assertEquals('css/bundle.css', $result->url); $this->assertEquals($this->path . 'css' . DS . 'bundle.css', $result->file); $Asset = new CssAsset('css/app/bundle.css', $this->file, $this->path); $result = AssetFactory::fromAsset($Asset, '../bundle'); $this->assertInstanceOf('Asset', $result); $this->assertEquals('css/bundle.css', $result->url); $this->assertEquals($this->path . 'css' . DS . 'bundle.css', $result->file); $Asset = new CssAsset('css/default.css', $this->file, $this->path); $result = AssetFactory::fromAsset($Asset, 'app/bundle'); $this->assertInstanceOf('Asset', $result); $this->assertEquals('css/app/bundle.css', $result->url); $this->assertEquals($this->path . 'css' . DS . 'app' . DS . 'bundle.css', $result->file); $result = AssetFactory::fromAsset($Asset, '/theme/admin/css/bundle'); $this->assertInstanceOf('Asset', $result); $this->assertEquals('theme/admin/css/bundle.css', $result->url); $this->assertEquals(App::themePath('admin') . 'webroot' . DS . 'css' . DS . 'bundle.css', $result->file); }
/** * Tests that $response->checkNotModified() is called and bypasses * file dispatching * * @return void */ public function testNotModified() { $filter = new AssetDispatcher(); Configure::write('Asset.filter', array('js' => '', 'css' => '')); App::build(array('Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS), 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS))); $time = filemtime(App::themePath('TestTheme') . 'webroot' . DS . 'img' . DS . 'cake.power.gif'); $time = new DateTime('@' . $time); $response = $this->getMock('CakeResponse', array('send', 'checkNotModified')); $request = new CakeRequest('theme/test_theme/img/cake.power.gif'); $response->expects($this->once())->method('checkNotModified')->with($request)->will($this->returnValue(true)); $event = new CakeEvent('DispatcherTest', $this, compact('request', 'response')); ob_start(); $this->assertSame($response, $filter->beforeDispatch($event)); ob_end_clean(); $this->assertEquals(200, $response->statusCode()); $this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified()); $response = $this->getMock('CakeResponse', array('_sendHeader', 'checkNotModified')); $request = new CakeRequest('theme/test_theme/img/cake.power.gif'); $response->expects($this->once())->method('checkNotModified')->with($request)->will($this->returnValue(true)); $response->expects($this->never())->method('send'); $event = new CakeEvent('DispatcherTest', $this, compact('request', 'response')); $this->assertSame($response, $filter->beforeDispatch($event)); $this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified()); }
/** * Resolve a themed file to its full path. The file will be found on the * current theme's path. * * @param string $file The theme file to find. * @param boolean $full Gives absolute paths * @return string Full path to theme file. */ protected function _resolveTheme($file, $full = true) { $file = preg_replace(self::THEME_PATTERN, '', $file); if ($full) { return App::themePath($this->_theme) . 'webroot' . DS . $file; } return DS . Inflector::underscore($this->_theme) . DS . $file; }
/** * Checks if a file exists when theme is used, if no file is found default location is returned * * PENDING Core Hack * * @param string $file * @return string $webPath web path to file. */ public function webroot($file) { // CUSTOMIZE ADD 2015/02/15 ryuring // >>> // フルパスの場合はそのまま返す if (preg_match('/^(http|https):\\/\\//', $file)) { return $file; } // <<< $asset = explode('?', $file); $asset[1] = isset($asset[1]) ? '?' . $asset[1] : null; // CUSTOMIZE MODIFY 2009/10/6 ryuring // Rewriteモジュールが利用できない場合、$this->Html->css / $javascript->link では、 // app/webroot/を付加してURLを生成してしまう為、vendors 内のパス解決ができない。 // URLの取得方法をRouterに変更 // Dispatcherクラスのハックが必須 // // CUSTOMIZE MODIFY 2010/02/12 ryuring // ファイルの存在チェックを行い存在しない場合のみRouterを利用するように変更した。 // // CUSTOMIZE MODIFY 2011/04/11 ryuring // Rewriteモジュールが利用できない場合、画像等で出力されるURL形式(/app/webroot/img/...)が // $file に設定された場合でもパス解決ができるようにした。 // // >>> //$webPath = "{$this->request->webroot}" . $asset[0]; // --- $asset[0] = preg_replace('/^\\//', '', $asset[0]); if ($this->request->webroot && $this->request->webroot != '/') { $filePath = preg_replace('/' . preg_quote($this->request->webroot, '/') . '/', '', $asset[0]); } else { $filePath = $asset[0]; } $filePath = str_replace('/', DS, $filePath); $docRoot = docRoot(); if (file_exists(WWW_ROOT . $filePath)) { $webPath = $this->request->webroot . $asset[0]; } elseif (file_exists($docRoot . DS . $filePath) && strpos($docRoot . DS . $filePath, ROOT . DS) !== false) { // ※ ファイルのパスが ROOT 配下にある事が前提 $webPath = $asset[0]; } else { $webPath = Router::url('/' . $asset[0]); } // <<< $file = $asset[0]; if (!empty($this->theme)) { $file = trim($file, '/'); $theme = $this->theme . '/'; if (DS === '\\') { $file = str_replace('/', '\\', $file); } if (file_exists(Configure::read('App.www_root') . 'theme' . DS . $this->theme . DS . $file)) { $webPath = "{$this->request->webroot}theme/" . $theme . $asset[0]; } else { $themePath = App::themePath($this->theme); $path = $themePath . 'webroot' . DS . $file; if (file_exists($path)) { // CUSTOMIZE 2013/6/18 ryuring // >>> // $webPath = Configure::read('App.baseUrl')."{$this->request->webroot}theme/" . $theme . $asset[0]; //} // --- if ($baseUrl) { // スマートURLオフ $webPath = Configure::read('App.baseUrl') . "/theme/" . $theme . $asset[0]; } else { // スマートURLオン $webPath = "{$this->request->webroot}theme/" . $theme . $asset[0]; } } else { // フロントのWebページを表示する際に、管理システム用のアセットファイルを参照する為のURLを生成する if (property_exists($this->_View, 'adminTheme') && $this->_View->adminTheme) { if (file_exists($themePath = Configure::read('App.www_root') . 'theme' . DS . $this->_View->adminTheme . DS . $file)) { $adminTheme = $this->_View->adminTheme . '/'; if ($baseUrl) { // スマートURLオフ $webPath = Configure::read('App.baseUrl') . "/theme/" . $adminTheme . $asset[0]; } else { // スマートURLオン $webPath = "{$this->request->webroot}theme/" . $adminTheme . $asset[0]; } } } } // <<< } } if (strpos($webPath, '//') !== false) { return str_replace('//', '/', $webPath . $asset[1]); } // >>> CUSTOMIZE ADD 2013/06/18 ryuring if (strpos($webPath, '\\') !== false) { $webPath = str_replace("\\", '/', $webPath); } // <<< return $webPath . $asset[1]; }
/** * A mix between Helper::webroot() and Helper::assetTimestamp() * * @param <type> $name * @param <type> $type * @return <type> */ function assetUrl($name, $type = null) { if (strpos($name, '://') !== false) { return $name; } // appends default extension if (strpos($name, '?') === false) { if ($type && !preg_match("/\\.{$type}\$/", $name)) { $name .= ".{$type}"; } } // either assets root, vendors or cdn file here if ($name[0] !== '/') { if (strpos($name, '/') === false) { if ($type) { $name = "{$type}/{$name}"; } } else { list($vendor, $filename) = explode('/', $name, 2); $cdn = SlConfigure::read("Asset.cdn.{$vendor}"); if ($cdn) { $cdnUrl = "{$cdn}/{$filename}"; if (SlConfigure::read("Asset.options.alwaysUseCdn")) { return $cdnUrl; } } if (file_exists(WWW_ROOT . "vendors/{$name}")) { $name = "vendors/{$name}"; } else { if ($type) { $name = "{$type}/{$name}"; } } } } else { $name = substr($name, 1); } if (file_exists(WWW_ROOT . "theme/{$this->theme}/" . $name)) { $path = WWW_ROOT . "theme/{$this->theme}/" . $name; $url = $this->webroot . "theme/{$this->theme}/" . $name; } elseif (file_exists(WWW_ROOT . $name)) { $path = WWW_ROOT . $name; $url = $this->webroot . $name; } else { $themePath = App::themePath($this->theme) . "webroot/{$name}"; if (file_exists($themePath)) { $path = $themePath; $url = $this->webroot . "theme/{$this->theme}/" . $name; } else { if (!empty($cdnUrl)) { return $cdnUrl; } list($plugin, $fileName) = explode('/', $name, 2); if ($plugin == 'theme') { list($theme, $fileName) = explode('/', $fileName, 2); $path = App::themePath($theme) . "webroot/{$filename}"; } else { $path = App::pluginPath($plugin) . "webroot/{$fileName}"; } $url = $this->webroot . $name; } } if (strpos($url, '?') === false && file_exists($path) && SlConfigure::read('Asset.options.timestamp')) { $url .= '?' . filemtime($path); } return $url; }
/** * Checks if a requested asset exists and sends it to the browser * * @param string $url Requested URL * @param CakeResponse $response The response object to put the file contents in. * @return boolean True on success if the asset file was found and sent */ public function asset($url, CakeResponse $response) { if (strpos($url, '..') !== false || strpos($url, '.') === false) { return false; } $filters = Configure::read('Asset.filter'); $isCss = strpos($url, 'ccss/') === 0 || preg_match('#^(theme/([^/]+)/ccss/)|(([^/]+)(?<!css)/ccss)/#i', $url); $isJs = strpos($url, 'cjs/') === 0 || preg_match('#^/((theme/[^/]+)/cjs/)|(([^/]+)(?<!js)/cjs)/#i', $url); if ($isCss && empty($filters['css']) || $isJs && empty($filters['js'])) { $response->statusCode(404); $response->send(); return true; } elseif ($isCss) { include WWW_ROOT . DS . $filters['css']; return true; } elseif ($isJs) { include WWW_ROOT . DS . $filters['js']; return true; } $pathSegments = explode('.', $url); $ext = array_pop($pathSegments); $parts = explode('/', $url); $assetFile = null; if ($parts[0] === 'theme') { $themeName = $parts[1]; unset($parts[0], $parts[1]); $fileFragment = urldecode(implode(DS, $parts)); $path = App::themePath($themeName) . 'webroot' . DS; if (file_exists($path . $fileFragment)) { $assetFile = $path . $fileFragment; } } else { $plugin = Inflector::camelize($parts[0]); if (CakePlugin::loaded($plugin)) { unset($parts[0]); $fileFragment = urldecode(implode(DS, $parts)); $pluginWebroot = CakePlugin::path($plugin) . 'webroot' . DS; if (file_exists($pluginWebroot . $fileFragment)) { $assetFile = $pluginWebroot . $fileFragment; } } } if ($assetFile !== null) { $this->_deliverAsset($response, $assetFile, $ext); return true; } return false; }
/** * test that pluginPath can find paths for plugins. * * @return void */ public function testThemePath() { App::build(array('View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS))); $path = App::themePath('test_theme'); $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS; $this->assertEquals($path, $expected); $path = App::themePath('TestTheme'); $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS; $this->assertEquals($path, $expected); App::build(); }
/** * Adds a timestamp to a file based resource based on the value of `Asset.timestamp` in * Configure. * If Asset.timestamp is true and debug > 0, or Asset.timestamp == 'force' * a timestamp will be added. * * @param string $path * The file path to timestamp, the path must be inside WWW_ROOT * @return string Path with a timestamp added, or not. * @access public */ function assetTimestamp($path) { $timestampEnabled = Configure::read('Asset.timestamp') === true && Configure::read() > 0 || Configure::read('Asset.timestamp') === 'force'; if (strpos($path, '?') === false && $timestampEnabled) { $filepath = preg_replace('/^' . preg_quote($this->webroot, '/') . '/', '', $path); $webrootPath = WWW_ROOT . str_replace('/', DS, $filepath); if (file_exists($webrootPath)) { return $path . '?' . @filemtime($webrootPath); } $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); return $path . '?' . @filemtime($themePath); } else { $plugin = $segments[0]; unset($segments[0]); $pluginPath = App::pluginPath($plugin) . 'webroot' . DS . implode(DS, $segments); return $path . '?' . @filemtime($pluginPath); } } return $path; }
protected function _resolve($type, $file) { $paths = array(APP); if ($this->_View->theme) { $paths[] = App::themePath($this->_View->theme); } $plugins = App::objects('plugin'); foreach ($plugins as $plugin) { if (!CakePlugin::loaded($plugin)) { continue; } $paths[] = App::pluginPath($plugin); } foreach ($paths as $path) { $filename = $path . 'webroot' . DS . $type . DS . $file . '.' . $type; if (file_exists($filename)) { return $filename; } } trigger_error('unable to resolve ' . $type . ' file: ' . $file, E_USER_NOTICE); return false; }
* Settings */ App::uses('CroogoJsonReader', 'Configure'); Configure::config('settings', new CroogoJsonReader()); if (file_exists(APP . 'Config' . DS . 'settings.json')) { Configure::load('settings', 'settings'); } /** * Locale */ Configure::write('Config.language', Configure::read('Site.locale')); /** * Setup custom paths */ if ($theme = Configure::read('Site.theme')) { App::build(array('View/Helper' => array(App::themePath($theme) . 'Helper' . DS))); } /** * List of core plugins */ Configure::write('Core.corePlugins', array('Settings', 'Acl', 'Blocks', 'Comments', 'Contacts', 'Menus', 'Meta', 'Nodes', 'Taxonomy', 'Users')); /** * Plugins */ $aclPlugin = Configure::read('Site.acl_plugin'); $pluginBootstraps = Configure::read('Hook.bootstraps'); $plugins = array_filter(explode(',', $pluginBootstraps)); if (!in_array($aclPlugin, $plugins)) { $plugins = Hash::merge((array) $aclPlugin, $plugins); } foreach ($plugins as $plugin) {
/** * Builds asset file path based off url * * @param string $url URL * @return string Absolute path for asset file */ protected function _getAssetFile($url) { $parts = explode('/', $url); if ($parts[0] === 'theme') { $themeName = $parts[1]; unset($parts[0], $parts[1]); $fileFragment = implode(DS, $parts); $path = App::themePath($themeName) . 'webroot' . DS; return $path . $fileFragment; } $plugin = Inflector::camelize($parts[0]); if ($plugin && CakePlugin::loaded($plugin)) { unset($parts[0]); $fileFragment = implode(DS, $parts); $pluginWebroot = CakePlugin::path($plugin) . 'webroot' . DS; return $pluginWebroot . $fileFragment; } }
/** * Checks if a requested asset exists and sends it to the browser * * @param $url string $url Requested URL * @return boolean True on success if the asset file was found and sent * @access public */ function asset($url) { if (strpos($url, '..') !== false || strpos($url, '.') === false) { return false; } $filters = Configure::read('Asset.filter'); $isCss = strpos($url, 'ccss/') === 0 || preg_match('#^(theme/([^/]+)/ccss/)|(([^/]+)(?<!css)/ccss)/#i', $url); $isJs = strpos($url, 'cjs/') === 0 || preg_match('#^/((theme/[^/]+)/cjs/)|(([^/]+)(?<!js)/cjs)/#i', $url); if ($isCss && empty($filters['css']) || $isJs && empty($filters['js'])) { header('HTTP/1.1 404 Not Found'); return $this->_stop(); } elseif ($isCss) { include WWW_ROOT . DS . $filters['css']; $this->_stop(); } elseif ($isJs) { include WWW_ROOT . DS . $filters['js']; $this->_stop(); } $controller = null; $ext = array_pop(explode('.', $url)); $parts = explode('/', $url); $assetFile = null; if ($parts[0] === 'theme') { $themeName = $parts[1]; unset($parts[0], $parts[1]); $fileFragment = implode(DS, $parts); $path = App::themePath($themeName) . 'webroot' . DS; if (file_exists($path . $fileFragment)) { $assetFile = $path . $fileFragment; } } else { $plugin = $parts[0]; unset($parts[0]); $fileFragment = implode(DS, $parts); $pluginWebroot = App::pluginPath($plugin) . 'webroot' . DS; if (file_exists($pluginWebroot . $fileFragment)) { $assetFile = $pluginWebroot . $fileFragment; } } if ($assetFile !== null) { $this->_deliverAsset($assetFile, $ext); return true; } return false; }
/** * test that pluginPath can find paths for plugins. * * @return void */ function testThemePath() { App::build(array( 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS) )); $path = App::themePath('test_theme'); $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS; $this->assertEqual($path, $expected); $path = App::themePath('TestTheme'); $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS; $this->assertEqual($path, $expected); App::build(); }
/** * Combine add and edit views * * @see Controller::render() */ public function render($view = null, $layout = null) { list($plugin, ) = pluginSplit(App::location(get_parent_class($this))); if ($plugin) { App::build(array('View' => array(CakePlugin::path($plugin) . 'View' . DS)), App::APPEND); } if (strpos($view, '/') !== false || $this instanceof CakeErrorController) { return parent::render($view, $layout); } $fallbackView = $this->__getDefaultFallbackView(); if (is_null($view) && in_array($this->request->action, array('admin_edit', 'admin_add', 'edit', 'add'))) { $viewPaths = App::path('View', $this->plugin); $themePath = $this->theme ? App::themePath($this->theme) : null; $searchPaths = array_merge((array) $themePath, $viewPaths); $view = $this->__findRequestedView($searchPaths); if (empty($view)) { $view = $fallbackView; } } return parent::render($view, $layout); }
/** * @brief load modules directly * * This is basically a short cut method to using ->element() but does * all the checking link is the plugin available, does the module exist * etc. * * @param string $module the module to load * @param string $params the params to pass to element() call * * @return string */ public function loadDirect($module, $params = array()) { list($plugin, $module) = pluginSplit($module); // @check plugin is loaded // if not plugin return 'plugin foobar is not loaded / does not exist' $admin = isset($this->_View->request->params['admin']) && $this->_View->request->params['admin'] || isset($params['admin']) && $params['admin']; if ($admin && (isset($params['admin']) && $params['admin'] == false)) { $admin = false; } if ($admin) { $module = sprintf('admin/%s', $module); } $module = sprintf('modules/%s', $module); $pluginDir = App::pluginPath($plugin) . 'View' . DS . 'Elements' . DS . $module . '.ctp'; $themeDir = App::themePath($this->theme) . $plugin . 'Elements' . DS . $module . '.ctp'; if (!is_file($pluginDir)) { if (!is_file($themeDir)) { if (Configure::read('debug')) { return sprintf('Could not find the module, looking in: <br/>%s<br/>%s', str_replace(APP, 'APP' . DS, $pluginDir), str_replace(APP, 'APP' . DS, $themeDir)); } } } $module = sprintf('%s.%s', $plugin, $module); try { return $this->_View->element($module, array('config' => $params)); } catch (Exception $e) { $message = sprintf('Error: Could not load module "%s" (%s)', $module, $e->getMessage()); throw new Exception($message); } }
/** * Adds a timestamp to a file based resource based on the value of `Asset.timestamp` in * Configure. If Asset.timestamp is true and debug > 0, or Asset.timestamp == 'force' * a timestamp will be added. * * @param string $path The file path to timestamp, the path must be inside WWW_ROOT * @return string Path with a timestamp added, or not. */ public function assetTimestamp($path) { $stamp = Configure::read('Asset.timestamp'); $timestampEnabled = $stamp === 'force' || $stamp === true && Configure::read('debug') > 0; if ($timestampEnabled && strpos($path, '?') === false) { $filepath = preg_replace('/^' . preg_quote($this->request->webroot, '/') . '/', '', $path); $webrootPath = WWW_ROOT . str_replace('/', DS, $filepath); if (file_exists($webrootPath)) { return $path . '?' . @filemtime($webrootPath); } $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); return $path . '?' . @filemtime($themePath); } else { $plugin = Inflector::camelize($segments[0]); if (CakePlugin::loaded($plugin)) { unset($segments[0]); $pluginPath = CakePlugin::path($plugin) . 'webroot' . DS . implode(DS, $segments); return $path . '?' . @filemtime($pluginPath); } } } return $path; }
/** * Get full webroot path for an asset * @param string $path * @param array $options * @return string */ private function path($path, array $options = []) { // get base and full paths $base = $this->assetUrl($path, $options); $base = $this->webroot($base); // do webroot path $filepath = preg_replace('/^' . preg_quote($this->request->webroot, '/') . '/', '', urldecode($base)); $webrootPath = WWW_ROOT . str_replace('/', DS, $filepath); if (file_exists($webrootPath)) { return $webrootPath; } // must be theme or plugin then? $segments = explode('/', ltrim($filepath, '/')); // do theme path if ($segments[0] === 'theme') { $theme = $segments[1]; unset($segments[0], $segments[1]); $themePath = str_replace('/', DS, App::themePath($theme)) . 'webroot' . DS . implode(DS, $segments); return $themePath; // do plugin path } else { $plugin = Inflector::camelize($segments[0]); if (CakePlugin::loaded($plugin)) { unset($segments[0]); $pluginPath = str_replace('/', DS, CakePlugin::path($plugin)) . 'webroot' . DS . implode(DS, $segments); return $pluginPath; } } }