From cacf0e9112781b9b51c9cdc702534dfff679e0c5 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 29 Sep 2022 11:45:06 -0400 Subject: [PATCH] [feature] switch to symfony cli for managing server --- .github/workflows/ci.yml | 10 ++++++- composer.json | 4 +-- src/BaseCommand.php | 63 +++++++++++++++++++++++++++------------- src/InitCommand.php | 32 +++++--------------- src/RunCommand.php | 13 ++++----- src/StatusCommand.php | 11 +++---- 6 files changed, 72 insertions(+), 61 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 877e1ce..d85640e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -74,6 +74,13 @@ jobs: name: phpmyadmin.phar path: . + - name: Setup PHP + uses: shivammathur/setup-php@2.7.0 + with: + php-version: 7.4 + coverage: none + tools: symfony + - name: Test init/run run: | mv phpmyadmin.phar phpmyadmin @@ -82,7 +89,7 @@ jobs: ./phpmyadmin ./phpmyadmin status sleep 2 - curl -I http://127.0.0.1:8888 + curl -I http://127.0.0.1:8000 - name: Test self-update/rollback if: github.event_name != 'release' @@ -100,6 +107,7 @@ jobs: - cs-check - composer-validate - test-phar + - sca if: github.event_name == 'release' steps: - uses: actions/download-artifact@v1 diff --git a/composer.json b/composer.json index fe4da51..9d72534 100644 --- a/composer.json +++ b/composer.json @@ -17,9 +17,9 @@ "bin": ["bin/phpmyadmin"], "require": { "php": ">=7.4", - "symfony/web-server-bundle": "^4.4", "symfony/filesystem": "^5.4", - "symfony/console": "^5.4" + "symfony/console": "^5.4", + "symfony/process": "^5.4" }, "require-dev": { "laravel-zero/phar-updater": "^1.1.1", diff --git a/src/BaseCommand.php b/src/BaseCommand.php index 5fcd87d..93bab9a 100644 --- a/src/BaseCommand.php +++ b/src/BaseCommand.php @@ -2,50 +2,73 @@ namespace Zenstruck\PMA\Server\Command; -use Symfony\Bundle\WebServerBundle\WebServerConfig; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Process\ExecutableFinder; +use Symfony\Component\Process\Process; /** * @author Kevin Bond */ abstract class BaseCommand extends Command { - protected function webserverConfig(): WebServerConfig + final protected function isRunning(OutputInterface $output): bool { - if (!\file_exists($address = $this->addressFile()) || !\file_exists($router = $this->routerFile())) { - throw new \RuntimeException('phpMyAdmin not initialized. Run "phpmyadmin init".'); - } + $process = $this->executeSymfonyCommand(['server:status'], $output); - return new WebServerConfig($this->documentRoot(), 'dev', \file_get_contents($address), $router); // @phpstan-ignore-line + return !\str_contains($process->getOutput(), 'Not Running'); } - protected function folderName(): string + /** + * @param string[] $parameters + */ + final protected function executeProcess(array $parameters, string $workingDir, OutputInterface $output): Process { - return 'phpmyadmin-server'; - } + $process = (new Process($parameters, $workingDir)) + ->setTimeout(null) + ; - protected function organizationDir(): string - { - return "{$_SERVER['HOME']}/.config/zenstruck"; + $process->run(function($type, $buffer) use ($output) { + if ($output->isVerbose()) { + $output->writeln($buffer); + } + }); + + if (!$process->isSuccessful()) { + throw new \RuntimeException($process->getErrorOutput()); + } + + return $process; } - protected function documentRoot(): string + /** + * @param string[] $parameters + */ + final protected function executeSymfonyCommand(array $parameters, OutputInterface $output): Process { - return "{$this->organizationDir()}/{$this->folderName()}"; + if (!\is_dir($this->documentRoot())) { + throw new \RuntimeException('phpMyAdmin not initialized. Run "phpmyadmin init".'); + } + + if (!$symfony = (new ExecutableFinder())->find('symfony')) { + throw new \RuntimeException('Symfony CLI is required: https://symfony.com/download#step-1-install-symfony-cli'); + } + + return $this->executeProcess(\array_merge([$symfony], $parameters), $this->documentRoot(), $output); } - protected function addressFile(): string + final protected function folderName(): string { - return "{$this->documentRoot()}/.address"; + return 'phpmyadmin-server'; } - protected function routerFile(): string + final protected function organizationDir(): string { - return "{$this->documentRoot()}/.router.php"; + return "{$_SERVER['HOME']}/.config/zenstruck"; } - protected function pidFile(): string + final protected function documentRoot(): string { - return $this->documentRoot().'/.pid'; + return "{$this->organizationDir()}/{$this->folderName()}"; } } diff --git a/src/InitCommand.php b/src/InitCommand.php index 2f8dae1..d9da40c 100644 --- a/src/InitCommand.php +++ b/src/InitCommand.php @@ -8,7 +8,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Process\Process; +use Symfony\Component\Process\ExecutableFinder; /** * @author Kevin Bond @@ -21,7 +21,6 @@ protected function configure(): void ->setName('init') ->setDescription('Initialize phpMyAdmin') ->addArgument('version', InputArgument::OPTIONAL, 'phpMyAdmin version (blank for latest)') - ->addOption('address', null, InputOption::VALUE_REQUIRED, 'URL Address', '127.0.0.1:8888') ->addOption('host', null, InputOption::VALUE_REQUIRED, 'MySQL Host', 'localhost') ->addOption('port', null, InputOption::VALUE_REQUIRED, 'MySQL Port', '3306') ->addOption('user', null, InputOption::VALUE_REQUIRED, 'MySQL User', 'root') @@ -34,7 +33,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io = new SymfonyStyle($input, $output); $version = $input->getArgument('version'); - $address = $input->getOption('address'); $mysqlHost = $input->getOption('host'); $mysqlPort = $input->getOption('port'); $mysqlUser = $input->getOption('user'); @@ -55,20 +53,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int $fs->mkdir($orgDir); } - $process = (new Process(['composer', 'create-project', $package, $this->folderName()], $orgDir)) - ->setTimeout(null) - ; - - $process->run(function($type, $buffer) use ($io) { - if ($io->isVerbose()) { - $io->writeln($buffer); - } - }); - - if (!$process->isSuccessful()) { - throw new \RuntimeException($process->getErrorOutput()); + if (!$composer = (new ExecutableFinder())->find('composer')) { + throw new \RuntimeException('Composer is required: https://symfony.com/download#step-1-install-symfony-cli'); } + $this->executeProcess([$composer, 'create-project', $package, $this->folderName()], $orgDir, $io); + $io->comment('Generating config.inc.php'); $config = \file_get_contents(__DIR__.'/../resources/config.inc.template'); \file_put_contents($this->documentRoot().'/config.inc.php', \strtr($config, [ // @phpstan-ignore-line @@ -78,14 +68,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int '%%mysql_password%%' => $mysqlPass, ])); - $io->comment('Writing address file...'); - \file_put_contents($this->addressFile(), $address); - - $io->comment('Writing router file...'); - \file_put_contents( - $this->routerFile(), - \file_get_contents(__DIR__.'/../vendor/symfony/web-server-bundle/Resources/router.php') - ); + $io->comment('Attaching Proxy Domain...'); + $this->executeSymfonyCommand(['proxy:domain:attach', 'phpmyadmin'], $io); $io->success('Initialized phpMyAdmin, run "phpmyadmin" to start web server.'); @@ -99,7 +83,7 @@ protected function interact(InputInterface $input, OutputInterface $output): voi $input->setArgument('version', $io->ask($definition->getArgument('version')->getDescription())); - foreach (['address', 'host', 'port', 'user'] as $option) { + foreach (['host', 'port', 'user'] as $option) { $input->setOption($option, $io->ask( $definition->getOption($option)->getDescription(), $definition->getOption($option)->getDefault() // @phpstan-ignore-line diff --git a/src/RunCommand.php b/src/RunCommand.php index 78e0dcb..933a98c 100644 --- a/src/RunCommand.php +++ b/src/RunCommand.php @@ -2,7 +2,6 @@ namespace Zenstruck\PMA\Server\Command; -use Symfony\Bundle\WebServerBundle\WebServer; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -23,20 +22,20 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); - $config = $this->webserverConfig(); - $server = new WebServer(); - if ($server->isRunning($this->pidFile())) { - $server->stop($this->pidFile()); + if ($this->isRunning($io)) { + $this->executeSymfonyCommand(['server:stop'], $io); $io->success('Stopped the phpMyAdmin web server.'); return 0; } - if (WebServer::STARTED === $server->start($config, $this->pidFile())) { - $io->success(\sprintf('phpMyAdmin web server listening on http://%s', $config->getAddress())); + if (!$io->isQuiet()) { + $io->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); } + $this->executeSymfonyCommand(['server:start', '-d'], $io); + return 0; } } diff --git a/src/StatusCommand.php b/src/StatusCommand.php index d4d333b..47c320a 100644 --- a/src/StatusCommand.php +++ b/src/StatusCommand.php @@ -2,7 +2,6 @@ namespace Zenstruck\PMA\Server\Command; -use Symfony\Bundle\WebServerBundle\WebServer; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -23,17 +22,15 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); - $server = new WebServer(); - $config = $this->webserverConfig(); - if ($server->isRunning($this->pidFile())) { - $io->success(\sprintf('phpMyAdmin web server is running (http://%s).', $config->getAddress())); + if (!$io->isQuiet()) { + $io->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); + } + if ($this->isRunning($io)) { return 0; } - $io->warning('phpMyAdmin web server is NOT running.'); - return 1; } }