No Description

ConfigDataCollector.php 7.1KB

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