ConfigDataCollector.php 8.5 KB

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