ConfigDataCollector.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\DataCollector;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Kernel;
  14. use Symfony\Component\HttpKernel\KernelInterface;
  15. /**
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class ConfigDataCollector extends DataCollector
  19. {
  20. /**
  21. * @var KernelInterface
  22. */
  23. private $kernel;
  24. private $name;
  25. private $version;
  26. /**
  27. * @param string $name The name of the application using the web profiler
  28. * @param string $version The version of the application using the web profiler
  29. */
  30. public function __construct($name = null, $version = null)
  31. {
  32. $this->name = $name;
  33. $this->version = $version;
  34. }
  35. /**
  36. * Sets the Kernel associated with this Request.
  37. */
  38. public function setKernel(KernelInterface $kernel = null)
  39. {
  40. $this->kernel = $kernel;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function collect(Request $request, Response $response, \Exception $exception = null)
  46. {
  47. $this->data = array(
  48. 'app_name' => $this->name,
  49. 'app_version' => $this->version,
  50. 'token' => $response->headers->get('X-Debug-Token'),
  51. 'symfony_version' => Kernel::VERSION,
  52. 'symfony_state' => 'unknown',
  53. 'name' => isset($this->kernel) ? $this->kernel->getName() : 'n/a',
  54. 'env' => isset($this->kernel) ? $this->kernel->getEnvironment() : 'n/a',
  55. 'debug' => isset($this->kernel) ? $this->kernel->isDebug() : 'n/a',
  56. 'php_version' => PHP_VERSION,
  57. 'xdebug_enabled' => \extension_loaded('xdebug'),
  58. 'eaccel_enabled' => \extension_loaded('eaccelerator') && ini_get('eaccelerator.enable'),
  59. 'apc_enabled' => \extension_loaded('apc') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN),
  60. 'xcache_enabled' => \extension_loaded('xcache') && filter_var(ini_get('xcache.cacher'), FILTER_VALIDATE_BOOLEAN),
  61. 'wincache_enabled' => \extension_loaded('wincache') && filter_var(ini_get('wincache.ocenabled'), FILTER_VALIDATE_BOOLEAN),
  62. 'zend_opcache_enabled' => \extension_loaded('Zend OPcache') && filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN),
  63. 'bundles' => array(),
  64. 'sapi_name' => \PHP_SAPI,
  65. );
  66. if (isset($this->kernel)) {
  67. foreach ($this->kernel->getBundles() as $name => $bundle) {
  68. $this->data['bundles'][$name] = $bundle->getPath();
  69. }
  70. $this->data['symfony_state'] = $this->determineSymfonyState();
  71. }
  72. }
  73. public function getApplicationName()
  74. {
  75. return $this->data['app_name'];
  76. }
  77. public function getApplicationVersion()
  78. {
  79. return $this->data['app_version'];
  80. }
  81. /**
  82. * Gets the token.
  83. *
  84. * @return string The token
  85. */
  86. public function getToken()
  87. {
  88. return $this->data['token'];
  89. }
  90. /**
  91. * Gets the Symfony version.
  92. *
  93. * @return string The Symfony version
  94. */
  95. public function getSymfonyVersion()
  96. {
  97. return $this->data['symfony_version'];
  98. }
  99. /**
  100. * Returns the state of the current Symfony release.
  101. *
  102. * @return string One of: unknown, dev, stable, eom, eol
  103. */
  104. public function getSymfonyState()
  105. {
  106. return $this->data['symfony_state'];
  107. }
  108. public function setCacheVersionInfo($cacheVersionInfo)
  109. {
  110. // no-op for BC
  111. }
  112. /**
  113. * Gets the PHP version.
  114. *
  115. * @return string The PHP version
  116. */
  117. public function getPhpVersion()
  118. {
  119. return $this->data['php_version'];
  120. }
  121. /**
  122. * Gets the application name.
  123. *
  124. * @return string The application name
  125. */
  126. public function getAppName()
  127. {
  128. return $this->data['name'];
  129. }
  130. /**
  131. * Gets the environment.
  132. *
  133. * @return string The environment
  134. */
  135. public function getEnv()
  136. {
  137. return $this->data['env'];
  138. }
  139. /**
  140. * Returns true if the debug is enabled.
  141. *
  142. * @return bool true if debug is enabled, false otherwise
  143. */
  144. public function isDebug()
  145. {
  146. return $this->data['debug'];
  147. }
  148. /**
  149. * Returns true if the XDebug is enabled.
  150. *
  151. * @return bool true if XDebug is enabled, false otherwise
  152. */
  153. public function hasXDebug()
  154. {
  155. return $this->data['xdebug_enabled'];
  156. }
  157. /**
  158. * Returns true if EAccelerator is enabled.
  159. *
  160. * @return bool true if EAccelerator is enabled, false otherwise
  161. */
  162. public function hasEAccelerator()
  163. {
  164. return $this->data['eaccel_enabled'];
  165. }
  166. /**
  167. * Returns true if APC is enabled.
  168. *
  169. * @return bool true if APC is enabled, false otherwise
  170. */
  171. public function hasApc()
  172. {
  173. return $this->data['apc_enabled'];
  174. }
  175. /**
  176. * Returns true if Zend OPcache is enabled.
  177. *
  178. * @return bool true if Zend OPcache is enabled, false otherwise
  179. */
  180. public function hasZendOpcache()
  181. {
  182. return $this->data['zend_opcache_enabled'];
  183. }
  184. /**
  185. * Returns true if XCache is enabled.
  186. *
  187. * @return bool true if XCache is enabled, false otherwise
  188. */
  189. public function hasXCache()
  190. {
  191. return $this->data['xcache_enabled'];
  192. }
  193. /**
  194. * Returns true if WinCache is enabled.
  195. *
  196. * @return bool true if WinCache is enabled, false otherwise
  197. */
  198. public function hasWinCache()
  199. {
  200. return $this->data['wincache_enabled'];
  201. }
  202. /**
  203. * Returns true if any accelerator is enabled.
  204. *
  205. * @return bool true if any accelerator is enabled, false otherwise
  206. */
  207. public function hasAccelerator()
  208. {
  209. return $this->hasApc() || $this->hasZendOpcache() || $this->hasEAccelerator() || $this->hasXCache() || $this->hasWinCache();
  210. }
  211. public function getBundles()
  212. {
  213. return $this->data['bundles'];
  214. }
  215. /**
  216. * Gets the PHP SAPI name.
  217. *
  218. * @return string The environment
  219. */
  220. public function getSapiName()
  221. {
  222. return $this->data['sapi_name'];
  223. }
  224. /**
  225. * {@inheritdoc}
  226. */
  227. public function getName()
  228. {
  229. return 'config';
  230. }
  231. /**
  232. * Tries to retrieve information about the current Symfony version.
  233. *
  234. * @return string One of: dev, stable, eom, eol
  235. */
  236. private function determineSymfonyState()
  237. {
  238. $now = new \DateTime();
  239. $eom = \DateTime::createFromFormat('m/Y', Kernel::END_OF_MAINTENANCE)->modify('last day of this month');
  240. $eol = \DateTime::createFromFormat('m/Y', Kernel::END_OF_LIFE)->modify('last day of this month');
  241. if ($now > $eol) {
  242. $versionState = 'eol';
  243. } elseif ($now > $eom) {
  244. $versionState = 'eom';
  245. } elseif ('' !== Kernel::EXTRA_VERSION) {
  246. $versionState = 'dev';
  247. } else {
  248. $versionState = 'stable';
  249. }
  250. return $versionState;
  251. }
  252. }