RendererAbstract.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. }
  80. /**
  81. * Set the parser
  82. *
  83. * @param Zend_Markup_Parser_ParserInterface $parser
  84. * @return Zend_Markup_Renderer_RendererAbstract
  85. */
  86. public function setParser(Zend_Markup_Parser_ParserInterface $parser)
  87. {
  88. $this->_parser = $parser;
  89. return $this;
  90. }
  91. /**
  92. * Get the parser
  93. *
  94. * @return Zend_Markup_Parser_ParserInterface
  95. */
  96. public function getParser()
  97. {
  98. return $this->_parser;
  99. }
  100. /**
  101. * Add a new tag
  102. *
  103. * @param string $name
  104. * @param string $type
  105. * @param array $info
  106. * @return Zend_Markup_Renderer_RendererAbstract
  107. */
  108. public function addTag($name, $type, array $info)
  109. {
  110. if (!isset($info['group']) && ($type ^ self::TYPE_ALIAS)) {
  111. require_once 'Zend/Markup/Renderer/Exception.php';
  112. throw new Zend_Markup_Renderer_Exception("There is no render group defined.");
  113. }
  114. if (isset($info['filter'])) {
  115. $filter = (boolean) $info['filter'];
  116. } else {
  117. $filter = true;
  118. }
  119. // check the type
  120. if ($type & self::TYPE_CALLBACK) {
  121. // add a callback tag
  122. if (!isset($info['callback']) || !is_callable($info['callback'])) {
  123. require_once 'Zend/Markup/Renderer/Exception.php';
  124. throw new Zend_Markup_Renderer_Exception("No valid callback defined for tag '$name'.");
  125. }
  126. $info['type'] = $type;
  127. $info['filter'] = $filter;
  128. $this->_tags[$name] = $info;
  129. } elseif ($type & self::TYPE_ALIAS) {
  130. // add an alias
  131. if (empty($info['name'])) {
  132. require_once 'Zend/Markup/Renderer/Exception.php';
  133. throw new Zend_Markup_Renderer_Exception(
  134. 'No alias was provided but tag was defined as such');
  135. }
  136. $this->_tags[$name] = array(
  137. 'type' => self::TYPE_ALIAS,
  138. 'name' => $info['name']
  139. );
  140. } else {
  141. if ($type & self::TAG_SINGLE) {
  142. // add a single replace tag
  143. $info['type'] = $type;
  144. $info['filter'] = $filter;
  145. $this->_tags[$name] = $info;
  146. } else {
  147. // add a replace tag
  148. $info['type'] = $type;
  149. $info['filter'] = $filter;
  150. $this->_tags[$name] = $info;
  151. }
  152. }
  153. return $this;
  154. }
  155. /**
  156. * Render function
  157. *
  158. * @param Zend_Markup_TokenList|string $tokenList
  159. * @return string
  160. */
  161. public function render($value)
  162. {
  163. if ($value instanceof Zend_Markup_TokenList) {
  164. $tokenList = $value;
  165. } else {
  166. $tokenList = $this->getParser()->parse($value);
  167. }
  168. $root = $tokenList->current();
  169. return $this->_render($root);
  170. }
  171. /**
  172. * Render a single token
  173. *
  174. * @param Zend_Markup_Token $token
  175. * @return string
  176. */
  177. protected function _render(Zend_Markup_Token $token)
  178. {
  179. $return = '';
  180. $oldFilter = $this->_filter;
  181. $oldGroup = $this->_group;
  182. // check filter and group usage in this tag
  183. if (isset($this->_tags[$token->getName()])) {
  184. if (isset($this->_tags[$token->getName()]['filter'])
  185. && $this->_tags[$token->getName()]['filter']
  186. ) {
  187. $this->_filter = true;
  188. } else {
  189. $this->_filter = false;
  190. }
  191. if ($group = $this->_getGroup($token)) {
  192. $this->_group = $group;
  193. }
  194. }
  195. // if this tag has children, execute them
  196. if ($token->hasChildren()) {
  197. foreach ($token->getChildren() as $child) {
  198. $return .= $this->_execute($child);
  199. }
  200. }
  201. $this->_filter = $oldFilter;
  202. $this->_group = $oldGroup;
  203. return $return;
  204. }
  205. /**
  206. * Get the group of a token
  207. *
  208. * @param Zend_Markup_Token $token
  209. * @return string|bool
  210. */
  211. protected function _getGroup(Zend_Markup_Token $token)
  212. {
  213. if (!isset($this->_tags[$token->getName()])) {
  214. return false;
  215. }
  216. $tag = $this->_tags[$token->getName()];
  217. // alias processing
  218. while ($tag['type'] & self::TYPE_ALIAS) {
  219. $tag = $this->_tags[$tag['name']];
  220. }
  221. return isset($tag['group']) ? $tag['group'] : false;
  222. }
  223. /**
  224. * Execute the token
  225. *
  226. * @param Zend_Markup_Token $token
  227. * @return string
  228. */
  229. protected function _execute(Zend_Markup_Token $token)
  230. {
  231. // first return the normal text tags
  232. if ($token->getType() == Zend_Markup_Token::TYPE_NONE) {
  233. return $this->_filter($token->getTag());
  234. }
  235. // if the token doesn't have a notation, return the plain text
  236. if (!isset($this->_tags[$token->getName()])) {
  237. return $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
  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. // check if the tag has content
  245. if (($tag['type'] & self::TAG_NORMAL) && !$token->hasChildren()) {
  246. return '';
  247. }
  248. // check for the context
  249. if (!in_array($tag['group'], $this->_groups[$this->_group])) {
  250. return $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
  251. }
  252. // callback
  253. if ($tag['type'] & self::TYPE_CALLBACK) {
  254. if ($tag['type'] & self::TAG_NORMAL) {
  255. return call_user_func_array($tag['callback'], array($token, $this->_render($token)));
  256. }
  257. return call_user_func_array($tag['callback'], array($token));
  258. }
  259. // replace
  260. if ($tag['type'] & self::TAG_NORMAL) {
  261. return $this->_executeReplace($token, $tag);
  262. }
  263. return $this->_executeSingleReplace($token, $tag);
  264. }
  265. // methods that will probably be interhited by subclasses
  266. /**
  267. * Execute a replace token
  268. *
  269. * @param Zend_Markup_Token $token
  270. * @param array $tag
  271. * @return string
  272. */
  273. protected function _executeReplace(Zend_Markup_Token $token, $tag)
  274. {
  275. return $tag['start'] . $this->_render($token) . $tag['end'];
  276. }
  277. /**
  278. * Execute a single replace token
  279. *
  280. * @param Zend_Markup_Token $token
  281. * @param array $tag
  282. * @return string
  283. */
  284. protected function _executeSingleReplace(Zend_Markup_Token $token, $tag)
  285. {
  286. return $tag['replace'];
  287. }
  288. /**
  289. * Abstract filter method
  290. *
  291. * @param string $value
  292. * @return string
  293. */
  294. abstract protected function _filter($value);
  295. }