sfWebDebugPanelView.class.php 9.11 KB
Newer Older
Игорь's avatar
init    
Игорь committed
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
<?php

/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfWebDebugPanelView adds a panel to the web debug toolbar with information about the view layer.
 * 
 * @package     symfony
 * @subpackage  debug
 * @author      Kris Wallsmith <kris.wallsmith@symfony-project.com>
 * @version     SVN: $Id: sfWebDebugPanelView.class.php 24069 2009-11-17 06:59:01Z Kris.Wallsmith $
 */
class sfWebDebugPanelView extends sfWebDebugPanel
{
  protected
    $actions  = array(),
    $partials = array();

  /**
   * Constructor.
   *
   * @param sfWebDebug $webDebug The web debug toolbar instance
   */
  public function __construct(sfWebDebug $webDebug)
  {
    parent::__construct($webDebug);

    $this->webDebug->getEventDispatcher()->connect('controller.change_action', array($this, 'listenForChangeAction'));
    $this->webDebug->getEventDispatcher()->connect('template.filter_parameters', array($this, 'filterTemplateParameters'));
  }

  /**
   * Resets the parameter collections.
   * 
   * @param sfEvent $event
   */
  public function listenForChangeAction(sfEvent $event)
  {
    $this->actions  = array();
    $this->partials = array();
  }

  /**
   * Stacks action and partial parameters in the template.filter_parameters event.
   * 
   * @param  sfEvent $event
   * @param  array   $parameters
   * 
   * @return array
   */
  public function filterTemplateParameters(sfEvent $event, $parameters)
  {
    $entry = array('parameters' => $parameters);

    if ('action' == $parameters['sf_type'] && $file = $this->getLastTemplate())
    {
      $this->actions[] = $entry + array('file' => $file);
    }
    else if ('partial' == $parameters['sf_type'] && $file = $this->getLastTemplate('sfPartialView'))
    {
      $this->partials[] = $entry + array('file' => $file);
    }

    return $parameters;
  }

  /**
   * Returns the path to the last template rendered.
   * 
   * @param  string $class Name of the rendering view class
   * 
   * @return string|null
   */
  protected function getLastTemplate($class = 'sfPHPView')
  {
    foreach (array_reverse($this->webDebug->getLogger()->getLogs()) as $log)
    {
      if (
        ($class == $log['type'] || (class_exists($log['type'], false) && is_subclass_of($log['type'], $class)))
        &&
        preg_match('/^Render "(.*)"$/', $log['message'], $match)
      )
      {
        return $match[1];
      }
    }
  }

  /**
   * @see sfWebDebugPanel
   */
  public function getTitle()
  {
    if (count($this->actions) || count($this->partials))
    {
      return '<img src="'.$this->webDebug->getOption('image_root_path').'/view.png" alt="View Layer" /> view';
    }
  }

  /**
   * @see sfWebDebugPanel
   */
  public function getPanelTitle()
  {
    return 'View Layer';
  }

  /**
   * @see sfWebDebugPanel
   */
  public function getPanelContent()
  {
    $html = array();

    foreach ($this->actions as $action)
    {
      $html[] = $this->renderTemplateInformation($action['file'], $action['parameters']);
    }

    foreach ($this->partials as $partial)
    {
      $html[] = $this->renderTemplateInformation($partial['file'], $partial['parameters'], 'Partial');
    }

    return join("\n", $html);
  }

  /**
   * Renders information about the passed template and its parameters.
   * 
   * The rendered HTML for each parameter is filtered through the "debug.web.view.filter_parameter_html" event.
   * 
   * @param  string $file       The template file path
   * @param  array  $parameters
   * @param  string $label
   * 
   * @return string
   */
  protected function renderTemplateInformation($file, $parameters, $label = 'Template')
  {
    static $i = 0;

    $parameters = $this->filterCoreParameters($parameters);
    $i++;

    $html = array();
    $html[] = sprintf('<h2>%s: %s %s</h2>', $label, $this->formatFileLink($file, null, $this->shortenTemplatePath($file)), $this->getToggler('sfWebDebugViewTemplate'.$i));
    $html[] = '<div id="sfWebDebugViewTemplate'.$i.'" style="display:'.(1 == $i ? 'block' : 'none').'">';
    if (count($parameters))
    {
      $html[] = '<p>Parameters:</p>';
      $html[] = '<ul>';
      foreach ($parameters as $name => $parameter)
      {
        $presentation = '<li>'.$this->formatParameterAsHtml($name, $parameter).'</li>';
        $html[] = $this->webDebug->getEventDispatcher()->filter(new sfEvent($this, 'debug.web.view.filter_parameter_html', array('parameter' => $parameter)), $presentation)->getReturnValue();
      }
      $html[] = '</ul>';
    }
    else
    {
      $html[] = '<p>No parameters were passed to this template.</p>';
    }
    $html[] = '</div>';

    return join("\n", $html);
  }

