ConfigDataCollector.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. use Symfony\Component\VarDumper\Caster\ClassStub;
  16. /**
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. *
  19. * @final
  20. */
  21. class ConfigDataCollector extends DataCollector implements LateDataCollectorInterface
  22. {
  23. /**
  24. * @var KernelInterface
  25. */
  26. private $kernel;
  27. /**
  28. * Sets the Kernel associated with this Request.
  29. */
  30. public function setKernel(KernelInterface $kernel = null)
  31. {
  32. $this->kernel = $kernel;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function collect(Request $request, Response $response, \Throwable $exception = null)
  38. {
  39. $this->data = [
  40. 'token' => $response->headers->get('X-Debug-Token'),
  41. 'symfony_version' => Kernel::VERSION,
  42. 'symfony_state' => 'unknown',
  43. 'env' => isset($this->kernel) ? $this->kernel->getEnvironment() : 'n/a',
  44. 'debug' => isset($this->kernel) ? $this->kernel->isDebug() : 'n/a',
  45. 'php_version' => \PHP_VERSION,
  46. 'php_architecture' => \PHP_INT_SIZE * 8,
  47. 'php_intl_locale' => class_exists(\Locale::class, false) && \Locale::getDefault() ? \Locale::getDefault() : 'n/a',
  48. 'php_timezone' => date_default_timezone_get(),
  49. 'xdebug_enabled' => \extension_loaded('xdebug'),
  50. 'apcu_enabled' => \extension_loaded('apcu') && filter_var(ini_get('apc.enabled'), \FILTER_VALIDATE_BOOLEAN),
  51. 'zend_opcache_enabled' => \extension_loaded('Zend OPcache') && filter_var(ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN),
  52. 'bundles' => [],
  53. 'sapi_name' => \PHP_SAPI,
  54. ];
  55. if (isset($this->kernel)) {
  56. foreach ($this->kernel->getBundles() as $name => $bundle) {
  57. $this->data['bundles'][$name] = new ClassStub(\get_class($bundle));
  58. }
  59. $this->data['symfony_state'] = $this->determineSymfonyState();
  60. $this->data['symfony_minor_version'] = sprintf('%s.%s', Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION);
  61. $this->data['symfony_lts'] = 4 === Kernel::MINOR_VERSION;
  62. $eom = \DateTime::createFromFormat('d/m/Y', '01/'.Kernel::END_OF_MAINTENANCE);
  63. $eol = \DateTime::createFromFormat('d/m/Y', '01/'.Kernel::END_OF_LIFE);
  64. $this->data['symfony_eom'] = $eom->format('F Y');
  65. $this->data['symfony_eol'] = $eol->format('F Y');
  66. }
  67. if (preg_match('~^(\d+(?:\.\d+)*)(.+)?$~', $this->data['php_version'], $matches) && isset($matches[2])) {
  68. $this->data['php_version'] = $matches[1];
  69. $this->data['php_version_extra'] = $matches[2];
  70. }
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function reset()
  76. {
  77. $this->data = [];
  78. }
  79. public function lateCollect()
  80. {
  81. $this->data = $this->cloneVar($this->data);
  82. }
  83. /**
  84. * Gets the token.
  85. *
  86. * @return string|null The token
  87. */
  88. public function getToken()
  89. {
  90. return $this->data['token'];
  91. }
  92. /**
  93. * Gets the Symfony version.
  94. *
  95. * @return string The Symfony version
  96. */
  97. public function getSymfonyVersion()
  98. {
  99. return $this->data['symfony_version'];
  100. }
  101. /**
  102. * Returns the state of the current Symfony release.
  103. *
  104. * @return string One of: unknown, dev, stable, eom, eol
  105. */
  106. public function getSymfonyState()
  107. {
  108. return $this->data['symfony_state'];
  109. }
  110. /**
  111. * Returns the minor Symfony version used (without patch numbers of extra
  112. * suffix like "RC", "beta", etc.).
  113. *
  114. * @return string
  115. */
  116. public function getSymfonyMinorVersion()
  117. {
  118. return $this->data['symfony_minor_version'];
  119. }
  120. /**
  121. * Returns if the current Symfony version is a Long-Term Support one.
  122. */
  123. public function isSymfonyLts(): bool
  124. {
  125. return $this->data['symfony_lts'];
  126. }
  127. /**
  128. * Returns the human redable date when this Symfony version ends its
  129. * maintenance period.
  130. *
  131. * @return string
  132. */
  133. public function getSymfonyEom()
  134. {
  135. return $this->data['symfony_eom'];
  136. }
  137. /**
  138. * Returns the human redable date when this Symfony version reaches its
  139. * "end of life" and won't receive bugs or security fixes.
  140. *
  141. * @return string
  142. */
  143. public function getSymfonyEol()
  144. {
  145. return $this->data['symfony_eol'];
  146. }
  147. /**
  148. * Gets the PHP version.
  149. *
  150. * @return string The PHP version
  151. */
  152. public function getPhpVersion()
  153. {
  154. return $this->data['php_version'];
  155. }
  156. /**
  157. * Gets the PHP version extra part.
  158. *
  159. * @return string|null The extra part
  160. */
  161. public function getPhpVersionExtra()
  162. {
  163. return $this->data['php_version_extra'] ?? null;
  164. }
  165. /**
  166. * @return int The PHP architecture as number of bits (e.g. 32 or 64)
  167. */
  168. public function getPhpArchitecture()
  169. {
  170. return $this->data['php_architecture'];
  171. }
  172. /**
  173. * @return string
  174. */
  175. public function getPhpIntlLocale()
  176. {
  177. return $this->data['php_intl_locale'];
  178. }
  179. /**
  180. * @return string
  181. */
  182. public function getPhpTimezone()
  183. {
  184. return $this->data['php_timezone'];
  185. }
  186. /**
  187. * Gets the environment.
  188. *
  189. * @return string The environment
  190. */
  191. public function getEnv()
  192. {
  193. return $this->data['env'];
  194. }
  195. /**
  196. * Returns true if the debug is enabled.
  197. *
  198. * @return bool true if debug is enabled, false otherwise
  199. */
  200. public function isDebug()
  201. {
  202. return $this->data['debug'];
  203. }
  204. /**
  205. * Returns true if the XDebug is enabled.
  206. *
  207. * @return bool true if XDebug is enabled, false otherwise
  208. */
  209. public function hasXDebug()
  210. {
  211. return $this->data['xdebug_enabled'];
  212. }
  213. /**
  214. * Returns true if APCu is enabled.
  215. *
  216. * @return bool true if APCu is enabled, false otherwise
  217. */
  218. public function hasApcu()
  219. {
  220. return $this->data['apcu_enabled'];
  221. }
  222. /**
  223. * Returns true if Zend OPcache is enabled.
  224. *
  225. * @return bool true if Zend OPcache is enabled, false otherwise
  226. */
  227. public function hasZendOpcache()
  228. {
  229. return $this->data['zend_opcache_enabled'];
  230. }
  231. public function getBundles()
  232. {
  233. return $this->data['bundles'];
  234. }
  235. /**
  236. * Gets the PHP SAPI name.
  237. *
  238. * @return string The environment
  239. */
  240. public function getSapiName()
  241. {
  242. return $this->data['sapi_name'];
  243. }
  244. /**
  245. * {@inheritdoc}
  246. */
  247. public function getName()
  248. {
  249. return 'config';
  250. }
  251. /**
  252. * Tries to retrieve information about the current Symfony version.
  253. *
  254. * @return string One of: dev, stable, eom, eol
  255. */
  256. private function determineSymfonyState(): string
  257. {
  258. $now = new \DateTime();
  259. $eom = \DateTime::createFromFormat('d/m/Y', '01/'.Kernel::END_OF_MAINTENANCE)->modify('last day of this month');
  260. $eol = \DateTime::createFromFormat('d/m/Y', '01/'.Kernel::END_OF_LIFE)->modify('last day of this month');
  261. if ($now > $eol) {
  262. $versionState = 'eol';
  263. } elseif ($now > $eom) {
  264. $versionState = 'eom';
  265. } elseif ('' !== Kernel::EXTRA_VERSION) {
  266. $versionState = 'dev';
  267. } else {
  268. $versionState = 'stable';
  269. }
  270. return $versionState;
  271. }
  272. }