1. sfDebug.class.php
  2. /** * sfDebug provides some method to help debugging a symfony application. * * @package symfony * @subpackage debug * @author Fabien Potencier * @version SVN: $Id: sfDebug.class.php 22118 2009-09-18 07:02:26Z fabien $ */
  3. class sfDebug
  4. {
  5. /**
  6. * Returns symfony information as an array.
  7. *
  8. * @return array An array of symfony information
  9. */
  10. public static function symfonyInfoAsArray()
  11. {
  12. return array(
  13. 'version' => SYMFONY_VERSION,
  14. 'path' => sfConfig::get('sf_symfony_lib_dir'),
  15. );
  16. }
  17. /**
  18. * Returns PHP information as an array.
  19. *
  20. * @return array An array of php information
  21. */
  22. public static function phpInfoAsArray()
  23. {
  24. $values = array(
  25. 'php' => phpversion(),
  26. 'os' => php_uname(),
  27. 'extensions' => get_loaded_extensions(),
  28. );
  29. natcasesort($values['extensions']);
  30. // assign extension version
  31. if ($values['extensions'])
  32. {
  33. foreach($values['extensions'] as $key => $extension)
  34. {
  35. $values['extensions'][$key] = phpversion($extension) ? sprintf('%s (%s)', $extension, phpversion($extension)) : $extension;
  36. }
  37. }
  38. return $values;
  39. }
  40. /**
  41. * Returns PHP globals variables as a sorted array.
  42. *
  43. * @return array PHP globals
  44. */
  45. public static function globalsAsArray()
  46. {
  47. $values = array();
  48. foreach (array('cookie', 'server', 'get', 'post', 'files', 'env', 'session') as $name)
  49. {
  50. if (!isset($GLOBALS['_'.strtoupper($name)]))
  51. {
  52. continue;
  53. }
  54. $values[$name] = array();
  55. foreach ($GLOBALS['_'.strtoupper($name)] as $key => $value)
  56. {
  57. $values[$name][$key] = $value;
  58. }
  59. ksort($values[$name]);
  60. }
  61. ksort($values);
  62. return $values;
  63. }
  64. /**
  65. * Returns sfConfig variables as a sorted array.
  66. *
  67. * @return array sfConfig variables
  68. */
  69. public static function settingsAsArray()
  70. {
  71. $config = sfConfig::getAll();
  72. ksort($config);
  73. return $config;
  74. }
  75. /**
  76. * Returns request parameter holders as an array.
  77. *
  78. * @param sfRequest $request A sfRequest instance
  79. *
  80. * @return array The request parameter holders
  81. */
  82. public static function requestAsArray(sfRequest $request = null)
  83. {
  84. if (!$request)
  85. {
  86. return array();
  87. }
  88. return array(
  89. 'options' => $request->getOptions(),
  90. 'parameterHolder' => self::flattenParameterHolder($request->getParameterHolder(), true),
  91. 'attributeHolder' => self::flattenParameterHolder($request->getAttributeHolder(), true),
  92. );
  93. }
  94. /**
  95. * Returns response parameters as an array.
  96. *
  97. * @param sfResponse $response A sfResponse instance
  98. *
  99. * @return array The response parameters
  100. */
  101. public static function responseAsArray(sfResponse $response = null)
  102. {
  103. if (!$response)
  104. {
  105. return array();
  106. }
  107. return array(
  108. 'status' => array('code' => $response->getStatusCode(), 'text' => $response->getStatusText()),
  109. 'options' => $response->getOptions(),
  110. 'cookies' => method_exists($response, 'getCookies') ? $response->getCookies() : array(),
  111. 'httpHeaders' => method_exists($response, 'getHttpHeaders') ? $response->getHttpHeaders() : array(),
  112. 'javascripts' => method_exists($response, 'getJavascripts') ? $response->getJavascripts('ALL') : array(),
  113. 'stylesheets' => method_exists($response, 'getStylesheets') ? $response->getStylesheets('ALL') : array(),
  114. 'metas' => method_exists($response, 'getMetas') ? $response->getMetas() : array(),
  115. 'httpMetas' => method_exists($response, 'getHttpMetas') ? $response->getHttpMetas() : array(),
  116. );
  117. }
  118. /**
  119. * Returns user parameters as an array.
  120. *
  121. * @param sfUser $user A sfUser instance
  122. *
  123. * @return array The user parameters
  124. */
  125. public static function userAsArray(sfUser $user = null)
  126. {
  127. if (!$user)
  128. {
  129. return array();
  130. }
  131. return array(
  132. 'options' => $user->getOptions(),
  133. 'attributeHolder' => self::flattenParameterHolder($user->getAttributeHolder(), true),
  134. 'culture' => $user->getCulture(),
  135. );
  136. }
  137. /**
  138. * Returns a parameter holder as an array.
  139. *
  140. * @param sfParameterHolder $parameterHolder A sfParameterHolder instance
  141. * @param boolean $removeObjects when set to true, objects are removed. default is false for BC.
  142. *
  143. * @return array The parameter holder as an array
  144. */
  145. public static function flattenParameterHolder($parameterHolder, $removeObjects = false)
  146. {
  147. $values = array();
  148. if ($parameterHolder instanceof sfNamespacedParameterHolder)
  149. {
  150. foreach ($parameterHolder->getNamespaces() as $ns)
  151. {
  152. $values[$ns] = array();
  153. foreach ($parameterHolder->getAll($ns) as $key => $value)
  154. {
  155. $values[$ns][$key] = $value;
  156. }
  157. ksort($values[$ns]);
  158. }
  159. }
  160. else
  161. {
  162. foreach ($parameterHolder->getAll() as $key => $value)
  163. {
  164. $values[$key] = $value;
  165. }
  166. }
  167. if ($removeObjects)
  168. {
  169. $values = self::removeObjects($values);
  170. }
  171. ksort($values);
  172. return $values;
  173. }
  174. /**
  175. * Removes objects from the array by replacing them with a String containing the class name.
  176. *
  177. * @param array $values an array
  178. *
  179. * @return array The array without objects
  180. */
  181. public static function removeObjects($values)
  182. {
  183. $nvalues = array();
  184. foreach ($values as $key => $value)
  185. {
  186. if (is_array($value))
  187. {
  188. $nvalues[$key] = self::removeObjects($value);
  189. }
  190. else if (is_object($value))
  191. {
  192. $nvalues[$key] = sprintf('%s Object()', get_class($value));
  193. }
  194. else
  195. {
  196. $nvalues[$key] = $value;
  197. }
  198. }
  199. return $nvalues;
  200. }
  201. /**
  202. * Shortens a file path by replacing symfony directory constants.
  203. *
  204. * @param string $file
  205. *
  206. * @return string
  207. */
  208. static public function shortenFilePath($file)
  209. {
  210. foreach (array('sf_root_dir', 'sf_symfony_lib_dir') as $key)
  211. {
  212. if (0 === strpos($file, $value = sfConfig::get($key)))
  213. {
  214. $file = str_replace($value, strtoupper($key), $file);
  215. break;
  216. }
  217. }
  218. return $file;
  219. }
  220. }

Debug toolbar