View.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. <?php
  2. /**
  3. * Methods for displaying presentation data in the view.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.View
  16. * @since CakePHP(tm) v 0.10.0.1076
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * Included libraries.
  21. */
  22. App::uses('HelperCollection', 'View');
  23. App::uses('AppHelper', 'View/Helper');
  24. App::uses('Router', 'Routing');
  25. /**
  26. * View, the V in the MVC triad. View interacts with Helpers and view variables passed
  27. * in from the controller to render the results of the controller action. Often this is HTML,
  28. * but can also take the form of JSON, XML, PDF's or streaming files.
  29. *
  30. * CakePHP uses a two-step-view pattern. This means that the view content is rendered first,
  31. * and then inserted into the selected layout. A special `$content_for_layout` variable is available
  32. * in the layout, and it contains the rendered view. This also means you can pass data from the view to the
  33. * layout using `$this->set()`
  34. *
  35. * @package Cake.View
  36. * @property CacheHelper $Cache
  37. * @property FormHelper $Form
  38. * @property HtmlHelper $Html
  39. * @property JsHelper $Js
  40. * @property NumberHelper $Number
  41. * @property PaginatorHelper $Paginator
  42. * @property RssHelper $Rss
  43. * @property SessionHelper $Session
  44. * @property TextHelper $Text
  45. * @property TimeHelper $Time
  46. */
  47. class View extends Object {
  48. /**
  49. * Helpers collection
  50. *
  51. * @var HelperCollection
  52. */
  53. public $Helpers;
  54. /**
  55. * Name of the plugin.
  56. *
  57. * @link http://manual.cakephp.org/chapter/plugins
  58. * @var string
  59. */
  60. public $plugin = null;
  61. /**
  62. * Name of the controller.
  63. *
  64. * @var string Name of controller
  65. */
  66. public $name = null;
  67. /**
  68. * Current passed params
  69. *
  70. * @var mixed
  71. */
  72. public $passedArgs = array();
  73. /**
  74. * An array of names of built-in helpers to include.
  75. *
  76. * @var mixed A single name as a string or a list of names as an array.
  77. */
  78. public $helpers = array('Html');
  79. /**
  80. * Path to View.
  81. *
  82. * @var string Path to View
  83. */
  84. public $viewPath = null;
  85. /**
  86. * Variables for the view
  87. *
  88. * @var array
  89. */
  90. public $viewVars = array();
  91. /**
  92. * Name of view to use with this View.
  93. *
  94. * @var string
  95. */
  96. public $view = null;
  97. /**
  98. * Name of layout to use with this View.
  99. *
  100. * @var string
  101. */
  102. public $layout = 'default';
  103. /**
  104. * Path to Layout.
  105. *
  106. * @var string Path to Layout
  107. */
  108. public $layoutPath = null;
  109. /**
  110. * Turns on or off Cake's conventional mode of applying layout files. On by default.
  111. * Setting to off means that layouts will not be automatically applied to rendered views.
  112. *
  113. * @var boolean
  114. */
  115. public $autoLayout = true;
  116. /**
  117. * File extension. Defaults to Cake's template ".ctp".
  118. *
  119. * @var string
  120. */
  121. public $ext = '.ctp';
  122. /**
  123. * Sub-directory for this view file. This is often used for extension based routing.
  124. * for example with an `xml` extension, $subDir would be `xml/`
  125. *
  126. * @var string
  127. */
  128. public $subDir = null;
  129. /**
  130. * Theme name. If you are using themes, you should remember to use ThemeView as well.
  131. *
  132. * @var string
  133. */
  134. public $theme = null;
  135. /**
  136. * Used to define methods a controller that will be cached.
  137. *
  138. * @see Controller::$cacheAction
  139. * @var mixed
  140. */
  141. public $cacheAction = false;
  142. /**
  143. * holds current errors for the model validation
  144. *
  145. * @var array
  146. */
  147. public $validationErrors = array();
  148. /**
  149. * True when the view has been rendered.
  150. *
  151. * @var boolean
  152. */
  153. public $hasRendered = false;
  154. /**
  155. * List of generated DOM UUIDs
  156. *
  157. * @var array
  158. */
  159. public $uuids = array();
  160. /**
  161. * Holds View output.
  162. *
  163. * @var string
  164. */
  165. public $output = false;
  166. /**
  167. * An instance of a CakeRequest object that contains information about the current request.
  168. * This object contains all the information about a request and several methods for reading
  169. * additional information about the request.
  170. *
  171. * @var CakeRequest
  172. */
  173. public $request;
  174. /**
  175. * The Cache configuration View will use to store cached elements. Changing this will change
  176. * the default configuration elements are stored under. You can also choose a cache config
  177. * per element.
  178. *
  179. * @var string
  180. * @see View::element()
  181. */
  182. public $elementCache = 'default';
  183. /**
  184. * List of variables to collect from the associated controller
  185. *
  186. * @var array
  187. */
  188. protected $_passedVars = array(
  189. 'viewVars', 'autoLayout', 'ext', 'helpers', 'view', 'layout', 'name',
  190. 'layoutPath', 'viewPath', 'request', 'plugin', 'passedArgs', 'cacheAction'
  191. );
  192. /**
  193. * Scripts (and/or other <head /> tags) for the layout
  194. *
  195. * @var array
  196. */
  197. protected $_scripts = array();
  198. /**
  199. * Holds an array of paths.
  200. *
  201. * @var array
  202. */
  203. protected $_paths = array();
  204. /**
  205. * boolean to indicate that helpers have been loaded.
  206. *
  207. * @var boolean
  208. */
  209. protected $_helpersLoaded = false;
  210. /**
  211. * Constructor
  212. *
  213. * @param Controller $controller A controller object to pull View::__passedArgs from.
  214. */
  215. public function __construct($controller) {
  216. if (is_object($controller)) {
  217. $count = count($this->_passedVars);
  218. for ($j = 0; $j < $count; $j++) {
  219. $var = $this->_passedVars[$j];
  220. $this->{$var} = $controller->{$var};
  221. }
  222. }
  223. $this->Helpers = new HelperCollection($this);
  224. parent::__construct();
  225. }
  226. /**
  227. * Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
  228. *
  229. * This realizes the concept of Elements, (or "partial layouts") and the $params array is used to send
  230. * data to be used in the element. Elements can be cached improving performance by using the `cache` option.
  231. *
  232. * @param string $name Name of template file in the/app/View/Elements/ folder
  233. * @param array $data Array of data to be made available to the rendered view (i.e. the Element)
  234. * @param array $options Array of options. Possible keys are:
  235. * - `cache` - Can either be `true`, to enable caching using the config in View::$elementCache. Or an array
  236. * If an array, the following keys can be used:
  237. * - `config` - Used to store the cached element in a custom cache configuration.
  238. * - `key` - Used to define the key used in the Cache::write(). It will be prefixed with `element_`
  239. * - `plugin` - Load an element from a specific plugin.
  240. * - `callbacks` - Set to true to fire beforeRender and afterRender helper callbacks for this element.
  241. * Defaults to false.
  242. * @return string Rendered Element
  243. */
  244. public function element($name, $data = array(), $options = array()) {
  245. $file = $plugin = $key = null;
  246. $callbacks = false;
  247. if (isset($options['plugin'])) {
  248. $plugin = Inflector::camelize($options['plugin']);
  249. }
  250. if (isset($this->plugin) && !$plugin) {
  251. $plugin = $this->plugin;
  252. }
  253. if (isset($options['callbacks'])) {
  254. $callbacks = $options['callbacks'];
  255. }
  256. if (isset($options['cache'])) {
  257. $underscored = null;
  258. if ($plugin) {
  259. $underscored = Inflector::underscore($plugin);
  260. }
  261. $keys = array_merge(array($underscored, $name), array_keys($options), array_keys($data));
  262. $caching = array(
  263. 'config' => $this->elementCache,
  264. 'key' => implode('_', $keys)
  265. );
  266. if (is_array($options['cache'])) {
  267. $defaults = array(
  268. 'config' => $this->elementCache,
  269. 'key' => $caching['key']
  270. );
  271. $caching = array_merge($defaults, $options['cache']);
  272. }
  273. $key = 'element_' . $caching['key'];
  274. $contents = Cache::read($key, $caching['config']);
  275. if ($contents !== false) {
  276. return $contents;
  277. }
  278. }
  279. $file = $this->_getElementFilename($name, $plugin);
  280. if ($file) {
  281. if (!$this->_helpersLoaded) {
  282. $this->loadHelpers();
  283. }
  284. if ($callbacks) {
  285. $this->Helpers->trigger('beforeRender', array($file));
  286. }
  287. $element = $this->_render($file, array_merge($this->viewVars, $data));
  288. if ($callbacks) {
  289. $this->Helpers->trigger('afterRender', array($file, $element));
  290. }
  291. if (isset($options['cache'])) {
  292. Cache::write($key, $element, $caching['config']);
  293. }
  294. return $element;
  295. }
  296. $file = 'Elements' . DS . $name . $this->ext;
  297. if (Configure::read('debug') > 0) {
  298. return "Element Not Found: " . $file;
  299. }
  300. }
  301. /**
  302. * Renders view for given view file and layout.
  303. *
  304. * Render triggers helper callbacks, which are fired before and after the view are rendered,
  305. * as well as before and after the layout. The helper callbacks are called
  306. *
  307. * - `beforeRender`
  308. * - `afterRender`
  309. * - `beforeLayout`
  310. * - `afterLayout`
  311. *
  312. * If View::$autoRender is false and no `$layout` is provided, the view will be returned bare.
  313. *
  314. * @param string $view Name of view file to use
  315. * @param string $layout Layout to use.
  316. * @return string Rendered Element
  317. * @throws CakeException if there is an error in the view.
  318. */
  319. public function render($view = null, $layout = null) {
  320. if ($this->hasRendered) {
  321. return true;
  322. }
  323. if (!$this->_helpersLoaded) {
  324. $this->loadHelpers();
  325. }
  326. $this->output = null;
  327. if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
  328. $this->Helpers->trigger('beforeRender', array($viewFileName));
  329. $this->output = $this->_render($viewFileName);
  330. $this->Helpers->trigger('afterRender', array($viewFileName));
  331. }
  332. if ($layout === null) {
  333. $layout = $this->layout;
  334. }
  335. if ($this->output === false) {
  336. throw new CakeException(__d('cake_dev', "Error in view %s, got no content.", $viewFileName));
  337. }
  338. if ($layout && $this->autoLayout) {
  339. $this->output = $this->renderLayout($this->output, $layout);
  340. }
  341. $this->hasRendered = true;
  342. return $this->output;
  343. }
  344. /**
  345. * Renders a layout. Returns output from _render(). Returns false on error.
  346. * Several variables are created for use in layout.
  347. *
  348. * - `title_for_layout` - A backwards compatible place holder, you should set this value if you want more control.
  349. * - `content_for_layout` - contains rendered view file
  350. * - `scripts_for_layout` - contains scripts added to header
  351. *
  352. * @param string $content_for_layout Content to render in a view, wrapped by the surrounding layout.
  353. * @param string $layout Layout name
  354. * @return mixed Rendered output, or false on error
  355. * @throws CakeException if there is an error in the view.
  356. */
  357. public function renderLayout($content_for_layout, $layout = null) {
  358. $layoutFileName = $this->_getLayoutFileName($layout);
  359. if (empty($layoutFileName)) {
  360. return $this->output;
  361. }
  362. if (!$this->_helpersLoaded) {
  363. $this->loadHelpers();
  364. }
  365. $this->Helpers->trigger('beforeLayout', array($layoutFileName));
  366. $this->viewVars = array_merge($this->viewVars, array(
  367. 'content_for_layout' => $content_for_layout,
  368. 'scripts_for_layout' => implode("\n\t", $this->_scripts),
  369. ));
  370. if (!isset($this->viewVars['title_for_layout'])) {
  371. $this->viewVars['title_for_layout'] = Inflector::humanize($this->viewPath);
  372. }
  373. $this->output = $this->_render($layoutFileName);
  374. if ($this->output === false) {
  375. throw new CakeException(__d('cake_dev', "Error in layout %s, got no content.", $layoutFileName));
  376. }
  377. $this->Helpers->trigger('afterLayout', array($layoutFileName));
  378. return $this->output;
  379. }
  380. /**
  381. * Render cached view. Works in concert with CacheHelper and Dispatcher to
  382. * render cached view files.
  383. *
  384. * @param string $filename the cache file to include
  385. * @param string $timeStart the page render start time
  386. * @return boolean Success of rendering the cached file.
  387. */
  388. public function renderCache($filename, $timeStart) {
  389. ob_start();
  390. include ($filename);
  391. if (Configure::read('debug') > 0 && $this->layout != 'xml') {
  392. echo "<!-- Cached Render Time: " . round(microtime(true) - $timeStart, 4) . "s -->";
  393. }
  394. $out = ob_get_clean();
  395. if (preg_match('/^<!--cachetime:(\\d+)-->/', $out, $match)) {
  396. if (time() >= $match['1']) {
  397. @unlink($filename);
  398. unset ($out);
  399. return false;
  400. } else {
  401. if ($this->layout === 'xml') {
  402. header('Content-type: text/xml');
  403. }
  404. $commentLength = strlen('<!--cachetime:' . $match['1'] . '-->');
  405. echo substr($out, $commentLength);
  406. return true;
  407. }
  408. }
  409. }
  410. /**
  411. * Returns a list of variables available in the current View context
  412. *
  413. * @return array Array of the set view variable names.
  414. */
  415. public function getVars() {
  416. return array_keys($this->viewVars);
  417. }
  418. /**
  419. * Returns the contents of the given View variable(s)
  420. *
  421. * @param string $var The view var you want the contents of.
  422. * @return mixed The content of the named var if its set, otherwise null.
  423. */
  424. public function getVar($var) {
  425. if (!isset($this->viewVars[$var])) {
  426. return null;
  427. } else {
  428. return $this->viewVars[$var];
  429. }
  430. }
  431. /**
  432. * Adds a script block or other element to be inserted in $scripts_for_layout in
  433. * the `<head />` of a document layout
  434. *
  435. * @param string $name Either the key name for the script, or the script content. Name can be used to
  436. * update/replace a script element.
  437. * @param string $content The content of the script being added, optional.
  438. * @return void
  439. */
  440. public function addScript($name, $content = null) {
  441. if (empty($content)) {
  442. if (!in_array($name, array_values($this->_scripts))) {
  443. $this->_scripts[] = $name;
  444. }
  445. } else {
  446. $this->_scripts[$name] = $content;
  447. }
  448. }
  449. /**
  450. * Generates a unique, non-random DOM ID for an object, based on the object type and the target URL.
  451. *
  452. * @param string $object Type of object, i.e. 'form' or 'link'
  453. * @param string $url The object's target URL
  454. * @return string
  455. */
  456. public function uuid($object, $url) {
  457. $c = 1;
  458. $url = Router::url($url);
  459. $hash = $object . substr(md5($object . $url), 0, 10);
  460. while (in_array($hash, $this->uuids)) {
  461. $hash = $object . substr(md5($object . $url . $c), 0, 10);
  462. $c++;
  463. }
  464. $this->uuids[] = $hash;
  465. return $hash;
  466. }
  467. /**
  468. * Allows a template or element to set a variable that will be available in
  469. * a layout or other element. Analagous to Controller::set().
  470. *
  471. * @param mixed $one A string or an array of data.
  472. * @param mixed $two Value in case $one is a string (which then works as the key).
  473. * Unused if $one is an associative array, otherwise serves as the values to $one's keys.
  474. * @return void
  475. */
  476. public function set($one, $two = null) {
  477. $data = null;
  478. if (is_array($one)) {
  479. if (is_array($two)) {
  480. $data = array_combine($one, $two);
  481. } else {
  482. $data = $one;
  483. }
  484. } else {
  485. $data = array($one => $two);
  486. }
  487. if ($data == null) {
  488. return false;
  489. }
  490. $this->viewVars = $data + $this->viewVars;
  491. }
  492. /**
  493. * Magic accessor for helpers. Provides access to attributes that were deprecated.
  494. *
  495. * @param string $name Name of the attribute to get.
  496. * @return mixed
  497. */
  498. public function __get($name) {
  499. if (isset($this->Helpers->{$name})) {
  500. return $this->Helpers->{$name};
  501. }
  502. switch ($name) {
  503. case 'base':
  504. case 'here':
  505. case 'webroot':
  506. case 'data':
  507. return $this->request->{$name};
  508. case 'action':
  509. return isset($this->request->params['action']) ? $this->request->params['action'] : '';
  510. case 'params':
  511. return $this->request;
  512. }
  513. return null;
  514. }
  515. /**
  516. * Interact with the HelperCollection to load all the helpers.
  517. *
  518. * @return void
  519. */
  520. public function loadHelpers() {
  521. $helpers = HelperCollection::normalizeObjectArray($this->helpers);
  522. foreach ($helpers as $name => $properties) {
  523. list($plugin, $class) = pluginSplit($properties['class']);
  524. $this->{$class} = $this->Helpers->load($properties['class'], $properties['settings']);
  525. }
  526. $this->_helpersLoaded = true;
  527. }
  528. /**
  529. * Renders and returns output for given view filename with its
  530. * array of data.
  531. *
  532. * @param string $___viewFn Filename of the view
  533. * @param array $___dataForView Data to include in rendered view. If empty the current View::$viewVars will be used.
  534. * @return string Rendered output
  535. */
  536. protected function _render($___viewFn, $___dataForView = array()) {
  537. if (empty($___dataForView)) {
  538. $___dataForView = $this->viewVars;
  539. }
  540. extract($___dataForView, EXTR_SKIP);
  541. ob_start();
  542. include $___viewFn;
  543. return ob_get_clean();
  544. }
  545. /**
  546. * Loads a helper. Delegates to the `HelperCollection::load()` to load the helper
  547. *
  548. * @param string $helperName Name of the helper to load.
  549. * @param array $settings Settings for the helper
  550. * @return Helper a constructed helper object.
  551. * @see HelperCollection::load()
  552. */
  553. public function loadHelper($helperName, $settings = array()) {
  554. return $this->Helpers->load($helperName, $settings);
  555. }
  556. /**
  557. * Returns filename of given action's template file (.ctp) as a string.
  558. * CamelCased action names will be under_scored! This means that you can have
  559. * LongActionNames that refer to long_action_names.ctp views.
  560. *
  561. * @param string $name Controller action to find template filename for
  562. * @return string Template filename
  563. * @throws MissingViewException when a view file could not be found.
  564. */
  565. protected function _getViewFileName($name = null) {
  566. $subDir = null;
  567. if (!is_null($this->subDir)) {
  568. $subDir = $this->subDir . DS;
  569. }
  570. if ($name === null) {
  571. $name = $this->view;
  572. }
  573. $name = str_replace('/', DS, $name);
  574. if (strpos($name, DS) === false && $name[0] !== '.') {
  575. $name = $this->viewPath . DS . $subDir . Inflector::underscore($name);
  576. } elseif (strpos($name, DS) !== false) {
  577. if ($name[0] === DS || $name[1] === ':') {
  578. if (is_file($name)) {
  579. return $name;
  580. }
  581. $name = trim($name, DS);
  582. } else if ($name[0] === '.') {
  583. $name = substr($name, 3);
  584. } else {
  585. $name = $this->viewPath . DS . $subDir . $name;
  586. }
  587. }
  588. $paths = $this->_paths($this->plugin);
  589. $exts = $this->_getExtensions();
  590. foreach ($exts as $ext) {
  591. foreach ($paths as $path) {
  592. if (file_exists($path . $name . $ext)) {
  593. return $path . $name . $ext;
  594. }
  595. }
  596. }
  597. $defaultPath = $paths[0];
  598. if ($this->plugin) {
  599. $pluginPaths = App::path('plugins');
  600. foreach ($paths as $path) {
  601. if (strpos($path, $pluginPaths[0]) === 0) {
  602. $defaultPath = $path;
  603. break;
  604. }
  605. }
  606. }
  607. throw new MissingViewException(array('file' => $defaultPath . $name . $this->ext));
  608. }
  609. /**
  610. * Returns layout filename for this template as a string.
  611. *
  612. * @param string $name The name of the layout to find.
  613. * @return string Filename for layout file (.ctp).
  614. * @throws MissingLayoutException when a layout cannot be located
  615. */
  616. protected function _getLayoutFileName($name = null) {
  617. if ($name === null) {
  618. $name = $this->layout;
  619. }
  620. $subDir = null;
  621. if (!is_null($this->layoutPath)) {
  622. $subDir = $this->layoutPath . DS;
  623. }
  624. $paths = $this->_paths($this->plugin);
  625. $file = 'Layouts' . DS . $subDir . $name;
  626. $exts = $this->_getExtensions();
  627. foreach ($exts as $ext) {
  628. foreach ($paths as $path) {
  629. if (file_exists($path . $file . $ext)) {
  630. return $path . $file . $ext;
  631. }
  632. }
  633. }
  634. throw new MissingLayoutException(array('file' => $paths[0] . $file . $this->ext));
  635. }
  636. /**
  637. * Get the extensions that view files can use.
  638. *
  639. * @return array Array of extensions view files use.
  640. */
  641. protected function _getExtensions() {
  642. $exts = array($this->ext);
  643. if ($this->ext !== '.ctp') {
  644. array_push($exts, '.ctp');
  645. }
  646. return $exts;
  647. }
  648. /**
  649. * Finds an element filename, returns false on failure.
  650. *
  651. * @param string $name The name of the element to find.
  652. * @param string $plugin The plugin name the element is in.
  653. * @return mixed Either a string to the element filename or false when one can't be found.
  654. */
  655. protected function _getElementFileName($name, $plugin = null) {
  656. $paths = $this->_paths($plugin);
  657. $exts = $this->_getExtensions();
  658. foreach ($exts as $ext) {
  659. foreach ($paths as $path) {
  660. if (file_exists($path . 'Elements' . DS . $name . $ext)) {
  661. return $path . 'Elements' . DS . $name . $ext;
  662. }
  663. }
  664. }
  665. return false;
  666. }
  667. /**
  668. * Return all possible paths to find view files in order
  669. *
  670. * @param string $plugin Optional plugin name to scan for view files.
  671. * @param boolean $cached Set to true to force a refresh of view paths.
  672. * @return array paths
  673. */
  674. protected function _paths($plugin = null, $cached = true) {
  675. if ($plugin === null && $cached === true && !empty($this->_paths)) {
  676. return $this->_paths;
  677. }
  678. $paths = array();
  679. $viewPaths = App::path('View');
  680. $corePaths = array_flip(App::core('View'));
  681. if (!empty($plugin)) {
  682. $count = count($viewPaths);
  683. for ($i = 0; $i < $count; $i++) {
  684. if (!isset($corePaths[$viewPaths[$i]])) {
  685. $paths[] = $viewPaths[$i] . 'Plugins' . DS . $plugin . DS;
  686. }
  687. }
  688. $paths = array_merge($paths, App::path('View', $plugin));
  689. }
  690. $this->_paths = array_unique(array_merge($paths, $viewPaths, array_keys($corePaths)));
  691. return $this->_paths;
  692. }
  693. }