RendererAbstract.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 TAG_SINGLE = 1;
  46. const TAG_NORMAL = 2;
  47. const TYPE_CALLBACK = 4;
  48. const TYPE_REPLACE = 8;
  49. const TYPE_ALIAS = 16;
  50. /**
  51. * Tag info
  52. *
  53. * @var array
  54. */
  55. protected $_markups = array();
  56. /**
  57. * Parser
  58. *
  59. * @var Zend_Markup_Parser_ParserInterface
  60. */
  61. protected $_parser;
  62. /**
  63. * What filter to use
  64. *
  65. * @var bool
  66. */
  67. protected $_filter;
  68. /**
  69. * Filter chain
  70. *
  71. * @var Zend_Filter
  72. */
  73. protected $_defaultFilter;
  74. /**
  75. * The current group
  76. *
  77. * @var string
  78. */
  79. protected $_group;
  80. /**
  81. * Groups definition
  82. *
  83. * @var array
  84. */
  85. protected $_groups = array();
  86. /**
  87. * Plugin loader for tags
  88. *
  89. * @var Zend_Loader_PluginLoader
  90. */
  91. protected $_pluginLoader;
  92. /**
  93. * The current token
  94. *
  95. * @var Zend_Markup_Token
  96. */
  97. protected $_token;
  98. /**
  99. * Constructor
  100. *
  101. * @param array|Zend_Config $options
  102. *
  103. * @return void
  104. */
  105. public function __construct($options = array())
  106. {
  107. if ($options instanceof Zend_Config) {
  108. $options = $options->toArray();
  109. }
  110. if (isset($options['parser'])) {
  111. $this->setParser($options['parser']);
  112. }
  113. if (isset($options['useDefaultTags']) && ($options['useDefaultTags'] === false)) {
  114. $this->removeDefaultTags();
  115. }
  116. if (!isset($options['useDefaultFilters']) || ($options['useDefaultFilters'] === true)) {
  117. $this->addDefaultFilters();
  118. }
  119. if (isset($options['defaultFilter'])) {
  120. $this->addDefaultFilter($options['defaultFilter']);
  121. }
  122. }
  123. /**
  124. * Set the parser
  125. *
  126. * @param Zend_Markup_Parser_ParserInterface $parser
  127. * @return Zend_Markup_Renderer_RendererAbstract
  128. */
  129. public function setParser(Zend_Markup_Parser_ParserInterface $parser)
  130. {
  131. $this->_parser = $parser;
  132. return $this;
  133. }
  134. /**
  135. * Get the parser
  136. *
  137. * @return Zend_Markup_Parser_ParserInterface
  138. */
  139. public function getParser()
  140. {
  141. return $this->_parser;
  142. }
  143. /**
  144. * Get the plugin loader
  145. *
  146. * @return Zend_Loader_PluginLoader
  147. */
  148. public function getPluginLoader()
  149. {
  150. return $this->_pluginLoader;
  151. }
  152. /**
  153. * Add a new markup
  154. *
  155. * @param string $name
  156. * @param string $type
  157. * @param array $options
  158. *
  159. * @return Zend_Markup_Renderer_RendererAbstract
  160. */
  161. public function addMarkup($name, $type, array $options)
  162. {
  163. if (!isset($options['group']) && ($type ^ self::TYPE_ALIAS)) {
  164. require_once 'Zend/Markup/Renderer/Exception.php';
  165. throw new Zend_Markup_Renderer_Exception("There is no render group defined.");
  166. }
  167. // add the filter
  168. if (isset($options['filter'])) {
  169. if ($options['filter'] instanceof Zend_Filter_Interface) {
  170. $filter = $options['filter'];
  171. } elseif ($options['filter'] === true) {
  172. $filter = $this->getDefaultFilter();
  173. } else {
  174. $filter = false;
  175. }
  176. } else {
  177. $filter = $this->getDefaultFilter();
  178. }
  179. // check the type
  180. if ($type & self::TYPE_CALLBACK) {
  181. // add a callback tag
  182. if (isset($options['callback'])) {
  183. if (!($options['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
  184. require_once 'Zend/Markup/Renderer/Exception.php';
  185. throw new Zend_Markup_Renderer_Exception("Not a valid tag callback.");
  186. }
  187. if (method_exists($options['callback'], 'setRenderer')) {
  188. $options['callback']->setRenderer($this);
  189. }
  190. } else {
  191. $options['callback'] = null;
  192. }
  193. $options['type'] = $type;
  194. $options['filter'] = $filter;
  195. $this->_markups[$name] = $options;
  196. } elseif ($type & self::TYPE_ALIAS) {
  197. // add an alias
  198. if (empty($options['name'])) {
  199. require_once 'Zend/Markup/Renderer/Exception.php';
  200. throw new Zend_Markup_Renderer_Exception(
  201. 'No alias was provided but tag was defined as such');
  202. }
  203. $this->_markups[$name] = array(
  204. 'type' => self::TYPE_ALIAS,
  205. 'name' => $options['name']
  206. );
  207. } else {
  208. if ($type & self::TAG_SINGLE) {
  209. // add a single replace tag
  210. $options['type'] = $type;
  211. $options['filter'] = $filter;
  212. $this->_markups[$name] = $options;
  213. } else {
  214. // add a replace tag
  215. $options['type'] = $type;
  216. $options['filter'] = $filter;
  217. $this->_markups[$name] = $options;
  218. }
  219. }
  220. return $this;
  221. }
  222. /**
  223. * Remove a markup
  224. *
  225. * @param string $name
  226. *
  227. * @return void
  228. */
  229. public function removeMarkup($name)
  230. {
  231. unset($this->_markups[$name]);
  232. }
  233. /**
  234. * Remove the default tags
  235. *
  236. * @return void
  237. */
  238. public function clearMarkups()
  239. {
  240. $this->_markups = array();
  241. }
  242. /**
  243. * Render function
  244. *
  245. * @param Zend_Markup_TokenList|string $tokenList
  246. * @return string
  247. */
  248. public function render($value)
  249. {
  250. if ($value instanceof Zend_Markup_TokenList) {
  251. $tokenList = $value;
  252. } else {
  253. $tokenList = $this->getParser()->parse($value);
  254. }
  255. $root = $tokenList->current();
  256. $this->_filter = $this->getDefaultFilter();
  257. return $this->_render($root);
  258. }
  259. /**
  260. * Render a single token
  261. *
  262. * @param Zend_Markup_Token $token
  263. * @return string
  264. */
  265. protected function _render(Zend_Markup_Token $token)
  266. {
  267. $return = '';
  268. // save old values to reset them after the work is done
  269. $oldFilter = $this->_filter;
  270. $oldGroup = $this->_group;
  271. // check filter and group usage in this tag
  272. if (isset($this->_markups[$token->getName()])) {
  273. if (isset($this->_markups[$token->getName()]['filter'])) {
  274. $this->_filter = $this->_markups[$token->getName()]['filter'];
  275. }
  276. if ($group = $this->_getGroup($token)) {
  277. $this->_group = $group;
  278. }
  279. }
  280. $this->_token = $token;
  281. // if this tag has children, execute them
  282. if ($token->hasChildren()) {
  283. foreach ($token->getChildren() as $child) {
  284. $return .= $this->_execute($child);
  285. }
  286. }
  287. // reset to the old values
  288. $this->_filter = $oldFilter;
  289. $this->_group = $oldGroup;
  290. return $return;
  291. }
  292. /**
  293. * Get the group of a token
  294. *
  295. * @param Zend_Markup_Token $token
  296. * @return string|bool
  297. */
  298. protected function _getGroup(Zend_Markup_Token $token)
  299. {
  300. if (!isset($this->_markups[$token->getName()])) {
  301. return false;
  302. }
  303. $tag = $this->_markups[$token->getName()];
  304. // alias processing
  305. while ($tag['type'] & self::TYPE_ALIAS) {
  306. $tag = $this->_markups[$tag['name']];
  307. }
  308. return isset($tag['group']) ? $tag['group'] : false;
  309. }
  310. /**
  311. * Execute the token
  312. *
  313. * @param Zend_Markup_Token $token
  314. * @return string
  315. */
  316. protected function _execute(Zend_Markup_Token $token)
  317. {
  318. // first return the normal text tags
  319. if ($token->getType() == Zend_Markup_Token::TYPE_NONE) {
  320. return $this->_filter($token->getTag());
  321. }
  322. // if the token doesn't have a notation, return the plain text
  323. if (!isset($this->_markups[$token->getName()])) {
  324. $oldToken = $this->_token;
  325. $return = $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
  326. $this->_token = $oldToken;
  327. return $return;
  328. }
  329. $name = $this->_getTagName($token);
  330. $tag = $this->_markups[$name];
  331. // check if the tag has content
  332. if (($tag['type'] & self::TAG_NORMAL) && !$token->hasChildren()) {
  333. return '';
  334. }
  335. // check for the context
  336. if (!in_array($tag['group'], $this->_groups[$this->_group])) {
  337. $oldToken = $this->_token;
  338. $return = $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
  339. $this->_token = $oldToken;
  340. return $return;
  341. }
  342. // check for the filter
  343. if (!isset($tag['filter'])
  344. || (!($tag['filter'] instanceof Zend_Filter_Interface) && ($tag['filter'] !== false))) {
  345. $this->_markups[$name]['filter'] = $this->getDefaultFilter();
  346. }
  347. // callback
  348. if ($tag['type'] & self::TYPE_CALLBACK) {
  349. // load the callback if the tag doesn't exist
  350. if (!($tag['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
  351. $class = $this->getPluginLoader()->load($name);
  352. $tag['callback'] = new $class;
  353. if (!($tag['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
  354. require_once 'Zend/Markup/Renderer/Exception.php';
  355. throw new Zend_Markup_Renderer_Exception("Callback for tag '$name' found, but it isn't valid.");
  356. }
  357. if (method_exists($tag['callback'], 'setRenderer')) {
  358. $tag['callback']->setRenderer($this);
  359. }
  360. }
  361. if ($tag['type'] & self::TAG_NORMAL) {
  362. return $tag['callback']->convert($token, $this->_render($token));
  363. }
  364. return $tag['callback']->convert($token, null);
  365. }
  366. // replace
  367. if ($tag['type'] & self::TAG_NORMAL) {
  368. return $this->_executeReplace($token, $tag);
  369. }
  370. return $this->_executeSingleReplace($token, $tag);
  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 tag name
  388. *
  389. * @param Zend_Markup_Token
  390. *
  391. * @return string
  392. */
  393. protected function _getTagName(Zend_Markup_Token $token)
  394. {
  395. $name = $token->getName();
  396. // process the aliases
  397. while ($this->_markups[$name]['type'] & self::TYPE_ALIAS) {
  398. $name = $this->_markups[$name]['name'];
  399. }
  400. return $name;
  401. }
  402. /**
  403. * Execute a replace token
  404. *
  405. * @param Zend_Markup_Token $token
  406. * @param array $tag
  407. * @return string
  408. */
  409. protected function _executeReplace(Zend_Markup_Token $token, $tag)
  410. {
  411. return $tag['start'] . $this->_render($token) . $tag['end'];
  412. }
  413. /**
  414. * Execute a single replace token
  415. *
  416. * @param Zend_Markup_Token $token
  417. * @param array $tag
  418. * @return string
  419. */
  420. protected function _executeSingleReplace(Zend_Markup_Token $token, $tag)
  421. {
  422. return $tag['replace'];
  423. }
  424. /**
  425. * Get the default filter
  426. *
  427. * @return void
  428. */
  429. public function getDefaultFilter()
  430. {
  431. if (null === $this->_defaultFilter) {
  432. $this->setDefaultFilter();
  433. }
  434. return $this->_defaultFilter;
  435. }
  436. /**
  437. * Add a default filter
  438. *
  439. * @param string $filter
  440. *
  441. * @return void
  442. */
  443. public function addDefaultFilter(Zend_Filter_Interface $filter, $placement = Zend_Filter::CHAIN_APPEND)
  444. {
  445. if (!($this->_defaultFilter instanceof Zend_Filter)) {
  446. $defaultFilter = new Zend_Filter();
  447. $defaultFilter->addFilter($filter);
  448. $this->_defaultFilter = $defaultFilter;
  449. }
  450. $this->_defaultFilter->addFilter($filter, $placement);
  451. }
  452. /**
  453. * Set the default filter
  454. *
  455. * @param Zend_Filter_Interface $filter
  456. *
  457. * @return void
  458. */
  459. public function setDefaultFilter(Zend_Filter_Interface $filter)
  460. {
  461. $this->_defaultFilter = $filter;
  462. }
  463. /**
  464. * Set the default filters
  465. *
  466. * @return void
  467. */
  468. abstract public function addDefaultFilters();
  469. }