Skip to content

Commit

Permalink
[feature] switch to symfony cli for managing server
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Sep 29, 2022
1 parent ea5e9c8 commit cacf0e9
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 61 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ jobs:
name: phpmyadmin.phar
path: .

- name: Setup PHP
uses: shivammathur/[email protected]
with:
php-version: 7.4
coverage: none
tools: symfony

- name: Test init/run
run: |
mv phpmyadmin.phar phpmyadmin
Expand All @@ -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'
Expand All @@ -100,6 +107,7 @@ jobs:
- cs-check
- composer-validate
- test-phar
- sca
if: github.event_name == 'release'
steps:
- uses: actions/download-artifact@v1
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
63 changes: 43 additions & 20 deletions src/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
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()}";
}
}
32 changes: 8 additions & 24 deletions src/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand All @@ -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')
Expand All @@ -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');
Expand All @@ -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
Expand All @@ -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.');

Expand All @@ -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
Expand Down
13 changes: 6 additions & 7 deletions src/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
11 changes: 4 additions & 7 deletions src/StatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}

0 comments on commit cacf0e9

Please sign in to comment.