You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
7.7 KiB

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