1 | <?php |
||
14 | class Application extends Container |
||
15 | { |
||
16 | use Toolkit; |
||
17 | |||
18 | private $reflection; |
||
19 | |||
20 | public function __construct(string $root) |
||
48 | |||
49 | 56 | public function dispatch(string $job, array $params = [], string $service = null) |
|
50 | { |
||
51 | 56 | if ($service !== null) { |
|
52 | if ($this->get(Service::class)->getName() == $service) { |
||
53 | $service = null; |
||
54 | } |
||
55 | } |
||
56 | |||
57 | 56 | return $this->get(Cache::class) |
|
58 | ->wrap([$job, $params, $service], function() use ($job, $params, $service) { |
||
59 | 56 | $span = $this->get(Tracer::class)->createSpan($job); |
|
60 | 56 | foreach ($params as $k => $v) { |
|
61 | 10 | if (is_numeric($v) || is_string($v)) { |
|
62 | 7 | $span->setAttribute($k, $v); |
|
63 | } |
||
64 | } |
||
65 | |||
66 | 56 | $serviceName = $this->get(Service::class)->getName(); |
|
67 | 56 | $dispatcher = $this->get(Dispatcher::class); |
|
68 | 56 | $runner = $this->get(Runner::class); |
|
69 | |||
70 | 56 | if ($service === null && $runner->hasJob($job)) { |
|
71 | 56 | $result = $runner->dispatch($job, $params); |
|
72 | 1 | } elseif ($service === null && explode('.', $job)[0] == $serviceName) { |
|
73 | $result = $runner->dispatch($job, $params); |
||
74 | } else { |
||
75 | 1 | $result = $dispatcher->dispatch($job, $params, $service); |
|
76 | } |
||
77 | |||
78 | 56 | $span->end(); |
|
79 | 56 | return $result; |
|
80 | 56 | }); |
|
81 | } |
||
82 | |||
83 | 56 | public function get($alias, bool $new = false) : object |
|
84 | { |
||
85 | 56 | if (!$this->hasInstance($alias)) { |
|
86 | 56 | $instance = null; |
|
87 | 56 | if (is_subclass_of($alias, Procedure::class)) { |
|
|
|||
88 | 7 | $instance = $this->get(Mapper::class) |
|
89 | 7 | ->getPlugin(ProcedurePlugin::class) |
|
90 | 7 | ->get($alias); |
|
91 | } |
||
92 | 56 | if (is_subclass_of($alias, Repository::class)) { |
|
93 | 1 | $spaceName = $this->get(Mapper::class) |
|
94 | 1 | ->getPlugin(Annotation::class) |
|
95 | 1 | ->getRepositorySpaceName($alias); |
|
96 | |||
97 | 1 | if ($spaceName) { |
|
98 | 1 | $instance = $this->get(Mapper::class)->getRepository($spaceName); |
|
99 | } |
||
100 | } |
||
101 | 56 | if ($instance) { |
|
102 | $this->share($alias, function () use ($instance) { |
||
103 | 8 | return $instance; |
|
104 | 8 | }); |
|
105 | } |
||
106 | } |
||
107 | 56 | return parent::get($alias, $new); |
|
108 | } |
||
109 | |||
110 | 56 | public function hasInstance($id) : bool |
|
123 | |||
124 | 56 | public function has($id) : bool |
|
140 | |||
141 | 56 | public function call($callback) |
|
145 | } |
||
146 |