2
0

ViewRenderer.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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_Controller
  17. * @subpackage Zend_Controller_Action_Helper
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Controller_Action_Helper_Abstract
  23. */
  24. require_once 'Zend/Controller/Action/Helper/Abstract.php';
  25. /**
  26. * @see Zend_View
  27. */
  28. require_once 'Zend/View.php';
  29. /**
  30. * View script integration
  31. *
  32. * Zend_Controller_Action_Helper_ViewRenderer provides transparent view
  33. * integration for action controllers. It allows you to create a view object
  34. * once, and populate it throughout all actions. Several global options may be
  35. * set:
  36. *
  37. * - noController: if set true, render() will not look for view scripts in
  38. * subdirectories named after the controller
  39. * - viewSuffix: what view script filename suffix to use
  40. *
  41. * The helper autoinitializes the action controller view preDispatch(). It
  42. * determines the path to the class file, and then determines the view base
  43. * directory from there. It also uses the module name as a class prefix for
  44. * helpers and views such that if your module name is 'Search', it will set the
  45. * helper class prefix to 'Search_View_Helper' and the filter class prefix to ;
  46. * 'Search_View_Filter'.
  47. *
  48. * Usage:
  49. * <code>
  50. * // In your bootstrap:
  51. * Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
  52. *
  53. * // In your action controller methods:
  54. * $viewHelper = $this->_helper->getHelper('view');
  55. *
  56. * // Don't use controller subdirectories
  57. * $viewHelper->setNoController(true);
  58. *
  59. * // Specify a different script to render:
  60. * $this->_helper->viewRenderer('form');
  61. *
  62. * </code>
  63. *
  64. * @uses Zend_Controller_Action_Helper_Abstract
  65. * @package Zend_Controller
  66. * @subpackage Zend_Controller_Action_Helper
  67. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  68. * @license http://framework.zend.com/license/new-bsd New BSD License
  69. */
  70. class Zend_Controller_Action_Helper_ViewRenderer extends Zend_Controller_Action_Helper_Abstract
  71. {
  72. /**
  73. * @var Zend_View_Interface
  74. */
  75. public $view;
  76. /**
  77. * Word delimiters
  78. * @var array
  79. */
  80. protected $_delimiters;
  81. /**
  82. * Front controller instance
  83. * @var Zend_Controller_Front
  84. */
  85. protected $_frontController;
  86. /**
  87. * @var Zend_Filter_Inflector
  88. */
  89. protected $_inflector;
  90. /**
  91. * Inflector target
  92. * @var string
  93. */
  94. protected $_inflectorTarget = '';
  95. /**
  96. * Current module directory
  97. * @var string
  98. */
  99. protected $_moduleDir = '';
  100. /**
  101. * Whether or not to autorender using controller name as subdirectory;
  102. * global setting (not reset at next invocation)
  103. * @var boolean
  104. */
  105. protected $_neverController = false;
  106. /**
  107. * Whether or not to autorender postDispatch; global setting (not reset at
  108. * next invocation)
  109. * @var boolean
  110. */
  111. protected $_neverRender = false;
  112. /**
  113. * Whether or not to use a controller name as a subdirectory when rendering
  114. * @var boolean
  115. */
  116. protected $_noController = false;
  117. /**
  118. * Whether or not to autorender postDispatch; per controller/action setting (reset
  119. * at next invocation)
  120. * @var boolean
  121. */
  122. protected $_noRender = false;
  123. /**
  124. * Characters representing path delimiters in the controller
  125. * @var string|array
  126. */
  127. protected $_pathDelimiters;
  128. /**
  129. * Which named segment of the response to utilize
  130. * @var string
  131. */
  132. protected $_responseSegment = null;
  133. /**
  134. * Which action view script to render
  135. * @var string
  136. */
  137. protected $_scriptAction = null;
  138. /**
  139. * View object basePath
  140. * @var string
  141. */
  142. protected $_viewBasePathSpec = ':moduleDir/views';
  143. /**
  144. * View script path specification string
  145. * @var string
  146. */
  147. protected $_viewScriptPathSpec = ':controller/:action.:suffix';
  148. /**
  149. * View script path specification string, minus controller segment
  150. * @var string
  151. */
  152. protected $_viewScriptPathNoControllerSpec = ':action.:suffix';
  153. /**
  154. * View script suffix
  155. * @var string
  156. */
  157. protected $_viewSuffix = 'phtml';
  158. /**
  159. * Constructor
  160. *
  161. * Optionally set view object and options.
  162. *
  163. * @param Zend_View_Interface $view
  164. * @param array $options
  165. * @return void
  166. */
  167. public function __construct(Zend_View_Interface $view = null, array $options = array())
  168. {
  169. if (null !== $view) {
  170. $this->setView($view);
  171. }
  172. if (!empty($options)) {
  173. $this->_setOptions($options);
  174. }
  175. }
  176. /**
  177. * Clone - also make sure the view is cloned.
  178. *
  179. * @return void
  180. */
  181. public function __clone()
  182. {
  183. if (isset($this->view) && $this->view instanceof Zend_View_Interface) {
  184. $this->view = clone $this->view;
  185. }
  186. }
  187. /**
  188. * Set the view object
  189. *
  190. * @param Zend_View_Interface $view
  191. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  192. */
  193. public function setView(Zend_View_Interface $view)
  194. {
  195. $this->view = $view;
  196. return $this;
  197. }
  198. /**
  199. * Get current module name
  200. *
  201. * @return string
  202. */
  203. public function getModule()
  204. {
  205. $request = $this->getRequest();
  206. $module = $request->getModuleName();
  207. if (null === $module) {
  208. $module = $this->getFrontController()->getDispatcher()->getDefaultModule();
  209. }
  210. return $module;
  211. }
  212. /**
  213. * Get module directory
  214. *
  215. * @throws Zend_Controller_Action_Exception
  216. * @return string
  217. */
  218. public function getModuleDirectory()
  219. {
  220. $module = $this->getModule();
  221. $moduleDir = $this->getFrontController()->getControllerDirectory($module);
  222. if ((null === $moduleDir) || is_array($moduleDir)) {
  223. /**
  224. * @see Zend_Controller_Action_Exception
  225. */
  226. require_once 'Zend/Controller/Action/Exception.php';
  227. throw new Zend_Controller_Action_Exception('ViewRenderer cannot locate module directory');
  228. }
  229. $this->_moduleDir = dirname($moduleDir);
  230. return $this->_moduleDir;
  231. }
  232. /**
  233. * Get inflector
  234. *
  235. * @return Zend_Filter_Inflector
  236. */
  237. public function getInflector()
  238. {
  239. if (null === $this->_inflector) {
  240. /**
  241. * @see Zend_Filter_Inflector
  242. */
  243. require_once 'Zend/Filter/Inflector.php';
  244. /**
  245. * @see Zend_Filter_PregReplace
  246. */
  247. require_once 'Zend/Filter/PregReplace.php';
  248. /**
  249. * @see Zend_Filter_Word_UnderscoreToSeparator
  250. */
  251. require_once 'Zend/Filter/Word/UnderscoreToSeparator.php';
  252. $this->_inflector = new Zend_Filter_Inflector();
  253. $this->_inflector->setStaticRuleReference('moduleDir', $this->_moduleDir) // moduleDir must be specified before the less specific 'module'
  254. ->addRules(array(
  255. ':module' => array('Word_CamelCaseToDash', 'StringToLower'),
  256. ':controller' => array('Word_CamelCaseToDash', new Zend_Filter_Word_UnderscoreToSeparator('/'), 'StringToLower', new Zend_Filter_PregReplace('/\./', '-')),
  257. ':action' => array('Word_CamelCaseToDash', new Zend_Filter_PregReplace('#[^a-z0-9' . preg_quote('/', '#') . ']+#i', '-'), 'StringToLower'),
  258. ))
  259. ->setStaticRuleReference('suffix', $this->_viewSuffix)
  260. ->setTargetReference($this->_inflectorTarget);
  261. }
  262. // Ensure that module directory is current
  263. $this->getModuleDirectory();
  264. return $this->_inflector;
  265. }
  266. /**
  267. * Set inflector
  268. *
  269. * @param Zend_Filter_Inflector $inflector
  270. * @param boolean $reference Whether the moduleDir, target, and suffix should be set as references to ViewRenderer properties
  271. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  272. */
  273. public function setInflector(Zend_Filter_Inflector $inflector, $reference = false)
  274. {
  275. $this->_inflector = $inflector;
  276. if ($reference) {
  277. $this->_inflector->setStaticRuleReference('suffix', $this->_viewSuffix)
  278. ->setStaticRuleReference('moduleDir', $this->_moduleDir)
  279. ->setTargetReference($this->_inflectorTarget);
  280. }
  281. return $this;
  282. }
  283. /**
  284. * Set inflector target
  285. *
  286. * @param string $target
  287. * @return void
  288. */
  289. protected function _setInflectorTarget($target)
  290. {
  291. $this->_inflectorTarget = (string) $target;
  292. }
  293. /**
  294. * Set internal module directory representation
  295. *
  296. * @param string $dir
  297. * @return void
  298. */
  299. protected function _setModuleDir($dir)
  300. {
  301. $this->_moduleDir = (string) $dir;
  302. }
  303. /**
  304. * Get internal module directory representation
  305. *
  306. * @return string
  307. */
  308. protected function _getModuleDir()
  309. {
  310. return $this->_moduleDir;
  311. }
  312. /**
  313. * Generate a class prefix for helper and filter classes
  314. *
  315. * @return string
  316. */
  317. protected function _generateDefaultPrefix()
  318. {
  319. $default = 'Zend_View';
  320. if (null === $this->_actionController) {
  321. return $default;
  322. }
  323. $class = get_class($this->_actionController);
  324. if (!strstr($class, '_')) {
  325. return $default;
  326. }
  327. $module = $this->getModule();
  328. if ('default' == $module) {
  329. return $default;
  330. }
  331. $prefix = substr($class, 0, strpos($class, '_')) . '_View';
  332. return $prefix;
  333. }
  334. /**
  335. * Retrieve base path based on location of current action controller
  336. *
  337. * @return string
  338. */
  339. protected function _getBasePath()
  340. {
  341. if (null === $this->_actionController) {
  342. return './views';
  343. }
  344. $inflector = $this->getInflector();
  345. $this->_setInflectorTarget($this->getViewBasePathSpec());
  346. $dispatcher = $this->_frontController->getDispatcher();
  347. $request = $this->getRequest();
  348. $parts = array(
  349. 'module' => (($moduleName = $request->getModuleName()) != '') ? $dispatcher->formatModuleName($moduleName) : $moduleName,
  350. 'controller' => $request->getControllerName(),
  351. 'action' => $dispatcher->formatActionName($request->getActionName())
  352. );
  353. $path = $inflector->filter($parts);
  354. return $path;
  355. }
  356. /**
  357. * Set options
  358. *
  359. * @param array $options
  360. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  361. */
  362. protected function _setOptions(array $options)
  363. {
  364. foreach ($options as $key => $value)
  365. {
  366. switch ($key) {
  367. case 'neverRender':
  368. case 'neverController':
  369. case 'noController':
  370. case 'noRender':
  371. $property = '_' . $key;
  372. $this->{$property} = ($value) ? true : false;
  373. break;
  374. case 'responseSegment':
  375. case 'scriptAction':
  376. case 'viewBasePathSpec':
  377. case 'viewScriptPathSpec':
  378. case 'viewScriptPathNoControllerSpec':
  379. case 'viewSuffix':
  380. $property = '_' . $key;
  381. $this->{$property} = (string) $value;
  382. break;
  383. default:
  384. break;
  385. }
  386. }
  387. return $this;
  388. }
  389. /**
  390. * Initialize the view object
  391. *
  392. * $options may contain the following keys:
  393. * - neverRender - flag dis/enabling postDispatch() autorender (affects all subsequent calls)
  394. * - noController - flag indicating whether or not to look for view scripts in subdirectories named after the controller
  395. * - noRender - flag indicating whether or not to autorender postDispatch()
  396. * - responseSegment - which named response segment to render a view script to
  397. * - scriptAction - what action script to render
  398. * - viewBasePathSpec - specification to use for determining view base path
  399. * - viewScriptPathSpec - specification to use for determining view script paths
  400. * - viewScriptPathNoControllerSpec - specification to use for determining view script paths when noController flag is set
  401. * - viewSuffix - what view script filename suffix to use
  402. *
  403. * @param string $path
  404. * @param string $prefix
  405. * @param array $options
  406. * @throws Zend_Controller_Action_Exception
  407. * @return void
  408. */
  409. public function initView($path = null, $prefix = null, array $options = array())
  410. {
  411. if (null === $this->view) {
  412. $this->setView(new Zend_View());
  413. }
  414. // Reset some flags every time
  415. $options['noController'] = (isset($options['noController'])) ? $options['noController'] : false;
  416. $options['noRender'] = (isset($options['noRender'])) ? $options['noRender'] : false;
  417. $this->_scriptAction = null;
  418. $this->_responseSegment = null;
  419. // Set options first; may be used to determine other initializations
  420. $this->_setOptions($options);
  421. // Get base view path
  422. if (empty($path)) {
  423. $path = $this->_getBasePath();
  424. if (empty($path)) {
  425. /**
  426. * @see Zend_Controller_Action_Exception
  427. */
  428. require_once 'Zend/Controller/Action/Exception.php';
  429. throw new Zend_Controller_Action_Exception('ViewRenderer initialization failed: retrieved view base path is empty');
  430. }
  431. }
  432. if (null === $prefix) {
  433. $prefix = $this->_generateDefaultPrefix();
  434. }
  435. // Determine if this path has already been registered
  436. $currentPaths = $this->view->getScriptPaths();
  437. $path = str_replace(array('/', '\\'), '/', $path);
  438. $pathExists = false;
  439. foreach ($currentPaths as $tmpPath) {
  440. $tmpPath = str_replace(array('/', '\\'), '/', $tmpPath);
  441. if (strstr($tmpPath, $path)) {
  442. $pathExists = true;
  443. break;
  444. }
  445. }
  446. if (!$pathExists) {
  447. $this->view->addBasePath($path, $prefix);
  448. }
  449. // Register view with action controller (unless already registered)
  450. if ((null !== $this->_actionController) && (null === $this->_actionController->view)) {
  451. $this->_actionController->view = $this->view;
  452. $this->_actionController->viewSuffix = $this->_viewSuffix;
  453. }
  454. }
  455. /**
  456. * init - initialize view
  457. *
  458. * @return void
  459. */
  460. public function init()
  461. {
  462. if ($this->getFrontController()->getParam('noViewRenderer')) {
  463. return;
  464. }
  465. $this->initView();
  466. }
  467. /**
  468. * Set view basePath specification
  469. *
  470. * Specification can contain one or more of the following:
  471. * - :moduleDir - current module directory
  472. * - :controller - name of current controller in the request
  473. * - :action - name of current action in the request
  474. * - :module - name of current module in the request
  475. *
  476. * @param string $path
  477. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  478. */
  479. public function setViewBasePathSpec($path)
  480. {
  481. $this->_viewBasePathSpec = (string) $path;
  482. return $this;
  483. }
  484. /**
  485. * Retrieve the current view basePath specification string
  486. *
  487. * @return string
  488. */
  489. public function getViewBasePathSpec()
  490. {
  491. return $this->_viewBasePathSpec;
  492. }
  493. /**
  494. * Set view script path specification
  495. *
  496. * Specification can contain one or more of the following:
  497. * - :moduleDir - current module directory
  498. * - :controller - name of current controller in the request
  499. * - :action - name of current action in the request
  500. * - :module - name of current module in the request
  501. *
  502. * @param string $path
  503. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  504. */
  505. public function setViewScriptPathSpec($path)
  506. {
  507. $this->_viewScriptPathSpec = (string) $path;
  508. return $this;
  509. }
  510. /**
  511. * Retrieve the current view script path specification string
  512. *
  513. * @return string
  514. */
  515. public function getViewScriptPathSpec()
  516. {
  517. return $this->_viewScriptPathSpec;
  518. }
  519. /**
  520. * Set view script path specification (no controller variant)
  521. *
  522. * Specification can contain one or more of the following:
  523. * - :moduleDir - current module directory
  524. * - :controller - name of current controller in the request
  525. * - :action - name of current action in the request
  526. * - :module - name of current module in the request
  527. *
  528. * :controller will likely be ignored in this variant.
  529. *
  530. * @param string $path
  531. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  532. */
  533. public function setViewScriptPathNoControllerSpec($path)
  534. {
  535. $this->_viewScriptPathNoControllerSpec = (string) $path;
  536. return $this;
  537. }
  538. /**
  539. * Retrieve the current view script path specification string (no controller variant)
  540. *
  541. * @return string
  542. */
  543. public function getViewScriptPathNoControllerSpec()
  544. {
  545. return $this->_viewScriptPathNoControllerSpec;
  546. }
  547. /**
  548. * Get a view script based on an action and/or other variables
  549. *
  550. * Uses values found in current request if no values passed in $vars.
  551. *
  552. * If {@link $_noController} is set, uses {@link $_viewScriptPathNoControllerSpec};
  553. * otherwise, uses {@link $_viewScriptPathSpec}.
  554. *
  555. * @param string $action
  556. * @param array $vars
  557. * @return string
  558. */
  559. public function getViewScript($action = null, array $vars = array())
  560. {
  561. $request = $this->getRequest();
  562. if ((null === $action) && (!isset($vars['action']))) {
  563. $action = $this->getScriptAction();
  564. if (null === $action) {
  565. $action = $request->getActionName();
  566. }
  567. $vars['action'] = $action;
  568. } elseif (null !== $action) {
  569. $vars['action'] = $action;
  570. }
  571. $inflector = $this->getInflector();
  572. if ($this->getNoController() || $this->getNeverController()) {
  573. $this->_setInflectorTarget($this->getViewScriptPathNoControllerSpec());
  574. } else {
  575. $this->_setInflectorTarget($this->getViewScriptPathSpec());
  576. }
  577. return $this->_translateSpec($vars);
  578. }
  579. /**
  580. * Set the neverRender flag (i.e., globally dis/enable autorendering)
  581. *
  582. * @param boolean $flag
  583. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  584. */
  585. public function setNeverRender($flag = true)
  586. {
  587. $this->_neverRender = ($flag) ? true : false;
  588. return $this;
  589. }
  590. /**
  591. * Retrieve neverRender flag value
  592. *
  593. * @return boolean
  594. */
  595. public function getNeverRender()
  596. {
  597. return $this->_neverRender;
  598. }
  599. /**
  600. * Set the noRender flag (i.e., whether or not to autorender)
  601. *
  602. * @param boolean $flag
  603. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  604. */
  605. public function setNoRender($flag = true)
  606. {
  607. $this->_noRender = ($flag) ? true : false;
  608. return $this;
  609. }
  610. /**
  611. * Retrieve noRender flag value
  612. *
  613. * @return boolean
  614. */
  615. public function getNoRender()
  616. {
  617. return $this->_noRender;
  618. }
  619. /**
  620. * Set the view script to use
  621. *
  622. * @param string $name
  623. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  624. */
  625. public function setScriptAction($name)
  626. {
  627. $this->_scriptAction = (string) $name;
  628. return $this;
  629. }
  630. /**
  631. * Retrieve view script name
  632. *
  633. * @return string
  634. */
  635. public function getScriptAction()
  636. {
  637. return $this->_scriptAction;
  638. }
  639. /**
  640. * Set the response segment name
  641. *
  642. * @param string $name
  643. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  644. */
  645. public function setResponseSegment($name)
  646. {
  647. if (null === $name) {
  648. $this->_responseSegment = null;
  649. } else {
  650. $this->_responseSegment = (string) $name;
  651. }
  652. return $this;
  653. }
  654. /**
  655. * Retrieve named response segment name
  656. *
  657. * @return string
  658. */
  659. public function getResponseSegment()
  660. {
  661. return $this->_responseSegment;
  662. }
  663. /**
  664. * Set the noController flag (i.e., whether or not to render into controller subdirectories)
  665. *
  666. * @param boolean $flag
  667. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  668. */
  669. public function setNoController($flag = true)
  670. {
  671. $this->_noController = ($flag) ? true : false;
  672. return $this;
  673. }
  674. /**
  675. * Retrieve noController flag value
  676. *
  677. * @return boolean
  678. */
  679. public function getNoController()
  680. {
  681. return $this->_noController;
  682. }
  683. /**
  684. * Set the neverController flag (i.e., whether or not to render into controller subdirectories)
  685. *
  686. * @param boolean $flag
  687. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  688. */
  689. public function setNeverController($flag = true)
  690. {
  691. $this->_neverController = ($flag) ? true : false;
  692. return $this;
  693. }
  694. /**
  695. * Retrieve neverController flag value
  696. *
  697. * @return boolean
  698. */
  699. public function getNeverController()
  700. {
  701. return $this->_neverController;
  702. }
  703. /**
  704. * Set view script suffix
  705. *
  706. * @param string $suffix
  707. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  708. */
  709. public function setViewSuffix($suffix)
  710. {
  711. $this->_viewSuffix = (string) $suffix;
  712. return $this;
  713. }
  714. /**
  715. * Get view script suffix
  716. *
  717. * @return string
  718. */
  719. public function getViewSuffix()
  720. {
  721. return $this->_viewSuffix;
  722. }
  723. /**
  724. * Set options for rendering a view script
  725. *
  726. * @param string $action View script to render
  727. * @param string $name Response named segment to render to
  728. * @param boolean $noController Whether or not to render within a subdirectory named after the controller
  729. * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
  730. */
  731. public function setRender($action = null, $name = null, $noController = null)
  732. {
  733. if (null !== $action) {
  734. $this->setScriptAction($action);
  735. }
  736. if (null !== $name) {
  737. $this->setResponseSegment($name);
  738. }
  739. if (null !== $noController) {
  740. $this->setNoController($noController);
  741. }
  742. return $this;
  743. }
  744. /**
  745. * Inflect based on provided vars
  746. *
  747. * Allowed variables are:
  748. * - :moduleDir - current module directory
  749. * - :module - current module name
  750. * - :controller - current controller name
  751. * - :action - current action name
  752. * - :suffix - view script file suffix
  753. *
  754. * @param array $vars
  755. * @return string
  756. */
  757. protected function _translateSpec(array $vars = array())
  758. {
  759. $inflector = $this->getInflector();
  760. $request = $this->getRequest();
  761. $dispatcher = $this->_frontController->getDispatcher();
  762. $module = $dispatcher->formatModuleName($request->getModuleName());
  763. $controller = $request->getControllerName();
  764. $action = $dispatcher->formatActionName($request->getActionName());
  765. $params = compact('module', 'controller', 'action');
  766. foreach ($vars as $key => $value) {
  767. switch ($key) {
  768. case 'module':
  769. case 'controller':
  770. case 'action':
  771. case 'moduleDir':
  772. case 'suffix':
  773. $params[$key] = (string) $value;
  774. break;
  775. default:
  776. break;
  777. }
  778. }
  779. if (isset($params['suffix'])) {
  780. $origSuffix = $this->getViewSuffix();
  781. $this->setViewSuffix($params['suffix']);
  782. }
  783. if (isset($params['moduleDir'])) {
  784. $origModuleDir = $this->_getModuleDir();
  785. $this->_setModuleDir($params['moduleDir']);
  786. }
  787. $filtered = $inflector->filter($params);
  788. if (isset($params['suffix'])) {
  789. $this->setViewSuffix($origSuffix);
  790. }
  791. if (isset($params['moduleDir'])) {
  792. $this->_setModuleDir($origModuleDir);
  793. }
  794. return $filtered;
  795. }
  796. /**
  797. * Render a view script (optionally to a named response segment)
  798. *
  799. * Sets the noRender flag to true when called.
  800. *
  801. * @param string $script
  802. * @param string $name
  803. * @return void
  804. */
  805. public function renderScript($script, $name = null)
  806. {
  807. if (null === $name) {
  808. $name = $this->getResponseSegment();
  809. }
  810. $this->getResponse()->appendBody(
  811. $this->view->render($script),
  812. $name
  813. );
  814. $this->setNoRender();
  815. }
  816. /**
  817. * Render a view based on path specifications
  818. *
  819. * Renders a view based on the view script path specifications.
  820. *
  821. * @param string $action
  822. * @param string $name
  823. * @param boolean $noController
  824. * @return void
  825. */
  826. public function render($action = null, $name = null, $noController = null)
  827. {
  828. $this->setRender($action, $name, $noController);
  829. $path = $this->getViewScript();
  830. $this->renderScript($path, $name);
  831. }
  832. /**
  833. * Render a script based on specification variables
  834. *
  835. * Pass an action, and one or more specification variables (view script suffix)
  836. * to determine the view script path, and render that script.
  837. *
  838. * @param string $action
  839. * @param array $vars
  840. * @param string $name
  841. * @return void
  842. */
  843. public function renderBySpec($action = null, array $vars = array(), $name = null)
  844. {
  845. if (null !== $name) {
  846. $this->setResponseSegment($name);
  847. }
  848. $path = $this->getViewScript($action, $vars);
  849. $this->renderScript($path);
  850. }
  851. /**
  852. * postDispatch - auto render a view
  853. *
  854. * Only autorenders if:
  855. * - _noRender is false
  856. * - action controller is present
  857. * - request has not been re-dispatched (i.e., _forward() has not been called)
  858. * - response is not a redirect
  859. *
  860. * @return void
  861. */
  862. public function postDispatch()
  863. {
  864. if ($this->_shouldRender()) {
  865. $this->render();
  866. }
  867. }
  868. /**
  869. * Should the ViewRenderer render a view script?
  870. *
  871. * @return boolean
  872. */
  873. protected function _shouldRender()
  874. {
  875. return (!$this->getFrontController()->getParam('noViewRenderer')
  876. && !$this->_neverRender
  877. && !$this->_noRender
  878. && (null !== $this->_actionController)
  879. && $this->getRequest()->isDispatched()
  880. && !$this->getResponse()->isRedirect()
  881. );
  882. }
  883. /**
  884. * Use this helper as a method; proxies to setRender()
  885. *
  886. * @param string $action
  887. * @param string $name
  888. * @param boolean $noController
  889. * @return void
  890. */
  891. public function direct($action = null, $name = null, $noController = null)
  892. {
  893. $this->setRender($action, $name, $noController);
  894. }
  895. }