RendererAbstract.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 $_tags = array();
  56. /**
  57. * Parser
  58. *
  59. * @var Zend_Markup_Parser_ParserInterface
  60. */
  61. protected $_parser;
  62. /**
  63. * Use the filter or not
  64. *
  65. * @var bool
  66. */
  67. protected $_filter = true;
  68. /**
  69. * Filter chain
  70. *
  71. * @var Zend_Filter
  72. */
  73. protected $_filterChain;
  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['filter'])) {
  117. $this->getFilterChain()->addFilter($options['filter']);
  118. }
  119. }
  120. /**
  121. * Set the parser
  122. *
  123. * @param Zend_Markup_Parser_ParserInterface $parser
  124. * @return Zend_Markup_Renderer_RendererAbstract
  125. */
  126. public function setParser(Zend_Markup_Parser_ParserInterface $parser)
  127. {
  128. $this->_parser = $parser;
  129. return $this;
  130. }
  131. /**
  132. * Get the parser
  133. *
  134. * @return Zend_Markup_Parser_ParserInterface
  135. */
  136. public function getParser()
  137. {
  138. return $this->_parser;
  139. }
  140. /**
  141. * Get the plugin loader
  142. *
  143. * @return Zend_Loader_PluginLoader
  144. */
  145. public function getPluginLoader()
  146. {
  147. return $this->_pluginLoader;
  148. }
  149. /**
  150. * Get the filter chain
  151. *
  152. * @return Zend_Filter
  153. */
  154. public function getFilterChain()
  155. {
  156. if (null === $this->_filterChain) {
  157. $this->_filterChain = new Zend_Filter();
  158. }
  159. return $this->_filterChain;
  160. }
  161. /**
  162. * Add a filter
  163. *
  164. * @param Zend_Filter_Interface $filter
  165. *
  166. * @return void
  167. */
  168. public function addFilter(Zend_Filter_Interface $filter)
  169. {
  170. $this->getFilterChain()->addFilter($filter);
  171. }
  172. /**
  173. * Add a new tag
  174. *
  175. * @param string $name
  176. * @param string $type
  177. * @param array $info
  178. *
  179. * @return Zend_Markup_Renderer_RendererAbstract
  180. */
  181. public function addTag($name, $type, array $info)
  182. {
  183. if (!isset($info['group']) && ($type ^ self::TYPE_ALIAS)) {
  184. require_once 'Zend/Markup/Renderer/Exception.php';
  185. throw new Zend_Markup_Renderer_Exception("There is no render group defined.");
  186. }
  187. if (isset($info['filter'])) {
  188. $filter = (boolean) $info['filter'];
  189. } else {
  190. $filter = true;
  191. }
  192. // check the type
  193. if ($type & self::TYPE_CALLBACK) {
  194. // add a callback tag
  195. if (isset($info['callback'])) {
  196. if (!($info['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
  197. require_once 'Zend/Markup/Renderer/Exception.php';
  198. throw new Zend_Markup_Renderer_Exception("Not a valid tag callback.");
  199. }
  200. if (method_exists($info['callback'], 'setRenderer')) {
  201. $info['callback']->setRenderer($this);
  202. }
  203. } else {
  204. $info['callback'] = null;
  205. }
  206. $info['type'] = $type;
  207. $info['filter'] = $filter;
  208. $this->_tags[$name] = $info;
  209. } elseif ($type & self::TYPE_ALIAS) {
  210. // add an alias
  211. if (empty($info['name'])) {
  212. require_once 'Zend/Markup/Renderer/Exception.php';
  213. throw new Zend_Markup_Renderer_Exception(
  214. 'No alias was provided but tag was defined as such');
  215. }
  216. $this->_tags[$name] = array(
  217. 'type' => self::TYPE_ALIAS,
  218. 'name' => $info['name']
  219. );
  220. } else {
  221. if ($type & self::TAG_SINGLE) {
  222. // add a single replace tag
  223. $info['type'] = $type;
  224. $info['filter'] = $filter;
  225. $this->_tags[$name] = $info;
  226. } else {
  227. // add a replace tag
  228. $info['type'] = $type;
  229. $info['filter'] = $filter;
  230. $this->_tags[$name] = $info;
  231. }
  232. }
  233. return $this;
  234. }
  235. /**
  236. * Remove a tag
  237. *
  238. * @param string $name
  239. *
  240. * @return void
  241. */
  242. public function removeTag($name)
  243. {
  244. unset($this->_tags[$name]);
  245. }
  246. /**
  247. * Remove the default tags
  248. *
  249. * @return void
  250. */
  251. public function clearTags()
  252. {
  253. $this->_tags = array();
  254. }
  255. /**
  256. * Render function
  257. *
  258. * @param Zend_Markup_TokenList|string $tokenList
  259. * @return string
  260. */
  261. public function render($value)
  262. {
  263. if ($value instanceof Zend_Markup_TokenList) {
  264. $tokenList = $value;
  265. } else {
  266. $tokenList = $this->getParser()->parse($value);
  267. }
  268. $root = $tokenList->current();
  269. return $this->_render($root);
  270. }
  271. /**
  272. * Render a single token
  273. *
  274. * @param Zend_Markup_Token $token
  275. * @return string
  276. */
  277. protected function _render(Zend_Markup_Token $token)
  278. {
  279. $return = '';
  280. // save old values to reset them after the work is done
  281. $oldFilter = $this->_filter;
  282. $oldGroup = $this->_group;
  283. $oldToken = $this->_token;
  284. // check filter and group usage in this tag
  285. if (isset($this->_tags[$token->getName()])) {
  286. if (isset($this->_tags[$token->getName()]['filter'])
  287. && $this->_tags[$token->getName()]['filter']
  288. ) {
  289. $this->_filter = true;
  290. } else {
  291. $this->_filter = false;
  292. }
  293. if ($group = $this->_getGroup($token)) {
  294. $this->_group = $group;
  295. }
  296. }
  297. $this->_token = $token;
  298. // if this tag has children, execute them
  299. if ($token->hasChildren()) {
  300. foreach ($token->getChildren() as $child) {
  301. $return .= $this->_execute($child);
  302. }
  303. }
  304. // reset to the old values
  305. $this->_token = $oldToken;
  306. $this->_filter = $oldFilter;
  307. $this->_group = $oldGroup;
  308. return $return;
  309. }
  310. /**
  311. * Get the group of a token
  312. *
  313. * @param Zend_Markup_Token $token
  314. * @return string|bool
  315. */
  316. protected function _getGroup(Zend_Markup_Token $token)
  317. {
  318. if (!isset($this->_tags[$token->getName()])) {
  319. return false;
  320. }
  321. $tag = $this->_tags[$token->getName()];
  322. // alias processing
  323. while ($tag['type'] & self::TYPE_ALIAS) {
  324. $tag = $this->_tags[$tag['name']];
  325. }
  326. return isset($tag['group']) ? $tag['group'] : false;
  327. }
  328. /**
  329. * Execute the token
  330. *
  331. * @param Zend_Markup_Token $token
  332. * @return string
  333. */
  334. protected function _execute(Zend_Markup_Token $token)
  335. {
  336. // first return the normal text tags
  337. if ($token->getType() == Zend_Markup_Token::TYPE_NONE) {
  338. return $this->_filter($token->getTag());
  339. }
  340. // if the token doesn't have a notation, return the plain text
  341. if (!isset($this->_tags[$token->getName()])) {
  342. return $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
  343. }
  344. $name = $this->_getTagName($token);
  345. $tag = $this->_tags[$name];
  346. // check if the tag has content
  347. if (($tag['type'] & self::TAG_NORMAL) && !$token->hasChildren()) {
  348. return '';
  349. }
  350. // check for the context
  351. if (!in_array($tag['group'], $this->_groups[$this->_group])) {
  352. return $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
  353. }
  354. // callback
  355. if ($tag['type'] & self::TYPE_CALLBACK) {
  356. // load the callback if the tag doesn't exist
  357. if (!($tag['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
  358. $class = $this->getPluginLoader()->load($name);
  359. $tag['callback'] = new $class;
  360. if (!($tag['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
  361. require_once 'Zend/Markup/Renderer/Exception.php';
  362. throw new Zend_Markup_Renderer_Exception("Callback for tag '$name' found, but it isn't valid.");
  363. }
  364. if (method_exists($tag['callback'], 'setRenderer')) {
  365. $tag['callback']->setRenderer($this);
  366. }
  367. }
  368. if ($tag['type'] & self::TAG_NORMAL) {
  369. return $tag['callback']->convert($token, $this->_render($token));
  370. }
  371. return $tag['callback']->convert($token, null);
  372. }
  373. // replace
  374. if ($tag['type'] & self::TAG_NORMAL) {
  375. return $this->_executeReplace($token, $tag);
  376. }
  377. return $this->_executeSingleReplace($token, $tag);
  378. }
  379. /**
  380. * Get the tag name
  381. *
  382. * @param Zend_Markup_Token
  383. *
  384. * @return string
  385. */
  386. protected function _getTagName(Zend_Markup_Token $token)
  387. {
  388. $name = $token->getName();
  389. // process the aliases
  390. while ($this->_tags[$name]['type'] & self::TYPE_ALIAS) {
  391. $name = $this->_tags[$name]['name'];
  392. }
  393. return $name;
  394. }
  395. /**
  396. * Filter method
  397. *
  398. * @param string $value
  399. *
  400. * @return string
  401. */
  402. protected function _filter($value)
  403. {
  404. if ($this->_filter) {
  405. return $this->getFilterChain()->filter($value);
  406. }
  407. return $value;
  408. }
  409. /**
  410. * Execute a replace token
  411. *
  412. * @param Zend_Markup_Token $token
  413. * @param array $tag
  414. * @return string
  415. */
  416. protected function _executeReplace(Zend_Markup_Token $token, $tag)
  417. {
  418. return $tag['start'] . $this->_render($token) . $tag['end'];
  419. }
  420. /**
  421. * Execute a single replace token
  422. *
  423. * @param Zend_Markup_Token $token
  424. * @param array $tag
  425. * @return string
  426. */
  427. protected function _executeSingleReplace(Zend_Markup_Token $token, $tag)
  428. {
  429. return $tag['replace'];
  430. }
  431. }