RendererAbstract.php 10 KB

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