ConfigDataCollector.php 7.0 KB

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