Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
CodeExtension.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Bridge\Twig\Extension;
013
014 /**
015 * Twig extension relate to PHP code and used by the profiler and the default exception templates.
016 *
017 * @author Fabien Potencier <fabien@symfony.com>
018 */
019 class CodeExtension extends \Twig_Extension
020 {
021 private $fileLinkFormat;
022 private $rootDir;
023 private $charset;
024
025 /**
026 * Constructor.
027 *
028 * @param string $fileLinkFormat The format for links to source files
029 * @param string $rootDir The project root directory
030 * @param string $charset The charset
031 */
032 public function __construct($fileLinkFormat, $rootDir, $charset)
033 {
034 $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
035 $this->rootDir = str_replace('/', DIRECTORY_SEPARATOR, dirname($rootDir)).DIRECTORY_SEPARATOR;
036 $this->charset = $charset;
037 }
038
039 /**
040 * {@inheritdoc}
041 */
042 public function getFilters()
043 {
044 return array(
045 new \Twig_SimpleFilter('abbr_class', array($this, 'abbrClass'), array('is_safe' => array('html'))),
046 new \Twig_SimpleFilter('abbr_method', array($this, 'abbrMethod'), array('is_safe' => array('html'))),
047 new \Twig_SimpleFilter('format_args', array($this, 'formatArgs'), array('is_safe' => array('html'))),
048 new \Twig_SimpleFilter('format_args_as_text', array($this, 'formatArgsAsText')),
049 new \Twig_SimpleFilter('file_excerpt', array($this, 'fileExcerpt'), array('is_safe' => array('html'))),
050 new \Twig_SimpleFilter('format_file', array($this, 'formatFile'), array('is_safe' => array('html'))),
051 new \Twig_SimpleFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
052 new \Twig_SimpleFilter('file_link', array($this, 'getFileLink')),
053 );
054 }
055
056 public function abbrClass($class)
057 {
058 $parts = explode('\\', $class);
059 $short = array_pop($parts);
060
061 return sprintf('<abbr title="%s">%s</abbr>', $class, $short);
062 }
063
064 public function abbrMethod($method)
065 {
066 if (false !== strpos($method, '::')) {
067 list($class, $method) = explode('::', $method, 2);
068 $result = sprintf('%s::%s()', $this->abbrClass($class), $method);
069 } elseif ('Closure' === $method) {
070 $result = sprintf('<abbr title="%s">%s</abbr>', $method, $method);
071 } else {
072 $result = sprintf('<abbr title="%s">%s</abbr>()', $method, $method);
073 }
074
075 return $result;
076 }
077
078 /**
079 * Formats an array as a string.
080 *
081 * @param array $args The argument array
082 *
083 * @return string
084 */
085 public function formatArgs($args)
086 {
087 $result = array();
088 foreach ($args as $key => $item) {
089 if ('object' === $item[0]) {
090 $parts = explode('\\', $item[1]);
091 $short = array_pop($parts);
092 $formattedValue = sprintf('<em>object</em>(<abbr title="%s">%s</abbr>)', $item[1], $short);
093 } elseif ('array' === $item[0]) {
094 $formattedValue = sprintf('<em>array</em>(%s)', is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
095 } elseif ('string' === $item[0]) {
096 $formattedValue = sprintf("'%s'", htmlspecialchars($item[1], ENT_QUOTES, $this->charset));
097 } elseif ('null' === $item[0]) {
098 $formattedValue = '<em>null</em>';
099 } elseif ('boolean' === $item[0]) {
100 $formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
101 } elseif ('resource' === $item[0]) {
102 $formattedValue = '<em>resource</em>';
103 } else {
104 $formattedValue = str_replace("\n", '', var_export(htmlspecialchars((string) $item[1], ENT_QUOTES, $this->charset), true));
105 }
106
107 $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);
108 }
109
110 return implode(', ', $result);
111 }
112
113 /**
114 * Formats an array as a string.
115 *
116 * @param array $args The argument array
117 *
118 * @return string
119 */
120 public function formatArgsAsText($args)
121 {
122 return strip_tags($this->formatArgs($args));
123 }
124
125 /**
126 * Returns an excerpt of a code file around the given line number.
127 *
128 * @param string $file A file path
129 * @param int $line The selected line number
130 *
131 * @return string An HTML string
132 */
133 public function fileExcerpt($file, $line)
134 {
135 if (is_readable($file)) {
136 // highlight_file could throw warnings
137 // see https://bugs.php.net/bug.php?id=25725
138 $code = @highlight_file($file, true);
139 // remove main code/span tags
140 $code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
141 $content = preg_split('#<br />#', $code);
142
143 $lines = array();
144 for ($i = max($line - 3, 1), $max = min($line + 3, count($content)); $i <= $max; ++$i) {
145 $lines[] = '<li'.($i == $line ? ' class="selected"' : '').'><code>'.self::fixCodeMarkup($content[$i - 1]).'</code></li>';
146 }
147
148 return '<ol start="'.max($line - 3, 1).'">'.implode("\n", $lines).'</ol>';
149 }
150 }
151
152 /**
153 * Formats a file path.
154 *
155 * @param string $file An absolute file path
156 * @param int $line The line number
157 * @param string $text Use this text for the link rather than the file path
158 *
159 * @return string
160 */
161 public function formatFile($file, $line, $text = null)
162 {
163 $file = trim($file);
164
165 if (null === $text) {
166 $text = str_replace('/', DIRECTORY_SEPARATOR, $file);
167 if (0 === strpos($text, $this->rootDir)) {
168 $text = substr($text, strlen($this->rootDir));
169 $text = explode(DIRECTORY_SEPARATOR, $text, 2);
170 $text = sprintf('<abbr title="%s%2$s">%s</abbr>%s', $this->rootDir, $text[0], isset($text[1]) ? DIRECTORY_SEPARATOR.$text[1] : '');
171 }
172 }
173
174 $text = "$text at line $line";
175
176 if (false !== $link = $this->getFileLink($file, $line)) {
177 if (PHP_VERSION_ID >= 50400) {
178 $flags = ENT_QUOTES | ENT_SUBSTITUTE;
179 } else {
180 $flags = ENT_QUOTES;
181 }
182
183 return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, $flags, $this->charset), $text);
184 }
185
186 return $text;
187 }
188
189 /**
190 * Returns the link for a given file/line pair.
191 *
192 * @param string $file An absolute file path
193 * @param int $line The line number
194 *
195 * @return string A link of false
196 */
197 public function getFileLink($file, $line)
198 {
199 if ($this->fileLinkFormat && is_file($file)) {
200 return strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line));
201 }
202
203 return false;
204 }
205
206 public function formatFileFromText($text)
207 {
208 $that = $this;
209
210 return preg_replace_callback('/in ("|")?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) use ($that) {
211 return 'in '.$that->formatFile($match[2], $match[3]);
212 }, $text);
213 }
214
215 /**
216 * {@inheritdoc}
217 */
218 public function getName()
219 {
220 return 'code';
221 }
222
223 protected static function fixCodeMarkup($line)
224 {
225 // </span> ending tag from previous line
226 $opening = strpos($line, '<span');
227 $closing = strpos($line, '</span>');
228 if (false !== $closing && (false === $opening || $closing < $opening)) {
229 $line = substr_replace($line, '', $closing, 7);
230 }
231
232 // missing </span> tag at the end of line
233 $opening = strpos($line, '<span');
234 $closing = strpos($line, '</span>');
235 if (false !== $opening && (false === $closing || $closing > $opening)) {
236 $line .= '</span>';
237 }
238
239 return $line;
240 }
241 }
242