PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/devblocks/libs/ZendFramework/Zend/View/Abstract.php

https://github.com/sluther/portsensor
PHP | 1042 lines | 535 code | 85 blank | 422 comment | 53 complexity | eb25bf10ab7eb350dd2e3070479416eb MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Zend
  22. */
  23. require_once 'Zend/Loader.php';
  24. /**
  25. * Zend_View_Interface
  26. */
  27. require_once 'Zend/View/Interface.php';
  28. /**
  29. * Abstract class for Zend_View to help enforce private constructs.
  30. *
  31. * @category Zend
  32. * @package Zend_View
  33. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. abstract class Zend_View_Abstract implements Zend_View_Interface
  37. {
  38. /**
  39. * Path stack for script, helper, and filter directories.
  40. *
  41. * @var array
  42. */
  43. private $_path = array(
  44. 'script' => array(),
  45. 'helper' => array(),
  46. 'filter' => array(),
  47. );
  48. /**
  49. * Script file name to execute
  50. *
  51. * @var string
  52. */
  53. private $_file = null;
  54. /**
  55. * Instances of helper objects.
  56. *
  57. * @var array
  58. */
  59. private $_helper = array();
  60. /**
  61. * Map of helper => class pairs to help in determining helper class from
  62. * name
  63. * @var array
  64. */
  65. private $_helperLoaded = array();
  66. /**
  67. * Map of helper => classfile pairs to aid in determining helper classfile
  68. * @var array
  69. */
  70. private $_helperLoadedDir = array();
  71. /**
  72. * Stack of Zend_View_Filter names to apply as filters.
  73. * @var array
  74. */
  75. private $_filter = array();
  76. /**
  77. * Stack of Zend_View_Filter objects that have been loaded
  78. * @var array
  79. */
  80. private $_filterClass = array();
  81. /**
  82. * Map of filter => class pairs to help in determining filter class from
  83. * name
  84. * @var array
  85. */
  86. private $_filterLoaded = array();
  87. /**
  88. * Map of filter => classfile pairs to aid in determining filter classfile
  89. * @var array
  90. */
  91. private $_filterLoadedDir = array();
  92. /**
  93. * Callback for escaping.
  94. *
  95. * @var string
  96. */
  97. private $_escape = 'htmlspecialchars';
  98. /**
  99. * Encoding to use in escaping mechanisms; defaults to latin1 (ISO-8859-1)
  100. * @var string
  101. */
  102. private $_encoding = 'ISO-8859-1';
  103. /**
  104. * Strict variables flag; when on, undefined variables accessed in the view
  105. * scripts will trigger notices
  106. * @var boolean
  107. */
  108. private $_strictVars = false;
  109. /**
  110. * Constructor.
  111. *
  112. * @param array $config Configuration key-value pairs.
  113. */
  114. public function __construct($config = array())
  115. {
  116. // set inital paths and properties
  117. $this->setScriptPath(null);
  118. $this->setHelperPath(null);
  119. $this->setFilterPath(null);
  120. // user-defined escaping callback
  121. if (array_key_exists('escape', $config)) {
  122. $this->setEscape($config['escape']);
  123. }
  124. // encoding
  125. if (array_key_exists('encoding', $config)) {
  126. $this->setEncoding($config['encoding']);
  127. }
  128. // base path
  129. if (array_key_exists('basePath', $config)) {
  130. $prefix = 'Zend_View';
  131. if (array_key_exists('basePathPrefix', $config)) {
  132. $prefix = $config['basePathPrefix'];
  133. }
  134. $this->setBasePath($config['basePath'], $prefix);
  135. }
  136. // user-defined view script path
  137. if (array_key_exists('scriptPath', $config)) {
  138. $this->addScriptPath($config['scriptPath']);
  139. }
  140. // user-defined helper path
  141. if (array_key_exists('helperPath', $config)) {
  142. $prefix = 'Zend_View_Helper';
  143. if (array_key_exists('helperPathPrefix', $config)) {
  144. $prefix = $config['helperPathPrefix'];
  145. }
  146. $this->addHelperPath($config['helperPath'], $prefix);
  147. }
  148. // user-defined filter path
  149. if (array_key_exists('filterPath', $config)) {
  150. $prefix = 'Zend_View_Filter';
  151. if (array_key_exists('filterPathPrefix', $config)) {
  152. $prefix = $config['filterPathPrefix'];
  153. }
  154. $this->addFilterPath($config['filterPath'], $prefix);
  155. }
  156. // user-defined filters
  157. if (array_key_exists('filter', $config)) {
  158. $this->addFilter($config['filter']);
  159. }
  160. // strict vars
  161. if (array_key_exists('strictVars', $config)) {
  162. $this->strictVars($config['strictVars']);
  163. }
  164. $this->init();
  165. }
  166. /**
  167. * Return the template engine object
  168. *
  169. * Returns the object instance, as it is its own template engine
  170. *
  171. * @return Zend_View_Abstract
  172. */
  173. public function getEngine()
  174. {
  175. return $this;
  176. }
  177. /**
  178. * Allow custom object initialization when extending Zend_View_Abstract or
  179. * Zend_View
  180. *
  181. * Triggered by {@link __construct() the constructor} as its final action.
  182. *
  183. * @return void
  184. */
  185. public function init()
  186. {
  187. }
  188. /**
  189. * Prevent E_NOTICE for nonexistent values
  190. *
  191. * If {@link strictVars()} is on, raises a notice.
  192. *
  193. * @param string $key
  194. * @return null
  195. */
  196. public function __get($key)
  197. {
  198. if ($this->_strictVars) {
  199. trigger_error('Key "' . $key . '" does not exist', E_USER_NOTICE);
  200. }
  201. return null;
  202. }
  203. /**
  204. * Allows testing with empty() and isset() to work inside
  205. * templates.
  206. *
  207. * @param string $key
  208. * @return boolean
  209. */
  210. public function __isset($key)
  211. {
  212. if ('_' != substr($key, 0, 1)) {
  213. return isset($this->$key);
  214. }
  215. return false;
  216. }
  217. /**
  218. * Directly assigns a variable to the view script.
  219. *
  220. * Checks first to ensure that the caller is not attempting to set a
  221. * protected or private member (by checking for a prefixed underscore); if
  222. * not, the public member is set; otherwise, an exception is raised.
  223. *
  224. * @param string $key The variable name.
  225. * @param mixed $val The variable value.
  226. * @return void
  227. * @throws Zend_View_Exception if an attempt to set a private or protected
  228. * member is detected
  229. */
  230. public function __set($key, $val)
  231. {
  232. if ('_' != substr($key, 0, 1)) {
  233. $this->$key = $val;
  234. return;
  235. }
  236. require_once 'Zend/View/Exception.php';
  237. throw new Zend_View_Exception('Setting private or protected class members is not allowed', $this);
  238. }
  239. /**
  240. * Allows unset() on object properties to work
  241. *
  242. * @param string $key
  243. * @return void
  244. */
  245. public function __unset($key)
  246. {
  247. if ('_' != substr($key, 0, 1) && isset($this->$key)) {
  248. unset($this->$key);
  249. }
  250. }
  251. /**
  252. * Accesses a helper object from within a script.
  253. *
  254. * If the helper class has a 'view' property, sets it with the current view
  255. * object.
  256. *
  257. * @param string $name The helper name.
  258. * @param array $args The parameters for the helper.
  259. * @return string The result of the helper output.
  260. */
  261. public function __call($name, $args)
  262. {
  263. // is the helper already loaded?
  264. $helper = $this->getHelper($name);
  265. // call the helper method
  266. return call_user_func_array(
  267. array($helper, $name),
  268. $args
  269. );
  270. }
  271. /**
  272. * Given a base path, sets the script, helper, and filter paths relative to it
  273. *
  274. * Assumes a directory structure of:
  275. * <code>
  276. * basePath/
  277. * scripts/
  278. * helpers/
  279. * filters/
  280. * </code>
  281. *
  282. * @param string $path
  283. * @param string $prefix Prefix to use for helper and filter paths
  284. * @return Zend_View_Abstract
  285. */
  286. public function setBasePath($path, $classPrefix = 'Zend_View')
  287. {
  288. $path = rtrim($path, '/');
  289. $path = rtrim($path, '\\');
  290. $path .= DIRECTORY_SEPARATOR;
  291. $classPrefix = rtrim($classPrefix, '_') . '_';
  292. $this->setScriptPath($path . 'scripts');
  293. $this->setHelperPath($path . 'helpers', $classPrefix . 'Helper');
  294. $this->setFilterPath($path . 'filters', $classPrefix . 'Filter');
  295. return $this;
  296. }
  297. /**
  298. * Given a base path, add script, helper, and filter paths relative to it
  299. *
  300. * Assumes a directory structure of:
  301. * <code>
  302. * basePath/
  303. * scripts/
  304. * helpers/
  305. * filters/
  306. * </code>
  307. *
  308. * @param string $path
  309. * @param string $prefix Prefix to use for helper and filter paths
  310. * @return Zend_View_Abstract
  311. */
  312. public function addBasePath($path, $classPrefix = 'Zend_View')
  313. {
  314. $path = rtrim($path, '/');
  315. $path = rtrim($path, '\\');
  316. $path .= DIRECTORY_SEPARATOR;
  317. $classPrefix = rtrim($classPrefix, '_') . '_';
  318. $this->addScriptPath($path . 'scripts');
  319. $this->addHelperPath($path . 'helpers', $classPrefix . 'Helper');
  320. $this->addFilterPath($path . 'filters', $classPrefix . 'Filter');
  321. return $this;
  322. }
  323. /**
  324. * Adds to the stack of view script paths in LIFO order.
  325. *
  326. * @param string|array The directory (-ies) to add.
  327. * @return Zend_View_Abstract
  328. */
  329. public function addScriptPath($path)
  330. {
  331. $this->_addPath('script', $path);
  332. return $this;
  333. }
  334. /**
  335. * Resets the stack of view script paths.
  336. *
  337. * To clear all paths, use Zend_View::setScriptPath(null).
  338. *
  339. * @param string|array The directory (-ies) to set as the path.
  340. * @return Zend_View_Abstract
  341. */
  342. public function setScriptPath($path)
  343. {
  344. $this->_path['script'] = array();
  345. $this->_addPath('script', $path);
  346. return $this;
  347. }
  348. /**
  349. * Return full path to a view script specified by $name
  350. *
  351. * @param string $name
  352. * @return false|string False if script not found
  353. * @throws Zend_View_Exception if no script directory set
  354. */
  355. public function getScriptPath($name)
  356. {
  357. try {
  358. $path = $this->_script($name);
  359. return $path;
  360. } catch (Zend_View_Exception $e) {
  361. if (strstr($e->getMessage(), 'no view script directory set')) {
  362. throw $e;
  363. }
  364. return false;
  365. }
  366. }
  367. /**
  368. * Returns an array of all currently set script paths
  369. *
  370. * @return array
  371. */
  372. public function getScriptPaths()
  373. {
  374. return $this->_getPaths('script');
  375. }
  376. /**
  377. * Adds to the stack of helper paths in LIFO order.
  378. *
  379. * @param string|array The directory (-ies) to add.
  380. * @param string $classPrefix Class prefix to use with classes in this
  381. * directory; defaults to Zend_View_Helper
  382. * @return Zend_View_Abstract
  383. */
  384. public function addHelperPath($path, $classPrefix = 'Zend_View_Helper_')
  385. {
  386. if (!empty($classPrefix) && ('_' != substr($classPrefix, -1))) {
  387. $classPrefix .= '_';
  388. }
  389. $this->_addPath('helper', $path, $classPrefix);
  390. return $this;
  391. }
  392. /**
  393. * Resets the stack of helper paths.
  394. *
  395. * To clear all paths, use Zend_View::setHelperPath(null).
  396. *
  397. * @param string|array $path The directory (-ies) to set as the path.
  398. * @param string $classPrefix The class prefix to apply to all elements in
  399. * $path; defaults to Zend_View_Helper
  400. * @return Zend_View_Abstract
  401. */
  402. public function setHelperPath($path, $classPrefix = 'Zend_View_Helper_')
  403. {
  404. if (!empty($classPrefix) && ('_' != substr($classPrefix, -1))) {
  405. $classPrefix .= '_';
  406. }
  407. $this->_setPath('helper', $path, $classPrefix);
  408. return $this;
  409. }
  410. /**
  411. * Get full path to a helper class file specified by $name
  412. *
  413. * @param string $name
  414. * @return string|false False on failure, path on success
  415. */
  416. public function getHelperPath($name)
  417. {
  418. if (isset($this->_helperLoadedDir[$name])) {
  419. return $this->_helperLoadedDir[$name];
  420. }
  421. try {
  422. $this->_loadClass('helper', $name);
  423. return $this->_helperLoadedDir[$name];
  424. } catch (Zend_View_Exception $e) {
  425. return false;
  426. }
  427. }
  428. /**
  429. * Returns an array of all currently set helper paths
  430. *
  431. * @return array
  432. */
  433. public function getHelperPaths()
  434. {
  435. return $this->_getPaths('helper');
  436. }
  437. /**
  438. * Get a helper by name
  439. *
  440. * @param string $name
  441. * @return object
  442. */
  443. public function getHelper($name)
  444. {
  445. $name = ucfirst($name);
  446. if (!isset($this->_helper[$name])) {
  447. $class = $this->_loadClass('helper', $name);
  448. $this->_helper[$name] = new $class();
  449. if (method_exists($this->_helper[$name], 'setView')) {
  450. $this->_helper[$name]->setView($this);
  451. }
  452. }
  453. return $this->_helper[$name];
  454. }
  455. /**
  456. * Get array of all active helpers
  457. *
  458. * Only returns those that have already been instantiated.
  459. *
  460. * @return array
  461. */
  462. public function getHelpers()
  463. {
  464. return $this->_helper;
  465. }
  466. /**
  467. * Adds to the stack of filter paths in LIFO order.
  468. *
  469. * @param string|array The directory (-ies) to add.
  470. * @param string $classPrefix Class prefix to use with classes in this
  471. * directory; defaults to Zend_View_Filter
  472. * @return Zend_View_Abstract
  473. */
  474. public function addFilterPath($path, $classPrefix = 'Zend_View_Filter_')
  475. {
  476. if (!empty($classPrefix) && ('_' != substr($classPrefix, -1))) {
  477. $classPrefix .= '_';
  478. }
  479. $this->_addPath('filter', $path, $classPrefix);
  480. return $this;
  481. }
  482. /**
  483. * Resets the stack of filter paths.
  484. *
  485. * To clear all paths, use Zend_View::setFilterPath(null).
  486. *
  487. * @param string|array The directory (-ies) to set as the path.
  488. * @param string $classPrefix The class prefix to apply to all elements in
  489. * $path; defaults to Zend_View_Filter
  490. * @return Zend_View_Abstract
  491. */
  492. public function setFilterPath($path, $classPrefix = 'Zend_View_Filter_')
  493. {
  494. if (!empty($classPrefix) && ('_' != substr($classPrefix, -1))) {
  495. $classPrefix .= '_';
  496. }
  497. $this->_setPath('filter', $path, $classPrefix);
  498. return $this;
  499. }
  500. /**
  501. * Get full path to a filter class file specified by $name
  502. *
  503. * @param string $name
  504. * @return string|false False on failure, path on success
  505. */
  506. public function getFilterPath($name)
  507. {
  508. if (isset($this->_filterLoadedDir[$name])) {
  509. return $this->_filterLoadedDir[$name];
  510. }
  511. try {
  512. $this->_loadClass('filter', $name);
  513. return $this->_filterLoadedDir[$name];
  514. } catch (Zend_View_Exception $e) {
  515. return false;
  516. }
  517. }
  518. /**
  519. * Get a filter object by name
  520. *
  521. * @param string $name
  522. * @return object
  523. */
  524. public function getFilter($name)
  525. {
  526. if (!isset($this->_filterClass[$name])) {
  527. $class = $this->_loadClass('filter', $name);
  528. $this->_filterClass[$name] = new $class();
  529. if (method_exists($this->_filterClass[$name], 'setView')) {
  530. $this->_filterClass[$name]->setView($this);
  531. }
  532. }
  533. return $this->_filterClass[$name];
  534. }
  535. /**
  536. * Return array of all currently active filters
  537. *
  538. * Only returns those that have already been instantiated.
  539. *
  540. * @return array
  541. */
  542. public function getFilters()
  543. {
  544. return $this->_filter;
  545. }
  546. /**
  547. * Returns an array of all currently set filter paths
  548. *
  549. * @return array
  550. */
  551. public function getFilterPaths()
  552. {
  553. return $this->_getPaths('filter');
  554. }
  555. /**
  556. * Return associative array of path types => paths
  557. *
  558. * @return array
  559. */
  560. public function getAllPaths()
  561. {
  562. return $this->_path;
  563. }
  564. /**
  565. * Add one or more filters to the stack in FIFO order.
  566. *
  567. * @param string|array One or more filters to add.
  568. * @return Zend_View_Abstract
  569. */
  570. public function addFilter($name)
  571. {
  572. foreach ((array) $name as $val) {
  573. $this->_filter[] = $val;
  574. }
  575. return $this;
  576. }
  577. /**
  578. * Resets the filter stack.
  579. *
  580. * To clear all filters, use Zend_View::setFilter(null).
  581. *
  582. * @param string|array One or more filters to set.
  583. * @return Zend_View_Abstract
  584. */
  585. public function setFilter($name)
  586. {
  587. $this->_filter = array();
  588. $this->addFilter($name);
  589. return $this;
  590. }
  591. /**
  592. * Sets the _escape() callback.
  593. *
  594. * @param mixed $spec The callback for _escape() to use.
  595. * @return Zend_View_Abstract
  596. */
  597. public function setEscape($spec)
  598. {
  599. $this->_escape = $spec;
  600. return $this;
  601. }
  602. /**
  603. * Assigns variables to the view script via differing strategies.
  604. *
  605. * Zend_View::assign('name', $value) assigns a variable called 'name'
  606. * with the corresponding $value.
  607. *
  608. * Zend_View::assign($array) assigns the array keys as variable
  609. * names (with the corresponding array values).
  610. *
  611. * @see __set()
  612. * @param string|array The assignment strategy to use.
  613. * @param mixed (Optional) If assigning a named variable, use this
  614. * as the value.
  615. * @return Zend_View_Abstract Fluent interface
  616. * @throws Zend_View_Exception if $spec is neither a string nor an array,
  617. * or if an attempt to set a private or protected member is detected
  618. */
  619. public function assign($spec, $value = null)
  620. {
  621. // which strategy to use?
  622. if (is_string($spec)) {
  623. // assign by name and value
  624. if ('_' == substr($spec, 0, 1)) {
  625. require_once 'Zend/View/Exception.php';
  626. throw new Zend_View_Exception('Setting private or protected class members is not allowed', $this);
  627. }
  628. $this->$spec = $value;
  629. } elseif (is_array($spec)) {
  630. // assign from associative array
  631. $error = false;
  632. foreach ($spec as $key => $val) {
  633. if ('_' == substr($key, 0, 1)) {
  634. $error = true;
  635. break;
  636. }
  637. $this->$key = $val;
  638. }
  639. if ($error) {
  640. require_once 'Zend/View/Exception.php';
  641. throw new Zend_View_Exception('Setting private or protected class members is not allowed', $this);
  642. }
  643. } else {
  644. require_once 'Zend/View/Exception.php';
  645. throw new Zend_View_Exception('assign() expects a string or array, received ' . gettype($spec), $this);
  646. }
  647. return $this;
  648. }
  649. /**
  650. * Return list of all assigned variables
  651. *
  652. * Returns all public properties of the object. Reflection is not used
  653. * here as testing reflection properties for visibility is buggy.
  654. *
  655. * @return array
  656. */
  657. public function getVars()
  658. {
  659. $vars = get_object_vars($this);
  660. foreach ($vars as $key => $value) {
  661. if ('_' == substr($key, 0, 1)) {
  662. unset($vars[$key]);
  663. }
  664. }
  665. return $vars;
  666. }
  667. /**
  668. * Clear all assigned variables
  669. *
  670. * Clears all variables assigned to Zend_View either via {@link assign()} or
  671. * property overloading ({@link __set()}).
  672. *
  673. * @return void
  674. */
  675. public function clearVars()
  676. {
  677. $vars = get_object_vars($this);
  678. foreach ($vars as $key => $value) {
  679. if ('_' != substr($key, 0, 1)) {
  680. unset($this->$key);
  681. }
  682. }
  683. }
  684. /**
  685. * Processes a view script and returns the output.
  686. *
  687. * @param string $name The script script name to process.
  688. * @return string The script output.
  689. */
  690. public function render($name)
  691. {
  692. // find the script file name using the parent private method
  693. $this->_file = $this->_script($name);
  694. unset($name); // remove $name from local scope
  695. ob_start();
  696. $this->_run($this->_file);
  697. return $this->_filter(ob_get_clean()); // filter output
  698. }
  699. /**
  700. * Escapes a value for output in a view script.
  701. *
  702. * If escaping mechanism is one of htmlspecialchars or htmlentities, uses
  703. * {@link $_encoding} setting.
  704. *
  705. * @param mixed $var The output to escape.
  706. * @return mixed The escaped value.
  707. */
  708. public function escape($var)
  709. {
  710. if (in_array($this->_escape, array('htmlspecialchars', 'htmlentities'))) {
  711. return call_user_func($this->_escape, $var, ENT_COMPAT, $this->_encoding);
  712. }
  713. return call_user_func($this->_escape, $var);
  714. }
  715. /**
  716. * Set encoding to use with htmlentities() and htmlspecialchars()
  717. *
  718. * @param string $encoding
  719. * @return Zend_View_Abstract
  720. */
  721. public function setEncoding($encoding)
  722. {
  723. $this->_encoding = $encoding;
  724. return $this;
  725. }
  726. /**
  727. * Return current escape encoding
  728. *
  729. * @return string
  730. */
  731. public function getEncoding()
  732. {
  733. return $this->_encoding;
  734. }
  735. /**
  736. * Enable or disable strict vars
  737. *
  738. * If strict variables are enabled, {@link __get()} will raise a notice
  739. * when a variable is not defined.
  740. *
  741. * Use in conjunction with {@link Zend_View_Helper_DeclareVars the declareVars() helper}
  742. * to enforce strict variable handling in your view scripts.
  743. *
  744. * @param boolean $flag
  745. * @return Zend_View_Abstract
  746. */
  747. public function strictVars($flag = true)
  748. {
  749. $this->_strictVars = ($flag) ? true : false;
  750. return $this;
  751. }
  752. /**
  753. * Finds a view script from the available directories.
  754. *
  755. * @param $name string The base name of the script.
  756. * @return void
  757. */
  758. protected function _script($name)
  759. {
  760. if (0 == count($this->_path['script'])) {
  761. require_once 'Zend/View/Exception.php';
  762. throw new Zend_View_Exception('no view script directory set; unable to determine location for view script',
  763. $this);
  764. }
  765. foreach ($this->_path['script'] as $dir) {
  766. if (is_readable($dir . $name)) {
  767. return $dir . $name;
  768. }
  769. }
  770. require_once 'Zend/View/Exception.php';
  771. $message = "script '$name' not found in path ("
  772. . implode(PATH_SEPARATOR, $this->_path['script'])
  773. . ")";
  774. throw new Zend_View_Exception($message, $this);
  775. }
  776. /**
  777. * Applies the filter callback to a buffer.
  778. *
  779. * @param string $buffer The buffer contents.
  780. * @return string The filtered buffer.
  781. */
  782. private function _filter($buffer)
  783. {
  784. // loop through each filter class
  785. foreach ($this->_filter as $name) {
  786. // load and apply the filter class
  787. $filter = $this->getFilter($name);
  788. $buffer = call_user_func(array($filter, 'filter'), $buffer);
  789. }
  790. // done!
  791. return $buffer;
  792. }
  793. /**
  794. * Adds paths to the path stack in LIFO order.
  795. *
  796. * Zend_View::_addPath($type, 'dirname') adds one directory
  797. * to the path stack.
  798. *
  799. * Zend_View::_addPath($type, $array) adds one directory for
  800. * each array element value.
  801. *
  802. * In the case of filter and helper paths, $prefix should be used to
  803. * specify what class prefix to use with the given path.
  804. *
  805. * @param string $type The path type ('script', 'helper', or 'filter').
  806. * @param string|array $path The path specification.
  807. * @param string $prefix Class prefix to use with path (helpers and filters
  808. * only)
  809. * @return void
  810. */
  811. private function _addPath($type, $path, $prefix = null)
  812. {
  813. foreach ((array) $path as $dir) {
  814. // attempt to strip any possible separator and
  815. // append the system directory separator
  816. $dir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $dir);
  817. $dir = rtrim($dir, DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR)
  818. . DIRECTORY_SEPARATOR;
  819. switch ($type) {
  820. case 'script':
  821. // add to the top of the stack.
  822. array_unshift($this->_path[$type], $dir);
  823. break;
  824. case 'filter':
  825. case 'helper':
  826. default:
  827. // add as array with prefix and dir keys
  828. array_unshift($this->_path[$type], array('prefix' => $prefix, 'dir' => $dir));
  829. break;
  830. }
  831. }
  832. }
  833. /**
  834. * Resets the path stack for helpers and filters.
  835. *
  836. * @param string $type The path type ('helper' or 'filter').
  837. * @param string|array $path The directory (-ies) to set as the path.
  838. * @param string $classPrefix Class prefix to apply to elements of $path
  839. */
  840. private function _setPath($type, $path, $classPrefix = null)
  841. {
  842. $dir = DIRECTORY_SEPARATOR . ucfirst($type) . DIRECTORY_SEPARATOR;
  843. switch ($type) {
  844. case 'script':
  845. $this->_path[$type] = array(dirname(__FILE__) . $dir);
  846. $this->_addPath($type, $path);
  847. break;
  848. case 'filter':
  849. case 'helper':
  850. default:
  851. $this->_path[$type] = array(array(
  852. 'prefix' => 'Zend_View_' . ucfirst($type) . '_',
  853. 'dir' => dirname(__FILE__) . $dir
  854. ));
  855. $this->_addPath($type, $path, $classPrefix);
  856. break;
  857. }
  858. }
  859. /**
  860. * Return all paths for a given path type
  861. *
  862. * @param string $type The path type ('helper', 'filter', 'script')
  863. * @return array
  864. */
  865. private function _getPaths($type)
  866. {
  867. return $this->_path[$type];
  868. }
  869. /**
  870. * Loads a helper or filter class.
  871. *
  872. * @param string $type The class type ('helper' or 'filter').
  873. * @param string $name The base name.
  874. * @param string The full class name.
  875. * @return string class name loaded
  876. * @throws Zend_View_Exception if unable to load class
  877. */
  878. private function _loadClass($type, $name)
  879. {
  880. // check to see if name => class mapping exists for helper/filter
  881. $classLoaded = '_' . $type . 'Loaded';
  882. $classAccess = '_set' . ucfirst($type) . 'Class';
  883. if (isset($this->$classLoaded[$name])) {
  884. return $this->$classLoaded[$name];
  885. }
  886. // only look for "$Name.php"
  887. $file = ucfirst($name) . '.php';
  888. // do LIFO search for helper
  889. foreach ($this->_path[$type] as $info) {
  890. $dir = $info['dir'];
  891. $prefix = $info['prefix'];
  892. $class = $prefix . ucfirst($name);
  893. if (class_exists($class, false)) {
  894. $reflection = new ReflectionClass($class);
  895. $file = $reflection->getFileName();
  896. $this->$classAccess($name, $class, $file);
  897. return $class;
  898. } elseif (Zend_Loader::isReadable($dir . $file)) {
  899. include_once $dir . $file;
  900. if (class_exists($class, false)) {
  901. $this->$classAccess($name, $class, $dir . $file);
  902. return $class;
  903. }
  904. }
  905. }
  906. require_once 'Zend/View/Exception.php';
  907. throw new Zend_View_Exception("$type '$name' not found in path", $this);
  908. }
  909. /**
  910. * Register helper class as loaded
  911. *
  912. * @param string $name
  913. * @param string $class
  914. * @param string $file path to class file
  915. * @return void
  916. */
  917. private function _setHelperClass($name, $class, $file)
  918. {
  919. $this->_helperLoadedDir[$name] = $file;
  920. $this->_helperLoaded[$name] = $class;
  921. }
  922. /**
  923. * Register filter class as loaded
  924. *
  925. * @param string $name
  926. * @param string $class
  927. * @param string $file path to class file
  928. * @return void
  929. */
  930. private function _setFilterClass($name, $class, $file)
  931. {
  932. $this->_filterLoadedDir[$name] = $file;
  933. $this->_filterLoaded[$name] = $class;
  934. }
  935. /**
  936. * Use to include the view script in a scope that only allows public
  937. * members.
  938. *
  939. * @return mixed
  940. */
  941. abstract protected function _run();
  942. }