ConfigDataCollector.php 9.1 KB

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