vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Profiler/FileProfilerStorage.php line 166

Open in your IDE?
  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 (!== 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($dsn5);
  39.         if (!is_dir($this->folder) && false === @mkdir($this->folder0777true) && !is_dir($this->folder)) {
  40.             throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).'$this->folder));
  41.         }
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function find($ip$url$limit$method$start null$end null$statusCode null)
  47.     {
  48.         $file $this->getIndexFilename();
  49.         if (!file_exists($file)) {
  50.             return [];
  51.         }
  52.         $file fopen($file'r');
  53.         fseek($file0, \SEEK_END);
  54.         $result = [];
  55.         while (\count($result) < $limit && $line $this->readLineFromFile($file)) {
  56.             $values str_getcsv($line);
  57.             list($csvToken$csvIp$csvMethod$csvUrl$csvTime$csvParent$csvStatusCode) = $values;
  58.             $csvTime = (int) $csvTime;
  59.             if ($ip && false === strpos($csvIp$ip) || $url && false === strpos($csvUrl$url) || $method && false === strpos($csvMethod$method) || $statusCode && false === strpos($csvStatusCode$statusCode)) {
  60.                 continue;
  61.             }
  62.             if (!empty($start) && $csvTime $start) {
  63.                 continue;
  64.             }
  65.             if (!empty($end) && $csvTime $end) {
  66.                 continue;
  67.             }
  68.             $result[$csvToken] = [
  69.                 'token' => $csvToken,
  70.                 'ip' => $csvIp,
  71.                 'method' => $csvMethod,
  72.                 'url' => $csvUrl,
  73.                 'time' => $csvTime,
  74.                 'parent' => $csvParent,
  75.                 'status_code' => $csvStatusCode,
  76.             ];
  77.         }
  78.         fclose($file);
  79.         return array_values($result);
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public function purge()
  85.     {
  86.         $flags = \FilesystemIterator::SKIP_DOTS;
  87.         $iterator = new \RecursiveDirectoryIterator($this->folder$flags);
  88.         $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
  89.         foreach ($iterator as $file) {
  90.             if (is_file($file)) {
  91.                 unlink($file);
  92.             } else {
  93.                 rmdir($file);
  94.             }
  95.         }
  96.     }
  97.     /**
  98.      * {@inheritdoc}
  99.      */
  100.     public function read($token)
  101.     {
  102.         if (!$token || !file_exists($file $this->getFilename($token))) {
  103.             return null;
  104.         }
  105.         return $this->createProfileFromData($tokenunserialize(file_get_contents($file)));
  106.     }
  107.     /**
  108.      * {@inheritdoc}
  109.      *
  110.      * @throws \RuntimeException
  111.      */
  112.     public function write(Profile $profile)
  113.     {
  114.         $file $this->getFilename($profile->getToken());
  115.         $profileIndexed is_file($file);
  116.         if (!$profileIndexed) {
  117.             // Create directory
  118.             $dir = \dirname($file);
  119.             if (!is_dir($dir) && false === @mkdir($dir0777true) && !is_dir($dir)) {
  120.                 throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).'$dir));
  121.             }
  122.         }
  123.         $profileToken $profile->getToken();
  124.         // when there are errors in sub-requests, the parent and/or children tokens
  125.         // may equal the profile token, resulting in infinite loops
  126.         $parentToken $profile->getParentToken() !== $profileToken $profile->getParentToken() : null;
  127.         $childrenToken array_filter(array_map(function ($p) use ($profileToken) {
  128.             return $profileToken !== $p->getToken() ? $p->getToken() : null;
  129.         }, $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.         ];
  142.         if (false === file_put_contents($fileserialize($data))) {
  143.             return false;
  144.         }
  145.         if (!$profileIndexed) {
  146.             // Add to index
  147.             if (false === $file fopen($this->getIndexFilename(), 'a')) {
  148.                 return false;
  149.             }
  150.             fputcsv($file, [
  151.                 $profile->getToken(),
  152.                 $profile->getIp(),
  153.                 $profile->getMethod(),
  154.                 $profile->getUrl(),
  155.                 $profile->getTime(),
  156.                 $profile->getParentToken(),
  157.                 $profile->getStatusCode(),
  158.             ]);
  159.             fclose($file);
  160.         }
  161.         return true;
  162.     }
  163.     /**
  164.      * Gets filename to store data, associated to the token.
  165.      *
  166.      * @param string $token
  167.      *
  168.      * @return string The profile filename
  169.      */
  170.     protected function getFilename($token)
  171.     {
  172.         // Uses 4 last characters, because first are mostly the same.
  173.         $folderA substr($token, -22);
  174.         $folderB substr($token, -42);
  175.         return $this->folder.'/'.$folderA.'/'.$folderB.'/'.$token;
  176.     }
  177.     /**
  178.      * Gets the index filename.
  179.      *
  180.      * @return string The index filename
  181.      */
  182.     protected function getIndexFilename()
  183.     {
  184.         return $this->folder.'/index.csv';
  185.     }
  186.     /**
  187.      * Reads a line in the file, backward.
  188.      *
  189.      * This function automatically skips the empty lines and do not include the line return in result value.
  190.      *
  191.      * @param resource $file The file resource, with the pointer placed at the end of the line to read
  192.      *
  193.      * @return mixed A string representing the line or null if beginning of file is reached
  194.      */
  195.     protected function readLineFromFile($file)
  196.     {
  197.         $line '';
  198.         $position ftell($file);
  199.         if (=== $position) {
  200.             return null;
  201.         }
  202.         while (true) {
  203.             $chunkSize min($position1024);
  204.             $position -= $chunkSize;
  205.             fseek($file$position);
  206.             if (=== $chunkSize) {
  207.                 // bof reached
  208.                 break;
  209.             }
  210.             $buffer fread($file$chunkSize);
  211.             if (false === ($upTo strrpos($buffer"\n"))) {
  212.                 $line $buffer.$line;
  213.                 continue;
  214.             }
  215.             $position += $upTo;
  216.             $line substr($buffer$upTo 1).$line;
  217.             fseek($filemax(0$position), \SEEK_SET);
  218.             if ('' !== $line) {
  219.                 break;
  220.             }
  221.         }
  222.         return '' === $line null $line;
  223.     }
  224.     protected function createProfileFromData($token$data$parent null)
  225.     {
  226.         $profile = new Profile($token);
  227.         $profile->setIp($data['ip']);
  228.         $profile->setMethod($data['method']);
  229.         $profile->setUrl($data['url']);
  230.         $profile->setTime($data['time']);
  231.         $profile->setStatusCode($data['status_code']);
  232.         $profile->setCollectors($data['data']);
  233.         if (!$parent && $data['parent']) {
  234.             $parent $this->read($data['parent']);
  235.         }
  236.         if ($parent) {
  237.             $profile->setParent($parent);
  238.         }
  239.         foreach ($data['children'] as $token) {
  240.             if (!$token || !file_exists($file $this->getFilename($token))) {
  241.                 continue;
  242.             }
  243.             $profile->addChild($this->createProfileFromData($tokenunserialize(file_get_contents($file)), $profile));
  244.         }
  245.         return $profile;
  246.     }
  247. }