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", 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(), ]; } } 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); } }