ConfigDataCollector.php 8.5KB

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