Ei kuvausta

ConfigDataCollector.php 9.2KB

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