| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Markup
- * @subpackage Renderer
- * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @see Zend_config
- */
- require_once 'Zend/Config.php';
- /**
- * Defines the basic rendering functionality
- *
- * @category Zend
- * @package Zend_Markup
- * @subpackage Renderer
- * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- abstract class Zend_Markup_Renderer_RendererAbstract
- {
- const TAG_SINGLE = 1;
- const TAG_NORMAL = 2;
- const TYPE_CALLBACK = 4;
- const TYPE_REPLACE = 8;
- const TYPE_ALIAS = 16;
- /**
- * Tag info
- *
- * @var array
- */
- protected $_tags = array();
- /**
- * Parser
- *
- * @var Zend_Markup_Parser_ParserInterface
- */
- protected $_parser;
- /**
- * Use the filter or not
- *
- * @var bool
- */
- protected $_filter = true;
- /**
- * The current group
- *
- * @var string
- */
- protected $_group;
- /**
- * Groups definition
- *
- * @var array
- */
- protected $_groups = array();
- /**
- * Plugin loader for tags
- *
- * @var Zend_Loader_PluginLoader
- */
- protected $_pluginLoader;
- /**
- * Constructor
- *
- * @param array|Zend_Config $options
- *
- * @return void
- */
- public function __construct($options = array())
- {
- if ($options instanceof Zend_Config) {
- $options = $options->toArray();
- }
- if (isset($options['parser'])) {
- $this->setParser($options['parser']);
- }
- if (isset($options['useDefaultTags']) && ($options['useDefaultTags'] == false)) {
- $this->removeDefaultTags();
- }
- }
- /**
- * Set the parser
- *
- * @param Zend_Markup_Parser_ParserInterface $parser
- * @return Zend_Markup_Renderer_RendererAbstract
- */
- public function setParser(Zend_Markup_Parser_ParserInterface $parser)
- {
- $this->_parser = $parser;
- return $this;
- }
- /**
- * Get the parser
- *
- * @return Zend_Markup_Parser_ParserInterface
- */
- public function getParser()
- {
- return $this->_parser;
- }
- /**
- * Get the plugin loader
- *
- * @return Zend_Loader_PluginLoader
- */
- public function getPluginLoader()
- {
- return $this->_pluginLoader;
- }
- /**
- * Add a new tag
- *
- * @param string $name
- * @param string $type
- * @param array $info
- * @return Zend_Markup_Renderer_RendererAbstract
- */
- public function addTag($name, $type, array $info)
- {
- if (!isset($info['group']) && ($type ^ self::TYPE_ALIAS)) {
- require_once 'Zend/Markup/Renderer/Exception.php';
- throw new Zend_Markup_Renderer_Exception("There is no render group defined.");
- }
- if (isset($info['filter'])) {
- $filter = (boolean) $info['filter'];
- } else {
- $filter = true;
- }
- // check the type
- if ($type & self::TYPE_CALLBACK) {
- // add a callback tag
- if (isset($info['callback']) && !($info['callback'] instanceof Zend_Markup_Renderer_TagInterface)) {
- require_once 'Zend/Markup/Renderer/Exception.php';
- throw new Zend_Markup_Renderer_Exception("Not a valid tag callback.");
- } else {
- $info['callback'] = null;
- }
- $info['type'] = $type;
- $info['filter'] = $filter;
- $this->_tags[$name] = $info;
- } elseif ($type & self::TYPE_ALIAS) {
- // add an alias
- if (empty($info['name'])) {
- require_once 'Zend/Markup/Renderer/Exception.php';
- throw new Zend_Markup_Renderer_Exception(
- 'No alias was provided but tag was defined as such');
- }
- $this->_tags[$name] = array(
- 'type' => self::TYPE_ALIAS,
- 'name' => $info['name']
- );
- } else {
- if ($type & self::TAG_SINGLE) {
- // add a single replace tag
- $info['type'] = $type;
- $info['filter'] = $filter;
- $this->_tags[$name] = $info;
- } else {
- // add a replace tag
- $info['type'] = $type;
- $info['filter'] = $filter;
- $this->_tags[$name] = $info;
- }
- }
- return $this;
- }
- /**
- * Remove a tag
- *
- * @param string $name
- *
- * @return void
- */
- protected function removeTag($name)
- {
- unset($this->_tags[$name]);
- }
- /**
- * Remove the default tags
- *
- * @return void
- */
- protected function clearTags()
- {
- $this->_tags = array();
- }
- /**
- * Render function
- *
- * @param Zend_Markup_TokenList|string $tokenList
- * @return string
- */
- public function render($value)
- {
- if ($value instanceof Zend_Markup_TokenList) {
- $tokenList = $value;
- } else {
- $tokenList = $this->getParser()->parse($value);
- }
- $root = $tokenList->current();
- return $this->_render($root);
- }
- /**
- * Render a single token
- *
- * @param Zend_Markup_Token $token
- * @return string
- */
- protected function _render(Zend_Markup_Token $token)
- {
- $return = '';
- $oldFilter = $this->_filter;
- $oldGroup = $this->_group;
- // check filter and group usage in this tag
- if (isset($this->_tags[$token->getName()])) {
- if (isset($this->_tags[$token->getName()]['filter'])
- && $this->_tags[$token->getName()]['filter']
- ) {
- $this->_filter = true;
- } else {
- $this->_filter = false;
- }
- if ($group = $this->_getGroup($token)) {
- $this->_group = $group;
- }
- }
- // if this tag has children, execute them
- if ($token->hasChildren()) {
- foreach ($token->getChildren() as $child) {
- $return .= $this->_execute($child);
- }
- }
- $this->_filter = $oldFilter;
- $this->_group = $oldGroup;
- return $return;
- }
- /**
- * Get the group of a token
- *
- * @param Zend_Markup_Token $token
- * @return string|bool
- */
- protected function _getGroup(Zend_Markup_Token $token)
- {
- if (!isset($this->_tags[$token->getName()])) {
- return false;
- }
- $tag = $this->_tags[$token->getName()];
- // alias processing
- while ($tag['type'] & self::TYPE_ALIAS) {
- $tag = $this->_tags[$tag['name']];
- }
- return isset($tag['group']) ? $tag['group'] : false;
- }
- /**
- * Execute the token
- *
- * @param Zend_Markup_Token $token
- * @return string
- */
- protected function _execute(Zend_Markup_Token $token)
- {
- // first return the normal text tags
- if ($token->getType() == Zend_Markup_Token::TYPE_NONE) {
- return $this->_filter($token->getTag());
- }
- // if the token doesn't have a notation, return the plain text
- if (!isset($this->_tags[$token->getName()])) {
- return $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
- }
- $tag = $this->_tags[$token->getName()];
- $name = $token->getName();
- // alias processing
- while ($tag['type'] & self::TYPE_ALIAS) {
- $name = $tag['name'];
- $tag = $this->_tags[$name];
- }
- // check if the tag has content
- if (($tag['type'] & self::TAG_NORMAL) && !$token->hasChildren()) {
- return '';
- }
- // check for the context
- if (!in_array($tag['group'], $this->_groups[$this->_group])) {
- return $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
- }
- // callback
- if ($tag['type'] & self::TYPE_CALLBACK) {
- // load the callback if the tag doesn't exist
- if (!($tag['callback'] instanceof Zend_Markup_Renderer_TagInterface)) {
- $class = $this->getPluginLoader()->load($name);
- $tag['callback'] = new $class;
- if (!($tag['callback'] instanceof Zend_Markup_Renderer_TagInterface)) {
- require_once 'Zend/Markup/Renderer/Exception.php';
- throw new Zend_Markup_Renderer_Exception("Callback for tag '$name' found, but it isn't valid.");
- }
- }
- if ($tag['type'] & self::TAG_NORMAL) {
- return $tag['callback']->convert($token, $this->_render($token));
- }
- return $tag['callback']->convert($token, null);
- }
- // replace
- if ($tag['type'] & self::TAG_NORMAL) {
- return $this->_executeReplace($token, $tag);
- }
- return $this->_executeSingleReplace($token, $tag);
- }
- // methods that will probably be interhited by subclasses
- /**
- * Execute a replace token
- *
- * @param Zend_Markup_Token $token
- * @param array $tag
- * @return string
- */
- protected function _executeReplace(Zend_Markup_Token $token, $tag)
- {
- return $tag['start'] . $this->_render($token) . $tag['end'];
- }
- /**
- * Execute a single replace token
- *
- * @param Zend_Markup_Token $token
- * @param array $tag
- * @return string
- */
- protected function _executeSingleReplace(Zend_Markup_Token $token, $tag)
- {
- return $tag['replace'];
- }
- /**
- * Abstract filter method
- *
- * @param string $value
- * @return string
- */
- abstract protected function _filter($value);
- }
|