PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Nette/Templates/Filters/TemplateFilters.php

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