1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Basis;
|
4
|
|
|
|
5
|
|
|
use LogicException;
|
6
|
|
|
use League\Container\Container;
|
7
|
|
|
use ReflectionClass;
|
8
|
|
|
use ReflectionProperty;
|
9
|
|
|
|
10
|
|
|
class Runner
|
11
|
|
|
{
|
12
|
|
|
private $app;
|
13
|
|
|
|
14
|
4 |
|
function __construct(Application $app)
|
|
|
|
|
15
|
|
|
{
|
16
|
4 |
|
$this->app = $app;
|
17
|
4 |
|
}
|
18
|
|
|
|
19
|
1 |
|
function listJobs()
|
|
|
|
|
20
|
|
|
{
|
21
|
1 |
|
$classes = array_merge(
|
22
|
1 |
|
$this->app->get(Filesystem::class)->listClasses('Jobs'),
|
23
|
1 |
|
$this->app->get(Framework::class)->listClasses('Jobs')
|
24
|
|
|
);
|
25
|
|
|
|
26
|
1 |
|
$jobs = [];
|
27
|
1 |
|
foreach($classes as $class) {
|
28
|
1 |
|
list($name, $group) = array_map('strtolower', array_reverse(explode("\\", $class)));
|
29
|
1 |
|
$nick = "$group.$name";
|
30
|
|
|
|
31
|
1 |
|
if(!array_key_exists($nick, $jobs)) {
|
32
|
1 |
|
$jobs[$nick] = $class;
|
33
|
|
|
}
|
34
|
|
|
}
|
35
|
|
|
|
36
|
1 |
|
return $jobs;
|
37
|
|
|
}
|
38
|
|
|
|
39
|
4 |
|
function dispatch($nick, $arguments = [])
|
40
|
|
|
{
|
41
|
4 |
|
if(!strstr($nick, '.')) {
|
42
|
|
|
throw new LogicException("Incorrect nick - $nick");
|
43
|
|
|
}
|
44
|
|
|
|
45
|
4 |
|
list($group, $name) = array_map('ucfirst', explode('.', $nick));
|
46
|
|
|
|
47
|
4 |
|
$className = "Jobs\\$group\\$name";
|
48
|
|
|
|
49
|
4 |
|
$class = $this->app->get(Filesystem::class)->completeClassName($className);
|
50
|
|
|
|
51
|
4 |
|
if(!class_exists($class)) {
|
52
|
3 |
|
$frameworkClass = $this->app->get(Framework::class)->completeClassName($className);
|
53
|
3 |
|
if(class_exists($frameworkClass)) {
|
54
|
3 |
|
$class = $frameworkClass;
|
55
|
|
|
}
|
56
|
|
|
}
|
57
|
|
|
|
58
|
4 |
|
if(!class_exists($class)) {
|
59
|
|
|
throw new LogicException("No job $nick");
|
60
|
|
|
}
|
61
|
|
|
|
62
|
4 |
|
$instance = $this->app->get($class);
|
63
|
4 |
|
if(array_key_exists(0, $arguments)) {
|
64
|
2 |
|
$arguments = $this->castArguments($class, $arguments);
|
65
|
|
|
}
|
66
|
|
|
|
67
|
4 |
|
foreach($arguments as $k => $v) {
|
68
|
3 |
|
$instance->$k = $v;
|
69
|
|
|
}
|
70
|
|
|
|
71
|
4 |
|
$container = $this->app->get(Container::class);
|
72
|
4 |
|
return $container->call([$instance, 'run']);
|
73
|
|
|
}
|
74
|
|
|
|
75
|
2 |
|
private function castArguments($class, $arguments)
|
76
|
|
|
{
|
77
|
2 |
|
$reflection = new ReflectionClass($class);
|
78
|
2 |
|
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
|
79
|
2 |
|
if(count($properties) == 1) {
|
80
|
|
|
return [
|
81
|
2 |
|
$properties[0]->getName() => count($arguments) == 1
|
82
|
2 |
|
? $arguments[0]
|
83
|
2 |
|
: implode(' ', $arguments)
|
84
|
|
|
];
|
85
|
|
|
}
|
86
|
|
|
return $arguments;
|
87
|
|
|
}
|
88
|
|
|
}
|
89
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.