RendererAbstract.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. * Constructor
  98. *
  99. * @param array|Zend_Config $options
  100. *
  101. * @return void
  102. */
  103. public function __construct($options = array())
  104. {
  105. if ($options instanceof Zend_Config) {
  106. $options = $options->toArray();
  107. }
  108. if (isset($options['parser'])) {
  109. $this->setParser($options['parser']);
  110. }
  111. if (isset($options['useDefaultTags']) && ($options['useDefaultTags'] === false)) {
  112. $this->removeDefaultTags();
  113. }
  114. if (!isset($options['useDefaultFilters']) || ($options['useDefaultFilters'] === true)) {
  115. $this->addDefaultFilters();
  116. }
  117. if (isset($options['defaultFilter'])) {
  118. $this->addDefaultFilter($options['defaultFilter']);
  119. }
  120. }
  121. /**
  122. * Set the parser
  123. *
  124. * @param Zend_Markup_Parser_ParserInterface $parser
  125. * @return Zend_Markup_Renderer_RendererAbstract
  126. */
  127. public function setParser(Zend_Markup_Parser_ParserInterface $parser)
  128. {
  129. $this->_parser = $parser;
  130. return $this;
  131. }
  132. /**
  133. * Get the parser
  134. *
  135. * @return Zend_Markup_Parser_ParserInterface
  136. */
  137. public function getParser()
  138. {
  139. return $this->_parser;
  140. }
  141. /**
  142. * Get the plugin loader
  143. *
  144. * @return Zend_Loader_PluginLoader
  145. */
  146. public function getPluginLoader()
  147. {
  148. return $this->_pluginLoader;
  149. }
  150. /**
  151. * Add a new markup
  152. *
  153. * @param string $name
  154. * @param string $type
  155. * @param array $options
  156. *
  157. * @return Zend_Markup_Renderer_RendererAbstract
  158. */
  159. public function addMarkup($name, $type, array $options)
  160. {
  161. if (!isset($options['group']) && ($type ^ self::TYPE_ALIAS)) {
  162. require_once 'Zend/Markup/Renderer/Exception.php';
  163. throw new Zend_Markup_Renderer_Exception("There is no render group defined.");
  164. }
  165. // add the filter
  166. if (isset($options['filter'])) {
  167. if ($options['filter'] instanceof Zend_Filter_Interface) {
  168. $filter = $options['filter'];
  169. } elseif ($options['filter'] === true) {
  170. $filter = $this->getDefaultFilter();
  171. } else {
  172. $filter = false;
  173. }
  174. } else {
  175. $filter = $this->getDefaultFilter();
  176. }
  177. // check the type
  178. if ($type & self::TYPE_CALLBACK) {
  179. // add a callback tag
  180. if (isset($options['callback'])) {
  181. if (!($options['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
  182. require_once 'Zend/Markup/Renderer/Exception.php';
  183. throw new Zend_Markup_Renderer_Exception("Not a valid tag callback.");
  184. }
  185. if (method_exists($options['callback'], 'setRenderer')) {
  186. $options['callback']->setRenderer($this);
  187. }
  188. } else {
  189. $options['callback'] = null;
  190. }
  191. $options['type'] = $type;
  192. $options['filter'] = $filter;
  193. $this->_markups[$name] = $options;
  194. } elseif ($type & self::TYPE_ALIAS) {
  195. // add an alias
  196. if (empty($options['name'])) {
  197. require_once 'Zend/Markup/Renderer/Exception.php';
  198. throw new Zend_Markup_Renderer_Exception(
  199. 'No alias was provided but tag was defined as such');
  200. }
  201. $this->_markups[$name] = array(
  202. 'type' => self::TYPE_ALIAS,
  203. 'name' => $options['name']
  204. );
  205. } else {
  206. if ($type && array_key_exists('empty', $options) && $options['empty']) {
  207. // add a single replace markup
  208. $options['type'] = $type;
  209. $options['filter'] = $filter;
  210. $this->_markups[$name] = $options;
  211. } else {
  212. // add a replace markup
  213. $options['type'] = $type;
  214. $options['filter'] = $filter;
  215. $this->_markups[$name] = $options;
  216. }
  217. }
  218. return $this;
  219. }
  220. /**
  221. * Remove a markup
  222. *
  223. * @param string $name
  224. *
  225. * @return void
  226. */
  227. public function removeMarkup($name)
  228. {
  229. unset($this->_markups[$name]);
  230. }
  231. /**
  232. * Remove the default tags
  233. *
  234. * @return void
  235. */
  236. public function clearMarkups()
  237. {
  238. $this->_markups = array();
  239. }
  240. /**
  241. * Render function
  242. *
  243. * @param Zend_Markup_TokenList|string $tokenList
  244. * @return string
  245. */
  246. public function render($value)
  247. {
  248. if ($value instanceof Zend_Markup_TokenList) {
  249. $tokenList = $value;
  250. } else {
  251. $tokenList = $this->getParser()->parse($value);
  252. }
  253. $root = $tokenList->current();
  254. $this->_filter = $this->getDefaultFilter();
  255. return $this->_render($root);
  256. }
  257. /**
  258. * Render a single token
  259. *
  260. * @param Zend_Markup_Token $token
  261. * @return string
  262. */
  263. protected function _render(Zend_Markup_Token $token)
  264. {
  265. $return = '';
  266. $this->_token = $token;
  267. // if this tag has children, execute them
  268. if ($token->hasChildren()) {
  269. foreach ($token->getChildren() as $child) {
  270. $return .= $this->_execute($child);
  271. }
  272. }
  273. return $return;
  274. }
  275. /**
  276. * Get the group of a token
  277. *
  278. * @param Zend_Markup_Token $token
  279. * @return string|bool
  280. */
  281. protected function _getGroup(Zend_Markup_Token $token)
  282. {
  283. if (!isset($this->_markups[$token->getName()])) {
  284. return false;
  285. }
  286. $tag = $this->_markups[$token->getName()];
  287. // alias processing
  288. while ($tag['type'] & self::TYPE_ALIAS) {
  289. $tag = $this->_markups[$tag['name']];
  290. }
  291. return isset($tag['group']) ? $tag['group'] : false;
  292. }
  293. /**
  294. * Execute the token
  295. *
  296. * @param Zend_Markup_Token $token
  297. * @return string
  298. */
  299. protected function _execute(Zend_Markup_Token $token)
  300. {
  301. // first return the normal text tags
  302. if ($token->getType() == Zend_Markup_Token::TYPE_NONE) {
  303. return $this->_filter($token->getTag());
  304. }
  305. // if the token doesn't have a notation, return the plain text
  306. if (!isset($this->_markups[$token->getName()])) {
  307. $oldToken = $this->_token;
  308. $return = $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
  309. $this->_token = $oldToken;
  310. return $return;
  311. }
  312. $name = $this->_getMarkupName($token);
  313. $markup = (!$name) ? false : $this->_markups[$name];
  314. $empty = (is_array($markup) && array_key_exists('empty', $markup) && $markup['empty']);
  315. // check if the tag has content
  316. if (!$empty && !$token->hasChildren()) {
  317. return '';
  318. }
  319. // check for the context
  320. if (is_array($markup) && !in_array($markup['group'], $this->_groups[$this->_group])) {
  321. $oldToken = $this->_token;
  322. $return = $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
  323. $this->_token = $oldToken;
  324. return $return;
  325. }
  326. // check for the filter
  327. if (!isset($markup['filter'])
  328. || (!($markup['filter'] instanceof Zend_Filter_Interface) && ($markup['filter'] !== false))) {
  329. $this->_markups[$name]['filter'] = $this->getDefaultFilter();
  330. }
  331. // save old values to reset them after the work is done
  332. $oldFilter = $this->_filter;
  333. $oldGroup = $this->_group;
  334. $return = '';
  335. // set the filter and the group
  336. $this->_filter = $this->getFilter($name);
  337. if ($group = $this->_getGroup($token)) {
  338. $this->_group = $group;
  339. }
  340. // callback
  341. if (is_array($markup) && ($markup['type'] & self::TYPE_CALLBACK)) {
  342. // load the callback if the tag doesn't exist
  343. if (!($markup['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
  344. $class = $this->getPluginLoader()->load($name);
  345. $markup['callback'] = new $class;
  346. if (!($markup['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
  347. require_once 'Zend/Markup/Renderer/Exception.php';
  348. throw new Zend_Markup_Renderer_Exception("Callback for tag '$name' found, but it isn't valid.");
  349. }
  350. if (method_exists($markup['callback'], 'setRenderer')) {
  351. $markup['callback']->setRenderer($this);
  352. }
  353. }
  354. if ($markup['type'] && !$empty) {
  355. $return = $markup['callback']->convert($token, $this->_render($token));
  356. } else {
  357. $return = $markup['callback']->convert($token, null);
  358. }
  359. } else {
  360. // replace
  361. if ($markup['type'] && !$empty) {
  362. $return = $this->_executeReplace($token, $markup);
  363. } else {
  364. $return = $this->_executeSingleReplace($token, $markup);
  365. }
  366. }
  367. // reset to the old values
  368. $this->_filter = $oldFilter;
  369. $this->_group = $oldGroup;
  370. return $return;
  371. }
  372. /**
  373. * Filter method
  374. *
  375. * @param string $value
  376. *
  377. * @return string
  378. */
  379. protected function _filter($value)
  380. {
  381. if ($this->_filter instanceof Zend_Filter_Interface) {
  382. return $this->_filter->filter($value);
  383. }
  384. return $value;
  385. }
  386. /**
  387. * Get the markup name
  388. *
  389. * @param Zend_Markup_Token
  390. *
  391. * @return string
  392. */
  393. protected function _getMarkupName(Zend_Markup_Token $token)
  394. {
  395. $name = $token->getName();
  396. if (empty($name)) {
  397. return false;
  398. }
  399. return $this->_resolveMarkupName($name);
  400. }
  401. /**
  402. * Resolve aliases for a markup name
  403. *
  404. * @param string $name
  405. *
  406. * @return string
  407. */
  408. protected function _resolveMarkupName($name)
  409. {
  410. while (($type = $this->_getMarkupType($name))
  411. && ($type & self::TYPE_ALIAS)
  412. ) {
  413. $name = $this->_markups[$name]['name'];
  414. }
  415. return $name;
  416. }
  417. /**
  418. * Retrieve markup type
  419. *
  420. * @param string $name
  421. * @return false|int
  422. */
  423. protected function _getMarkupType($name)
  424. {
  425. if (!isset($this->_markups[$name])) {
  426. return false;
  427. }
  428. if (!isset($this->_markups[$name]['type'])) {
  429. return false;
  430. }
  431. return $this->_markups[$name]['type'];
  432. }
  433. /**
  434. * Execute a replace token
  435. *
  436. * @param Zend_Markup_Token $token
  437. * @param array $tag
  438. * @return string
  439. */
  440. protected function _executeReplace(Zend_Markup_Token $token, $tag)
  441. {
  442. return $tag['start'] . $this->_render($token) . $tag['end'];
  443. }
  444. /**
  445. * Execute a single replace token
  446. *
  447. * @param Zend_Markup_Token $token
  448. * @param array $tag
  449. * @return string
  450. */
  451. protected function _executeSingleReplace(Zend_Markup_Token $token, $tag)
  452. {
  453. return $tag['replace'];
  454. }
  455. /**
  456. * Get the default filter
  457. *
  458. * @return void
  459. */
  460. public function getDefaultFilter()
  461. {
  462. if (null === $this->_defaultFilter) {
  463. $this->setDefaultFilter();
  464. }
  465. return $this->_defaultFilter;
  466. }
  467. /**
  468. * Add a default filter
  469. *
  470. * @param string $filter
  471. *
  472. * @return void
  473. */
  474. public function addDefaultFilter(Zend_Filter_Interface $filter, $placement = Zend_Filter::CHAIN_APPEND)
  475. {
  476. if (!($this->_defaultFilter instanceof Zend_Filter)) {
  477. $defaultFilter = new Zend_Filter();
  478. $defaultFilter->addFilter($filter);
  479. $this->_defaultFilter = $defaultFilter;
  480. }
  481. $this->_defaultFilter->addFilter($filter, $placement);
  482. }
  483. /**
  484. * Set the default filter
  485. *
  486. * @param Zend_Filter_Interface $filter
  487. *
  488. * @return void
  489. */
  490. public function setDefaultFilter(Zend_Filter_Interface $filter)
  491. {
  492. $this->_defaultFilter = $filter;
  493. }
  494. /**
  495. * Get the filter for an existing markup
  496. *
  497. * @param string $markup
  498. *
  499. * @return Zend_Filter_Interface
  500. */
  501. public function getFilter($markup)
  502. {
  503. $markup = $this->_resolveMarkupName($markup);
  504. if (!isset($this->_markups[$markup]['filter'])
  505. || !($this->_markups[$markup]['filter'] instanceof Zend_Filter_Interface)
  506. ) {
  507. if (isset($this->_markups[$markup]['filter']) && $this->_markups[$markup]['filter']) {
  508. $this->_markups[$markup]['filter'] = $this->getDefaultFilter();
  509. } else {
  510. return false;
  511. }
  512. }
  513. return $this->_markups[$markup]['filter'];
  514. }
  515. /**
  516. * Add a filter for an existing markup
  517. *
  518. * @param Zend_Filter_Interface $filter
  519. * @param string $markup
  520. * @param string $placement
  521. *
  522. * @return Zend_Markup_Renderer_RendererAbstract
  523. */
  524. public function addFilter(Zend_Filter_Interface $filter, $markup, $placement = Zend_Filter::CHAIN_APPEND)
  525. {
  526. $markup = $this->_resolveMarkupName($markup);
  527. $oldFilter = $this->getFilter($markup);
  528. // if this filter is the default filter, clone it first
  529. if ($oldFilter === $this->getDefaultFilter()) {
  530. $oldFilter = clone $oldFilter;
  531. }
  532. if (!($oldFilter instanceof Zend_Filter)) {
  533. $this->_markups[$markup]['filter'] = new Zend_Filter();
  534. if ($oldFilter instanceof Zend_Filter_Interface) {
  535. $this->_markups[$markup]['filter']->addFilter($oldFilter);
  536. }
  537. } else {
  538. $this->_markups[$markup]['filter'] = $oldFilter;
  539. }
  540. $this->_markups[$markup]['filter']->addFilter($filter, $placement);
  541. return $this;
  542. }
  543. /**
  544. * Set the filter for an existing
  545. *
  546. * @param Zend_Filter_Interface $filter
  547. * @param string $markup
  548. *
  549. * @return Zend_Markup_Renderer_RendererAbstract
  550. */
  551. public function setFilter(Zend_Filter_Interface $filter, $markup)
  552. {
  553. $markup = $this->_resolveMarkupName($markup);
  554. $this->_markups[$markup]['filter'] = $filter;
  555. return $this;
  556. }
  557. /**
  558. * Set the default filters
  559. *
  560. * @return void
  561. */
  562. abstract public function addDefaultFilters();
  563. }