comparison vendor/symfony/http-kernel/DataCollector/MemoryDataCollector.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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->data = array(
27 'memory' => 0,
28 'memory_limit' => $this->convertToBytes(ini_get('memory_limit')),
29 );
30 }
31
32 /**
33 * {@inheritdoc}
34 */
35 public function collect(Request $request, Response $response, \Exception $exception = null)
36 {
37 $this->updateMemoryUsage();
38 }
39
40 /**
41 * {@inheritdoc}
42 */
43 public function lateCollect()
44 {
45 $this->updateMemoryUsage();
46 }
47
48 /**
49 * Gets the memory.
50 *
51 * @return int The memory
52 */
53 public function getMemory()
54 {
55 return $this->data['memory'];
56 }
57
58 /**
59 * Gets the PHP memory limit.
60 *
61 * @return int The memory limit
62 */
63 public function getMemoryLimit()
64 {
65 return $this->data['memory_limit'];
66 }
67
68 /**
69 * Updates the memory usage data.
70 */
71 public function updateMemoryUsage()
72 {
73 $this->data['memory'] = memory_get_peak_usage(true);
74 }
75
76 /**
77 * {@inheritdoc}
78 */
79 public function getName()
80 {
81 return 'memory';
82 }
83
84 private function convertToBytes($memoryLimit)
85 {
86 if ('-1' === $memoryLimit) {
87 return -1;
88 }
89
90 $memoryLimit = strtolower($memoryLimit);
91 $max = strtolower(ltrim($memoryLimit, '+'));
92 if (0 === strpos($max, '0x')) {
93 $max = intval($max, 16);
94 } elseif (0 === strpos($max, '0')) {
95 $max = intval($max, 8);
96 } else {
97 $max = (int) $max;
98 }
99
100 switch (substr($memoryLimit, -1)) {
101 case 't': $max *= 1024;
102 case 'g': $max *= 1024;
103 case 'm': $max *= 1024;
104 case 'k': $max *= 1024;
105 }
106
107 return $max;
108 }
109 }