Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpKernel\Profiler;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
|
Chris@0
|
15 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
16 use Symfony\Component\HttpFoundation\Response;
|
Chris@0
|
17 use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
|
Chris@0
|
18 use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
|
Chris@0
|
19 use Psr\Log\LoggerInterface;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Profiler.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
25 */
|
Chris@0
|
26 class Profiler
|
Chris@0
|
27 {
|
Chris@0
|
28 /**
|
Chris@0
|
29 * @var ProfilerStorageInterface
|
Chris@0
|
30 */
|
Chris@0
|
31 private $storage;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * @var DataCollectorInterface[]
|
Chris@0
|
35 */
|
Chris@0
|
36 private $collectors = array();
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * @var LoggerInterface
|
Chris@0
|
40 */
|
Chris@0
|
41 private $logger;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * @var bool
|
Chris@0
|
45 */
|
Chris@0
|
46 private $enabled = true;
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * Constructor.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @param ProfilerStorageInterface $storage A ProfilerStorageInterface instance
|
Chris@0
|
52 * @param LoggerInterface $logger A LoggerInterface instance
|
Chris@0
|
53 */
|
Chris@0
|
54 public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null)
|
Chris@0
|
55 {
|
Chris@0
|
56 $this->storage = $storage;
|
Chris@0
|
57 $this->logger = $logger;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * Disables the profiler.
|
Chris@0
|
62 */
|
Chris@0
|
63 public function disable()
|
Chris@0
|
64 {
|
Chris@0
|
65 $this->enabled = false;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * Enables the profiler.
|
Chris@0
|
70 */
|
Chris@0
|
71 public function enable()
|
Chris@0
|
72 {
|
Chris@0
|
73 $this->enabled = true;
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Loads the Profile for the given Response.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @param Response $response A Response instance
|
Chris@0
|
80 *
|
Chris@0
|
81 * @return Profile|false A Profile instance
|
Chris@0
|
82 */
|
Chris@0
|
83 public function loadProfileFromResponse(Response $response)
|
Chris@0
|
84 {
|
Chris@0
|
85 if (!$token = $response->headers->get('X-Debug-Token')) {
|
Chris@0
|
86 return false;
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 return $this->loadProfile($token);
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Loads the Profile for the given token.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @param string $token A token
|
Chris@0
|
96 *
|
Chris@0
|
97 * @return Profile A Profile instance
|
Chris@0
|
98 */
|
Chris@0
|
99 public function loadProfile($token)
|
Chris@0
|
100 {
|
Chris@0
|
101 return $this->storage->read($token);
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * Saves a Profile.
|
Chris@0
|
106 *
|
Chris@0
|
107 * @param Profile $profile A Profile instance
|
Chris@0
|
108 *
|
Chris@0
|
109 * @return bool
|
Chris@0
|
110 */
|
Chris@0
|
111 public function saveProfile(Profile $profile)
|
Chris@0
|
112 {
|
Chris@0
|
113 // late collect
|
Chris@0
|
114 foreach ($profile->getCollectors() as $collector) {
|
Chris@0
|
115 if ($collector instanceof LateDataCollectorInterface) {
|
Chris@0
|
116 $collector->lateCollect();
|
Chris@0
|
117 }
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
|
Chris@0
|
121 $this->logger->warning('Unable to store the profiler information.', array('configured_storage' => get_class($this->storage)));
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 return $ret;
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * Purges all data from the storage.
|
Chris@0
|
129 */
|
Chris@0
|
130 public function purge()
|
Chris@0
|
131 {
|
Chris@0
|
132 $this->storage->purge();
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * Finds profiler tokens for the given criteria.
|
Chris@0
|
137 *
|
Chris@0
|
138 * @param string $ip The IP
|
Chris@0
|
139 * @param string $url The URL
|
Chris@0
|
140 * @param string $limit The maximum number of tokens to return
|
Chris@0
|
141 * @param string $method The request method
|
Chris@0
|
142 * @param string $start The start date to search from
|
Chris@0
|
143 * @param string $end The end date to search to
|
Chris@0
|
144 * @param string $statusCode The request status code
|
Chris@0
|
145 *
|
Chris@0
|
146 * @return array An array of tokens
|
Chris@0
|
147 *
|
Chris@0
|
148 * @see http://php.net/manual/en/datetime.formats.php for the supported date/time formats
|
Chris@0
|
149 */
|
Chris@0
|
150 public function find($ip, $url, $limit, $method, $start, $end, $statusCode = null)
|
Chris@0
|
151 {
|
Chris@0
|
152 return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end), $statusCode);
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 /**
|
Chris@0
|
156 * Collects data for the given Response.
|
Chris@0
|
157 *
|
Chris@0
|
158 * @param Request $request A Request instance
|
Chris@0
|
159 * @param Response $response A Response instance
|
Chris@0
|
160 * @param \Exception $exception An exception instance if the request threw one
|
Chris@0
|
161 *
|
Chris@0
|
162 * @return Profile|null A Profile instance or null if the profiler is disabled
|
Chris@0
|
163 */
|
Chris@0
|
164 public function collect(Request $request, Response $response, \Exception $exception = null)
|
Chris@0
|
165 {
|
Chris@0
|
166 if (false === $this->enabled) {
|
Chris@0
|
167 return;
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 $profile = new Profile(substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6));
|
Chris@0
|
171 $profile->setTime(time());
|
Chris@0
|
172 $profile->setUrl($request->getUri());
|
Chris@0
|
173 $profile->setMethod($request->getMethod());
|
Chris@0
|
174 $profile->setStatusCode($response->getStatusCode());
|
Chris@0
|
175 try {
|
Chris@0
|
176 $profile->setIp($request->getClientIp());
|
Chris@0
|
177 } catch (ConflictingHeadersException $e) {
|
Chris@0
|
178 $profile->setIp('Unknown');
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 $response->headers->set('X-Debug-Token', $profile->getToken());
|
Chris@0
|
182
|
Chris@0
|
183 foreach ($this->collectors as $collector) {
|
Chris@0
|
184 $collector->collect($request, $response, $exception);
|
Chris@0
|
185
|
Chris@0
|
186 // we need to clone for sub-requests
|
Chris@0
|
187 $profile->addCollector(clone $collector);
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 return $profile;
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 /**
|
Chris@0
|
194 * Gets the Collectors associated with this profiler.
|
Chris@0
|
195 *
|
Chris@0
|
196 * @return array An array of collectors
|
Chris@0
|
197 */
|
Chris@0
|
198 public function all()
|
Chris@0
|
199 {
|
Chris@0
|
200 return $this->collectors;
|
Chris@0
|
201 }
|
Chris@0
|
202
|
Chris@0
|
203 /**
|
Chris@0
|
204 * Sets the Collectors associated with this profiler.
|
Chris@0
|
205 *
|
Chris@0
|
206 * @param DataCollectorInterface[] $collectors An array of collectors
|
Chris@0
|
207 */
|
Chris@0
|
208 public function set(array $collectors = array())
|
Chris@0
|
209 {
|
Chris@0
|
210 $this->collectors = array();
|
Chris@0
|
211 foreach ($collectors as $collector) {
|
Chris@0
|
212 $this->add($collector);
|
Chris@0
|
213 }
|
Chris@0
|
214 }
|
Chris@0
|
215
|
Chris@0
|
216 /**
|
Chris@0
|
217 * Adds a Collector.
|
Chris@0
|
218 *
|
Chris@0
|
219 * @param DataCollectorInterface $collector A DataCollectorInterface instance
|
Chris@0
|
220 */
|
Chris@0
|
221 public function add(DataCollectorInterface $collector)
|
Chris@0
|
222 {
|
Chris@0
|
223 $this->collectors[$collector->getName()] = $collector;
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 /**
|
Chris@0
|
227 * Returns true if a Collector for the given name exists.
|
Chris@0
|
228 *
|
Chris@0
|
229 * @param string $name A collector name
|
Chris@0
|
230 *
|
Chris@0
|
231 * @return bool
|
Chris@0
|
232 */
|
Chris@0
|
233 public function has($name)
|
Chris@0
|
234 {
|
Chris@0
|
235 return isset($this->collectors[$name]);
|
Chris@0
|
236 }
|
Chris@0
|
237
|
Chris@0
|
238 /**
|
Chris@0
|
239 * Gets a Collector by name.
|
Chris@0
|
240 *
|
Chris@0
|
241 * @param string $name A collector name
|
Chris@0
|
242 *
|
Chris@0
|
243 * @return DataCollectorInterface A DataCollectorInterface instance
|
Chris@0
|
244 *
|
Chris@0
|
245 * @throws \InvalidArgumentException if the collector does not exist
|
Chris@0
|
246 */
|
Chris@0
|
247 public function get($name)
|
Chris@0
|
248 {
|
Chris@0
|
249 if (!isset($this->collectors[$name])) {
|
Chris@0
|
250 throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 return $this->collectors[$name];
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 private function getTimestamp($value)
|
Chris@0
|
257 {
|
Chris@0
|
258 if (null === $value || '' == $value) {
|
Chris@0
|
259 return;
|
Chris@0
|
260 }
|
Chris@0
|
261
|
Chris@0
|
262 try {
|
Chris@0
|
263 $value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
|
Chris@0
|
264 } catch (\Exception $e) {
|
Chris@0
|
265 return;
|
Chris@0
|
266 }
|
Chris@0
|
267
|
Chris@0
|
268 return $value->getTimestamp();
|
Chris@0
|
269 }
|
Chris@0
|
270 }
|