Completed
Push — master ( c9e021...a2d906 )
by Anton
15s
created

CliBootstrap::getInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Application;
13
14
use Application\Users\Table;
15
use Bluz\Application\Application;
16
use Bluz\Application\Exception\ApplicationException;
17
use Bluz\Proxy\Auth;
18
use Bluz\Proxy\Config;
19
use Bluz\Proxy\Logger;
20
use Bluz\Proxy\Request;
21
use Bluz\Proxy\Response;
22
use Bluz\Proxy\Router;
23
use Bluz\Request\RequestFactory;
24
use function GuzzleHttp\Psr7\parse_query;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Symfony\Component\Console\Style\SymfonyStyle;
28
29
/**
30
 * Bootstrap for CLI
31
 *
32
 * @category Application
33
 * @package  Bootstrap
34
 *
35
 * @author   Anton Shevchuk
36
 * @created  17.12.12 15:24
37
 */
38
class CliBootstrap extends Application
39
{
40
    /**
41
     * Layout flag
42
     * @var boolean
43
     */
44
    protected $layoutFlag = false;
45
46
    /**
47
     * @var InputInterface
48
     */
49
    protected $input;
50
51
    /**
52
     * @var OutputInterface
53
     */
54
    protected $output;
55
56
    /**
57
     * @param InputInterface $input
58
     */
59
    public function setInput(InputInterface $input)
60
    {
61
        $this->input = $input;
62
    }
63
64
    /**
65
     * @return InputInterface
66
     */
67
    public function getInput()
68
    {
69
        return $this->input;
70
    }
71
72
    /**
73
     * @param OutputInterface $output
74
     */
75
    public function setOutput(OutputInterface $output)
76
    {
77
        $this->output = $output;
78
    }
79
80
    /**
81
     * @return OutputInterface
82
     */
83
    public function getOutput()
84
    {
85
        return $this->output;
86
    }
87
88
    /**
89
     * get CLI Request
90
     * @return void
91
     * @throws ApplicationException
92
     */
93
    public function initRequest()
94
    {
95
        $uri = $this->getInput()->getArgument('uri');
96
97
        if (!$parsedQuery = parse_url($uri, PHP_URL_QUERY)) {
98
            $this->getOutput()->writeln('<error>ERROR: unvalid URI format</error>');
99
        }
100
101
        parse_str($parsedQuery, $query);
102
103
        $request = RequestFactory::fromGlobals(['REQUEST_URI' => $uri, 'REQUEST_METHOD' => 'CLI'], $query);
104
105
        Request::setInstance($request);
106
    }
107
108
    /**
109
     * initConfig
110
     *
111
     * @return void
112
     */
113
    public function initConfig()
114
    {
115
        $config = new \Bluz\Config\Config();
116
        $config->setPath(self::getInstance()->getPath());
117
        $config->setEnvironment(self::getInstance()->getEnvironment());
118
        $config->init();
119
120
        Config::setInstance($config);
121
122
        parent::initConfig();
123
    }
124
125
126
    /**
127
     * Pre process
128
     * @return void
129
     */
130
    protected function preProcess()
131
    {
132
        Router::process();
133
        Response::switchType('CLI');
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     *
139
     * @param string $module
140
     * @param string $controller
141
     * @param array $params
142
     * @return void
143
     */
144
    protected function preDispatch($module, $controller, $params = array())
145
    {
146
        // auth as CLI user
147
        $cliUser = Table::findRowWhere(['login' => 'system']);
148
        Auth::setIdentity($cliUser);
149
150
        parent::preDispatch($module, $controller, $params);
151
    }
152
153
    /**
154
     * Render, is send Response
155
     *
156
     * @return void
157
     */
158
    public function render()
159
    {
160
        $io = new SymfonyStyle($this->getInput(), $this->getOutput());
161
        $io->title('Bluz CLI');
162
163
        if ($params = Request::getParams()) {
164
            foreach ($params as $key => $value) {
165
                $io->writeln("<info>$key</info>: $value");
166
            }
167
        }
168
169
        $io->writeln('');
170
        $io->writeln('========');
171
        $io->writeln('');
172
173
        $data = Response::getBody()->getData()->toArray();
174
175
        foreach ($data as $key => $value) {
176
            $io->writeln("<info>$key</info>: $value");
177
        }
178
179
        $io->writeln('');
180
    }
181
182
    /**
183
     * @return void
184
     */
185 View Code Duplication
    public function end()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
    {
187
        if ($messages = Logger::get('error')) {
188
            foreach ($messages as $message) {
189
                errorLog(new \ErrorException($message, 0, E_USER_ERROR));
190
            }
191
        }
192
193
        // return code 1 for invalid behaviour of application
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
194
//        if ($exception = $this->getException()) {
195
//            echo $exception->getMessage();
196
//            exit(1);
197
//        }
198
        exit;
199
    }
200
}
201