1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhalconExt\Cli; |
4
|
|
|
|
5
|
|
|
use Ahc\Cli\Application; |
|
|
|
|
6
|
|
|
use Ahc\Cli\Helper\OutputHelper; |
|
|
|
|
7
|
|
|
use Ahc\Cli\Input\Command; |
|
|
|
|
8
|
|
|
use Ahc\Cli\IO\Interactor; |
|
|
|
|
9
|
|
|
use Phalcon\Cli\Task; |
|
|
|
|
10
|
|
|
use Phalcon\DiInterface; |
|
|
|
|
11
|
|
|
use PhalconExt\Cli\Task\ScheduleTask; |
12
|
|
|
use PhalconExt\Di\ProvidesDi; |
13
|
|
|
|
14
|
|
|
trait Extension |
15
|
|
|
{ |
16
|
|
|
use ProvidesDi; |
17
|
|
|
use MiddlewareTrait; |
18
|
|
|
|
19
|
|
|
/** @var array Tasks namespaces */ |
20
|
|
|
protected $namespaces = []; |
21
|
|
|
|
22
|
|
|
/** @var array Tasks provided by package already */ |
23
|
|
|
protected $factoryTasks = []; |
24
|
|
|
|
25
|
|
|
/** @var array Scheduled taskIds mapped to schedule time */ |
26
|
|
|
protected $scheduled = []; |
27
|
|
|
|
28
|
|
|
/** @var array Raw argv sent to handle() [OR read from $_SERVER] */ |
29
|
|
|
protected $argv = []; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
protected $lastTask; |
33
|
|
|
|
34
|
|
|
public function __construct(DiInterface $di, string $name, string $version = '0.0.1') |
35
|
|
|
{ |
36
|
|
|
parent::__construct($di); |
37
|
|
|
|
38
|
|
|
$di->setShared('console', $this); |
39
|
|
|
$di->setShared('interactor', Interactor::class); |
40
|
|
|
|
41
|
|
|
$this->initialize($name, $version); |
42
|
|
|
$this->bindEvents($this); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function initialize(string $name, string $version) |
46
|
|
|
{ |
47
|
|
|
$this->app = new Application($name, $version, function () { |
|
|
|
|
48
|
|
|
return false; |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
$this->factoryTasks = [ |
52
|
|
|
'schedule' => ScheduleTask::class, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$this->initTasks(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function app() |
59
|
|
|
{ |
60
|
|
|
return $this->app; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function argv(): array |
64
|
|
|
{ |
65
|
|
|
return $this->argv; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function handle(array $argv = null) |
69
|
|
|
{ |
70
|
|
|
$this->argv = $argv ?? $_SERVER['argv']; |
71
|
|
|
$parameters = $this->getTaskParameters($this->argv); |
72
|
|
|
|
73
|
|
|
return $this->doHandle($parameters); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function doHandle(array $parameters) |
77
|
|
|
{ |
78
|
|
|
if (isset($this->namespaces[$parameters['task']])) { |
79
|
|
|
$parameters['task'] = $this->namespaces[$parameters['task']]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return parent::handle($parameters); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function addTask(string $task, string $descr = '', bool $allowUnknown = false): Command |
86
|
|
|
{ |
87
|
|
|
$this->lastTask = $taskId = \str_ireplace(['task', 'action'], '', $task); |
88
|
|
|
|
89
|
|
|
if (\strpos($task, ':main')) { |
90
|
|
|
$alias = \str_replace(':main', '', $taskId); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (\strpos($task, ':') === false) { |
94
|
|
|
$alias = $taskId . ':main'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->app->command($taskId, $descr, $allowUnknown, $alias ?? ''); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function schedule(string $cronExpr, string $taskId = ''): self |
101
|
|
|
{ |
102
|
|
|
$taskId = $taskId ?: $this->lastTask; |
103
|
|
|
|
104
|
|
|
$this->scheduled[$taskId] = $cronExpr; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function scheduled(): array |
110
|
|
|
{ |
111
|
|
|
return $this->scheduled; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function getTaskParameters(array $argv) |
115
|
|
|
{ |
116
|
|
|
$taskAction = []; |
117
|
|
|
\array_shift($argv); |
118
|
|
|
|
119
|
|
|
foreach ($argv as $i => $value) { |
120
|
|
|
if ($value[0] === '-' || isset($taskAction[1])) { |
121
|
|
|
break; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$taskAction = \array_merge($taskAction, \explode(':', $value, 2)); |
125
|
|
|
unset($argv[$i]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Respect phalcon default. |
129
|
|
|
$taskAction += ['main', 'main']; |
130
|
|
|
|
131
|
|
|
return [ |
132
|
|
|
'task' => $taskAction[0], |
133
|
|
|
'action' => $taskAction[1], |
134
|
|
|
// For BC, still send params to handle() |
135
|
|
|
'params' => \array_values($argv), |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function initTasks() |
140
|
|
|
{ |
141
|
|
|
foreach ($this->getTaskClasses() as $name => $class) { |
142
|
|
|
if (!$this->di()->has($class)) { |
143
|
|
|
// Force load! |
144
|
|
|
$this->di($class); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->namespaces[$name] = \preg_replace('#Task$#', '', $class); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected function getTaskClasses(): array |
154
|
|
|
{ |
155
|
|
|
if ($tasks = $this->di('config')->path('console.tasks')) { |
156
|
|
|
$tasks = $tasks->toArray(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this->factoryTasks + ($tasks ?: []); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths