|
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\Cli; |
|
18
|
|
|
use Bluz\Proxy\Auth; |
|
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
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Bootstrap for CLI |
|
27
|
|
|
* |
|
28
|
|
|
* @category Application |
|
29
|
|
|
* @package Bootstrap |
|
30
|
|
|
* |
|
31
|
|
|
* @author Anton Shevchuk |
|
32
|
|
|
* @created 17.12.12 15:24 |
|
33
|
|
|
*/ |
|
34
|
|
|
class CliBootstrap extends Application |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Layout flag |
|
38
|
|
|
* @var boolean |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $layoutFlag = false; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* get CLI Request |
|
44
|
|
|
* @return void |
|
45
|
|
|
* @throws ApplicationException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function initRequest() |
|
48
|
|
|
{ |
|
49
|
|
|
$arguments = getopt("u:", ["uri:"]); |
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
if (!array_key_exists('u', $arguments) && !array_key_exists('uri', $arguments)) { |
|
|
|
|
|
|
52
|
|
|
throw new ApplicationException('Attribute `--uri` is required'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$uri = $arguments['u'] ?? $arguments['uri']; |
|
56
|
|
|
|
|
57
|
|
|
$request = RequestFactory::fromGlobals(['REQUEST_URI' => $uri, 'REQUEST_METHOD' => 'CLI']); |
|
58
|
|
|
|
|
59
|
|
|
Request::setInstance($request); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Pre process |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function preProcess() |
|
67
|
|
|
{ |
|
68
|
|
|
Router::process(); |
|
69
|
|
|
Response::switchType('CLI'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $module |
|
76
|
|
|
* @param string $controller |
|
77
|
|
|
* @param array $params |
|
78
|
|
|
* @return void |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function preDispatch($module, $controller, $params = array()) |
|
81
|
|
|
{ |
|
82
|
|
|
// auth as CLI user |
|
83
|
|
|
$cliUser = Table::findRowWhere(['login' => 'system']); |
|
84
|
|
|
Auth::setIdentity($cliUser); |
|
85
|
|
|
|
|
86
|
|
|
parent::preDispatch($module, $controller, $params); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return void |
|
91
|
|
|
*/ |
|
92
|
|
View Code Duplication |
public function end() |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
if ($messages = Logger::get('error')) { |
|
95
|
|
|
foreach ($messages as $message) { |
|
96
|
|
|
errorLog(new \ErrorException($message, 0, E_USER_ERROR)); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// return code 1 for invalid behaviour of application |
|
|
|
|
|
|
101
|
|
|
// if ($exception = $this->getException()) { |
|
102
|
|
|
// echo $exception->getMessage(); |
|
103
|
|
|
// exit(1); |
|
104
|
|
|
// } |
|
105
|
|
|
exit; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
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.