PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/treeview-master/libs/Nette/Templates/Filters/TemplateFilters.php

https://github.com/indesigner/tests
PHP | 169 lines | 89 code | 34 blank | 46 comment | 7 complexity | 5e0d38641e79b975546c908e5fb4f015 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * @copyright Copyright (c) 2004, 2010 David Grudl
  6. * @license http://nettephp.com/license Nette license
  7. * @link http://nettephp.com
  8. * @category Nette
  9. * @package Nette\Templates
  10. */
  11. /**
  12. * Standard template compile-time filters shipped with Nette Framework.
  13. *
  14. * @copyright Copyright (c) 2004, 2010 David Grudl
  15. * @package Nette\Templates
  16. */
  17. final class TemplateFilters
  18. {
  19. /**
  20. * Static class - cannot be instantiated.
  21. */
  22. final public function __construct()
  23. {
  24. throw new LogicException("Cannot instantiate static class " . get_class($this));
  25. }
  26. /********************* Filter removePhp ****************d*g**/
  27. /**
  28. * Filters out PHP code.
  29. *
  30. * @param string
  31. * @return string
  32. */
  33. public static function removePhp($s)
  34. {
  35. return preg_replace('#\x01@php:p\d+@\x02#', '<?php ?>', $s); // Template hides PHP code in these snippets
  36. }
  37. /********************* Filter relativeLinks ****************d*g**/
  38. /**
  39. * Filter relativeLinks: prepends root to relative links.
  40. * @param string
  41. * @return string
  42. */
  43. public static function relativeLinks($s)
  44. {
  45. return preg_replace(
  46. '#(src|href|action)\s*=\s*(["\'])(?![a-z]+:|[\x01/\\#])#', // \x01 is PHP snippet
  47. '$1=$2<?php echo \\$baseUri ?>',
  48. $s
  49. );
  50. }
  51. /********************* Filter netteLinks ****************d*g**/
  52. /**
  53. * Filter netteLinks: translates links "nette:...".
  54. * nette:destination?arg
  55. * @param string
  56. * @return string
  57. */
  58. public static function netteLinks($s)
  59. {
  60. return preg_replace_callback(
  61. '#(src|href|action)\s*=\s*(["\'])(nette:.*?)([\#"\'])#',
  62. array(__CLASS__, 'netteLinksCb'),
  63. $s
  64. );
  65. }
  66. /**
  67. * Callback for self::netteLinks.
  68. * Parses a "nette" URI (scheme is 'nette') and converts to real URI
  69. */
  70. private static function netteLinksCb($m)
  71. {
  72. list(, $attr, $quote, $uri, $fragment) = $m;
  73. $parts = parse_url($uri);
  74. if (isset($parts['scheme']) && $parts['scheme'] === 'nette') {
  75. return $attr . '=' . $quote . '<?php echo $template->escape($control->'
  76. . "link('"
  77. . (isset($parts['path']) ? $parts['path'] : 'this!')
  78. . (isset($parts['query']) ? '?' . $parts['query'] : '')
  79. . '\'))?>'
  80. . $fragment;
  81. } else {
  82. return $m[0];
  83. }
  84. }
  85. /********************* Filter texyElements ****************d*g**/
  86. /** @var Texy */
  87. public static $texy;
  88. /**
  89. * Process <texy>...</texy> elements.
  90. * @param string
  91. * @return string
  92. */
  93. public static function texyElements($s)
  94. {
  95. return preg_replace_callback(
  96. '#<texy([^>]*)>(.*?)</texy>#s',
  97. array(__CLASS__, 'texyCb'),
  98. $s
  99. );
  100. }
  101. /**
  102. * Callback for self::texyBlocks.
  103. */
  104. private static function texyCb($m)
  105. {
  106. list(, $mAttrs, $mContent) = $m;
  107. // parse attributes
  108. $attrs = array();
  109. if ($mAttrs) {
  110. preg_match_all(
  111. '#([a-z0-9:-]+)\s*(?:=\s*(\'[^\']*\'|"[^"]*"|[^\'"\s]+))?()#isu',
  112. $mAttrs,
  113. $arr,
  114. PREG_SET_ORDER
  115. );
  116. foreach ($arr as $m) {
  117. $key = strtolower($m[1]);
  118. $val = $m[2];
  119. if ($val == NULL) $attrs[$key] = TRUE;
  120. elseif ($val{0} === '\'' || $val{0} === '"') $attrs[$key] = html_entity_decode(substr($val, 1, -1), ENT_QUOTES, 'UTF-8');
  121. else $attrs[$key] = html_entity_decode($val, ENT_QUOTES, 'UTF-8');
  122. }
  123. }
  124. return self::$texy->process($m[2]);
  125. }
  126. }