]> BookStack Code Mirror - system-cli/blob - run.php
Bumped version
[system-cli] / run.php
1 #!/usr/bin/env php
2 <?php
3 declare(strict_types=1);
4
5 if (php_sapi_name() !== 'cli') {
6     exit;
7 }
8
9 require __DIR__ . '/vendor/autoload.php';
10
11 use Cli\Commands\CommandError;
12 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
13 use Symfony\Component\Console\Output\ConsoleOutput;
14
15 // Get the app with commands loaded
16 $app = require __DIR__ . '/src/app.php';
17
18 // Configure output formatting
19 $output =  new ConsoleOutput();
20 $formatter = $output->getFormatter();
21 $formatter->setStyle('warn', new OutputFormatterStyle('yellow'));
22 $formatter->setStyle('info', new OutputFormatterStyle('cyan'));
23 $formatter->setStyle('success', new OutputFormatterStyle('green'));
24 $formatter->setStyle('error', new OutputFormatterStyle('red'));
25
26 // Run the command and handle errors
27 try {
28     $output->writeln("<warn>WARNING: This CLI is in alpha testing.</warn>");
29     $output->writeln("<warn>There's a high chance of issues, and the CLI API is subject to change.</warn>");
30     $output->writeln("");
31
32     $app->run(null, $output);
33 } catch (CommandError $error) {
34     $output = (new ConsoleOutput())->getErrorOutput();
35     $output->getFormatter()->setStyle('error', new OutputFormatterStyle('red'));
36     $output->writeln('<error>' . $error->getMessage() . '</error>');
37     exit(1);
38 } catch (Exception $error) {
39     $output = (new ConsoleOutput())->getErrorOutput();
40     $output->getFormatter()->setStyle('error', new OutputFormatterStyle('red'));
41     $output->writeln("<error>\nAn error occurred when attempting to run a command:\n</error>");
42     $output->writeln($error->getMessage());
43     exit(1);
44 }