From 5b0784fadd69b91dc92d381910aa55da7d74e513 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:01:22 +0300 Subject: [PATCH 01/17] tmp --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b67a47742..593b58bad 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,5 +62,5 @@ jobs: - name: Run tests with codeception run: | - php -S 127.0.0.1:8080 -t public > ./runtime/yii.log 2>&1 & + yii serve 127.0.0.1 > ./runtime/yii.log 2>&1 & vendor/bin/codecept run From 37c0744bc39acbc233e3cf64ab6b22ecc35940ab Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:09:19 +0300 Subject: [PATCH 02/17] tmp --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 593b58bad..62c09e2ff 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,5 +62,5 @@ jobs: - name: Run tests with codeception run: | - yii serve 127.0.0.1 > ./runtime/yii.log 2>&1 & + yii serve 127.0.0.1:8080 > ./runtime/yii.log 2>&1 & vendor/bin/codecept run From 402e898c9f6cf64a3cdf464f17ae7df21bc477af Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:10:33 +0300 Subject: [PATCH 03/17] tmp --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 62c09e2ff..973c96105 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,5 +62,5 @@ jobs: - name: Run tests with codeception run: | - yii serve 127.0.0.1:8080 > ./runtime/yii.log 2>&1 & + ./yii serve 127.0.0.1:8080 > ./runtime/yii.log 2>&1 & vendor/bin/codecept run From 3ede1c713227d37719952a266ed111cc2766be85 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:12:54 +0300 Subject: [PATCH 04/17] tmp --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 973c96105..de2272932 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,5 +62,5 @@ jobs: - name: Run tests with codeception run: | - ./yii serve 127.0.0.1:8080 > ./runtime/yii.log 2>&1 & + ./yii serve 127.0.0.1:8080 & vendor/bin/codecept run From cf6148e527e929fce4da23c9e43c9195191b9d18 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:15:26 +0300 Subject: [PATCH 05/17] tmp --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index de2272932..c08c7a33c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,5 +62,5 @@ jobs: - name: Run tests with codeception run: | - ./yii serve 127.0.0.1:8080 & + php -S 127.0.0.1:8080 -t public & vendor/bin/codecept run From d4b3563408569430f3a626ebb3e5110d066429cc Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:24:59 +0300 Subject: [PATCH 06/17] tmp --- .github/workflows/build.yml | 2 +- config/params.php | 2 + src/Command/Serve2.php | 132 ++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/Command/Serve2.php diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c08c7a33c..59123e1af 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,5 +62,5 @@ jobs: - name: Run tests with codeception run: | - php -S 127.0.0.1:8080 -t public & + ./yii serve2 127.0.0.1:8080 & vendor/bin/codecept run diff --git a/config/params.php b/config/params.php index 90de310c2..7f42ed9da 100644 --- a/config/params.php +++ b/config/params.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use App\Command\Serve2; use App\Middleware\LocaleMiddleware; use App\ViewInjection\CommonViewInjection; use App\ViewInjection\LayoutViewInjection; @@ -93,6 +94,7 @@ 'autoExit' => false, 'commands' => [ 'serve' => Serve::class, + 'serve2' => Serve2::class, 'user/create' => App\User\Console\CreateCommand::class, 'user/assignRole' => App\User\Console\AssignRoleCommand::class, 'fixture/add' => App\Command\Fixture\AddCommand::class, diff --git a/src/Command/Serve2.php b/src/Command/Serve2.php new file mode 100644 index 000000000..41f4d89ae --- /dev/null +++ b/src/Command/Serve2.php @@ -0,0 +1,132 @@ +setHelp('In order to access server from remote machines use 0.0.0.0:8000. That is especially useful when running server in a virtual machine.') + ->addArgument('address', InputArgument::OPTIONAL, 'Host to serve at', 'localhost') + ->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Port to serve at', self::DEFAULT_PORT) + ->addOption('docroot', 't', InputOption::VALUE_OPTIONAL, 'Document root to serve from', self::DEFAULT_DOCROOT) + ->addOption('router', 'r', InputOption::VALUE_OPTIONAL, 'Path to router script', self::DEFAULT_ROUTER) + ->addOption('env', 'e', InputOption::VALUE_OPTIONAL, 'It is only used for testing.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + /** @var string $address */ + $address = $input->getArgument('address'); + + /** @var string $router */ + $router = $input->getOption('router'); + + /** @var string $port */ + $port = $input->getOption('port'); + + /** @var string $docroot */ + $docroot = $input->getOption('docroot'); + + if ($router === self::DEFAULT_ROUTER && !file_exists(self::DEFAULT_ROUTER)) { + $io->warning('Default router "' . self::DEFAULT_ROUTER . '" does not exist. Serving without router. URLs with dots may fail.'); + $router = null; + } + + /** @var string $env */ + $env = $input->getOption('env'); + + $documentRoot = getcwd() . '/' . $docroot; // TODO: can we do it better? + + if (strpos($address, ':') === false) { + $address .= ':' . $port; + } + + if (!is_dir($documentRoot)) { + $io->error("Document root \"$documentRoot\" does not exist."); + return self::EXIT_CODE_NO_DOCUMENT_ROOT; + } + + if ($this->isAddressTaken($address)) { + $io->error("http://$address is taken by another process."); + return self::EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS; + } + + if ($router !== null && !file_exists($router)) { + $io->error("Routing file \"$router\" does not exist."); + return self::EXIT_CODE_NO_ROUTING_FILE; + } + + $output->writeLn("Server started on http://$address/"); + $output->writeLn("Document root is \"$documentRoot\""); + + if ($router) { + $output->writeLn("Routing file is \"$router\""); + } + + $output->writeLn('Quit the server with CTRL-C or COMMAND-C.'); + + if ($env === 'test') { + return ExitCode::OK; + } + + $output->writeln('"' . PHP_BINARY . '"' . " -S $address -t \"$documentRoot\" $router"); + + $x = passthru('"' . PHP_BINARY . '"' . " -S $address -t \"$documentRoot\" $router"); + + $output->writeln($x===null ? 'null' : ($x?'true':'false')); + + return ExitCode::OK; + } + + /** + * @param string $address The server address. + * + * @return bool If address is already in use. + */ + private function isAddressTaken(string $address): bool + { + [$hostname, $port] = explode(':', $address); + $fp = @fsockopen($hostname, (int)$port, $errno, $errstr, 3); + + if ($fp === false) { + return false; + } + + fclose($fp); + return true; + } +} From 0cdff992e0600c5d7fcaecef98ee4251a4a829b8 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 09:25:16 +0000 Subject: [PATCH 07/17] Apply fixes from StyleCI --- src/Command/Serve2.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/Serve2.php b/src/Command/Serve2.php index 41f4d89ae..60f0dced4 100644 --- a/src/Command/Serve2.php +++ b/src/Command/Serve2.php @@ -107,7 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $x = passthru('"' . PHP_BINARY . '"' . " -S $address -t \"$documentRoot\" $router"); - $output->writeln($x===null ? 'null' : ($x?'true':'false')); + $output->writeln($x===null ? 'null' : ($x ? 'true' : 'false')); return ExitCode::OK; } From 036cac1f6c1e0dab8ab39068d0f2ab7494d8dd63 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:27:23 +0300 Subject: [PATCH 08/17] tmp --- .github/workflows/build.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 59123e1af..464653c6e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,5 +62,7 @@ jobs: - name: Run tests with codeception run: | + "php" -v & + "php8.0" -v & ./yii serve2 127.0.0.1:8080 & vendor/bin/codecept run From 25c047fd899f961be9fc79b6b5795039d459537a Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:29:48 +0300 Subject: [PATCH 09/17] tmp --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 464653c6e..a9fa0ed9d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -64,5 +64,5 @@ jobs: run: | "php" -v & "php8.0" -v & - ./yii serve2 127.0.0.1:8080 & + ./yii serve 127.0.0.1:8080 & vendor/bin/codecept run From 037bce639b34ab42381496a6cc2c6db37294f23f Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:32:26 +0300 Subject: [PATCH 10/17] tmp --- .github/workflows/build.yml | 2 +- src/Command/Serve2.php | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a9fa0ed9d..464653c6e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -64,5 +64,5 @@ jobs: run: | "php" -v & "php8.0" -v & - ./yii serve 127.0.0.1:8080 & + ./yii serve2 127.0.0.1:8080 & vendor/bin/codecept run diff --git a/src/Command/Serve2.php b/src/Command/Serve2.php index 60f0dced4..14a83ef0d 100644 --- a/src/Command/Serve2.php +++ b/src/Command/Serve2.php @@ -105,9 +105,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln('"' . PHP_BINARY . '"' . " -S $address -t \"$documentRoot\" $router"); - $x = passthru('"' . PHP_BINARY . '"' . " -S $address -t \"$documentRoot\" $router"); - - $output->writeln($x===null ? 'null' : ($x ? 'true' : 'false')); + passthru('"' . PHP_BINARY . '"' . " -S $address -t \"$documentRoot\" $router"); return ExitCode::OK; } From 0d3343cdf67bb7a7f638704b2a78f8bbae23677c Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:34:36 +0300 Subject: [PATCH 11/17] tmp --- src/Command/Serve2.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Command/Serve2.php b/src/Command/Serve2.php index 14a83ef0d..c060a2bac 100644 --- a/src/Command/Serve2.php +++ b/src/Command/Serve2.php @@ -99,6 +99,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeLn('Quit the server with CTRL-C or COMMAND-C.'); + $output->writeln(empty($env) ? 'EMPTY' : $env); + if ($env === 'test') { return ExitCode::OK; } From b937c1d3e06b65cb560745d6f9380052859fa3f1 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:44:34 +0300 Subject: [PATCH 12/17] tmp --- .github/workflows/build.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 464653c6e..2f32c10ed 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,7 +62,6 @@ jobs: - name: Run tests with codeception run: | - "php" -v & - "php8.0" -v & - ./yii serve2 127.0.0.1:8080 & + yii server 127.0.0.1:8080 > ./runtime/yii.log 2>&1 & + sleep 5s & vendor/bin/codecept run From 2442ec3af9b93fb96428375b65a75c7e50db2b3f Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:48:03 +0300 Subject: [PATCH 13/17] tmp --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2f32c10ed..08ff2166d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -63,5 +63,5 @@ jobs: - name: Run tests with codeception run: | yii server 127.0.0.1:8080 > ./runtime/yii.log 2>&1 & - sleep 5s & + sleep 30s & vendor/bin/codecept run From b738fd845c2641263da5bd8cdb84a11eabdab874 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:49:53 +0300 Subject: [PATCH 14/17] tmp --- .github/workflows/build.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 08ff2166d..20512273e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -63,5 +63,4 @@ jobs: - name: Run tests with codeception run: | yii server 127.0.0.1:8080 > ./runtime/yii.log 2>&1 & - sleep 30s & - vendor/bin/codecept run + sleep 10s; vendor/bin/codecept run From 0d92f0c6d0cf2a6f1e2dd793d8a2410b25766260 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:51:07 +0300 Subject: [PATCH 15/17] tmp --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 20512273e..55b60fd2c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,5 +62,5 @@ jobs: - name: Run tests with codeception run: | - yii server 127.0.0.1:8080 > ./runtime/yii.log 2>&1 & - sleep 10s; vendor/bin/codecept run + yii server 127.0.0.1:8080 & + sleep 30s; vendor/bin/codecept run From 5daad63adc538894b1bbb5402972faaca920768f Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:52:17 +0300 Subject: [PATCH 16/17] tmp --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 55b60fd2c..a54e259f2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,5 +62,5 @@ jobs: - name: Run tests with codeception run: | - yii server 127.0.0.1:8080 & - sleep 30s; vendor/bin/codecept run + ./yii server 127.0.0.1:8080 & + sleep 10s; vendor/bin/codecept run From 8c39db949233b1dcb7b27e87fdcc96fa185c3554 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 13 Nov 2021 12:53:18 +0300 Subject: [PATCH 17/17] tmp --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a54e259f2..5ea2e3ea5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,5 +62,5 @@ jobs: - name: Run tests with codeception run: | - ./yii server 127.0.0.1:8080 & + ./yii serve 127.0.0.1:8080 & sleep 10s; vendor/bin/codecept run