1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
|
<?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: RendererAbstract.php 22198 2010-05-19 13:33:52Z kokx $
*/
/**
* @see Zend_config
*/
require_once 'Zend/Config.php';
/**
* @see Zend_Filter
*/
require_once 'Zend/Filter.php';
/**
* @see Zend_Markup_Renderer_TokenConverterInterface
*/
require_once 'Zend/Markup/Renderer/TokenConverterInterface.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 TYPE_CALLBACK = 4;
const TYPE_REPLACE = 8;
const TYPE_ALIAS = 16;
/**
* Tag info
*
* @var array
*/
protected $_markups = array();
/**
* Parser
*
* @var Zend_Markup_Parser_ParserInterface
*/
protected $_parser;
/**
* What filter to use
*
* @var bool
*/
protected $_filter;
/**
* Filter chain
*
* @var Zend_Filter
*/
protected $_defaultFilter;
/**
* The current group
*
* @var string
*/
protected $_group;
/**
* Groups definition
*
* @var array
*/
protected $_groups = array();
/**
* Plugin loader for tags
*
* @var Zend_Loader_PluginLoader
*/
protected $_pluginLoader;
/**
* The current token
*
* @var Zend_Markup_Token
*/
protected $_token;
/**
* Encoding
*
* @var string
*/
protected static $_encoding = 'UTF-8';
/**
* Constructor
*
* @param array|Zend_Config $options
*
* @return void
*/
public function __construct($options = array())
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
if (isset($options['encoding'])) {
$this->setEncoding($options['encoding']);
}
if (isset($options['parser'])) {
$this->setParser($options['parser']);
}
if (!isset($options['useDefaultFilters']) || ($options['useDefaultFilters'] === true)) {
$this->addDefaultFilters();
}
if (isset($options['defaultFilter'])) {
$this->addDefaultFilter($options['defaultFilter']);
}
}
/**
* 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;
}
/**
* Set the renderer's encoding
*
* @param string $encoding
*
* @return void
*/
public static function setEncoding($encoding)
{
self::$_encoding = $encoding;
}
/**
* Get the renderer's encoding
*
* @return string
*/
public static function getEncoding()
{
return self::$_encoding;
}
/**
* Add a new markup
*
* @param string $name
* @param string $type
* @param array $options
*
* @return Zend_Markup_Renderer_RendererAbstract
*/
public function addMarkup($name, $type, array $options)
{
if (!isset($options['group']) && ($type ^ self::TYPE_ALIAS)) {
require_once 'Zend/Markup/Renderer/Exception.php';
throw new Zend_Markup_Renderer_Exception("There is no render group defined.");
}
// add the filter
if (isset($options['filter'])) {
if ($options['filter'] instanceof Zend_Filter_Interface) {
$filter = $options['filter'];
} elseif ($options['filter'] === true) {
$filter = $this->getDefaultFilter();
} else {
$filter = false;
}
} else {
$filter = $this->getDefaultFilter();
}
// check the type
if ($type & self::TYPE_CALLBACK) {
// add a callback tag
if (isset($options['callback'])) {
if (!($options['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
require_once 'Zend/Markup/Renderer/Exception.php';
throw new Zend_Markup_Renderer_Exception("Not a valid tag callback.");
}
if (method_exists($options['callback'], 'setRenderer')) {
$options['callback']->setRenderer($this);
}
} else {
$options['callback'] = null;
}
$options['type'] = $type;
$options['filter'] = $filter;
$this->_markups[$name] = $options;
} elseif ($type & self::TYPE_ALIAS) {
// add an alias
if (empty($options['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->_markups[$name] = array(
'type' => self::TYPE_ALIAS,
'name' => $options['name']
);
} else {
if ($type && array_key_exists('empty', $options) && $options['empty']) {
// add a single replace markup
$options['type'] = $type;
$options['filter'] = $filter;
$this->_markups[$name] = $options;
} else {
// add a replace markup
$options['type'] = $type;
$options['filter'] = $filter;
$this->_markups[$name] = $options;
}
}
return $this;
}
/**
* Remove a markup
*
* @param string $name
*
* @return void
*/
public function removeMarkup($name)
{
unset($this->_markups[$name]);
}
/**
* Remove the default tags
*
* @return void
*/
public function clearMarkups()
{
$this->_markups = 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();
$this->_filter = $this->getDefaultFilter();
return $this->_render($root);
}
/**
* Render a single token
*
* @param Zend_Markup_Token $token
* @return string
*/
protected function _render(Zend_Markup_Token $token)
{
$return = '';
$this->_token = $token;
// if this tag has children, execute them
if ($token->hasChildren()) {
foreach ($token->getChildren() as $child) {
$return .= $this->_execute($child);
}
}
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->_markups[$token->getName()])) {
return false;
}
$tag = $this->_markups[$token->getName()];
// alias processing
while ($tag['type'] & self::TYPE_ALIAS) {
$tag = $this->_markups[$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->_markups[$token->getName()])) {
$oldToken = $this->_token;
$return = $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
$this->_token = $oldToken;
return $return;
}
$name = $this->_getMarkupName($token);
$markup = (!$name) ? false : $this->_markups[$name];
$empty = (is_array($markup) && array_key_exists('empty', $markup) && $markup['empty']);
// check if the tag has content
if (!$empty && !$token->hasChildren()) {
return '';
}
// check for the context
if (is_array($markup) && !in_array($markup['group'], $this->_groups[$this->_group])) {
$oldToken = $this->_token;
$return = $this->_filter($token->getTag()) . $this->_render($token) . $token->getStopper();
$this->_token = $oldToken;
return $return;
}
// check for the filter
if (!isset($markup['filter'])
|| (!($markup['filter'] instanceof Zend_Filter_Interface) && ($markup['filter'] !== false))) {
$this->_markups[$name]['filter'] = $this->getDefaultFilter();
}
// save old values to reset them after the work is done
$oldFilter = $this->_filter;
$oldGroup = $this->_group;
$return = '';
// set the filter and the group
$this->_filter = $this->getFilter($name);
if ($group = $this->_getGroup($token)) {
$this->_group = $group;
}
// callback
if (is_array($markup) && ($markup['type'] & self::TYPE_CALLBACK)) {
// load the callback if the tag doesn't exist
if (!($markup['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
$class = $this->getPluginLoader()->load($name);
$markup['callback'] = new $class;
if (!($markup['callback'] instanceof Zend_Markup_Renderer_TokenConverterInterface)) {
require_once 'Zend/Markup/Renderer/Exception.php';
throw new Zend_Markup_Renderer_Exception("Callback for tag '$name' found, but it isn't valid.");
}
if (method_exists($markup['callback'], 'setRenderer')) {
$markup['callback']->setRenderer($this);
}
}
if ($markup['type'] && !$empty) {
$return = $markup['callback']->convert($token, $this->_render($token));
} else {
$return = $markup['callback']->convert($token, null);
}
} else {
// replace
if ($markup['type'] && !$empty) {
$return = $this->_executeReplace($token, $markup);
} else {
$return = $this->_executeSingleReplace($token, $markup);
}
}
// reset to the old values
$this->_filter = $oldFilter;
$this->_group = $oldGroup;
return $return;
}
/**
* Filter method
*
* @param string $value
*
* @return string
*/
protected function _filter($value)
{
if ($this->_filter instanceof Zend_Filter_Interface) {
return $this->_filter->filter($value);
}
return $value;
}
/**
* Get the markup name
*
* @param Zend_Markup_Token
*
* @return string
*/
protected function _getMarkupName(Zend_Markup_Token $token)
{
$name = $token->getName();
if (empty($name)) {
return false;
}
return $this->_resolveMarkupName($name);
}
/**
* Resolve aliases for a markup name
*
* @param string $name
*
* @return string
*/
protected function _resolveMarkupName($name)
{
while (($type = $this->_getMarkupType($name))
&& ($type & self::TYPE_ALIAS)
) {
$name = $this->_markups[$name]['name'];
}
return $name;
}
/**
* Retrieve markup type
*
* @param string $name
* @return false|int
*/
protected function _getMarkupType($name)
{
if (!isset($this->_markups[$name])) {
return false;
}
if (!isset($this->_markups[$name]['type'])) {
return false;
}
return $this->_markups[$name]['type'];
}
/**
* 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'];
}
/**
* Get the default filter
*
* @return void
*/
public function getDefaultFilter()
{
if (null === $this->_defaultFilter) {
$this->addDefaultFilters();
}
return $this->_defaultFilter;
}
/**
* Add a default filter
*
* @param string $filter
*
* @return void
*/
public function addDefaultFilter(Zend_Filter_Interface $filter, $placement = Zend_Filter::CHAIN_APPEND)
{
if (!($this->_defaultFilter instanceof Zend_Filter)) {
$defaultFilter = new Zend_Filter();
$defaultFilter->addFilter($filter);
$this->_defaultFilter = $defaultFilter;
}
$this->_defaultFilter->addFilter($filter, $placement);
}
/**
* Set the default filter
*
* @param Zend_Filter_Interface $filter
*
* @return void
*/
public function setDefaultFilter(Zend_Filter_Interface $filter)
{
$this->_defaultFilter = $filter;
}
/**
* Get the filter for an existing markup
*
* @param string $markup
*
* @return Zend_Filter_Interface
*/
public function getFilter($markup)
{
$markup = $this->_resolveMarkupName($markup);
if (!isset($this->_markups[$markup]['filter'])
|| !($this->_markups[$markup]['filter'] instanceof Zend_Filter_Interface)
) {
if (isset($this->_markups[$markup]['filter']) && $this->_markups[$markup]['filter']) {
$this->_markups[$markup]['filter'] = $this->getDefaultFilter();
} else {
return false;
}
}
return $this->_markups[$markup]['filter'];
}
/**
* Add a filter for an existing markup
*
* @param Zend_Filter_Interface $filter
* @param string $markup
* @param string $placement
*
* @return Zend_Markup_Renderer_RendererAbstract
*/
public function addFilter(Zend_Filter_Interface $filter, $markup, $placement = Zend_Filter::CHAIN_APPEND)
{
$markup = $this->_resolveMarkupName($markup);
$oldFilter = $this->getFilter($markup);
// if this filter is the default filter, clone it first
if ($oldFilter === $this->getDefaultFilter()) {
$oldFilter = clone $oldFilter;
}
if (!($oldFilter instanceof Zend_Filter)) {
$this->_markups[$markup]['filter'] = new Zend_Filter();
if ($oldFilter instanceof Zend_Filter_Interface) {
$this->_markups[$markup]['filter']->addFilter($oldFilter);
}
} else {
$this->_markups[$markup]['filter'] = $oldFilter;
}
$this->_markups[$markup]['filter']->addFilter($filter, $placement);
return $this;
}
/**
* Set the filter for an existing
*
* @param Zend_Filter_Interface $filter
* @param string $markup
*
* @return Zend_Markup_Renderer_RendererAbstract
*/
public function setFilter(Zend_Filter_Interface $filter, $markup)
{
$markup = $this->_resolveMarkupName($markup);
$this->_markups[$markup]['filter'] = $filter;
return $this;
}
/**
* Add a render group
*
* @param string $name
* @param array $allowedInside
* @param array $allowsInside
*
* @return void
*/
public function addGroup($name, array $allowedInside = array(), array $allowsInside = array())
{
$this->_groups[$name] = $allowsInside;
foreach ($allowedInside as $group) {
$this->_groups[$group][] = $name;
}
}
/**
* Get group definitions
*
* @return array
*/
public function getGroups()
{
return $this->_groups;
}
/**
* Set the default filters
*
* @return void
*/
abstract public function addDefaultFilters();
}
|