|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpDoc\Dev; |
| 6 | + |
| 7 | +use PhpDoc\Dev\Command\BuildCommand; |
| 8 | +use PhpDoc\Dev\Command\ConfigureCommand; |
| 9 | +use PhpDoc\Dev\Command\HelpCommand; |
| 10 | +use PhpDoc\Dev\Command\LintCommand; |
| 11 | +use PhpDoc\Dev\Command\PullCommand; |
| 12 | +use PhpDoc\Dev\Command\RenderCommand; |
| 13 | +use PhpDoc\Dev\Command\ServeCommand; |
| 14 | +use PhpDoc\Dev\Command\ShellCommand; |
| 15 | +use PhpDoc\Dev\Environment\DockerEnvironment; |
| 16 | +use PhpDoc\Dev\Environment\LocalEnvironment; |
| 17 | + |
| 18 | +final class Application |
| 19 | +{ |
| 20 | + public function __construct(private readonly string $basedir) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @param list<string> $args Command line arguments, without argv[0]. |
| 26 | + */ |
| 27 | + public function run(array $args): int |
| 28 | + { |
| 29 | + $options = new Options(); |
| 30 | + $command = $this->parse($args, $options); |
| 31 | + |
| 32 | + if ($command === null) { |
| 33 | + return 1; |
| 34 | + } |
| 35 | + |
| 36 | + if ($command === 'help') { |
| 37 | + return (new HelpCommand())->execute($options); |
| 38 | + } |
| 39 | + |
| 40 | + $subcommand = null; |
| 41 | + |
| 42 | + if ($command === 'docker') { |
| 43 | + $subcommand = array_shift($options->args); |
| 44 | + |
| 45 | + if (!in_array($subcommand, ['build', 'shell'], true)) { |
| 46 | + fwrite(STDERR, "Usage: php dev.php docker <build|shell> (see: php dev.php help)\n"); |
| 47 | + return 1; |
| 48 | + } |
| 49 | + |
| 50 | + $options->docker = true; |
| 51 | + } |
| 52 | + |
| 53 | + if ($command === 'render') { |
| 54 | + $subcommand = array_shift($options->args); |
| 55 | + |
| 56 | + if (!in_array($subcommand, ['xhtml', 'php'], true)) { |
| 57 | + fwrite(STDERR, "Usage: php dev.php render <xhtml|php> (see: php dev.php help)\n"); |
| 58 | + return 1; |
| 59 | + } |
| 60 | + |
| 61 | + $options->format = $subcommand; |
| 62 | + } |
| 63 | + |
| 64 | + if ($command === 'cs') { |
| 65 | + $subcommand = array_shift($options->args); |
| 66 | + |
| 67 | + if (!in_array($subcommand, ['lint', 'fix'], true)) { |
| 68 | + fwrite(STDERR, "Usage: php dev.php cs <lint|fix> (see: php dev.php help)\n"); |
| 69 | + return 1; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + $runner = new ProcessRunner(); |
| 74 | + $dockerAvailable = $runner->runQuiet(['docker', 'version', '--format', '{{.Server.Version}}']) === 0; |
| 75 | + |
| 76 | + if ($options->docker === true && !$dockerAvailable) { |
| 77 | + fwrite(STDERR, "error: Docker requested but the docker command is not available.\n"); |
| 78 | + return 1; |
| 79 | + } |
| 80 | + |
| 81 | + $workspace = new Workspace($this->basedir, $runner, $options->assumeYes); |
| 82 | + $environment = ($options->docker ?? $dockerAvailable) |
| 83 | + ? new DockerEnvironment($workspace, $runner) |
| 84 | + : new LocalEnvironment($workspace, $runner); |
| 85 | + |
| 86 | + $configure = new ConfigureCommand($workspace, $environment); |
| 87 | + |
| 88 | + switch ($command) { |
| 89 | + case 'pull': |
| 90 | + return (new PullCommand($workspace, $environment))->execute($options); |
| 91 | + case 'configure': |
| 92 | + return $configure->execute($options); |
| 93 | + case 'render': |
| 94 | + return (new RenderCommand($workspace, $environment, $configure))->execute($options); |
| 95 | + case 'cs': |
| 96 | + return (new LintCommand($workspace, $environment, $configure, fix: $subcommand === 'fix')) |
| 97 | + ->execute($options); |
| 98 | + case 'serve': |
| 99 | + return (new ServeCommand($workspace, $environment))->execute($options); |
| 100 | + case 'docker': |
| 101 | + return $subcommand === 'build' |
| 102 | + ? (new BuildCommand($environment))->execute($options) |
| 103 | + : (new ShellCommand($environment))->execute($options); |
| 104 | + } |
| 105 | + |
| 106 | + fwrite(STDERR, "Unknown command: $command (see: php dev.php help)\n"); |
| 107 | + |
| 108 | + return 1; |
| 109 | + } |
| 110 | + |
| 111 | + /** @param list<string> $args */ |
| 112 | + private function parse(array $args, Options $options): ?string |
| 113 | + { |
| 114 | + $command = null; |
| 115 | + |
| 116 | + foreach ($args as $arg) { |
| 117 | + if (preg_match('/^--lang=(.+)$/', $arg, $m)) { |
| 118 | + $options->lang = $m[1]; |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + if (preg_match('/^--port=(\d+)$/', $arg, $m)) { |
| 123 | + $options->port = (int) $m[1]; |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + if ($arg === '--docker') { |
| 128 | + $options->docker = true; |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + if ($arg === '--no-docker') { |
| 133 | + $options->docker = false; |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + if ($arg === '--yes' || $arg === '-y') { |
| 138 | + $options->assumeYes = true; |
| 139 | + continue; |
| 140 | + } |
| 141 | + |
| 142 | + if ($command === null) { |
| 143 | + if ($arg[0] !== '-') { |
| 144 | + $command = $arg; |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + if ($arg === '-h' || $arg === '--help') { |
| 149 | + $command = 'help'; |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + $fileName = $_SERVER['SCRIPT_FILENAME']; |
| 154 | + fwrite(STDERR, "Unknown option: $arg (see: php $fileName help)\n"); |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + $options->args[] = $arg; |
| 159 | + } |
| 160 | + |
| 161 | + return $command ?? 'help'; |
| 162 | + } |
| 163 | +} |
0 commit comments