Test Failed
Pull Request — master (#9)
by Maximo
03:26
created

library/Bootstrap/Cli.php (1 issue)

1
<?php
0 ignored issues
show
End of line character is invalid; expected "\n" but found "\r\n"
Loading history...
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Bootstrap;
6
7
use function Gewaer\Core\appPath;
8
use Phalcon\Cli\Console;
9
use Phalcon\Di\FactoryDefault\Cli as PhCli;
10
use Throwable;
11
use Gewaer\Exception\ServerErrorHttpException;
12
use Gewaer\Constants\Flags;
13
14
/**
15
 * Class Cli
16
 *
17
 * @package Gewaer\Bootstrap
18
 *
19
 * @property Console $application
20
 */
21
class Cli extends AbstractBootstrap
22
{
23
    private $argv;
24
25
    /**
26
     * Run the application
27
     *
28
     * @return mixed
29
     */
30
    public function run()
31
    {
32
        try {
33
            $config = $this->container->getConfig();
34
35
            return $this->application->handle($this->options);
36
        } catch (Throwable $e) {
37
            //only log when server error production is seerver error or dev
38
            if ($e instanceof ServerErrorHttpException || strtolower($config->app->env) != Flags::PRODUCTION) {
39
                $this->container->getLog()->error($e->getTraceAsString());
40
            }
41
42
            //we need to see it on the console -_-
43
            echo $e->getMessage();
44
        }
45
    }
46
47
    /**
48
     * @return mixed
49
     */
50
    public function setup()
51
    {
52
        $this->container = new PhCli();
53
        $this->providers = require appPath('cli/config/providers.php');
54
55
        $this->processArguments();
56
57
        parent::setup();
58
    }
59
60
    /**
61
     * Setup the application object in the container
62
     *
63
     * @return void
64
     */
65
    protected function setupApplication()
66
    {
67
        $this->application = new Console($this->container);
68
        $this->container->setShared('application', $this->application);
69
    }
70
71
    /**
72
     * Pass php argv
73
     *
74
     * @param array $argv
75
     * @return void
76
     */
77
    public function setArgv(array $argv): void
78
    {
79
        $this->argv = $argv;
80
    }
81
82
    /**
83
     * Parses arguments from the command line
84
     */
85
    private function processArguments()
86
    {
87
        $arguments = [];
88
        foreach ($this->argv as $k => $arg) {
89
            if ($k == 1) {
90
                $arguments['task'] = $arg;
91
            } elseif ($k == 2) {
92
                $arguments['action'] = $arg;
93
            } elseif ($k >= 3) {
94
                $arguments['params'][] = $arg;
95
            }
96
        }
97
98
        $this->options = $arguments;
99
    }
100
}
101