4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Profiler;
15 * Storage for profiler using files.
17 * @author Alexandre Salomé <alexandre.salome@gmail.com>
19 class FileProfilerStorage implements ProfilerStorageInterface
22 * Folder where profiler data are stored.
29 * Constructs the file storage using a "dsn-like" path.
31 * Example : "file:/path/to/the/storage/folder"
33 * @param string $dsn The DSN
35 * @throws \RuntimeException
37 public function __construct($dsn)
39 if (0 !== strpos($dsn, 'file:')) {
40 throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use FileStorage with an invalid dsn "%s". The expected format is "file:/path/to/the/storage/folder".', $dsn));
42 $this->folder = substr($dsn, 5);
44 if (!is_dir($this->folder) && false === @mkdir($this->folder, 0777, true) && !is_dir($this->folder)) {
45 throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $this->folder));
52 public function find($ip, $url, $limit, $method, $start = null, $end = null)
54 $file = $this->getIndexFilename();
56 if (!file_exists($file)) {
60 $file = fopen($file, 'r');
61 fseek($file, 0, SEEK_END);
64 while (count($result) < $limit && $line = $this->readLineFromFile($file)) {
65 $values = str_getcsv($line);
66 list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent) = $values;
67 $csvStatusCode = isset($values[6]) ? $values[6] : null;
69 $csvTime = (int) $csvTime;
71 if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method)) {
75 if (!empty($start) && $csvTime < $start) {
79 if (!empty($end) && $csvTime > $end) {
83 $result[$csvToken] = array(
86 'method' => $csvMethod,
89 'parent' => $csvParent,
90 'status_code' => $csvStatusCode,
96 return array_values($result);
102 public function purge()
104 $flags = \FilesystemIterator::SKIP_DOTS;
105 $iterator = new \RecursiveDirectoryIterator($this->folder, $flags);
106 $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
108 foreach ($iterator as $file) {
109 if (is_file($file)) {
120 public function read($token)
122 if (!$token || !file_exists($file = $this->getFilename($token))) {
126 return $this->createProfileFromData($token, unserialize(file_get_contents($file)));
132 * @throws \RuntimeException
134 public function write(Profile $profile)
136 $file = $this->getFilename($profile->getToken());
138 $profileIndexed = is_file($file);
139 if (!$profileIndexed) {
141 $dir = dirname($file);
142 if (!is_dir($dir) && false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
143 throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $dir));
149 'token' => $profile->getToken(),
150 'parent' => $profile->getParentToken(),
151 'children' => array_map(function ($p) { return $p->getToken(); }, $profile->getChildren()),
152 'data' => $profile->getCollectors(),
153 'ip' => $profile->getIp(),
154 'method' => $profile->getMethod(),
155 'url' => $profile->getUrl(),
156 'time' => $profile->getTime(),
159 if (false === file_put_contents($file, serialize($data))) {
163 if (!$profileIndexed) {
165 if (false === $file = fopen($this->getIndexFilename(), 'a')) {
169 fputcsv($file, array(
170 $profile->getToken(),
172 $profile->getMethod(),
175 $profile->getParentToken(),
176 $profile->getStatusCode(),
185 * Gets filename to store data, associated to the token.
187 * @param string $token
189 * @return string The profile filename
191 protected function getFilename($token)
193 // Uses 4 last characters, because first are mostly the same.
194 $folderA = substr($token, -2, 2);
195 $folderB = substr($token, -4, 2);
197 return $this->folder.'/'.$folderA.'/'.$folderB.'/'.$token;
201 * Gets the index filename.
203 * @return string The index filename
205 protected function getIndexFilename()
207 return $this->folder.'/index.csv';
211 * Reads a line in the file, backward.
213 * This function automatically skips the empty lines and do not include the line return in result value.
215 * @param resource $file The file resource, with the pointer placed at the end of the line to read
217 * @return mixed A string representing the line or null if beginning of file is reached
219 protected function readLineFromFile($file)
222 $position = ftell($file);
224 if (0 === $position) {
229 $chunkSize = min($position, 1024);
230 $position -= $chunkSize;
231 fseek($file, $position);
233 if (0 === $chunkSize) {
238 $buffer = fread($file, $chunkSize);
240 if (false === ($upTo = strrpos($buffer, "\n"))) {
241 $line = $buffer.$line;
246 $line = substr($buffer, $upTo + 1).$line;
247 fseek($file, max(0, $position), SEEK_SET);
254 return '' === $line ? null : $line;
257 protected function createProfileFromData($token, $data, $parent = null)
259 $profile = new Profile($token);
260 $profile->setIp($data['ip']);
261 $profile->setMethod($data['method']);
262 $profile->setUrl($data['url']);
263 $profile->setTime($data['time']);
264 $profile->setCollectors($data['data']);
266 if (!$parent && $data['parent']) {
267 $parent = $this->read($data['parent']);
271 $profile->setParent($parent);
274 foreach ($data['children'] as $token) {
275 if (!$token || !file_exists($file = $this->getFilename($token))) {
279 $profile->addChild($this->createProfileFromData($token, unserialize(file_get_contents($file)), $profile));