RendererAbstract.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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_Markup
  17. * @subpackage Renderer
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_config
  24. */
  25. require_once 'Zend/Config.php';
  26. /**
  27. * @see Zend_Filter
  28. */
  29. require_once 'Zend/Filter.php';
  30. /**
  31. * @see Zend_Markup_Renderer_TokenConverterInterface
  32. */
  33. require_once 'Zend/Markup/Renderer/TokenConverterInterface.php';
  34. /**
  35. * Defines the basic rendering functionality
  36. *
  37. * @category Zend
  38. * @package Zend_Markup
  39. * @subpackage Renderer
  40. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. abstract class Zend_Markup_Renderer_RendererAbstract
  44. {
  45. const TYPE_CALLBACK = 4;
  46. const TYPE_REPLACE = 8;
  47. const TYPE_ALIAS = 16;
  48. /**
  49. * Tag info
  50. *
  51. * @var array
  52. */
  53. protected $_markups = array();
  54. /**
  55. * Parser
  56. *
  57. * @var Zend_Markup_Parser_ParserInterface
  58. */
  59. protected $_parser;
  60. /**
  61. * What filter to use
  62. *
  63. * @var bool
  64. */
  65. protected $_filter;
  66. /**
  67. * Filter chain
  68. *
  69. * @var Zend_Filter
  70. */
  71. protected $_defaultFilter;
  72. /**
  73. * The current group
  74. *
  75. * @var string
  76. */
  77. protected $_group;
  78. /**
  79. * Groups definition
  80. *
  81. * @var array
  82. */
  83. protected $_groups = array();
  84. /**
  85. * Plugin loader for tags
  86. *
  87. * @var Zend_Loader_PluginLoader
  88. */
  89. protected $_pluginLoader;
  90. /**
  91. * The current token
  92. *
  93. * @var Zend_Markup_Token
  94. */
  95. protected $_token;
  96. /**
  97. * Encoding
  98. *
  99. * @var string
  100. */
  101. protected static $_encoding = 'UTF-8';
  102. /**
  103. * Constructor
  104. *
  105. * @param array|Zend_Config $options
  106. *
  107. * @return void
  108. */
  109. public function __construct($options = array())
  110. {
  111. if ($options instanceof Zend_Config) {
  112. $options = $options->toArray();
  113. }
  114. if (isset($options['encoding'])) {
  115. $this->setEncoding($options['encoding']);
  116. }
  117. if (isset($options['parser'])) {
  118. $this->setParser($options['parser']);
  119. }
  120. if (isset($options['useDefaultTags']) && ($options['useDefaultTags'] === false)) {
  121. $this->removeDefaultTags();
  122. }
  123. if (!isset($options['useDefaultFilters']) || ($options['useDefaultFilters'] === true)) {
  124. $this->addDefaultFilters();
  125. }
  126. if (isset($options['defaultFilter'])) {
  127. $this->addDefaultFilter($options['defaultFilter']);
  128. }
  129. }
  130. /**
  131. * Set the parser
  132. *
  133. * @param Zend_Markup_Parser_ParserInterface $parser
  134. * @return Zend_Markup_Renderer_RendererAbstract
  135. */
  136. public function setParser(Zend_Markup_Parser_ParserInterface $parser)
  137. {
  138. $this->_parser = $parser;
  139. return $this;
  140. }
  141. /**
  142. * Get the parser
  143. *
  144. * @return Zend_Markup_Parser_ParserInterface
  145. */
  146. public function getParser()
  147. {
  148. return $this->_parser;
  149. }
  150. /**
  151. * Get the plugin loader
  152. *
  153. * @return Zend_Loader_PluginLoader
  154. */
  155. public function getPluginLoader()
  156. {
  157. return $this->_pluginLoader;
  158. }
  159. /**
  160. * Set the renderer's encoding
  161. *
  162. * @param string $encoding
  163. *
  164. * @return Zend_Markup_Renderer_RendererAbstract
  165. */
  166. public static function setEncoding($encoding)
  167. {
  168. self::$_encoding = $encoding;
  169. return $this;
  170. }
  171. /**
  172. * Get the renderer's encoding
  173. *
  174. * @return string
  175. */
  176. public static function getEncoding()
  177. {
  178. return self::$_encoding;
  179. }
  180. /**
  181. * Add a new markup
  182. *
  183. * @param string $name
  184. * @param string $type
  185. * @param array $options
  186. *
  187. * @return Zend_Markup_Renderer_RendererAbstract
  188. */
  189. public function addMarkup($name, $type, array $options)
  190. {
  191. if (!isset($options['group']) && ($type ^ self::TYPE_ALIAS)) {
  192. require_once 'Zend/Markup/Renderer/Exception.php';
  193. throw new Zend_Markup_Renderer_Exception("There is no render group defined.");
  194. }
  195. // add the filter
  196. if (isset($options['filter'])) {
  197. if ($options['filter'] instanceof Zend_Filter_Interface) {
  198. $filter = $options['filter'];
  199. } elseif ($options['filter'] === true) {
  200. $filter = $this->getDefaultFilter();
  201. } else {
  202. $filter = false;
  203. }
  204. } else {
  205. $filter = $this->getDefaultFilter();
  206. }
  207. // check the type
  208. if ($type & self::TYPE_CALLBACK) {
  209. // add a callback tag
  210. if (isset($options['callback'])) {
  211. if (!($options['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
  212. require_once 'Zend/Markup/Renderer/Exception.php';
  213. throw new Zend_Markup_Renderer_Exception("Not a valid tag callback.");
  214. }
  215. if (method_exists($options['callback'], 'setRenderer')) {
  216. $options['callback']->setRenderer($this);
  217. }
  218. } else {
  219. $options['callback'] = null;
  220. }
  221. $options['type'] = $type;
  222. $options['filter'] = $filter;
  223. $this->_markups[$name] = $options;
  224. } elseif ($type & self::TYPE_ALIAS) {
  225. // add an alias
  226. if (empty($options['name'])) {
  227. require_once 'Zend/Markup/Renderer/Exception.php';
  228. throw new Zend_Markup_Renderer_Exception(
  229. 'No alias was provided but tag was defined as such');
  230. }
  231. $this->_markups[$name] = array(
  232. 'type' => self::TYPE_ALIAS,
  233. 'name' => $options['name']
  234. );
  235. } else {
  236. if ($type && array_key_exists('empty', $options) && $options['empty']) {
  237. // add a single replace markup
  238. $options['type'] = $type;
  239. $options['filter'] = $filter;
  240. $this->_markups[$name] = $options;
  241. } else {
  242. // add a replace markup
  243. $options['type'] = $type;
  244. $options['filter'] = $filter;
  245. $this->_markups[$name] = $options;
  246. }
  247. }
  248. return $this;
  249. }
  250. /**
  251. * Remove a markup
  252. *
  253. * @param string $name
  254. *
  255. * @return void
  256. */
  257. public function removeMarkup($name)
  258. {
  259. unset($this->_markups[$name]);
  260. }
  261. /**
  262. * Remove the default tags
  263. *
  264. * @return void
  265. */
  266. public function clearMarkups()
  267. {
  268. $this->_markups = array();
  269. }
  270. /**
  271. * Render function
  272. *
  273. * @param Zend_Markup_TokenList|string $tokenList
  274. * @return string
  275. */
  276. public function render($value)
  277. {
  278. if ($value instanceof Zend_Markup_TokenList) {
  279. $tokenList = $value;
  280. } else {
  281. $tokenList = $this->getParser()->parse($value);
  282. }
  283. $root = $tokenList->current();
  284. $this->_filter = $this->getDefaultFilter();
  285. return $this->_render($root);
  286. }
  287. /**
  288. * Render a single token
  289. *
  290. * @param Zend_Markup_Token $token
  291. * @return string
  292. */
  293. protected function _render(Zend_Markup_Token $token)
  294. {
  295. $return = '';
  296. $this->_token = $token;
  297. // if this tag has children, execute them
  298. if ($token->hasChildren()) {
  299. foreach ($token->getChildren() as $child) {
  300. $return .= $this->_execute($child);
  301. }
  302. }
  303. return $return;
  304. }
  305. /**
  306. * Get the group of a token
  307. *
  308. * @param Zend_Markup_Token $token
  309. * @return string|bool
  310. */
  311. protected function _getGroup(Zend_Markup_Token $token)
  312. {
  313. if (!isset($this->_markups[$token->getName()])) {
  314. return false;
  315. }
  316. $tag = $this->_markups[$token->getName()];
  317. // alias processing
  318. while ($tag['type'] & self::TYPE_ALIAS) {
  319. $tag = $this->_markups[$tag['name']];
  320. }
  321. return isset($tag['group']) ? $tag['group'] : false;
  322. }
  323. /**
  324. * Execute the token
  325. *
  326. * @param Zend_Markup_Token $token
  327. * @return string
  328. */
  329. protected function _execute(Zend_Markup_Token $token)
  330. {
  331. // first return the normal text tags
  332. if ($token->getType() == Zend_Markup_Token::TYPE_NONE) {
  333. return $this->_filter($token->getTag());
  334. }
  335. // if the token doesn't have a notation, return the plain text
  336. if (!isset($this->_markups[$token->getName()])) {
  337. $oldToken = $this->_token;
  338. $return = $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
  339. $this->_token = $oldToken;
  340. return $return;
  341. }
  342. $name = $this->_getMarkupName($token);
  343. $markup = (!$name) ? false : $this->_markups[$name];
  344. $empty = (is_array($markup) && array_key_exists('empty', $markup) && $markup['empty']);
  345. // check if the tag has content
  346. if (!$empty && !$token->hasChildren()) {
  347. return '';
  348. }
  349. // check for the context
  350. if (is_array($markup) && !in_array($markup['group'], $this->_groups[$this->_group])) {
  351. $oldToken = $this->_token;
  352. $return = $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
  353. $this->_token = $oldToken;
  354. return $return;
  355. }
  356. // check for the filter
  357. if (!isset($markup['filter'])
  358. || (!($markup['filter'] instanceof Zend_Filter_Interface) && ($markup['filter'] !== false))) {
  359. $this->_markups[$name]['filter'] = $this->getDefaultFilter();
  360. }
  361. // save old values to reset them after the work is done
  362. $oldFilter = $this->_filter;
  363. $oldGroup = $this->_group;
  364. $return = '';
  365. // set the filter and the group
  366. $this->_filter = $this->getFilter($name);
  367. if ($group = $this->_getGroup($token)) {
  368. $this->_group = $group;
  369. }
  370. // callback
  371. if (is_array($markup) && ($markup['type'] & self::TYPE_CALLBACK)) {
  372. // load the callback if the tag doesn't exist
  373. if (!($markup['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
  374. $class = $this->getPluginLoader()->load($name);
  375. $markup['callback'] = new $class;
  376. if (!($markup['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
  377. require_once 'Zend/Markup/Renderer/Exception.php';
  378. throw new Zend_Markup_Renderer_Exception("Callback for tag '$name' found, but it isn't valid.");
  379. }
  380. if (method_exists($markup['callback'], 'setRenderer')) {
  381. $markup['callback']->setRenderer($this);
  382. }
  383. }
  384. if ($markup['type'] && !$empty) {
  385. $return = $markup['callback']->convert($token, $this->_render($token));
  386. } else {
  387. $return = $markup['callback']->convert($token, null);
  388. }
  389. } else {
  390. // replace
  391. if ($markup['type'] && !$empty) {
  392. $return = $this->_executeReplace($token, $markup);
  393. } else {
  394. $return = $this->_executeSingleReplace($token, $markup);
  395. }
  396. }
  397. // reset to the old values
  398. $this->_filter = $oldFilter;
  399. $this->_group = $oldGroup;
  400. return $return;
  401. }
  402. /**
  403. * Filter method
  404. *
  405. * @param string $value
  406. *
  407. * @return string
  408. */
  409. protected function _filter($value)
  410. {
  411. if ($this->_filter instanceof Zend_Filter_Interface) {
  412. return $this->_filter->filter($value);
  413. }
  414. return $value;
  415. }
  416. /**
  417. * Get the markup name
  418. *
  419. * @param Zend_Markup_Token
  420. *
  421. * @return string
  422. */
  423. protected function _getMarkupName(Zend_Markup_Token $token)
  424. {
  425. $name = $token->getName();
  426. if (empty($name)) {
  427. return false;
  428. }
  429. return $this->_resolveMarkupName($name);
  430. }
  431. /**
  432. * Resolve aliases for a markup name
  433. *
  434. * @param string $name
  435. *
  436. * @return string
  437. */
  438. protected function _resolveMarkupName($name)
  439. {
  440. while (($type = $this->_getMarkupType($name))
  441. && ($type & self::TYPE_ALIAS)
  442. ) {
  443. $name = $this->_markups[$name]['name'];
  444. }
  445. return $name;
  446. }
  447. /**
  448. * Retrieve markup type
  449. *
  450. * @param string $name
  451. * @return false|int
  452. */
  453. protected function _getMarkupType($name)
  454. {
  455. if (!isset($this->_markups[$name])) {
  456. return false;
  457. }
  458. if (!isset($this->_markups[$name]['type'])) {
  459. return false;
  460. }
  461. return $this->_markups[$name]['type'];
  462. }
  463. /**
  464. * Execute a replace token
  465. *
  466. * @param Zend_Markup_Token $token
  467. * @param array $tag
  468. * @return string
  469. */
  470. protected function _executeReplace(Zend_Markup_Token $token, $tag)
  471. {
  472. return $tag['start'] . $this->_render($token) . $tag['end'];
  473. }
  474. /**
  475. * Execute a single replace token
  476. *
  477. * @param Zend_Markup_Token $token
  478. * @param array $tag
  479. * @return string
  480. */
  481. protected function _executeSingleReplace(Zend_Markup_Token $token, $tag)
  482. {
  483. return $tag['replace'];
  484. }
  485. /**
  486. * Get the default filter
  487. *
  488. * @return void
  489. */
  490. public function getDefaultFilter()
  491. {
  492. if (null === $this->_defaultFilter) {
  493. $this->setDefaultFilter();
  494. }
  495. return $this->_defaultFilter;
  496. }
  497. /**
  498. * Add a default filter
  499. *
  500. * @param string $filter
  501. *
  502. * @return void
  503. */
  504. public function addDefaultFilter(Zend_Filter_Interface $filter, $placement = Zend_Filter::CHAIN_APPEND)
  505. {
  506. if (!($this->_defaultFilter instanceof Zend_Filter)) {
  507. $defaultFilter = new Zend_Filter();
  508. $defaultFilter->addFilter($filter);
  509. $this->_defaultFilter = $defaultFilter;
  510. }
  511. $this->_defaultFilter->addFilter($filter, $placement);
  512. }
  513. /**
  514. * Set the default filter
  515. *
  516. * @param Zend_Filter_Interface $filter
  517. *
  518. * @return void
  519. */
  520. public function setDefaultFilter(Zend_Filter_Interface $filter)
  521. {
  522. $this->_defaultFilter = $filter;
  523. }
  524. /**
  525. * Get the filter for an existing markup
  526. *
  527. * @param string $markup
  528. *
  529. * @return Zend_Filter_Interface
  530. */
  531. public function getFilter($markup)
  532. {
  533. $markup = $this->_resolveMarkupName($markup);
  534. if (!isset($this->_markups[$markup]['filter'])
  535. || !($this->_markups[$markup]['filter'] instanceof Zend_Filter_Interface)
  536. ) {
  537. if (isset($this->_markups[$markup]['filter']) && $this->_markups[$markup]['filter']) {
  538. $this->_markups[$markup]['filter'] = $this->getDefaultFilter();
  539. } else {
  540. return false;
  541. }
  542. }
  543. return $this->_markups[$markup]['filter'];
  544. }
  545. /**
  546. * Add a filter for an existing markup
  547. *
  548. * @param Zend_Filter_Interface $filter
  549. * @param string $markup
  550. * @param string $placement
  551. *
  552. * @return Zend_Markup_Renderer_RendererAbstract
  553. */
  554. public function addFilter(Zend_Filter_Interface $filter, $markup, $placement = Zend_Filter::CHAIN_APPEND)
  555. {
  556. $markup = $this->_resolveMarkupName($markup);
  557. $oldFilter = $this->getFilter($markup);
  558. // if this filter is the default filter, clone it first
  559. if ($oldFilter === $this->getDefaultFilter()) {
  560. $oldFilter = clone $oldFilter;
  561. }
  562. if (!($oldFilter instanceof Zend_Filter)) {
  563. $this->_markups[$markup]['filter'] = new Zend_Filter();
  564. if ($oldFilter instanceof Zend_Filter_Interface) {
  565. $this->_markups[$markup]['filter']->addFilter($oldFilter);
  566. }
  567. } else {
  568. $this->_markups[$markup]['filter'] = $oldFilter;
  569. }
  570. $this->_markups[$markup]['filter']->addFilter($filter, $placement);
  571. return $this;
  572. }
  573. /**
  574. * Set the filter for an existing
  575. *
  576. * @param Zend_Filter_Interface $filter
  577. * @param string $markup
  578. *
  579. * @return Zend_Markup_Renderer_RendererAbstract
  580. */
  581. public function setFilter(Zend_Filter_Interface $filter, $markup)
  582. {
  583. $markup = $this->_resolveMarkupName($markup);
  584. $this->_markups[$markup]['filter'] = $filter;
  585. return $this;
  586. }
  587. /**
  588. * Add a render group
  589. *
  590. * @param string $name
  591. * @param array $allowedInside
  592. * @param array $allowsInside
  593. *
  594. * @return void
  595. */
  596. public function addGroup($name, array $allowedInside = array(), array $allowsInside = array())
  597. {
  598. $this->_groups[$name] = $allowsInside;
  599. foreach ($allowedInside as $group) {
  600. $this->_groups[$group][] = $name;
  601. }
  602. }
  603. /**
  604. * Get group definitions
  605. *
  606. * @return array
  607. */
  608. public function getGroups()
  609. {
  610. return $this->_groups;
  611. }
  612. /**
  613. * Set the default filters
  614. *
  615. * @return void
  616. */
  617. abstract public function addDefaultFilters();
  618. }