PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/site/lib/vendor/symfony/lib/debug/sfWebDebugPanelView.class.php

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