ConfigDataCollector.php 9.4 KB

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