Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
CliApp
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 init
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 handleException
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Dynart\Micro;
4
5/**
6 * Handles CLI commands
7 * @package Dynart\Micro
8 */
9class CliApp extends AbstractApp {
10
11    const EVENT_COMMAND_MATCHED = 'cliapp:command_matched';
12
13    protected CliCommandsInterface $commands;
14
15    protected CliOutputInterface $output;
16
17    public function __construct(array $configPaths) {
18        parent::__construct($configPaths);
19        Micro::add(CliCommandsInterface::class, CliCommands::class);
20        Micro::add(CliOutputInterface::class, CliOutput::class);
21    }
22
23    public function init(): void {
24        $this->commands = Micro::get(CliCommandsInterface::class);
25    }
26
27    public function process(): void {
28        list($callable, $params) = $this->commands->matchCurrent();
29        if ($callable) {
30            $callable = Micro::getCallable($callable);
31            Micro::get(EventServiceInterface::class)->emit(self::EVENT_COMMAND_MATCHED, [$callable, $params]);
32            if (empty($params)) {
33                $content = call_user_func($callable);
34            } else {
35                $content = call_user_func_array($callable, [$params]);
36            }
37            $this->finish($content);
38        } else {
39            error_log("Unknown command: ".$this->commands->current());
40            $this->finish(1);
41        }
42    }
43
44    protected function handleException(\Exception $e): void {
45        parent::handleException($e);
46        $this->finish(1);
47    }
48}