From 0c3e5833d533e2c29ecc5d3e928861f9432f95e8 Mon Sep 17 00:00:00 2001 From: Thiago Victorino Date: Sun, 27 Sep 2020 23:43:34 -0300 Subject: [PATCH 1/5] Add ext-zend-opcache on composer as requirement. --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 541af3e..2211208 100755 --- a/composer.json +++ b/composer.json @@ -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", From 6672e138d4572908dcc8e8b5674ee819ac190f71 Mon Sep 17 00:00:00 2001 From: Thiago Victorino Date: Sun, 27 Sep 2020 23:44:29 -0300 Subject: [PATCH 2/5] Fix deprecated phpunit functions --- tests/ClearTest.php | 2 +- tests/CompileTest.php | 2 +- tests/ConfigTest.php | 4 ++-- tests/StatusTest.php | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/ClearTest.php b/tests/ClearTest.php index dd3f157..ea836bb 100644 --- a/tests/ClearTest.php +++ b/tests/ClearTest.php @@ -13,6 +13,6 @@ public function is_cleared() $output = Artisan::output(); - $this->assertContains('cleared', $output); + $this->assertStringContainsString('cleared', $output); } } diff --git a/tests/CompileTest.php b/tests/CompileTest.php index 3aaa633..c15cbe5 100644 --- a/tests/CompileTest.php +++ b/tests/CompileTest.php @@ -13,6 +13,6 @@ public function optimizes() $output = Artisan::output(); - $this->assertContains('files compiled', $output); + $this->assertStringContainsString('files compiled', $output); } } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 8d7d5dd..8acf2fe 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -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); } } diff --git a/tests/StatusTest.php b/tests/StatusTest.php index 8dc1e14..1641cd6 100644 --- a/tests/StatusTest.php +++ b/tests/StatusTest.php @@ -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); } } From e7c0ca9a9caa2c869e848026b831da493137d732 Mon Sep 17 00:00:00 2001 From: Thiago Victorino Date: Sun, 27 Sep 2020 23:45:55 -0300 Subject: [PATCH 3/5] Refactor commands to improve readability --- src/Commands/Clear.php | 7 ++---- src/Commands/Compile.php | 14 +++++------ src/Commands/Config.php | 36 +++++++++++++++-------------- src/Commands/Status.php | 50 +++++++++++++++++++++++++++------------- src/CreatesRequest.php | 12 ++++++++-- src/OpcacheClass.php | 16 +++++-------- 6 files changed, 78 insertions(+), 57 deletions(-) diff --git a/src/Commands/Clear.php b/src/Commands/Clear.php index 665fce7..daf6753 100644 --- a/src/Commands/Clear.php +++ b/src/Commands/Clear.php @@ -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'); } } diff --git a/src/Commands/Compile.php b/src/Commands/Compile.php index 94189b4..770085f 100644 --- a/src/Commands/Compile.php +++ b/src/Commands/Compile.php @@ -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'])); } } diff --git a/src/Commands/Config.php b/src/Commands/Config.php index 1048347..67315af 100644 --- a/src/Commands/Config.php +++ b/src/Commands/Config.php @@ -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'])); } /** @@ -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'; + } } diff --git a/src/Commands/Status.php b/src/Commands/Status.php index 574b0b6..4e5f1bb 100644 --- a/src/Commands/Status.php +++ b/src/Commands/Status.php @@ -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']); } /** @@ -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; + } } diff --git a/src/CreatesRequest.php b/src/CreatesRequest.php index 8eb4ab3..8601785 100644 --- a/src/CreatesRequest.php +++ b/src/CreatesRequest.php @@ -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, '/'); + } } diff --git a/src/OpcacheClass.php b/src/OpcacheClass.php index 337363b..b79afe3 100755 --- a/src/OpcacheClass.php +++ b/src/OpcacheClass.php @@ -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')) @@ -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(), ]; } } From b47a66da5fc251b604a84fd6529ad78c5c655146 Mon Sep 17 00:00:00 2001 From: Thiago Victorino Date: Sun, 27 Sep 2020 23:59:21 -0300 Subject: [PATCH 4/5] Fix CI issues --- src/Commands/Clear.php | 3 ++- src/Commands/Compile.php | 4 +++- src/Commands/Config.php | 7 +++++-- src/Commands/Status.php | 14 ++++++++++---- src/CreatesRequest.php | 3 ++- src/OpcacheClass.php | 1 + 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Commands/Clear.php b/src/Commands/Clear.php index daf6753..e54cda0 100644 --- a/src/Commands/Clear.php +++ b/src/Commands/Clear.php @@ -32,8 +32,9 @@ public function handle() { $response = $this->sendRequest('clear'); $response->throw(); - if (!$response->successful()) { + if (! $response->successful()) { $this->error('OPcache not configured'); + return 2; } $this->info('OPcache cleared'); diff --git a/src/Commands/Compile.php b/src/Commands/Compile.php index 770085f..ba4f60f 100644 --- a/src/Commands/Compile.php +++ b/src/Commands/Compile.php @@ -33,13 +33,15 @@ public function handle() $response = $this->sendRequest('compile', ['force' => $this->option('force') ?? false]); $response->throw(); - if (!$response->successful()) { + if (! $response->successful()) { $this->error('OPcache not configured'); + return 2; } if (isset($response['result']['message'])) { $this->warn($response['result']['message']); + return 1; } diff --git a/src/Commands/Config.php b/src/Commands/Config.php index 67315af..46e2f28 100644 --- a/src/Commands/Config.php +++ b/src/Commands/Config.php @@ -33,8 +33,9 @@ public function handle() $response = $this->sendRequest('config'); $response->throw(); - if (!$response->successful()) { + if (! $response->successful()) { $this->error('OPcache not configured'); + return 2; } @@ -68,10 +69,12 @@ protected function parseTable($input) * @param $value * @return string */ - protected function parseValue($key, $value) { + protected function parseValue($key, $value) + { if (in_array($key, ['opcache.memory_consumption'])) { return number_format($value / 1048576, 2).' MB'; } + return $value ? 'true' : 'false'; } } diff --git a/src/Commands/Status.php b/src/Commands/Status.php index 4e5f1bb..9f1b8d7 100644 --- a/src/Commands/Status.php +++ b/src/Commands/Status.php @@ -33,8 +33,9 @@ public function handle() $response = $this->sendRequest('status'); $response->throw(); - if (!$response['result']) { + if (! $response->successful()) { $this->error('OPcache not configured'); + return 2; } @@ -90,7 +91,7 @@ protected function parseTable($input) return array_map(function ($key, $value) { return [ 'key' => $key, - 'value' => $this->parseValue($key, $value) + 'value' => $this->parseValue($key, $value), ]; }, array_keys($input), $input); } @@ -100,23 +101,28 @@ protected function parseTable($input) * @param $value * @return string */ - protected function parseValue($key, $value) { + 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)) { + if (is_array($value)) { + return implode(PHP_EOL, $value); } diff --git a/src/CreatesRequest.php b/src/CreatesRequest.php index 8601785..65a435d 100644 --- a/src/CreatesRequest.php +++ b/src/CreatesRequest.php @@ -25,7 +25,8 @@ public function sendRequest($url, $parameters = []) * @param string $url * @return string */ - protected function buildEndpoint(string $url): string { + protected function buildEndpoint(string $url): string + { return rtrim(config('opcache.url'), '/').'/'.trim(config('opcache.prefix'), '/').'/'.ltrim($url, '/'); } } diff --git a/src/OpcacheClass.php b/src/OpcacheClass.php index b79afe3..8cf421e 100755 --- a/src/OpcacheClass.php +++ b/src/OpcacheClass.php @@ -68,6 +68,7 @@ public function compile($force = false) return false; } @opcache_compile_file($file); + return true; }); From 5093c7ab357dba42b3a7307483083bb127e3de16 Mon Sep 17 00:00:00 2001 From: Thiago Victorino Date: Mon, 28 Sep 2020 00:00:39 -0300 Subject: [PATCH 5/5] Fix Style CI issues --- src/Commands/Status.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Commands/Status.php b/src/Commands/Status.php index 9f1b8d7..cb5665f 100644 --- a/src/Commands/Status.php +++ b/src/Commands/Status.php @@ -37,7 +37,6 @@ public function handle() $this->error('OPcache not configured'); return 2; - } $this->displayTables($response['result']); @@ -107,22 +106,18 @@ protected function parseValue($key, $value) $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); }