| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* sfWebDebugPanelView adds a panel to the web debug toolbar with information about the view layer.
|
|
|
13 |
*
|
|
|
14 |
* @package symfony
|
|
|
15 |
* @subpackage debug
|
|
|
16 |
* @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
|
|
|
17 |
* @version SVN: $Id: sfWebDebugPanelView.class.php 24069 2009-11-17 06:59:01Z Kris.Wallsmith $
|
|
|
18 |
*/
|
|
|
19 |
class sfWebDebugPanelView extends sfWebDebugPanel
|
|
|
20 |
{
|
|
|
21 |
protected
|
|
|
22 |
$actions = array(),
|
|
|
23 |
$partials = array();
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Constructor.
|
|
|
27 |
*
|
|
|
28 |
* @param sfWebDebug $webDebug The web debug toolbar instance
|
|
|
29 |
*/
|
|
|
30 |
public function __construct(sfWebDebug $webDebug)
|
|
|
31 |
{
|
|
|
32 |
parent::__construct($webDebug);
|
|
|
33 |
|
|
|
34 |
$this->webDebug->getEventDispatcher()->connect('controller.change_action', array($this, 'listenForChangeAction'));
|
|
|
35 |
$this->webDebug->getEventDispatcher()->connect('template.filter_parameters', array($this, 'filterTemplateParameters'));
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Resets the parameter collections.
|
|
|
40 |
*
|
|
|
41 |
* @param sfEvent $event
|
|
|
42 |
*/
|
|
|
43 |
public function listenForChangeAction(sfEvent $event)
|
|
|
44 |
{
|
|
|
45 |
$this->actions = array();
|
|
|
46 |
$this->partials = array();
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Stacks action and partial parameters in the template.filter_parameters event.
|
|
|
51 |
*
|
|
|
52 |
* @param sfEvent $event
|
|
|
53 |
* @param array $parameters
|
|
|
54 |
*
|
|
|
55 |
* @return array
|
|
|
56 |
*/
|
|
|
57 |
public function filterTemplateParameters(sfEvent $event, $parameters)
|
|
|
58 |
{
|
|
|
59 |
$entry = array('parameters' => $parameters);
|
|
|
60 |
|
|
|
61 |
if ('action' == $parameters['sf_type'] && $file = $this->getLastTemplate())
|
|
|
62 |
{
|
|
|
63 |
$this->actions[] = $entry + array('file' => $file);
|
|
|
64 |
}
|
|
|
65 |
else if ('partial' == $parameters['sf_type'] && $file = $this->getLastTemplate('sfPartialView'))
|
|
|
66 |
{
|
|
|
67 |
$this->partials[] = $entry + array('file' => $file);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
return $parameters;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Returns the path to the last template rendered.
|
|
|
75 |
*
|
|
|
76 |
* @param string $class Name of the rendering view class
|
|
|
77 |
*
|
|
|
78 |
* @return string|null
|
|
|
79 |
*/
|
|
|
80 |
protected function getLastTemplate($class = 'sfPHPView')
|
|
|
81 |
{
|
|
|
82 |
foreach (array_reverse($this->webDebug->getLogger()->getLogs()) as $log)
|
|
|
83 |
{
|
|
|
84 |
if (
|
|
|
85 |
($class == $log['type'] || (class_exists($log['type'], false) && is_subclass_of($log['type'], $class)))
|
|
|
86 |
&&
|
|
|
87 |
preg_match('/^Render "(.*)"$/', $log['message'], $match)
|
|
|
88 |
)
|
|
|
89 |
{
|
|
|
90 |
return $match[1];
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* @see sfWebDebugPanel
|
|
|
97 |
*/
|
|
|
98 |
public function getTitle()
|
|
|
99 |
{
|
|
|
100 |
if (count($this->actions) || count($this->partials))
|
|
|
101 |
{
|
|
|
102 |
return '<img src="'.$this->webDebug->getOption('image_root_path').'/view.png" alt="View Layer" /> view';
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* @see sfWebDebugPanel
|
|
|
108 |
*/
|
|
|
109 |
public function getPanelTitle()
|
|
|
110 |
{
|
|
|
111 |
return 'View Layer';
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* @see sfWebDebugPanel
|
|
|
116 |
*/
|
|
|
117 |
public function getPanelContent()
|
|
|
118 |
{
|
|
|
119 |
$html = array();
|
|
|
120 |
|
|
|
121 |
foreach ($this->actions as $action)
|
|
|
122 |
{
|
|
|
123 |
$html[] = $this->renderTemplateInformation($action['file'], $action['parameters']);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
foreach ($this->partials as $partial)
|
|
|
127 |
{
|
|
|
128 |
$html[] = $this->renderTemplateInformation($partial['file'], $partial['parameters'], 'Partial');
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
return join("\n", $html);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Renders information about the passed template and its parameters.
|
|
|
136 |
*
|
|
|
137 |
* The rendered HTML for each parameter is filtered through the "debug.web.view.filter_parameter_html" event.
|
|
|
138 |
*
|
|
|
139 |
* @param string $file The template file path
|
|
|
140 |
* @param array $parameters
|
|
|
141 |
* @param string $label
|
|
|
142 |
*
|
|
|
143 |
* @return string
|
|
|
144 |
*/
|
|
|
145 |
protected function renderTemplateInformation($file, $parameters, $label = 'Template')
|
|
|
146 |
{
|
|
|
147 |
static $i = 0;
|
|
|
148 |
|
|
|
149 |
$parameters = $this->filterCoreParameters($parameters);
|
|
|
150 |
$i++;
|
|
|
151 |
|
|
|
152 |
$html = array();
|
|
|
153 |
$html[] = sprintf('<h2>%s: %s %s</h2>', $label, $this->formatFileLink($file, null, $this->shortenTemplatePath($file)), $this->getToggler('sfWebDebugViewTemplate'.$i));
|
|
|
154 |
$html[] = '<div id="sfWebDebugViewTemplate'.$i.'" style="display:'.(1 == $i ? 'block' : 'none').'">';
|
|
|
155 |
if (count($parameters))
|
|
|
156 |
{
|
|
|
157 |
$html[] = '<p>Parameters:</p>';
|
|
|
158 |
$html[] = '<ul>';
|
|
|
159 |
foreach ($parameters as $name => $parameter)
|
|
|
160 |
{
|
|
|
161 |
$presentation = '<li>'.$this->formatParameterAsHtml($name, $parameter).'</li>';
|
|
|
162 |
$html[] = $this->webDebug->getEventDispatcher()->filter(new sfEvent($this, 'debug.web.view.filter_parameter_html', array('parameter' => $parameter)), $presentation)->getReturnValue();
|
|
|
163 |
}
|
|
|
164 |
$html[] = '</ul>';
|
|
|
165 |
}
|
|
|
166 |
else
|
|
|
167 |
{
|
|
|
168 |
$html[] = '<p>No parameters were passed to this template.</p>';
|
|
|
169 |
}
|
|
|
170 |
$html[] = '</div>';
|
|
|
171 |
|
|
|
172 |
return join("\n", $html);
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* Formats information about a parameter as HTML.
|
|
|
177 |
*
|
|
|
178 |
* @param string $name
|
|
|
179 |
* @param mixed $parameter
|
|
|
180 |
*
|
|
|
181 |
* @return string
|
|
|
182 |
*/
|
|
|
183 |
protected function formatParameterAsHtml($name, $parameter)
|
|
|
184 |
{
|
|
|
185 |
if (!method_exists($this, $method = 'format'.ucwords(gettype($parameter)).'AsHtml'))
|
|
|
186 |
{
|
|
|
187 |
$method = 'getParameterDescription';
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
return $this->$method($name, $parameter);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Formats object information as HTML.
|
|
|
195 |
*
|
|
|
196 |
* @param string $name
|
|
|
197 |
* @param object $parameter
|
|
|
198 |
*
|
|
|
199 |
* @return string
|
|
|
200 |
*/
|
|
|
201 |
protected function formatObjectAsHtml($name, $parameter)
|
|
|
202 |
{
|
|
|
203 |
if ($parameter instanceof sfForm)
|
|
|
204 |
{
|
|
|
205 |
return $this->formatFormAsHtml($name, $parameter);
|
|
|
206 |
}
|
|
|
207 |
else
|
|
|
208 |
{
|
|
|
209 |
return $this->getParameterDescription($name, $parameter);
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Formats form information as HTML.
|
|
|
215 |
*
|
|
|
216 |
* @param string $name
|
|
|
217 |
* @param sfForm $form
|
|
|
218 |
*
|
|
|
219 |
* @return string
|
|
|
220 |
*/
|
|
|
221 |
protected function formatFormAsHtml($name, sfForm $form)
|
|
|
222 |
{
|
|
|
223 |
static $i = 0;
|
|
|
224 |
|
|
|
225 |
$i++;
|
|
|
226 |
|
|
|
227 |
if ($form->hasErrors() && sfLogger::NOTICE < $this->getStatus())
|
|
|
228 |
{
|
|
|
229 |
$this->setStatus(sfLogger::NOTICE);
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
$html = array();
|
|
|
233 |
$html[] = $this->getParameterDescription($name, $form, $form->hasErrors() ? '<code class="sfWebDebugWarning">$%s</code>' : null);
|
|
|
234 |
$html[] = $this->getToggler('sfWebDebugViewForm'.$i);
|
|
|
235 |
$html[] = '<div id="sfWebDebugViewForm'.$i.'" style="display:none">';
|
|
|
236 |
|
|
|
237 |
foreach ($form->getGlobalErrors() as $error)
|
|
|
238 |
{
|
|
|
239 |
$html[] = sprintf('<p><span class="sfWebDebugWarning">%s</span></p>', $error);
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
$html[] = '<ul>'.$this->formatFormFieldSchemaAsHtml($form->getFormFieldSchema(), $name.'[%s]').'</ul>';
|
|
|
243 |
$html[] = '</div>';
|
|
|
244 |
|
|
|
245 |
return join("\n", $html);
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* Formats form field schema information as HTML.
|
|
|
250 |
*
|
|
|
251 |
* @param sfFormFieldSchema $fieldSchema
|
|
|
252 |
* @param string $nameFormat
|
|
|
253 |
*
|
|
|
254 |
* @return string
|
|
|
255 |
*/
|
|
|
256 |
protected function formatFormFieldSchemaAsHtml(sfFormFieldSchema $fieldSchema, $nameFormat = '%s')
|
|
|
257 |
{
|
|
|
258 |
$html = array();
|
|
|
259 |
|
|
|
260 |
foreach ($fieldSchema as $field)
|
|
|
261 |
{
|
|
|
262 |
$name = sprintf($nameFormat, $this->varExport($field->getName()));
|
|
|
263 |
if ($field instanceof sfFormFieldSchema)
|
|
|
264 |
{
|
|
|
265 |
$html[] = $this->formatFormFieldSchemaAsHtml($field, $name.'[%s]');
|
|
|
266 |
}
|
|
|
267 |
else
|
|
|
268 |
{
|
|
|
269 |
$html[] = '<li>';
|
|
|
270 |
$html[] = $this->getParameterDescription($name, $field->getWidget());
|
|
|
271 |
|
|
|
272 |
if ($field->hasError())
|
|
|
273 |
{
|
|
|
274 |
$html[] = sprintf('<p><span class="sfWebDebugWarning">%s</span></p>', $field->getError());
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
$html[] = '</li>';
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
return join("\n", $html);
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
/**
|
|
|
285 |
* Formats information about a parameter as HTML.
|
|
|
286 |
*
|
|
|
287 |
* @param string $name
|
|
|
288 |
* @param mixed $parameter
|
|
|
289 |
*
|
|
|
290 |
* @return string
|
|
|
291 |
*/
|
|
|
292 |
protected function getParameterDescription($name, $parameter, $nameFormat = null, $typeFormat = null)
|
|
|
293 |
{
|
|
|
294 |
if (null === $nameFormat)
|
|
|
295 |
{
|
|
|
296 |
$nameFormat = '<code>$%s</code>';
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
if (null === $typeFormat)
|
|
|
300 |
{
|
|
|
301 |
$typeFormat = '<span class="sfWebDebugDataType">(%s)</span>';
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
return sprintf($nameFormat.' '.$typeFormat, $name, is_object($parameter) ? $this->formatFileLink(get_class($parameter)) : gettype($parameter));
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
/**
|
|
|
308 |
* Shortens an action's template path.
|
|
|
309 |
*
|
|
|
310 |
* @param string $path
|
|
|
311 |
*
|
|
|
312 |
* @return string
|
|
|
313 |
*/
|
|
|
314 |
protected function shortenTemplatePath($path)
|
|
|
315 |
{
|
|
|
316 |
$path = realpath($path);
|
|
|
317 |
|
|
|
318 |
// application module
|
|
|
319 |
$sep = preg_quote(DIRECTORY_SEPARATOR);
|
|
|
320 |
if (preg_match('#modules'.$sep.'(\w+)'.$sep.'templates'.$sep.'(.*)$#', $path, $match))
|
|
|
321 |
{
|
|
|
322 |
return $match[1].' … '.$match[2];
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
return str_replace('SF_ROOT_DIR'.DIRECTORY_SEPARATOR, '', sfDebug::shortenFilePath($path));
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
/**
|
|
|
329 |
* Removes parameters prefixed with "sf_" from the array.
|
|
|
330 |
*
|
|
|
331 |
* @param array $parameters
|
|
|
332 |
*
|
|
|
333 |
* @return array
|
|
|
334 |
*/
|
|
|
335 |
protected function filterCoreParameters($parameters)
|
|
|
336 |
{
|
|
|
337 |
$filtered = array();
|
|
|
338 |
|
|
|
339 |
foreach ($parameters as $name => $value)
|
|
|
340 |
{
|
|
|
341 |
if (0 !== strpos($name, 'sf_'))
|
|
|
342 |
{
|
|
|
343 |
$filtered[$name] = $value;
|
|
|
344 |
}
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
return $filtered;
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
/**
|
|
|
351 |
* Returns a string representation of a value.
|
|
|
352 |
*
|
|
|
353 |
* @param string $value
|
|
|
354 |
*
|
|
|
355 |
* @return string
|
|
|
356 |
*/
|
|
|
357 |
protected function varExport($value)
|
|
|
358 |
{
|
|
|
359 |
if (is_numeric($value))
|
|
|
360 |
{
|
|
|
361 |
$value = (integer) $value;
|
|
|
362 |
}
|
|
|
363 |
|
|
|
364 |
return var_export($value, true);
|
|
|
365 |
}
|
|
|
366 |
}
|