菜谱项目

ConfigDataCollector.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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($name = null, $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. public function lateCollect()
  86. {
  87. $this->data = $this->cloneVar($this->data);
  88. }
  89. public function getApplicationName()
  90. {
  91. return $this->data['app_name'];
  92. }
  93. public function getApplicationVersion()
  94. {
  95. return $this->data['app_version'];
  96. }
  97. /**
  98. * Gets the token.
  99. *
  100. * @return string The token
  101. */
  102. public function getToken()
  103. {
  104. return $this->data['token'];
  105. }
  106. /**
  107. * Gets the Symfony version.
  108. *
  109. * @return string The Symfony version
  110. */
  111. public function getSymfonyVersion()
  112. {
  113. return $this->data['symfony_version'];
  114. }
  115. /**
  116. * Returns the state of the current Symfony release.
  117. *
  118. * @return string One of: unknown, dev, stable, eom, eol
  119. */
  120. public function getSymfonyState()
  121. {
  122. return $this->data['symfony_state'];
  123. }
  124. /**
  125. * Returns the minor Symfony version used (without patch numbers of extra
  126. * suffix like "RC", "beta", etc.).
  127. *
  128. * @return string
  129. */
  130. public function getSymfonyMinorVersion()
  131. {
  132. return $this->data['symfony_minor_version'];
  133. }
  134. /**
  135. * Returns the human redable date when this Symfony version ends its
  136. * maintenance period.
  137. *
  138. * @return string
  139. */
  140. public function getSymfonyEom()
  141. {
  142. return $this->data['symfony_eom'];
  143. }
  144. /**
  145. * Returns the human redable date when this Symfony version reaches its
  146. * "end of life" and won't receive bugs or security fixes.
  147. *
  148. * @return string
  149. */
  150. public function getSymfonyEol()
  151. {
  152. return $this->data['symfony_eol'];
  153. }
  154. /**
  155. * Gets the PHP version.
  156. *
  157. * @return string The PHP version
  158. */
  159. public function getPhpVersion()
  160. {
  161. return $this->data['php_version'];
  162. }
  163. /**
  164. * Gets the PHP version extra part.
  165. *
  166. * @return string|null The extra part
  167. */
  168. public function getPhpVersionExtra()
  169. {
  170. return isset($this->data['php_version_extra']) ? $this->data['php_version_extra'] : null;
  171. }
  172. /**
  173. * @return int The PHP architecture as number of bits (e.g. 32 or 64)
  174. */
  175. public function getPhpArchitecture()
  176. {
  177. return $this->data['php_architecture'];
  178. }
  179. /**
  180. * @return string
  181. */
  182. public function getPhpIntlLocale()
  183. {
  184. return $this->data['php_intl_locale'];
  185. }
  186. /**
  187. * @return string
  188. */
  189. public function getPhpTimezone()
  190. {
  191. return $this->data['php_timezone'];
  192. }
  193. /**
  194. * Gets the application name.
  195. *
  196. * @return string The application name
  197. */
  198. public function getAppName()
  199. {
  200. return $this->data['name'];
  201. }
  202. /**
  203. * Gets the environment.
  204. *
  205. * @return string The environment
  206. */
  207. public function getEnv()
  208. {
  209. return $this->data['env'];
  210. }
  211. /**
  212. * Returns true if the debug is enabled.
  213. *
  214. * @return bool true if debug is enabled, false otherwise
  215. */
  216. public function isDebug()
  217. {
  218. return $this->data['debug'];
  219. }
  220. /**
  221. * Returns true if the XDebug is enabled.
  222. *
  223. * @return bool true if XDebug is enabled, false otherwise
  224. */
  225. public function hasXDebug()
  226. {
  227. return $this->data['xdebug_enabled'];
  228. }
  229. /**
  230. * Returns true if APCu is enabled.
  231. *
  232. * @return bool true if APCu is enabled, false otherwise
  233. */
  234. public function hasApcu()
  235. {
  236. return $this->data['apcu_enabled'];
  237. }
  238. /**
  239. * Returns true if Zend OPcache is enabled.
  240. *
  241. * @return bool true if Zend OPcache is enabled, false otherwise
  242. */
  243. public function hasZendOpcache()
  244. {
  245. return $this->data['zend_opcache_enabled'];
  246. }
  247. public function getBundles()
  248. {
  249. return $this->data['bundles'];
  250. }
  251. /**
  252. * Gets the PHP SAPI name.
  253. *
  254. * @return string The environment
  255. */
  256. public function getSapiName()
  257. {
  258. return $this->data['sapi_name'];
  259. }
  260. /**
  261. * {@inheritdoc}
  262. */
  263. public function getName()
  264. {
  265. return 'config';
  266. }
  267. /**
  268. * Tries to retrieve information about the current Symfony version.
  269. *
  270. * @return string One of: dev, stable, eom, eol
  271. */
  272. private function determineSymfonyState()
  273. {
  274. $now = new \DateTime();
  275. $eom = \DateTime::createFromFormat('m/Y', Kernel::END_OF_MAINTENANCE)->modify('last day of this month');
  276. $eol = \DateTime::createFromFormat('m/Y', Kernel::END_OF_LIFE)->modify('last day of this month');
  277. if ($now > $eol) {
  278. $versionState = 'eol';
  279. } elseif ($now > $eom) {
  280. $versionState = 'eom';
  281. } elseif ('' !== Kernel::EXTRA_VERSION) {
  282. $versionState = 'dev';
  283. } else {
  284. $versionState = 'stable';
  285. }
  286. return $versionState;
  287. }
  288. }