FileProfilerStorage.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. * @var string
  22. */
  23. private $folder;
  24. /**
  25. * Constructs the file storage using a "dsn-like" path.
  26. *
  27. * Example : "file:/path/to/the/storage/folder"
  28. *
  29. * @param string $dsn The DSN
  30. *
  31. * @throws \RuntimeException
  32. */
  33. public function __construct($dsn)
  34. {
  35. if (0 !== strpos($dsn, 'file:')) {
  36. 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));
  37. }
  38. $this->folder = substr($dsn, 5);
  39. if (!is_dir($this->folder)) {
  40. mkdir($this->folder, 0777, true);
  41. }
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function find($ip, $url, $limit, $method, $start = null, $end = null)
  47. {
  48. $file = $this->getIndexFilename();
  49. if (!file_exists($file)) {
  50. return array();
  51. }
  52. $file = fopen($file, 'r');
  53. fseek($file, 0, SEEK_END);
  54. $result = array();
  55. while (count($result) < $limit && $line = $this->readLineFromFile($file)) {
  56. list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent) = str_getcsv($line);
  57. $csvTime = (int) $csvTime;
  58. if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method)) {
  59. continue;
  60. }
  61. if (!empty($start) && $csvTime < $start) {
  62. continue;
  63. }
  64. if (!empty($end) && $csvTime > $end) {
  65. continue;
  66. }
  67. $result[$csvToken] = array(
  68. 'token' => $csvToken,
  69. 'ip' => $csvIp,
  70. 'method' => $csvMethod,
  71. 'url' => $csvUrl,
  72. 'time' => $csvTime,
  73. 'parent' => $csvParent,
  74. );
  75. }
  76. fclose($file);
  77. return array_values($result);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function purge()
  83. {
  84. $flags = \FilesystemIterator::SKIP_DOTS;
  85. $iterator = new \RecursiveDirectoryIterator($this->folder, $flags);
  86. $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
  87. foreach ($iterator as $file) {
  88. if (is_file($file)) {
  89. unlink($file);
  90. } else {
  91. rmdir($file);
  92. }
  93. }
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function read($token)
  99. {
  100. if (!$token || !file_exists($file = $this->getFilename($token))) {
  101. return null;
  102. }
  103. return $this->createProfileFromData($token, unserialize(file_get_contents($file)));
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function write(Profile $profile)
  109. {
  110. $file = $this->getFilename($profile->getToken());
  111. $profileIndexed = is_file($file);
  112. if (!$profileIndexed) {
  113. // Create directory
  114. $dir = dirname($file);
  115. if (!is_dir($dir)) {
  116. mkdir($dir, 0777, true);
  117. }
  118. }
  119. // Store profile
  120. $data = array(
  121. 'token' => $profile->getToken(),
  122. 'parent' => $profile->getParentToken(),
  123. 'children' => array_map(function ($p) { return $p->getToken(); }, $profile->getChildren()),
  124. 'data' => $profile->getCollectors(),
  125. 'ip' => $profile->getIp(),
  126. 'method' => $profile->getMethod(),
  127. 'url' => $profile->getUrl(),
  128. 'time' => $profile->getTime(),
  129. );
  130. if (false === file_put_contents($file, serialize($data))) {
  131. return false;
  132. }
  133. if (!$profileIndexed) {
  134. // Add to index
  135. if (false === $file = fopen($this->getIndexFilename(), 'a')) {
  136. return false;
  137. }
  138. fputcsv($file, array(
  139. $profile->getToken(),
  140. $profile->getIp(),
  141. $profile->getMethod(),
  142. $profile->getUrl(),
  143. $profile->getTime(),
  144. $profile->getParentToken(),
  145. ));
  146. fclose($file);
  147. }
  148. return true;
  149. }
  150. /**
  151. * Gets filename to store data, associated to the token.
  152. *
  153. * @param string $token
  154. *
  155. * @return string The profile filename
  156. */
  157. protected function getFilename($token)
  158. {
  159. // Uses 4 last characters, because first are mostly the same.
  160. $folderA = substr($token, -2, 2);
  161. $folderB = substr($token, -4, 2);
  162. return $this->folder.'/'.$folderA.'/'.$folderB.'/'.$token;
  163. }
  164. /**
  165. * Gets the index filename.
  166. *
  167. * @return string The index filename
  168. */
  169. protected function getIndexFilename()
  170. {
  171. return $this->folder.'/index.csv';
  172. }
  173. /**
  174. * Reads a line in the file, backward.
  175. *
  176. * This function automatically skips the empty lines and do not include the line return in result value.
  177. *
  178. * @param resource $file The file resource, with the pointer placed at the end of the line to read
  179. *
  180. * @return mixed A string representing the line or null if beginning of file is reached
  181. */
  182. protected function readLineFromFile($file)
  183. {
  184. $line = '';
  185. $position = ftell($file);
  186. if (0 === $position) {
  187. return null;
  188. }
  189. while (true) {
  190. $chunkSize = min($position, 1024);
  191. $position -= $chunkSize;
  192. fseek($file, $position);
  193. if (0 === $chunkSize) {
  194. // bof reached
  195. break;
  196. }
  197. $buffer = fread($file, $chunkSize);
  198. if (false === ($upTo = strrpos($buffer, "\n"))) {
  199. $line = $buffer.$line;
  200. continue;
  201. }
  202. $position += $upTo;
  203. $line = substr($buffer, $upTo + 1).$line;
  204. fseek($file, max(0, $position), SEEK_SET);
  205. if ('' !== $line) {
  206. break;
  207. }
  208. }
  209. return '' === $line ? null : $line;
  210. }
  211. protected function createProfileFromData($token, $data, $parent = null)
  212. {
  213. $profile = new Profile($token);
  214. $profile->setIp($data['ip']);
  215. $profile->setMethod($data['method']);
  216. $profile->setUrl($data['url']);
  217. $profile->setTime($data['time']);
  218. $profile->setCollectors($data['data']);
  219. if (!$parent && $data['parent']) {
  220. $parent = $this->read($data['parent']);
  221. }
  222. if ($parent) {
  223. $profile->setParent($parent);
  224. }
  225. foreach ($data['children'] as $token) {
  226. if (!$token || !file_exists($file = $this->getFilename($token))) {
  227. continue;
  228. }
  229. $profile->addChild($this->createProfileFromData($token, unserialize(file_get_contents($file)), $profile));
  230. }
  231. return $profile;
  232. }
  233. }