Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\HttpKernel\DataCollector;
13
 
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
 
17
/**
18
 * @author Fabien Potencier <fabien@symfony.com>
19
 *
20
 * @final
21
 */
22
class MemoryDataCollector extends DataCollector implements LateDataCollectorInterface
23
{
24
    public function __construct()
25
    {
26
        $this->reset();
27
    }
28
 
29
    public function collect(Request $request, Response $response, \Throwable $exception = null)
30
    {
31
        $this->updateMemoryUsage();
32
    }
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
    public function lateCollect()
43
    {
44
        $this->updateMemoryUsage();
45
    }
46
 
47
    public function getMemory(): int
48
    {
49
        return $this->data['memory'];
50
    }
51
 
52
    public function getMemoryLimit(): int|float
53
    {
54
        return $this->data['memory_limit'];
55
    }
56
 
57
    public function updateMemoryUsage()
58
    {
59
        $this->data['memory'] = memory_get_peak_usage(true);
60
    }
61
 
62
    public function getName(): string
63
    {
64
        return 'memory';
65
    }
66
 
67
    private function convertToBytes(string $memoryLimit): int|float
68
    {
69
        if ('-1' === $memoryLimit) {
70
            return -1;
71
        }
72
 
73
        $memoryLimit = strtolower($memoryLimit);
74
        $max = strtolower(ltrim($memoryLimit, '+'));
75
        if (str_starts_with($max, '0x')) {
76
            $max = \intval($max, 16);
77
        } elseif (str_starts_with($max, '0')) {
78
            $max = \intval($max, 8);
79
        } else {
80
            $max = (int) $max;
81
        }
82
 
83
        switch (substr($memoryLimit, -1)) {
84
            case 't': $max *= 1024;
85
                // no break
86
            case 'g': $max *= 1024;
87
                // no break
88
            case 'm': $max *= 1024;
89
                // no break
90
            case 'k': $max *= 1024;
91
        }
92
 
93
        return $max;
94
    }
95
}