ConfigDataCollector.php 7.2 KB

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