1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of tenside/core. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Schiffler <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* This project is provided in good faith and hope to be usable by anyone. |
12
|
|
|
* |
13
|
|
|
* @package tenside/core |
14
|
|
|
* @author Christian Schiffler <[email protected]> |
15
|
|
|
* @copyright 2015 Christian Schiffler <[email protected]> |
16
|
|
|
* @license https://github.com/tenside/core/blob/master/LICENSE MIT |
17
|
|
|
* @link https://github.com/tenside/core |
18
|
|
|
* @filesource |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Tenside\Core\SelfTest\Cli; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This class tests that a valid php-cli binary is available. |
27
|
|
|
*/ |
28
|
|
|
class SelfTestCliArguments extends AbstractSelfTestCli |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The output buffer to keep track of the detection. |
32
|
|
|
* |
33
|
|
|
* @var BufferedOutput |
34
|
|
|
*/ |
35
|
|
|
private $log; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Check that we have a correct CLI executable of PHP. |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function doTest() |
43
|
|
|
{ |
44
|
|
|
$this->setMessage('Check which arguments to pass to the PHP CLI executable.'); |
45
|
|
|
|
46
|
|
|
if (!$this->hasInterpreter()) { |
47
|
|
|
$this->markFailed('No PHP interpreter detected, can not test.'); |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->log = new BufferedOutput(); |
52
|
|
|
|
53
|
|
|
if ($this->check()) { |
54
|
|
|
$data = $this->log->fetch(); |
55
|
|
|
if (empty($data)) { |
56
|
|
|
$data = 'No special arguments needed.'; |
57
|
|
|
} |
58
|
|
|
$this->markSuccess($data); |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->markWarning( |
63
|
|
|
'Could not determine command line arguments, leaving unconfigured and hope the best.' |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check the needed parameters. |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
private function check() |
73
|
|
|
{ |
74
|
|
|
return |
75
|
|
|
$this->testMemoryLimit() |
76
|
|
|
&& $this->testMaxExecutionTime(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Test if raising the memory limit is needed. |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
private function testMemoryLimit() |
85
|
|
|
{ |
86
|
|
|
$output = $this->testCliRuntime('echo ini_get(\'memory_limit\');'); |
87
|
|
|
if ('-1' !== $output && ($this->memoryInBytes($output) < 2 * 1024 * 1024 * 1024)) { |
88
|
|
View Code Duplication |
if ($this->testOverride('echo ini_get(\'memory_limit\');', '-d memory_limit=2G', '2G')) { |
|
|
|
|
89
|
|
|
$this->getAutoConfig()->addCommandLineArgument('-d memory_limit=2G'); |
90
|
|
|
$this->log->writeln('Will override memory_limit of ' . $output . ' with 2G.'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Test if raising the memory limit is needed. |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
private function testMaxExecutionTime() |
103
|
|
|
{ |
104
|
|
|
$output = $this->testCliRuntime('echo ini_get(\'max_execution_time\');'); |
105
|
|
|
if ((0 !== intval($output)) && (900 > intval($output))) { |
106
|
|
View Code Duplication |
if ($this->testOverride('echo ini_get(\'max_execution_time\');', '-d max_execution_time=900', '900')) { |
|
|
|
|
107
|
|
|
$this->getAutoConfig()->addCommandLineArgument('-d max_execution_time=900'); |
108
|
|
|
$this->log->writeln('Will override max_execution_time of ' . $output . ' with 900 seconds.'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Test if overriding a parameter works. |
117
|
|
|
* |
118
|
|
|
* @param string $script The script to run. |
119
|
|
|
* |
120
|
|
|
* @param string $definition The argument to pass. |
121
|
|
|
* |
122
|
|
|
* @param string $expectedValue The expected output value. |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
private function testOverride($script, $definition, $expectedValue) |
127
|
|
|
{ |
128
|
|
|
$output = $this->testCliRuntime($script, $definition); |
129
|
|
|
if ($expectedValue !== $output) { |
130
|
|
|
$this->log->writeln('Could not override via ' . $definition); |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Convert a human readable memory amount to the exact byte count. |
139
|
|
|
* |
140
|
|
|
* @param string $value The human readable memory string. |
141
|
|
|
* |
142
|
|
|
* @return int |
143
|
|
|
*/ |
144
|
|
|
private function memoryInBytes($value) |
145
|
|
|
{ |
146
|
|
|
$unit = strtolower(substr($value, -1, 1)); |
147
|
|
|
$value = (int) $value; |
148
|
|
|
switch ($unit) { |
149
|
|
|
case 'g': |
150
|
|
|
$value *= 1024; |
151
|
|
|
// no break (cumulative multiplier) |
152
|
|
|
case 'm': |
153
|
|
|
$value *= 1024; |
154
|
|
|
// no break (cumulative multiplier) |
155
|
|
|
case 'k': |
156
|
|
|
$value *= 1024; |
157
|
|
|
break; |
158
|
|
|
default: |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $value; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.