Profiler.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace Symfony\Components\HttpKernel\Profiler;
  3. use Symfony\Components\HttpKernel\Response;
  4. use Symfony\Components\HttpKernel\Profiler\ProfilerStorage;
  5. use Symfony\Components\HttpKernel\Profiler\DataCollector\DataCollectorInterface;
  6. use Symfony\Components\HttpKernel\LoggerInterface;
  7. /*
  8. * This file is part of the Symfony framework.
  9. *
  10. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. /**
  16. * Profiler.
  17. *
  18. * @package Symfony
  19. * @subpackage Components_HttpKernel
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. class Profiler implements \ArrayAccess
  23. {
  24. protected $profilerStorage;
  25. protected $collectors;
  26. protected $response;
  27. protected $logger;
  28. public function __construct(ProfilerStorage $profilerStorage, LoggerInterface $logger = null)
  29. {
  30. $this->profilerStorage = $profilerStorage;
  31. $this->logger = $logger;
  32. $this->collectors = array();
  33. }
  34. /**
  35. * Clones the Profiler instance.
  36. */
  37. public function __clone()
  38. {
  39. $this->profilerStorage = clone $this->profilerStorage;
  40. }
  41. /**
  42. * Returns a new Profiler for the given Response.
  43. *
  44. * @param Symfony\Components\HttpKernel\Response $response A Response instance
  45. *
  46. * @return Symfony\Components\HttpKernel\Profiler\Profiler A new Profiler instance
  47. */
  48. public function load(Response $response)
  49. {
  50. if (!$token = $response->headers->get('X-Debug-Token')) {
  51. return null;
  52. }
  53. return $this->getProfilerForToken($token);
  54. }
  55. /**
  56. * Returns a new Profiler for the given token.
  57. *
  58. * @param string $token A token
  59. *
  60. * @return Symfony\Components\HttpKernel\Profiler\Profiler A new Profiler instance
  61. */
  62. public function getProfilerForToken($token)
  63. {
  64. $profiler = clone $this;
  65. $profiler->profilerStorage->setToken($token);
  66. $profiler->loadCollectorData();
  67. return $profiler;
  68. }
  69. /**
  70. * Collects data for the given Response.
  71. *
  72. * @param Symfony\Components\HttpKernel\Response $response A Response instance
  73. */
  74. public function collect(Response $response)
  75. {
  76. $this->response = $response;
  77. $this->response->headers->set('X-Debug-Token', $this->profilerStorage->getToken());
  78. $data = array();
  79. foreach ($this->collectors as $name => $collector) {
  80. $collector->collect();
  81. $data[$name] = $collector->getData();
  82. }
  83. try {
  84. $this->profilerStorage->write($data);
  85. $this->profilerStorage->purge();
  86. } catch (\Exception $e) {
  87. if (null !== $this->logger) {
  88. $this->logger->err('Unable to store the profiler information.');
  89. }
  90. }
  91. }
  92. /**
  93. * Loads the data stored in the storage for all collectors.
  94. */
  95. public function loadCollectorData()
  96. {
  97. try {
  98. foreach ($this->collectors as $name => $collector) {
  99. $collector->setData($this->profilerStorage->getData($name));
  100. }
  101. } catch (\Exception $e) {
  102. if (null !== $this->logger) {
  103. $this->logger->err('Unable to read the profiler information.');
  104. }
  105. }
  106. }
  107. /**
  108. * Gets the profiler storage.
  109. *
  110. * @return Symfony\Components\HttpKernel\Profiler\ProfilerStorage A ProfilerStorage instance
  111. */
  112. public function getProfilerStorage()
  113. {
  114. return $this->profilerStorage;
  115. }
  116. /**
  117. * Gets the Response.
  118. *
  119. * @return Symfony\Components\HttpKernel\Response A Response instance
  120. */
  121. public function getResponse()
  122. {
  123. return $this->response;
  124. }
  125. /**
  126. * Gets the Collectors associated with this profiler.
  127. *
  128. * @return array An array of collectors
  129. */
  130. public function getCollectors()
  131. {
  132. return $this->collectors;
  133. }
  134. /**
  135. * Sets the Collectors associated with this profiler.
  136. *
  137. * @param array $collectors An array of collectors
  138. */
  139. public function setCollectors(array $collectors = array())
  140. {
  141. $this->collectors = array();
  142. foreach ($collectors as $name => $collector) {
  143. $this->addCollector($collector);
  144. }
  145. }
  146. /**
  147. * Adds a Collector.
  148. *
  149. * @param Symfony\Components\HttpKernel\Profiler\DataCollector\DataCollectorInterface $collector A DataCollectorInterface instance
  150. */
  151. public function addCollector(DataCollectorInterface $collector)
  152. {
  153. $this->collectors[$collector->getName()] = $collector;
  154. }
  155. /**
  156. * Returns true if a Collector for the given name exists.
  157. *
  158. * @param string $name A collector name
  159. */
  160. public function hasCollector($name)
  161. {
  162. return isset($this->collectors[$name]);
  163. }
  164. /**
  165. * Gets a Collector by name.
  166. *
  167. * @param string $name A collector name
  168. *
  169. * @return Symfony\Components\HttpKernel\Profiler\DataCollector\DataCollectorInterface A DataCollectorInterface instance
  170. *
  171. * @throws \InvalidArgumentException if the collector does not exist
  172. */
  173. public function getCollector($name)
  174. {
  175. if (!isset($this->collectors[$name])) {
  176. throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
  177. }
  178. return $this->collectors[$name];
  179. }
  180. /**
  181. * Returns true if the named collector exists.
  182. *
  183. * @param string $name The collector name
  184. *
  185. * @param Boolean true if the collector exists, false otherwise
  186. */
  187. public function offsetExists($name)
  188. {
  189. return $this->hasCollector($name);
  190. }
  191. /**
  192. * Gets a collector.
  193. *
  194. * @param string $name The collector name
  195. *
  196. * @throws \InvalidArgumentException if the collector does not exist
  197. */
  198. public function offsetGet($name)
  199. {
  200. return $this->getCollector($name);
  201. }
  202. /**
  203. * Unimplemented.
  204. *
  205. * @param string $name The collector name
  206. * @param string|array $value The collector
  207. *
  208. * @throws \LogicException
  209. */
  210. public function offsetSet($name, $value)
  211. {
  212. throw new \LogicException('A Collector cannot be set.');
  213. }
  214. /**
  215. * Unimplemented.
  216. *
  217. * @param string $name The collector name
  218. *
  219. * @throws \LogicException
  220. */
  221. public function offsetUnset($name)
  222. {
  223. throw new \LogicException('A Collector cannot be removed.');
  224. }
  225. }