sfDebug.class.php 5.8 KB

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