Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor commands to improve readability #1

Merged
merged 3 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"require": {
"php": "^7.2.5",
"laravel/framework": ">=7.0",
"guzzlehttp/guzzle": "^6.3.1|^7.0"
"guzzlehttp/guzzle": "^6.3.1|^7.0",
"ext-zend-opcache": "*"
},
"require-dev": {
"orchestra/testbench": "^5.0",
Expand Down
7 changes: 2 additions & 5 deletions src/Commands/Clear.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ public function handle()
{
$response = $this->sendRequest('clear');
$response->throw();

if ($response['result']) {
$this->info('OPcache cleared');
} else {
if (!$response->successful()) {
$this->error('OPcache not configured');

return 2;
}
$this->info('OPcache cleared');
}
}
14 changes: 7 additions & 7 deletions src/Commands/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public function handle()
$response = $this->sendRequest('compile', ['force' => $this->option('force') ?? false]);
$response->throw();

if (!$response->successful()) {
$this->error('OPcache not configured');
return 2;
}

if (isset($response['result']['message'])) {
$this->warn($response['result']['message']);

return 1;
} elseif ($response['result']) {
$this->info(sprintf('%s of %s files compiled', $response['result']['compiled_count'], $response['result']['total_files_count']));
} else {
$this->error('OPcache not configured');

return 2;
}

$this->info(sprintf('%s of %s files compiled', $response['result']['compiled_count'], $response['result']['total_files_count']));
}
}
36 changes: 19 additions & 17 deletions src/Commands/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ public function handle()
$response = $this->sendRequest('config');
$response->throw();

if ($response['result']) {
$this->line('Version info:');
$this->table([], $this->parseTable($response['result']['version']));

$this->line(PHP_EOL.'Configuration info:');
$this->table([], $this->parseTable($response['result']['directives']));
} else {
if (!$response->successful()) {
$this->error('OPcache not configured');

return 2;
}

$this->line('Version info:');
$this->table([], $this->parseTable($response['result']['version']));
$this->line(PHP_EOL.'Configuration info:');
$this->table([], $this->parseTable($response['result']['directives']));
}

/**
Expand All @@ -58,18 +56,22 @@ protected function parseTable($input)
$input = (array) $input;

return array_map(function ($key, $value) {
$bytes = ['opcache.memory_consumption'];

if (in_array($key, $bytes)) {
$value = number_format($value / 1048576, 2).' MB';
} elseif (is_bool($value)) {
$value = $value ? 'true' : 'false';
}

return [
'key' => $key,
'value' => $value,
'value' => $this->parseValue($key, $value),
];
}, array_keys($input), $input);
}

/**
* @param $key
* @param $value
* @return string
*/
protected function parseValue($key, $value) {
if (in_array($key, ['opcache.memory_consumption'])) {
return number_format($value / 1048576, 2).' MB';
}
return $value ? 'true' : 'false';
}
}
50 changes: 34 additions & 16 deletions src/Commands/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public function handle()
$response = $this->sendRequest('status');
$response->throw();

if ($response['result']) {
$this->displayTables($response['result']);
} else {
if (!$response['result']) {
$this->error('OPcache not configured');

return 2;

}

$this->displayTables($response['result']);
}

/**
Expand Down Expand Up @@ -86,22 +86,40 @@ protected function displayTables($data)
protected function parseTable($input)
{
$input = (array) $input;
$bytes = ['used_memory', 'free_memory', 'wasted_memory', 'buffer_size'];
$times = ['start_time', 'last_restart_time'];

return array_map(function ($key, $value) use ($bytes, $times) {
if (in_array($key, $bytes)) {
$value = number_format($value / 1048576, 2).' MB';
} elseif (in_array($key, $times)) {
$value = date('Y-m-d H:i:s', $value);
} elseif (is_bool($value)) {
$value = $value ? 'true' : 'false';
}

return array_map(function ($key, $value) {
return [
'key' => $key,
'value' => is_array($value) ? implode(PHP_EOL, $value) : $value,
'value' => $this->parseValue($key, $value)
];
}, array_keys($input), $input);
}

/**
* @param $key
* @param $value
* @return string
*/
protected function parseValue($key, $value) {
$bytes = ['used_memory', 'free_memory', 'wasted_memory', 'buffer_size'];
$times = ['start_time', 'last_restart_time'];

if (in_array($key, $bytes)) {
return number_format($value / 1048576, 2).' MB';
}

if (in_array($key, $times)) {
return date('Y-m-d H:i:s', $value);
}

if (is_bool($value)) {
return $value ? 'true' : 'false';
}

if(is_array($value)) {
return implode(PHP_EOL, $value);
}

return $value;
}
}
12 changes: 10 additions & 2 deletions src/CreatesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ trait CreatesRequest
/**
* @param $url
* @param $parameters
* @return \Appstract\LushHttp\Response\LushResponse
* @return \Illuminate\Http\Client\Response
*/
public function sendRequest($url, $parameters = [])
{
return Http::withHeaders(config('opcache.headers'))
->withOptions(['verify' => config('opcache.verify')])
->get(rtrim(config('opcache.url'), '/').'/'.trim(config('opcache.prefix'), '/').'/'.ltrim($url, '/'),
->get($this->buildEndpoint($url),
array_merge(['key' => Crypt::encrypt('opcache')], $parameters)
);
}

/**
* @param string $url
* @return string
*/
protected function buildEndpoint(string $url): string {
return rtrim(config('opcache.url'), '/').'/'.trim(config('opcache.prefix'), '/').'/'.ltrim($url, '/');
}
}
16 changes: 6 additions & 10 deletions src/OpcacheClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public function compile($force = false)
}

if (function_exists('opcache_compile_file')) {
$compiled = 0;

// Get files in these paths
$files = collect(Finder::create()->in(config('opcache.directories'))
Expand All @@ -64,20 +63,17 @@ public function compile($force = false)
->followLinks());

// optimized files
$files->each(function ($file) use (&$compiled) {
try {
if (! opcache_is_script_cached($file)) {
opcache_compile_file($file);
}

$compiled++;
} catch (\Exception $e) {
$compiled = $files->filter(function ($file) {
if (opcache_is_script_cached($file)) {
return false;
}
@opcache_compile_file($file);
return true;
});

return [
'total_files_count' => $files->count(),
'compiled_count' => $compiled,
'compiled_count' => $compiled->count(),
];
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ClearTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public function is_cleared()

$output = Artisan::output();

$this->assertContains('cleared', $output);
$this->assertStringContainsString('cleared', $output);
}
}
2 changes: 1 addition & 1 deletion tests/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public function optimizes()

$output = Artisan::output();

$this->assertContains('files compiled', $output);
$this->assertStringContainsString('files compiled', $output);
}
}
4 changes: 2 additions & 2 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function shows_config()

$output = Artisan::output();

$this->assertContains('Version info', $output);
$this->assertContains('Configuration info', $output);
$this->assertStringContainsString('Version info', $output);
$this->assertStringContainsString('Configuration info', $output);
}
}
6 changes: 3 additions & 3 deletions tests/StatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function shows_status()

$output = Artisan::output();

$this->assertContains('Memory usage:', $output);
$this->assertContains('Interned strings usage:', $output);
$this->assertContains('Statistics:', $output);
$this->assertStringContainsString('Memory usage:', $output);
$this->assertStringContainsString('Interned strings usage:', $output);
$this->assertStringContainsString('Statistics:', $output);
}
}