diff options
author | Wolfy-J <[email protected]> | 2019-05-04 00:09:12 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2019-05-04 00:09:12 +0300 |
commit | 727602f9d6e3b5a92f6fa0d7a497ab1ef358a349 (patch) | |
tree | ec41356d3e421284f44d8c82f3b297e5c8ab2c7a /src | |
parent | f4515e276179d77e3f9457ba0d99545081ae50ea (diff) | |
parent | 8a2f79a622e9b3b6e20718a257bfdaaf8dbb8747 (diff) |
Merge pull request #144 from spiral/feature/updates
WIP: 1.4.0
Diffstat (limited to 'src')
-rw-r--r-- | src/Diactoros/ServerRequestFactory.php | 3 | ||||
-rw-r--r-- | src/Diactoros/StreamFactory.php | 3 | ||||
-rw-r--r-- | src/Diactoros/UploadedFileFactory.php | 3 | ||||
-rw-r--r-- | src/Exception/RoadRunnerException.php | 3 | ||||
-rw-r--r-- | src/Exceptions/RoadRunnerException.php | 1 | ||||
-rw-r--r-- | src/HttpClient.php | 3 | ||||
-rw-r--r-- | src/PSR7Client.php | 3 | ||||
-rw-r--r-- | src/Worker.php | 3 | ||||
-rwxr-xr-x | src/bin/roadrunner | 207 |
9 files changed, 215 insertions, 14 deletions
diff --git a/src/Diactoros/ServerRequestFactory.php b/src/Diactoros/ServerRequestFactory.php index 4d427121..cb534577 100644 --- a/src/Diactoros/ServerRequestFactory.php +++ b/src/Diactoros/ServerRequestFactory.php @@ -1,11 +1,10 @@ <?php -declare(strict_types=1); - /** * High-performance PHP process supervisor and load balancer written in Go * * @author Wolfy-J */ +declare(strict_types=1); namespace Spiral\RoadRunner\Diactoros; diff --git a/src/Diactoros/StreamFactory.php b/src/Diactoros/StreamFactory.php index 6004ef11..1c51d911 100644 --- a/src/Diactoros/StreamFactory.php +++ b/src/Diactoros/StreamFactory.php @@ -1,11 +1,10 @@ <?php -declare(strict_types=1); - /** * High-performance PHP process supervisor and load balancer written in Go * * @author Wolfy-J */ +declare(strict_types=1); namespace Spiral\RoadRunner\Diactoros; diff --git a/src/Diactoros/UploadedFileFactory.php b/src/Diactoros/UploadedFileFactory.php index 1543a826..7de9a30f 100644 --- a/src/Diactoros/UploadedFileFactory.php +++ b/src/Diactoros/UploadedFileFactory.php @@ -1,11 +1,10 @@ <?php -declare(strict_types=1); - /** * High-performance PHP process supervisor and load balancer written in Go * * @author Wolfy-J */ +declare(strict_types=1); namespace Spiral\RoadRunner\Diactoros; diff --git a/src/Exception/RoadRunnerException.php b/src/Exception/RoadRunnerException.php index 7c5c5929..348cb106 100644 --- a/src/Exception/RoadRunnerException.php +++ b/src/Exception/RoadRunnerException.php @@ -1,11 +1,10 @@ <?php -declare(strict_types=1); - /** * High-performance PHP process supervisor and load balancer written in Go * * @author Wolfy-J */ +declare(strict_types=1); namespace Spiral\RoadRunner\Exception; diff --git a/src/Exceptions/RoadRunnerException.php b/src/Exceptions/RoadRunnerException.php index 1a5da18c..803e4e31 100644 --- a/src/Exceptions/RoadRunnerException.php +++ b/src/Exceptions/RoadRunnerException.php @@ -5,6 +5,7 @@ * @license MIT * @author Anton Titov (Wolfy-J) */ +declare(strict_types=1); namespace Spiral\RoadRunner\Exceptions; diff --git a/src/HttpClient.php b/src/HttpClient.php index e469dd30..f31a9b50 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -1,11 +1,10 @@ <?php -declare(strict_types=1); - /** * High-performance PHP process supervisor and load balancer written in Go * * @author Alex Bond */ +declare(strict_types=1); namespace Spiral\RoadRunner; diff --git a/src/PSR7Client.php b/src/PSR7Client.php index 8229b7d5..5b9425d6 100644 --- a/src/PSR7Client.php +++ b/src/PSR7Client.php @@ -1,11 +1,10 @@ <?php -declare(strict_types=1); - /** * High-performance PHP process supervisor and load balancer written in Go * * @author Wolfy-J */ +declare(strict_types=1); namespace Spiral\RoadRunner; diff --git a/src/Worker.php b/src/Worker.php index da80e461..b67ebd3b 100644 --- a/src/Worker.php +++ b/src/Worker.php @@ -1,11 +1,10 @@ <?php -declare(strict_types=1); - /** * High-performance PHP process supervisor and load balancer written in Go * * @author Wolfy-J */ +declare(strict_types=1); namespace Spiral\RoadRunner; diff --git a/src/bin/roadrunner b/src/bin/roadrunner new file mode 100755 index 00000000..cf4317d7 --- /dev/null +++ b/src/bin/roadrunner @@ -0,0 +1,207 @@ +#!/usr/bin/env php +<?php +/** + * RoadRunner + * High-performance PHP process supervisor and load balancer written in Go + * + * This file responsive for cli commands + */ +declare(strict_types=1); + +foreach ([ + __DIR__ . '/../../../../autoload.php', + __DIR__ . '/../../vendor/autoload.php', + __DIR__ . '/vendor/autoload.php' + ] as $file) { + if (file_exists($file)) { + define('RR_COMPOSER_INSTALL', $file); + + break; + } +} + +unset($file); + +if (!defined('RR_COMPOSER_INSTALL')) { + fwrite( + STDERR, + 'You need to set up the project dependencies using Composer:' . PHP_EOL . PHP_EOL . + ' composer install' . PHP_EOL . PHP_EOL . + 'You can learn all about Composer on https://getcomposer.org/.' . PHP_EOL + ); + + die(1); +} + +if (!class_exists('ZipArchive')) { + fwrite(STDERR, 'Extension `php-zip` is required.' . PHP_EOL); + die(1); +} + +if (!function_exists('curl_init')) { + fwrite(STDERR, 'Extension `php-curl` is required.' . PHP_EOL); + die(1); +} + +require RR_COMPOSER_INSTALL; + +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Helper\ProgressBar; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\ConfirmationQuestion; + +class RoadRunnerCLIHelper +{ + /** + * Returns version of RoadRunner based on build.sh file + * + * @return string Version of RoadRunner + * @throws Exception + */ + public static function getVersion(): string + { + $file = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'build.sh'; + $fileResource = fopen($file, 'r') or die(1); + while (!feof($fileResource)) { + $line = fgets($fileResource, 4096); + $matches = []; + if (preg_match("/^RR_VERSION=(.*)/", $line, $matches)) { + return $matches[1]; + } + } + fclose($fileResource); + throw new Exception("Can't find version of RoadRunner"); + } + + /** + * Returns OS Type for filename + * + * @return string OS Type + */ + public static function getOSType(): string + { + switch (PHP_OS) { + case 'Darwin': + return 'darwin'; + case 'Linux': + return 'linux'; + case 'FreeBSD': + return 'freebsd'; + case 'WIN32': + case 'WINNT': + case 'Windows': + return 'windows'; + default: + return 'linux'; + } + } + + /** + * Returns generated URL to zip file on GitHub with binary file + * + * @return string URL + * @throws Exception + */ + public static function getBinaryDownloadUrl() + { + return 'https://github.com/spiral/roadrunner/releases/download/v' . static::getVersion() . '/roadrunner-' . static::getVersion() . '-' . static::getOSType() . '-amd64.zip'; + } +} + +(new Application('RoadRunner', RoadRunnerCLIHelper::getVersion())) + ->register('update-binaries') + ->setDescription("Install or update RoadRunner binaries in specified folder (current folder by default)") + ->addOption('location', 'l', InputArgument::OPTIONAL, 'destination folder', '.') + ->setCode(function (InputInterface $input, OutputInterface $output) { + $output->writeln('<info>Updating binary file of RoadRunner</info>'); + + $finalFile = $input->getOption('location') . DIRECTORY_SEPARATOR . 'rr'; + + if (is_file($finalFile)) { + $output->writeln('<error>RoadRunner binary file already exists!</error>'); + $helper = $this->getHelper('question'); + $question = new ConfirmationQuestion('Do you want overwrite it? [Y/n]'); + + if (!$helper->ask($input, $output, $question)) { + return; + } + } + + $output->writeln('<info>Downloading RoadRunner archive for ' . RoadRunnerCLIHelper::getOSType() . '</info>'); + + $progressBar = new ProgressBar($output); + $progressBar->setFormat('verbose'); + $progressBar->start(); + + $zipFileName = tempnam('.', "rr_zip"); + $zipFile = fopen($zipFileName, "w+"); + $curlResource = curl_init(); + curl_setopt($curlResource, CURLOPT_URL, RoadRunnerCLIHelper::getBinaryDownloadUrl()); + curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curlResource, CURLOPT_BINARYTRANSFER, true); + curl_setopt($curlResource, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($curlResource, CURLOPT_FILE, $zipFile); + curl_setopt($curlResource, CURLOPT_PROGRESSFUNCTION, + function ($resource, $download_size, $downloaded, $upload_size, $uploaded) use ($progressBar) { + if ($download_size == 0) { + return; + } + $progressBar->setFormat('[%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% ' . intval($download_size / 1024) . 'KB'); + $progressBar->setMaxSteps($download_size); + $progressBar->setProgress($downloaded); + }); + curl_setopt($curlResource, CURLOPT_NOPROGRESS, false); // needed to make progress function work + curl_setopt($curlResource, CURLOPT_HEADER, 0); + curl_exec($curlResource); + curl_close($curlResource); + fclose($zipFile); + + $progressBar->finish(); + $output->writeln(""); + + $output->writeln('<info>Unpacking ' . basename(RoadRunnerCLIHelper::getBinaryDownloadUrl()) . '</info>'); + + $zipArchive = new ZipArchive(); + $zipArchive->open($zipFileName); + $fileStreamFromZip = $zipArchive->getStream('roadrunner-' . RoadRunnerCLIHelper::getVersion() . '-' . RoadRunnerCLIHelper::getOSType() . '-amd64/rr'); + $finalFileResource = fopen($finalFile, 'w'); + + if (!$fileStreamFromZip) { + throw new Exception('Unable to extract the file.'); + } + + while (!feof($fileStreamFromZip)) { + fwrite($finalFileResource, fread($fileStreamFromZip, 8192)); + } + + fclose($fileStreamFromZip); + fclose($finalFileResource); + $zipArchive->extractTo('.', []); + $zipArchive->close(); + unlink($zipFileName); + + chmod($finalFile, 0755); + $output->writeln('<info>Binary file updated!</info>'); + }) + ->getApplication() + ->register("init-config") + ->setDescription("Inits default .rr.yaml config in specified folder (current folder by default)") + ->addOption('location', 'l', InputArgument::OPTIONAL, 'destination folder', '.') + ->setCode(function (InputInterface $input, OutputInterface $output) { + if (is_file($input->getOption('location') . DIRECTORY_SEPARATOR . '.rr.yaml')) { + $output->writeln('<error>Config file already exists!</error>'); + $helper = $this->getHelper('question'); + $question = new ConfirmationQuestion('Do you want overwrite it? [Y/n]'); + + if (!$helper->ask($input, $output, $question)) { + return; + } + } + copy(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '.rr.yaml', + $input->getOption('location') . DIRECTORY_SEPARATOR . '.rr.yaml'); + $output->writeln('<info>Config file created!</info>'); + }) + ->getApplication() + ->run(); |