8d8cc1a04d1818d7cfaa0f83fd3ca5c1d6fea81a
[yaffs-website] / vendor / symfony / http-kernel / DataCollector / MemoryDataCollector.php
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  * MemoryDataCollector.
19  *
20  * @author Fabien Potencier <fabien@symfony.com>
21  */
22 class MemoryDataCollector extends DataCollector implements LateDataCollectorInterface
23 {
24     public function __construct()
25     {
26         $this->reset();
27     }
28
29     /**
30      * {@inheritdoc}
31      */
32     public function collect(Request $request, Response $response, \Exception $exception = null)
33     {
34         $this->updateMemoryUsage();
35     }
36
37     /**
38      * {@inheritdoc}
39      */
40     public function reset()
41     {
42         $this->data = array(
43             'memory' => 0,
44             'memory_limit' => $this->convertToBytes(ini_get('memory_limit')),
45         );
46     }
47
48     /**
49      * {@inheritdoc}
50      */
51     public function lateCollect()
52     {
53         $this->updateMemoryUsage();
54     }
55
56     /**
57      * Gets the memory.
58      *
59      * @return int The memory
60      */
61     public function getMemory()
62     {
63         return $this->data['memory'];
64     }
65
66     /**
67      * Gets the PHP memory limit.
68      *
69      * @return int The memory limit
70      */
71     public function getMemoryLimit()
72     {
73         return $this->data['memory_limit'];
74     }
75
76     /**
77      * Updates the memory usage data.
78      */
79     public function updateMemoryUsage()
80     {
81         $this->data['memory'] = memory_get_peak_usage(true);
82     }
83
84     /**
85      * {@inheritdoc}
86      */
87     public function getName()
88     {
89         return 'memory';
90     }
91
92     private function convertToBytes($memoryLimit)
93     {
94         if ('-1' === $memoryLimit) {
95             return -1;
96         }
97
98         $memoryLimit = strtolower($memoryLimit);
99         $max = strtolower(ltrim($memoryLimit, '+'));
100         if (0 === strpos($max, '0x')) {
101             $max = intval($max, 16);
102         } elseif (0 === strpos($max, '0')) {
103             $max = intval($max, 8);
104         } else {
105             $max = (int) $max;
106         }
107
108         switch (substr($memoryLimit, -1)) {
109             case 't': $max *= 1024;
110             // no break
111             case 'g': $max *= 1024;
112             // no break
113             case 'm': $max *= 1024;
114             // no break
115             case 'k': $max *= 1024;
116         }
117
118         return $max;
119     }
120 }