DataCollectorManager.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Symfony\Framework\ProfilerBundle\DataCollector;
  3. use Symfony\Components\DependencyInjection\ContainerInterface;
  4. use Symfony\Components\EventDispatcher\Event;
  5. use Symfony\Components\RequestHandler\Response;
  6. use Symfony\Framework\ProfilerBundle\ProfilerStorage;
  7. use Symfony\Foundation\LoggerInterface;
  8. /*
  9. * This file is part of the Symfony framework.
  10. *
  11. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  12. *
  13. * This source file is subject to the MIT license that is bundled
  14. * with this source code in the file LICENSE.
  15. */
  16. /**
  17. * DataCollectorManager.
  18. *
  19. * @package Symfony
  20. * @subpackage Framework_ProfilerBundle
  21. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22. */
  23. class DataCollectorManager
  24. {
  25. protected $container;
  26. protected $profilerStorage;
  27. protected $collectors;
  28. protected $response;
  29. protected $lifetime;
  30. protected $logger;
  31. public function __construct(ContainerInterface $container, LoggerInterface $logger, ProfilerStorage $profilerStorage, $lifetime = 86400)
  32. {
  33. $this->container = $container;
  34. $this->logger = $logger;
  35. $this->lifetime = $lifetime;
  36. $this->profilerStorage = $profilerStorage;
  37. $this->collectors = $this->initCollectors();
  38. }
  39. public function register()
  40. {
  41. $this->container->getEventDispatcherService()->connect('core.response', array($this, 'handle'));
  42. }
  43. public function handle(Event $event, Response $response)
  44. {
  45. if (!$event->getParameter('main_request'))
  46. {
  47. return $response;
  48. }
  49. $this->response = $response;
  50. $data = array();
  51. foreach ($this->collectors as $name => $collector)
  52. {
  53. $data[$name] = $collector->getData();
  54. }
  55. try
  56. {
  57. $this->profilerStorage->write($data);
  58. $this->profilerStorage->purge($this->lifetime);
  59. }
  60. catch (\Exception $e)
  61. {
  62. $this->logger->err('Unable to store the profiler information.');
  63. }
  64. return $response;
  65. }
  66. public function getProfilerStorage()
  67. {
  68. return $this->profilerStorage;
  69. }
  70. public function getResponse()
  71. {
  72. return $this->response;
  73. }
  74. public function getCollectors()
  75. {
  76. return $this->collectors;
  77. }
  78. public function initCollectors()
  79. {
  80. $config = $this->container->findAnnotatedServiceIds('data_collector');
  81. $ids = array();
  82. $coreCollectors = array();
  83. $userCollectors = array();
  84. foreach ($config as $id => $attributes)
  85. {
  86. $collector = $this->container->getService($id);
  87. $collector->setCollectorManager($this);
  88. if (isset($attributes[0]['core']) && $attributes[0]['core'])
  89. {
  90. $coreCollectors[$collector->getName()] = $collector;
  91. }
  92. else
  93. {
  94. $userCollectors[$collector->getName()] = $collector;
  95. }
  96. }
  97. return $this->collectors = array_merge($coreCollectors, $userCollectors);
  98. }
  99. }