/
MemoryLimit.php
117 lines (104 loc) · 3.3 KB
/
MemoryLimit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace Concrete\Core\Install\Preconditions;
use Concrete\Core\Install\PreconditionInterface;
use Concrete\Core\Install\PreconditionResult;
use Concrete\Core\Utility\Service\Number;
class MemoryLimit implements PreconditionInterface
{
/**
* The minimum memory limit (in bytes).
*
* @var string
*/
const MINIMUM_MEMORY = '24m';
/**
* The minimum memory limit (in bytes).
*
* @var string
*/
const MINIMUM_RECOMMENDED_MEMORY = '64m';
/**
* The number helper.
*
* @var Number
*/
protected $numberHelper;
/**
* Initialize the instance.
*
* @param Number $numberHelper the number helper
*/
public function __construct(Number $numberHelper)
{
$this->numberHelper = $numberHelper;
}
/**
* {@inheritdoc}
*
* @see PreconditionInterface::getName()
*/
public function getName()
{
$bytes = $this->numberHelper->getBytes(static::MINIMUM_RECOMMENDED_MEMORY);
return t('Memory limit %s.', $this->numberHelper->formatSize($bytes));
}
/**
* {@inheritdoc}
*
* @see PreconditionInterface::getUniqueIdentifier()
*/
public function getUniqueIdentifier()
{
return 'memory_limit';
}
/**
* {@inheritdoc}
*
* @see PreconditionInterface::isOptional()
*/
public function isOptional()
{
return false;
}
/**
* {@inheritdoc}
*
* @see PreconditionInterface::performCheck()
*/
public function performCheck()
{
$result = new PreconditionResult();
$memoryLimit = ini_get('memory_limit');
if (empty($memoryLimit) || $memoryLimit == -1) {
$memoryLimit = null;
} else {
$memoryLimit = $this->numberHelper->getBytes($memoryLimit);
}
$recommended = $this->numberHelper->getBytes(static::MINIMUM_RECOMMENDED_MEMORY);
$minumum = $this->numberHelper->getBytes(static::MINIMUM_MEMORY);
if ($memoryLimit !== null && $memoryLimit < $minumum) {
$result
->setState(PreconditionResult::STATE_FAILED)
->setMessage(
t('Concrete will not install with less than %1$s of RAM. Your memory limit is currently %2$s. Please increase your memory_limit using ini_set.',
$this->numberHelper->formatSize($minumum),
$this->numberHelper->formatSize($memoryLimit)
)
)
;
} elseif ($memoryLimit !== null && $memoryLimit < $recommended) {
$result
->setState(PreconditionResult::STATE_WARNING)
->setMessage(
t('Concrete runs best with at least %1$s of RAM. Your memory limit is currently %2$s. You may experience problems uploading and resizing large images, and may have to install Concrete without sample content.',
$this->numberHelper->formatSize($recommended),
$this->numberHelper->formatSize($memoryLimit)
)
)
;
} elseif ($memoryLimit !== null) {
$result->setMessage(t('current memory limit: %1$s', $this->numberHelper->formatSize($memoryLimit)));
}
return $result;
}
}