vendor/symfony/http-kernel/DataCollector/MemoryDataCollector.php line 32

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\DataCollector;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. /**
  14.  * @author Fabien Potencier <fabien@symfony.com>
  15.  *
  16.  * @final
  17.  */
  18. class MemoryDataCollector extends DataCollector implements LateDataCollectorInterface
  19. {
  20.     public function __construct()
  21.     {
  22.         $this->reset();
  23.     }
  24.     /**
  25.      * {@inheritdoc}
  26.      */
  27.     public function collect(Request $requestResponse $response\Throwable $exception null)
  28.     {
  29.         $this->updateMemoryUsage();
  30.     }
  31.     /**
  32.      * {@inheritdoc}
  33.      */
  34.     public function reset()
  35.     {
  36.         $this->data = [
  37.             'memory' => 0,
  38.             'memory_limit' => $this->convertToBytes(\ini_get('memory_limit')),
  39.         ];
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function lateCollect()
  45.     {
  46.         $this->updateMemoryUsage();
  47.     }
  48.     public function getMemory(): int
  49.     {
  50.         return $this->data['memory'];
  51.     }
  52.     /**
  53.      * @return int|float
  54.      */
  55.     public function getMemoryLimit()
  56.     {
  57.         return $this->data['memory_limit'];
  58.     }
  59.     public function updateMemoryUsage()
  60.     {
  61.         $this->data['memory'] = memory_get_peak_usage(true);
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function getName(): string
  67.     {
  68.         return 'memory';
  69.     }
  70.     /**
  71.      * @return int|float
  72.      */
  73.     private function convertToBytes(string $memoryLimit)
  74.     {
  75.         if ('-1' === $memoryLimit) {
  76.             return -1;
  77.         }
  78.         $memoryLimit strtolower($memoryLimit);
  79.         $max strtolower(ltrim($memoryLimit'+'));
  80.         if (str_starts_with($max'0x')) {
  81.             $max \intval($max16);
  82.         } elseif (str_starts_with($max'0')) {
  83.             $max \intval($max8);
  84.         } else {
  85.             $max = (int) $max;
  86.         }
  87.         switch (substr($memoryLimit, -1)) {
  88.             case 't'$max *= 1024;
  89.             // no break
  90.             case 'g'$max *= 1024;
  91.             // no break
  92.             case 'm'$max *= 1024;
  93.             // no break
  94.             case 'k'$max *= 1024;
  95.         }
  96.         return $max;
  97.     }
  98. }