PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Profiler/FileProfilerStorage.php

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