  /**
   * Formats information about a parameter as HTML.
   * 
   * @param  string $name
   * @param  mixed  $parameter
   * 
   * @return string
   */
  protected function formatParameterAsHtml($name, $parameter)
  {
    if (!method_exists($this, $method = 'format'.ucwords(gettype($parameter)).'AsHtml'))
    {
      $method = 'getParameterDescription';
    }

    return $this->$method($name, $parameter);
  }

  /**
   * Formats object information as HTML.
   * 
   * @param  string $name
   * @param  object $parameter
   * 
   * @return string
   */
  protected function formatObjectAsHtml($name, $parameter)
  {
    if ($parameter instanceof sfForm)
    {
      return $this->formatFormAsHtml($name, $parameter);
    }
    else
    {
      return $this->getParameterDescription($name, $parameter);
    }
  }

  /**
   * Formats form information as HTML.
   * 
   * @param  string $name
   * @param  sfForm $form
   * 
   * @return string
   */
  protected function formatFormAsHtml($name, sfForm $form)
  {
    static $i = 0;

    $i++;

    if ($form->hasErrors() && sfLogger::NOTICE < $this->getStatus())
    {
      $this->setStatus(sfLogger::NOTICE);
    }

    $html = array();
    $html[] = $this->getParameterDescription($name, $form, $form->hasErrors() ? '<code class="sfWebDebugWarning">$%s</code>' : null);
    $html[] = $this->getToggler('sfWebDebugViewForm'.$i);
    $html[] = '<div id="sfWebDebugViewForm'.$i.'" style="display:none">';

    foreach ($form->getGlobalErrors() as $error)
    {
      $html[] = sprintf('<p><span class="sfWebDebugWarning">%s</span></p>', $error);
    }

    $html[] = '<ul>'.$this->formatFormFieldSchemaAsHtml($form->getFormFieldSchema(), $name.'[%s]').'</ul>';
    $html[] = '</div>';

    return join("\n", $html);
  }

  /**
   * Formats form field schema information as HTML.
   * 
   * @param  sfFormFieldSchema $fieldSchema
   * @param  string            $nameFormat
   * 
   * @return string
   */
  protected function formatFormFieldSchemaAsHtml(sfFormFieldSchema $fieldSchema, $nameFormat = '%s')
  {
    $html = array();

    foreach ($fieldSchema as $field)
    {
      $name = sprintf($nameFormat, $this->varExport($field->getName()));
      if ($field instanceof sfFormFieldSchema)
      {
        $html[] = $this->formatFormFieldSchemaAsHtml($field, $name.'[%s]');
      }
      else
      {
        $html[] = '<li>';
        $html[] = $this->getParameterDescription($name, $field->getWidget());

        if ($field->hasError())
        {
          $html[] = sprintf('<p><span class="sfWebDebugWarning">%s</span></p>', $field->getError());
        }

        $html[] = '</li>';
      }
    }

    return join("\n", $html);
  }

  /**
   * Formats information about a parameter as HTML.
   * 
   * @param  string $name
   * @param  mixed  $parameter
   * 
   * @return string
   */
  protected function getParameterDescription($name, $parameter, $nameFormat = null, $typeFormat = null)
  {
    if (null === $nameFormat)
    {
      $nameFormat = '<code>$%s</code>';
    }

    if (null === $typeFormat)
    {
      $typeFormat = '<span class="sfWebDebugDataType">(%s)</span>';
    }

    return sprintf($nameFormat.' '.$typeFormat, $name, is_object($parameter) ? $this->formatFileLink(get_class($parameter)) : gettype($parameter));
  }

  /**
   * Shortens an action's template path.
   * 
   * @param  string $path
   * 
   * @return string
   */
  protected function shortenTemplatePath($path)
  {
    $path = realpath($path);

    // application module
    $sep = preg_quote(DIRECTORY_SEPARATOR);
    if (preg_match('#modules'.$sep.'(\w+)'.$sep.'templates'.$sep.'(.*)$#', $path, $match))
    {
      return $match[1].'&nbsp;&hellip;&nbsp;'.$match[2];
    }

    return str_replace('SF_ROOT_DIR'.DIRECTORY_SEPARATOR, '', sfDebug::shortenFilePath($path));
  }

  /**
   * Removes parameters prefixed with "sf_" from the array.
   * 
   * @param  array $parameters
   * 
   * @return array
   */
  protected function filterCoreParameters($parameters)
  {
    $filtered = array();

    foreach ($parameters as $name => $value)
    {
      if (0 !== strpos($name, 'sf_'))
      {
        $filtered[$name] = $value;
      }
    }

    return $filtered;
  }

  /**
   * Returns a string representation of a value.
   * 
   * @param  string $value
   * 
   * @return string
   */
  protected function varExport($value)
  {
    if (is_numeric($value))
    {
      $value = (integer) $value;
    }

    return var_export($value, true);
  }
}