Failed Conditions
Pull Request — master (#321)
by Anton
23:35 queued 08:33
created

application/library/Application/CliBootstrap.php (11 issues)

1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Application;
12
13
use Application\Users\Table;
14
use Bluz\Application\Application;
0 ignored issues
show
The type Bluz\Application\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Bluz\Controller\Controller;
0 ignored issues
show
The type Bluz\Controller\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Bluz\Proxy\Auth;
0 ignored issues
show
The type Bluz\Proxy\Auth was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Bluz\Proxy\Logger;
0 ignored issues
show
The type Bluz\Proxy\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Bluz\Proxy\Request;
0 ignored issues
show
The type Bluz\Proxy\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Bluz\Proxy\Response;
0 ignored issues
show
The type Bluz\Proxy\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Bluz\Proxy\Router;
0 ignored issues
show
The type Bluz\Proxy\Router was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Bluz\Request\RequestFactory;
0 ignored issues
show
The type Bluz\Request\RequestFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Symfony\Component\Console\Style\SymfonyStyle;
0 ignored issues
show
The type Symfony\Component\Console\Style\SymfonyStyle was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
/**
27
 * Bootstrap for CLI
28
 *
29
 * @category Application
30
 * @package  Bootstrap
31
 */
32
class CliBootstrap extends Application
33
{
34
    /**
35
     * Layout flag
36
     *
37
     * @var boolean
38
     */
39
    protected $layoutFlag = false;
40
41
    /**
42
     * @var InputInterface
43
     */
44
    protected $input;
45
46
    /**
47
     * @var OutputInterface
48
     */
49
    protected $output;
50
51
    /**
52
     * @param InputInterface $input
53
     */
54
    public function setInput(InputInterface $input): void
55
    {
56
        $this->input = $input;
57
    }
58
59
    /**
60
     * @return InputInterface
61
     */
62
    public function getInput()
63
    {
64
        return $this->input;
65
    }
66
67
    /**
68
     * @param OutputInterface $output
69
     */
70
    public function setOutput(OutputInterface $output): void
71
    {
72
        $this->output = $output;
73
    }
74
75
    /**
76
     * @return OutputInterface
77
     */
78
    public function getOutput()
79
    {
80
        return $this->output;
81
    }
82
83
    /**
84
     * get CLI Request
85
     *
86
     * @return void
87
     * @throws \InvalidArgumentException
88
     */
89
    public function initRequest(): void
90
    {
91
        $uri = $this->getInput()->getArgument('uri');
92
93
        $parsedQuery = parse_url($uri, PHP_URL_QUERY);
94
        if (false !== $parsedQuery && null !== $parsedQuery) {
95
            parse_str($parsedQuery, $query);
96
        } else {
97
            $query = [];
98
        }
99
100
        $request = RequestFactory::fromGlobals(['REQUEST_URI' => $uri, 'REQUEST_METHOD' => 'CLI'], $query);
101
102
        Request::setInstance($request);
103
    }
104
105
    /**
106
     * Pre process
107
     *
108
     * @return void
109
     */
110
    protected function preProcess(): void
111
    {
112
        Router::process();
113
        Response::setType('CLI');
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     *
119
     * @param Controller $controller
120
     *
121
     * @return void
122
     * @throws \Bluz\Db\Exception\DbException
123
     */
124
    protected function preDispatch($controller): void
125
    {
126
        // auth as CLI user
127
        if ($cliUser = Table::findRowWhere(['login' => 'system'])) {
128
            Auth::setIdentity($cliUser);
129
        }
130
131
        parent::preDispatch($controller);
132
    }
133
134
    /**
135
     * Render, is send Response
136
     *
137
     * @return void
138
     */
139
    public function render(): void
140
    {
141
        $io = new SymfonyStyle($this->getInput(), $this->getOutput());
142
        $io->title('Bluz CLI');
143
144
        if ($params = Request::getParams()) {
145
            foreach ($params as $key => $value) {
146
                $key = \is_int($key) ? "<comment>$key</comment>" : "<info>$key</info>";
147
                $io->writeln("$key: $value");
148
            }
149
        }
150
151
        $io->writeln('');
152
        $io->writeln('========');
153
        $io->writeln('');
154
        $io->writeln(json_encode(Response::getBody()));
155
        $io->writeln('');
156
    }
157
158
    /**
159
     * Finish it
160
     *
161
     * @return void
162
     */
163
    public function end(): void
164
    {
165
        if ($errors = Logger::get('error')) {
166
            $this->sendErrors($errors);
167
        }
168
        // return code 1 for invalid behaviour of application
169
//        if ($exception = $this->getException()) {
170
//            echo $exception->getMessage();
171
//            exit(1);
172
//        }
173
        exit;
174
    }
175
176
    /**
177
     * Send errors to logger
178
     *
179
     * @param array $errors
180
     *
181
     * @return void
182
     */
183
    protected function sendErrors($errors): void
184
    {
185
        foreach ($errors as $message) {
186
            errorLog(new \ErrorException($message, 0, E_USER_ERROR));
187
        }
188
    }
189
}
190