Skip to content

Commit

Permalink
Finish README
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander De la Marche committed Jan 9, 2023
1 parent 1368482 commit 3c9b0b8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Analogo

Minimal dependency to reset the content of the OPcache through PHP-FPM.

## Installation

composer require dreadnip/analogo

## Usage
This tool only supports OPcache and fcgi/PHP-FPM.

If no option is passed, it'll look for a php-fpm socket file in all the common places. If multiple are found, it'll try to select one for your PHP version.

If no file can be found, it will default to 127.0.0.1:9000.

Since this is a single-command application, you just need to call the binary:

php /path/to/your/project/vendor/bin/analogo

You can also pass an IP address or a unix socket to the --fcgi option.

php /path/to/your/project/vendor/bin/analogo --fcgi=/var/run/php-fpm.sock

## Credit

This tool is of course inspired and based on the awesome [cachetool](https://github.com/gordalina/cachetool). All of the FCGI socket code is handled by [hollodotme/fast-cgi-client](https://github.com/hollodotme/fast-cgi-client).
9 changes: 1 addition & 8 deletions analogo
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;

(new SingleCommandApplication())
->setName('Reset the opcache')
->addArgument('fcgi', InputArgument::OPTIONAL, 'Path to a socket file or network address.')
->setCode(function (InputInterface $input, OutputInterface $output) {
if ($input->hasArgument('fcgi')) {
$kernel = new Kernel($input->getArgument('fcgi'));
} else {
$kernel = new Kernel();
}

$kernel->reset();
(new Kernel($input->getArgument('fcgi')))->reset();

return Command::SUCCESS;
})
Expand Down
27 changes: 5 additions & 22 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ public function __construct(string $host = null)

$IPv6 = '/^(?:[A-F0-9]{0,4}:){1,7}[A-F0-9]{0,4}$/';
if (preg_match($IPv6, $host) === 1) {
// IPv6 addresses need to be surrounded by brackets
// see: https://www.php.net/manual/en/function.stream-socket-client.php#refsect1-function.stream-socket-client-notes
$host = "[$host]";
}

Expand All @@ -83,12 +81,13 @@ public function __construct(string $host = null)

public function reset(): void
{
$file = $this->createResetScript();
$file = sprintf("%s/analogo-%s.php", sys_get_temp_dir(), bin2hex(random_bytes(16)));

file_put_contents($file, '<?php opcache_reset();');
chmod($file, 0666);

try {
$client = new Client();
$request = new PostRequest($file, '');
$client->sendRequest($this->connection, $request);
(new Client())->sendRequest($this->connection, new PostRequest($file, ''));

unlink($file);
} catch (\Throwable $exception) {
Expand All @@ -101,20 +100,4 @@ public function reset(): void
);
}
}

protected function createResetScript(): string
{
$resetScript = <<<RESET
<?php
opcache_reset();
RESET;

$file = sprintf("%s/analogo-%s.php", sys_get_temp_dir(), bin2hex(random_bytes(16)));

file_put_contents($file, $resetScript);
chmod($file, 0666);

return $file;
}
}

0 comments on commit 3c9b0b8

Please sign in to comment.