PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/lib/debug/sfDebug.class.php

http://streeme.googlecode.com/
PHP | 261 lines | 155 code | 27 blank | 79 comment | 11 complexity | 6e1005ea0903bb1d728fbfe8701431b0 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause
  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 22118 2009-09-18 07:02:26Z fabien $
  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. natcasesort($values['extensions']);
  44. // assign extension version
  45. if ($values['extensions'])
  46. {
  47. foreach($values['extensions'] as $key => $extension)
  48. {
  49. $values['extensions'][$key] = phpversion($extension) ? sprintf('%s (%s)', $extension, phpversion($extension)) : $extension;
  50. }
  51. }
  52. return $values;
  53. }
  54. /**
  55. * Returns PHP globals variables as a sorted array.
  56. *
  57. * @return array PHP globals
  58. */
  59. public static function globalsAsArray()
  60. {
  61. $values = array();
  62. foreach (array('cookie', 'server', 'get', 'post', 'files', 'env', 'session') as $name)
  63. {
  64. if (!isset($GLOBALS['_'.strtoupper($name)]))
  65. {
  66. continue;
  67. }
  68. $values[$name] = array();
  69. foreach ($GLOBALS['_'.strtoupper($name)] as $key => $value)
  70. {
  71. $values[$name][$key] = $value;
  72. }
  73. ksort($values[$name]);
  74. }
  75. ksort($values);
  76. return $values;
  77. }
  78. /**
  79. * Returns sfConfig variables as a sorted array.
  80. *
  81. * @return array sfConfig variables
  82. */
  83. public static function settingsAsArray()
  84. {
  85. $config = sfConfig::getAll();
  86. ksort($config);
  87. return $config;
  88. }
  89. /**
  90. * Returns request parameter holders as an array.
  91. *
  92. * @param sfRequest $request A sfRequest instance
  93. *
  94. * @return array The request parameter holders
  95. */
  96. public static function requestAsArray(sfRequest $request = null)
  97. {
  98. if (!$request)
  99. {
  100. return array();
  101. }
  102. return array(
  103. 'options' => $request->getOptions(),
  104. 'parameterHolder' => self::flattenParameterHolder($request->getParameterHolder(), true),
  105. 'attributeHolder' => self::flattenParameterHolder($request->getAttributeHolder(), true),
  106. );
  107. }
  108. /**
  109. * Returns response parameters as an array.
  110. *
  111. * @param sfResponse $response A sfResponse instance
  112. *
  113. * @return array The response parameters
  114. */
  115. public static function responseAsArray(sfResponse $response = null)
  116. {
  117. if (!$response)
  118. {
  119. return array();
  120. }
  121. return array(
  122. 'status' => array('code' => $response->getStatusCode(), 'text' => $response->getStatusText()),
  123. 'options' => $response->getOptions(),
  124. 'cookies' => method_exists($response, 'getCookies') ? $response->getCookies() : array(),
  125. 'httpHeaders' => method_exists($response, 'getHttpHeaders') ? $response->getHttpHeaders() : array(),
  126. 'javascripts' => method_exists($response, 'getJavascripts') ? $response->getJavascripts('ALL') : array(),
  127. 'stylesheets' => method_exists($response, 'getStylesheets') ? $response->getStylesheets('ALL') : array(),
  128. 'metas' => method_exists($response, 'getMetas') ? $response->getMetas() : array(),
  129. 'httpMetas' => method_exists($response, 'getHttpMetas') ? $response->getHttpMetas() : array(),
  130. );
  131. }
  132. /**
  133. * Returns user parameters as an array.
  134. *
  135. * @param sfUser $user A sfUser instance
  136. *
  137. * @return array The user parameters
  138. */
  139. public static function userAsArray(sfUser $user = null)
  140. {
  141. if (!$user)
  142. {
  143. return array();
  144. }
  145. return array(
  146. 'options' => $user->getOptions(),
  147. 'attributeHolder' => self::flattenParameterHolder($user->getAttributeHolder(), true),
  148. 'culture' => $user->getCulture(),
  149. );
  150. }
  151. /**
  152. * Returns a parameter holder as an array.
  153. *
  154. * @param sfParameterHolder $parameterHolder A sfParameterHolder instance
  155. * @param boolean $removeObjects when set to true, objects are removed. default is false for BC.
  156. *
  157. * @return array The parameter holder as an array
  158. */
  159. public static function flattenParameterHolder($parameterHolder, $removeObjects = false)
  160. {
  161. $values = array();
  162. if ($parameterHolder instanceof sfNamespacedParameterHolder)
  163. {
  164. foreach ($parameterHolder->getNamespaces() as $ns)
  165. {
  166. $values[$ns] = array();
  167. foreach ($parameterHolder->getAll($ns) as $key => $value)
  168. {
  169. $values[$ns][$key] = $value;
  170. }
  171. ksort($values[$ns]);
  172. }
  173. }
  174. else
  175. {
  176. foreach ($parameterHolder->getAll() as $key => $value)
  177. {
  178. $values[$key] = $value;
  179. }
  180. }
  181. if ($removeObjects)
  182. {
  183. $values = self::removeObjects($values);
  184. }
  185. ksort($values);
  186. return $values;
  187. }
  188. /**
  189. * Removes objects from the array by replacing them with a String containing the class name.
  190. *
  191. * @param array $values an array
  192. *
  193. * @return array The array without objects
  194. */
  195. public static function removeObjects($values)
  196. {
  197. $nvalues = array();
  198. foreach ($values as $key => $value)
  199. {
  200. if (is_array($value))
  201. {
  202. $nvalues[$key] = self::removeObjects($value);
  203. }
  204. else if (is_object($value))
  205. {
  206. $nvalues[$key] = sprintf('%s Object()', get_class($value));
  207. }
  208. else
  209. {
  210. $nvalues[$key] = $value;
  211. }
  212. }
  213. return $nvalues;
  214. }
  215. /**
  216. * Shortens a file path by replacing symfony directory constants.
  217. *
  218. * @param string $file
  219. *
  220. * @return string
  221. */
  222. static public function shortenFilePath($file)
  223. {
  224. foreach (array('sf_root_dir', 'sf_symfony_lib_dir') as $key)
  225. {
  226. if (0 === strpos($file, $value = sfConfig::get($key)))
  227. {
  228. $file = str_replace($value, strtoupper($key), $file);
  229. break;
  230. }
  231. }
  232. return $file;
  233. }
  234. }