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