Skip to content

Commit

Permalink
[feature] add status command (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored Sep 29, 2022
1 parent bb940d7 commit e49b924
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
uses: ramsey/composer-install@v1

- name: Download Box
run: wget https://github.com/box-project/box/releases/latest/download/box.phar
run: wget https://github.com/box-project/box/releases/download/3.16.0/box.phar

- name: Update Version
if: github.event_name == 'release'
Expand Down Expand Up @@ -100,6 +100,7 @@ jobs:
chmod +x phpmyadmin
./phpmyadmin init --no-interaction --password=1234
./phpmyadmin
./phpmyadmin status
sleep 2
curl -I http://127.0.0.1:8888
Expand Down
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ in the background.
### PHAR

The preferred method of installation is to use the PHAR which can be downloaded from the most
recent [Github Release](https://github.com/zenstruck/phpmyadmin-server/releases). This method
recent [GitHub Release](https://github.com/zenstruck/phpmyadmin-server/releases). This method
ensures you will not have any dependency conflict issue.

```bash
Expand All @@ -22,26 +22,47 @@ mv phpmyadmin ~/bin # assumes ~/bin is in your PATH

### Composer

```
```bash
composer global require zenstruck/phpmyadmin-server
```

**Note**: Ensure you have `~/.composer/vendor/bin` in your `PATH` to give access to the `phpmyadmin`
**Note**: Ensure you have `~/.config/composer/vendor/bin` in your `PATH` to give access to the `phpmyadmin`
command.

## Initialization

```
```bash
phpmyadmin init
```

This command will ask you questions about your setup and download the latest version of phpMyAdmin
to `~/.phpmyadmin`.

> *Note*: If you ever need to change your configuration, run `phpmyadmin init` again.
## Start/Stop Server

```
```bash
phpmyadmin
```

If you ever need to change your configuration, run `phpmyadmin init` again.
## Check Status

```bash
phpmyadmin status
```

This command exits with `0` if running and `1` if not. You can add the following in your `.bash_profile`
to ensure it's always running:

```bash
phpmyadmin status || phpmyadmin
```

## Self-Update

If installed via [PHAR](#phar), use the `self-update` command:

```bash
phpmyadmin self-update
```
2 changes: 2 additions & 0 deletions bin/phpmyadmin
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use Symfony\Component\Console\Application;
use Zenstruck\PMA\Server\Command\InitCommand;
use Zenstruck\PMA\Server\Command\RunCommand;
use Zenstruck\PMA\Server\Command\SelfUpdateCommand;
use Zenstruck\PMA\Server\Command\StatusCommand;

set_time_limit(0);

Expand All @@ -21,6 +22,7 @@ $application = new Application('phpmyadmin-server', '@dev');
$application->addCommands([
$command = new RunCommand(),
new InitCommand(),
new StatusCommand()
]);

if (\Phar::running()) {
Expand Down
15 changes: 15 additions & 0 deletions src/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

namespace Zenstruck\PMA\Server\Command;

use Symfony\Bundle\WebServerBundle\WebServerConfig;
use Symfony\Component\Console\Command\Command;

/**
* @author Kevin Bond <[email protected]>
*/
abstract class BaseCommand extends Command
{
protected function webserverConfig(): WebServerConfig
{
if (!\file_exists($address = $this->addressFile()) || !\file_exists($router = $this->routerFile())) {
throw new \RuntimeException('phpMyAdmin not initialized. Run "phpmyadmin init".');
}

return new WebServerConfig($this->documentRoot(), 'dev', \file_get_contents($address), $router);
}

protected function folderName(): string
{
return 'phpmyadmin-server';
Expand All @@ -33,4 +43,9 @@ protected function routerFile(): string
{
return "{$this->documentRoot()}/.router.php";
}

protected function pidFile(): string
{
return $this->documentRoot().'/.pid';
}
}
19 changes: 1 addition & 18 deletions src/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Zenstruck\PMA\Server\Command;

use Symfony\Bundle\WebServerBundle\WebServer;
use Symfony\Bundle\WebServerBundle\WebServerConfig;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand All @@ -24,11 +23,7 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

if (!\file_exists($address = $this->addressFile()) || !\file_exists($router = $this->routerFile())) {
throw new \RuntimeException('phpMyAdmin not initialized. Run "phpmyadmin init".');
}

$config = $this->webserverConfig();
$server = new WebServer();

if ($server->isRunning($this->pidFile())) {
Expand All @@ -38,22 +33,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

$config = new WebServerConfig(
$this->documentRoot(),
'dev',
\file_get_contents($address),
$router
);

if (WebServer::STARTED === $server->start($config, $this->pidFile())) {
$io->success(\sprintf('phpMyAdmin web server listening on http://%s', $config->getAddress()));
}

return 0;
}

protected function pidFile(): string
{
return $this->documentRoot().'/.pid';
}
}
39 changes: 39 additions & 0 deletions src/StatusCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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;

/**
* @author Kevin Bond <[email protected]>
*/
final class StatusCommand extends BaseCommand
{
protected function configure(): void
{
$this
->setName('status')
->setDescription('Check the status of the phpMyAdmin server')
;
}

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()));

return 0;
}

$io->warning('phpMyAdmin web server is NOT running.');

return 1;
}
}

0 comments on commit e49b924

Please sign in to comment.