FileProfilerStorage.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Profiler;
  11. /**
  12. * Storage for profiler using files.
  13. *
  14. * @author Alexandre Salomé <alexandre.salome@gmail.com>
  15. */
  16. class FileProfilerStorage implements ProfilerStorageInterface
  17. {
  18. /**
  19. * Folder where profiler data are stored.
  20. */
  21. private string $folder;
  22. /**
  23. * Constructs the file storage using a "dsn-like" path.
  24. *
  25. * Example : "file:/path/to/the/storage/folder"
  26. *
  27. * @throws \RuntimeException
  28. */
  29. public function __construct(string $dsn)
  30. {
  31. if (!str_starts_with($dsn, 'file:')) {
  32. 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));
  33. }
  34. $this->folder = substr($dsn, 5);
  35. if (!is_dir($this->folder) && false === @mkdir($this->folder, 0777, true) && !is_dir($this->folder)) {
  36. throw new \RuntimeException(\sprintf('Unable to create the storage directory (%s).', $this->folder));
  37. }
  38. }
  39. public function find(?string $ip, ?string $url, ?int $limit, ?string $method, ?int $start = null, ?int $end = null, ?string $statusCode = null, ?\Closure $filter = null): array
  40. {
  41. $file = $this->getIndexFilename();
  42. if (!file_exists($file)) {
  43. return [];
  44. }
  45. $file = fopen($file, 'r');
  46. fseek($file, 0, \SEEK_END);
  47. $result = [];
  48. while (\count($result) < $limit && $line = $this->readLineFromFile($file)) {
  49. $values = str_getcsv($line, ',', '"', '\\');
  50. if (7 > \count($values)) {
  51. // skip invalid lines
  52. continue;
  53. }
  54. [$csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode, $csvVirtualType] = $values + [7 => null];
  55. $csvTime = (int) $csvTime;
  56. $urlFilter = false;
  57. if ($url) {
  58. $urlFilter = str_starts_with($url, '!') ? str_contains($csvUrl, substr($url, 1)) : !str_contains($csvUrl, $url);
  59. }
  60. if ($ip && !str_contains($csvIp, $ip) || $urlFilter || $method && !str_contains($csvMethod, $method) || $statusCode && !str_contains($csvStatusCode, $statusCode)) {
  61. continue;
  62. }
  63. if ($start && $csvTime < $start) {
  64. continue;
  65. }
  66. if ($end && $csvTime > $end) {
  67. continue;
  68. }
  69. $profile = [
  70. 'token' => $csvToken,
  71. 'ip' => $csvIp,
  72. 'method' => $csvMethod,
  73. 'url' => $csvUrl,
  74. 'time' => $csvTime,
  75. 'parent' => $csvParent,
  76. 'status_code' => $csvStatusCode,
  77. 'virtual_type' => $csvVirtualType ?: 'request',
  78. ];
  79. if ($filter && !$filter($profile)) {
  80. continue;
  81. }
  82. $result[$csvToken] = $profile;
  83. }
  84. fclose($file);
  85. return array_values($result);
  86. }
  87. public function purge(): void
  88. {
  89. $flags = \FilesystemIterator::SKIP_DOTS;
  90. $iterator = new \RecursiveDirectoryIterator($this->folder, $flags);
  91. $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
  92. foreach ($iterator as $file) {
  93. if (is_file($file)) {
  94. unlink($file);
  95. } else {
  96. rmdir($file);
  97. }
  98. }
  99. }
  100. public function read(string $token): ?Profile
  101. {
  102. return $this->doRead($token);
  103. }
  104. /**
  105. * @throws \RuntimeException
  106. */
  107. public function write(Profile $profile): bool
  108. {
  109. $file = $this->getFilename($profile->getToken());
  110. $profileIndexed = is_file($file);
  111. if (!$profileIndexed) {
  112. // Create directory
  113. $dir = \dirname($file);
  114. if (!is_dir($dir) && false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
  115. throw new \RuntimeException(\sprintf('Unable to create the storage directory (%s).', $dir));
  116. }
  117. }
  118. $profileToken = $profile->getToken();
  119. // when there are errors in sub-requests, the parent and/or children tokens
  120. // may equal the profile token, resulting in infinite loops
  121. $parentToken = $profile->getParentToken() !== $profileToken ? $profile->getParentToken() : null;
  122. $childrenToken = array_filter(array_map(fn (Profile $p) => $profileToken !== $p->getToken() ? $p->getToken() : null, $profile->getChildren()));
  123. // Store profile
  124. $data = [
  125. 'token' => $profileToken,
  126. 'parent' => $parentToken,
  127. 'children' => $childrenToken,
  128. 'data' => $profile->getCollectors(),
  129. 'ip' => $profile->getIp(),
  130. 'method' => $profile->getMethod(),
  131. 'url' => $profile->getUrl(),
  132. 'time' => $profile->getTime(),
  133. 'status_code' => $profile->getStatusCode(),
  134. 'virtual_type' => $profile->getVirtualType() ?? 'request',
  135. ];
  136. $data = serialize($data);
  137. if (\function_exists('gzencode')) {
  138. $data = gzencode($data, 3);
  139. }
  140. if (false === file_put_contents($file, $data, \LOCK_EX)) {
  141. return false;
  142. }
  143. if (!$profileIndexed) {
  144. // Add to index
  145. if (false === $file = fopen($this->getIndexFilename(), 'a')) {
  146. return false;
  147. }
  148. fputcsv($file, [
  149. $profile->getToken(),
  150. $profile->getIp(),
  151. $profile->getMethod(),
  152. $profile->getUrl(),
  153. $profile->getTime() ?: time(),
  154. $profile->getParentToken(),
  155. $profile->getStatusCode(),
  156. $profile->getVirtualType() ?? 'request',
  157. ], ',', '"', '\\');
  158. fclose($file);
  159. if (1 === mt_rand(1, 10)) {
  160. $this->removeExpiredProfiles();
  161. }
  162. }
  163. return true;
  164. }
  165. /**
  166. * Gets filename to store data, associated to the token.
  167. */
  168. protected function getFilename(string $token): string
  169. {
  170. // Uses 4 last characters, because first are mostly the same.
  171. $folderA = substr($token, -2, 2);
  172. $folderB = substr($token, -4, 2);
  173. return $this->folder.'/'.$folderA.'/'.$folderB.'/'.$token;
  174. }
  175. /**
  176. * Gets the index filename.
  177. */
  178. protected function getIndexFilename(): string
  179. {
  180. return $this->folder.'/index.csv';
  181. }
  182. /**
  183. * Reads a line in the file, backward.
  184. *
  185. * This function automatically skips the empty lines and do not include the line return in result value.
  186. *
  187. * @param resource $file The file resource, with the pointer placed at the end of the line to read
  188. */
  189. protected function readLineFromFile($file): mixed
  190. {
  191. $line = '';
  192. $position = ftell($file);
  193. if (0 === $position) {
  194. return null;
  195. }
  196. while (true) {
  197. $chunkSize = min($position, 1024);
  198. $position -= $chunkSize;
  199. fseek($file, $position);
  200. if (0 === $chunkSize) {
  201. // bof reached
  202. break;
  203. }
  204. $buffer = fread($file, $chunkSize);
  205. if (false === ($upTo = strrpos($buffer, "\n"))) {
  206. $line = $buffer.$line;
  207. continue;
  208. }
  209. $position += $upTo;
  210. $line = substr($buffer, $upTo + 1).$line;
  211. fseek($file, max(0, $position), \SEEK_SET);
  212. if ('' !== $line) {
  213. break;
  214. }
  215. }
  216. return '' === $line ? null : $line;
  217. }
  218. protected function createProfileFromData(string $token, array $data, ?Profile $parent = null): Profile
  219. {
  220. $profile = new Profile($token);
  221. $profile->setIp($data['ip']);
  222. $profile->setMethod($data['method']);
  223. $profile->setUrl($data['url']);
  224. $profile->setTime($data['time']);
  225. $profile->setStatusCode($data['status_code']);
  226. $profile->setVirtualType($data['virtual_type'] ?: 'request');
  227. $profile->setCollectors($data['data']);
  228. if (!$parent && $data['parent']) {
  229. $parent = $this->read($data['parent']);
  230. }
  231. if ($parent) {
  232. $profile->setParent($parent);
  233. }
  234. foreach ($data['children'] as $token) {
  235. if (null !== $childProfile = $this->doRead($token, $profile)) {
  236. $profile->addChild($childProfile);
  237. }
  238. }
  239. return $profile;
  240. }
  241. private function doRead($token, ?Profile $profile = null): ?Profile
  242. {
  243. if (!$token || !file_exists($file = $this->getFilename($token))) {
  244. return null;
  245. }
  246. $h = fopen($file, 'r');
  247. flock($h, \LOCK_SH);
  248. $data = stream_get_contents($h);
  249. flock($h, \LOCK_UN);
  250. fclose($h);
  251. if (\function_exists('gzdecode')) {
  252. $data = @gzdecode($data) ?: $data;
  253. }
  254. if (!$data = unserialize($data)) {
  255. return null;
  256. }
  257. return $this->createProfileFromData($token, $data, $profile);
  258. }
  259. private function removeExpiredProfiles(): void
  260. {
  261. $minimalProfileTimestamp = time() - 2 * 86400;
  262. $file = $this->getIndexFilename();
  263. $handle = fopen($file, 'r');
  264. if ($offset = is_file($file.'.offset') ? (int) file_get_contents($file.'.offset') : 0) {
  265. fseek($handle, $offset);
  266. }
  267. while ($line = fgets($handle)) {
  268. $values = str_getcsv($line, ',', '"', '\\');
  269. if (7 > \count($values)) {
  270. // skip invalid lines
  271. $offset += \strlen($line);
  272. continue;
  273. }
  274. [$csvToken, , , , $csvTime] = $values;
  275. if ($csvTime >= $minimalProfileTimestamp) {
  276. break;
  277. }
  278. @unlink($this->getFilename($csvToken));
  279. $offset += \strlen($line);
  280. }
  281. fclose($handle);
  282. file_put_contents($file.'.offset', $offset);
  283. }
  284. }