FileProfilerStorage.php 11 KB

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