RendererAbstract.php 11 KB

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