RendererAbstract.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. * Defines the basic rendering functionality
  24. *
  25. * @category Zend
  26. * @package Zend_Markup
  27. * @subpackage Renderer
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. abstract class Zend_Markup_Renderer_RendererAbstract
  32. {
  33. const TAG_SINGLE = 1;
  34. const TAG_NORMAL = 2;
  35. const TYPE_CALLBACK = 4;
  36. const TYPE_REPLACE = 8;
  37. const TYPE_ALIAS = 16;
  38. /**
  39. * Tag info
  40. *
  41. * @var array
  42. */
  43. protected $_tags = array();
  44. /**
  45. * Parser
  46. *
  47. * @var Zend_Markup_Parser_ParserInterface
  48. */
  49. protected $_parser;
  50. /**
  51. * Use the filter or not
  52. *
  53. * @var bool
  54. */
  55. protected $_filter = true;
  56. /**
  57. * The current group
  58. *
  59. * @var string
  60. */
  61. protected $_group;
  62. /**
  63. * Groups definition
  64. *
  65. * @var array
  66. */
  67. protected $_groups = array();
  68. /**
  69. * Constructor
  70. *
  71. * @param array $options
  72. * @return void
  73. */
  74. public function __construct(array $options = array())
  75. {
  76. if (isset($options['parser'])) {
  77. $this->setParser($options['parser']);
  78. }
  79. if (isset($options['useDefaultTags']) && ($options['useDefaultTags'] == false)) {
  80. $this->removeDefaultTags();
  81. }
  82. }
  83. /**
  84. * Set the parser
  85. *
  86. * @param Zend_Markup_Parser_ParserInterface $parser
  87. * @return Zend_Markup_Renderer_RendererAbstract
  88. */
  89. public function setParser(Zend_Markup_Parser_ParserInterface $parser)
  90. {
  91. $this->_parser = $parser;
  92. return $this;
  93. }
  94. /**
  95. * Get the parser
  96. *
  97. * @return Zend_Markup_Parser_ParserInterface
  98. */
  99. public function getParser()
  100. {
  101. return $this->_parser;
  102. }
  103. /**
  104. * Add a new tag
  105. *
  106. * @param string $name
  107. * @param string $type
  108. * @param array $info
  109. * @return Zend_Markup_Renderer_RendererAbstract
  110. */
  111. public function addTag($name, $type, array $info)
  112. {
  113. if (!isset($info['group']) && ($type ^ self::TYPE_ALIAS)) {
  114. require_once 'Zend/Markup/Renderer/Exception.php';
  115. throw new Zend_Markup_Renderer_Exception("There is no render group defined.");
  116. }
  117. if (isset($info['filter'])) {
  118. $filter = (boolean) $info['filter'];
  119. } else {
  120. $filter = true;
  121. }
  122. // check the type
  123. if ($type & self::TYPE_CALLBACK) {
  124. // add a callback tag
  125. if (!isset($info['callback']) || !is_callable($info['callback'])) {
  126. require_once 'Zend/Markup/Renderer/Exception.php';
  127. throw new Zend_Markup_Renderer_Exception("No valid callback defined for tag '$name'.");
  128. }
  129. $info['type'] = $type;
  130. $info['filter'] = $filter;
  131. $this->_tags[$name] = $info;
  132. } elseif ($type & self::TYPE_ALIAS) {
  133. // add an alias
  134. if (empty($info['name'])) {
  135. require_once 'Zend/Markup/Renderer/Exception.php';
  136. throw new Zend_Markup_Renderer_Exception(
  137. 'No alias was provided but tag was defined as such');
  138. }
  139. $this->_tags[$name] = array(
  140. 'type' => self::TYPE_ALIAS,
  141. 'name' => $info['name']
  142. );
  143. } else {
  144. if ($type & self::TAG_SINGLE) {
  145. // add a single replace tag
  146. $info['type'] = $type;
  147. $info['filter'] = $filter;
  148. $this->_tags[$name] = $info;
  149. } else {
  150. // add a replace tag
  151. $info['type'] = $type;
  152. $info['filter'] = $filter;
  153. $this->_tags[$name] = $info;
  154. }
  155. }
  156. return $this;
  157. }
  158. /**
  159. * Remove a tag
  160. *
  161. * @param string $name
  162. *
  163. * @return void
  164. */
  165. protected function removeTag($name)
  166. {
  167. unset($this->_tags[$name]);
  168. }
  169. /**
  170. * Remove the default tags
  171. *
  172. * @return void
  173. */
  174. protected function clearTags()
  175. {
  176. $this->_tags = array();
  177. }
  178. /**
  179. * Render function
  180. *
  181. * @param Zend_Markup_TokenList|string $tokenList
  182. * @return string
  183. */
  184. public function render($value)
  185. {
  186. if ($value instanceof Zend_Markup_TokenList) {
  187. $tokenList = $value;
  188. } else {
  189. $tokenList = $this->getParser()->parse($value);
  190. }
  191. $root = $tokenList->current();
  192. return $this->_render($root);
  193. }
  194. /**
  195. * Render a single token
  196. *
  197. * @param Zend_Markup_Token $token
  198. * @return string
  199. */
  200. protected function _render(Zend_Markup_Token $token)
  201. {
  202. $return = '';
  203. $oldFilter = $this->_filter;
  204. $oldGroup = $this->_group;
  205. // check filter and group usage in this tag
  206. if (isset($this->_tags[$token->getName()])) {
  207. if (isset($this->_tags[$token->getName()]['filter'])
  208. && $this->_tags[$token->getName()]['filter']
  209. ) {
  210. $this->_filter = true;
  211. } else {
  212. $this->_filter = false;
  213. }
  214. if ($group = $this->_getGroup($token)) {
  215. $this->_group = $group;
  216. }
  217. }
  218. // if this tag has children, execute them
  219. if ($token->hasChildren()) {
  220. foreach ($token->getChildren() as $child) {
  221. $return .= $this->_execute($child);
  222. }
  223. }
  224. $this->_filter = $oldFilter;
  225. $this->_group = $oldGroup;
  226. return $return;
  227. }
  228. /**
  229. * Get the group of a token
  230. *
  231. * @param Zend_Markup_Token $token
  232. * @return string|bool
  233. */
  234. protected function _getGroup(Zend_Markup_Token $token)
  235. {
  236. if (!isset($this->_tags[$token->getName()])) {
  237. return false;
  238. }
  239. $tag = $this->_tags[$token->getName()];
  240. // alias processing
  241. while ($tag['type'] & self::TYPE_ALIAS) {
  242. $tag = $this->_tags[$tag['name']];
  243. }
  244. return isset($tag['group']) ? $tag['group'] : false;
  245. }
  246. /**
  247. * Execute the token
  248. *
  249. * @param Zend_Markup_Token $token
  250. * @return string
  251. */
  252. protected function _execute(Zend_Markup_Token $token)
  253. {
  254. // first return the normal text tags
  255. if ($token->getType() == Zend_Markup_Token::TYPE_NONE) {
  256. return $this->_filter($token->getTag());
  257. }
  258. // if the token doesn't have a notation, return the plain text
  259. if (!isset($this->_tags[$token->getName()])) {
  260. return $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
  261. }
  262. $tag = $this->_tags[$token->getName()];
  263. // alias processing
  264. while ($tag['type'] & self::TYPE_ALIAS) {
  265. $tag = $this->_tags[$tag['name']];
  266. }
  267. // check if the tag has content
  268. if (($tag['type'] & self::TAG_NORMAL) && !$token->hasChildren()) {
  269. return '';
  270. }
  271. // check for the context
  272. if (!in_array($tag['group'], $this->_groups[$this->_group])) {
  273. return $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
  274. }
  275. // callback
  276. if ($tag['type'] & self::TYPE_CALLBACK) {
  277. if ($tag['type'] & self::TAG_NORMAL) {
  278. return call_user_func_array($tag['callback'], array($token, $this->_render($token)));
  279. }
  280. return call_user_func_array($tag['callback'], array($token));
  281. }
  282. // replace
  283. if ($tag['type'] & self::TAG_NORMAL) {
  284. return $this->_executeReplace($token, $tag);
  285. }
  286. return $this->_executeSingleReplace($token, $tag);
  287. }
  288. // methods that will probably be interhited by subclasses
  289. /**
  290. * Execute a replace token
  291. *
  292. * @param Zend_Markup_Token $token
  293. * @param array $tag
  294. * @return string
  295. */
  296. protected function _executeReplace(Zend_Markup_Token $token, $tag)
  297. {
  298. return $tag['start'] . $this->_render($token) . $tag['end'];
  299. }
  300. /**
  301. * Execute a single replace token
  302. *
  303. * @param Zend_Markup_Token $token
  304. * @param array $tag
  305. * @return string
  306. */
  307. protected function _executeSingleReplace(Zend_Markup_Token $token, $tag)
  308. {
  309. return $tag['replace'];
  310. }
  311. /**
  312. * Abstract filter method
  313. *
  314. * @param string $value
  315. * @return string
  316. */
  317. abstract protected function _filter($value);
  318. }