|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Thinktomorrow\Chief\Management\Assistants; |
|
5
|
|
|
|
|
6
|
|
|
use Thinktomorrow\Chief\Management\Exceptions\MissingAssistant; |
|
7
|
|
|
|
|
8
|
|
|
trait ManagesAssistants |
|
9
|
|
|
{ |
|
10
|
|
|
protected $assistants = []; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Check if this manager is assisted by a certain assistant |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $assistantKey |
|
16
|
|
|
* @return bool |
|
17
|
|
|
*/ |
|
18
|
|
|
public function isAssistedBy(string $assistantKey): bool |
|
19
|
|
|
{ |
|
20
|
|
|
return !!$this->getAssistantClass($assistantKey); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param bool $asInstances |
|
25
|
|
|
* @return array |
|
26
|
|
|
* @throws \Exception |
|
27
|
|
|
*/ |
|
28
|
|
|
public function assistants($asInstances = true): array |
|
29
|
|
|
{ |
|
30
|
|
|
$assistants = []; |
|
31
|
|
|
|
|
32
|
|
|
foreach ($this->assistants as $assistant) { |
|
33
|
|
|
$assistants[] = $asInstances ? $this->assistant($assistant) : $assistant; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $assistants; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function assistantsAsClassNames() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->assistants(false); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Instantiate the assistant |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $assistantKey |
|
48
|
|
|
* @return Assistant |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
*/ |
|
51
|
|
|
public function assistant(string $assistantKey): Assistant |
|
52
|
|
|
{ |
|
53
|
|
|
if (!$this->isAssistedBy($assistantKey)) { |
|
54
|
|
|
throw new MissingAssistant('No assistant [' . $assistantKey . '] registered on manager ' . static::class); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$instance = app($this->getAssistantClass($assistantKey)); |
|
58
|
|
|
$instance->manager($this); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
return $instance; |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get assistant class by key or assistant classname |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $assistantKey |
|
67
|
|
|
* @return string|null |
|
68
|
|
|
*/ |
|
69
|
|
|
private function getAssistantClass(string $assistantKey): ?string |
|
70
|
|
|
{ |
|
71
|
|
|
foreach ($this->assistants as $class) { |
|
72
|
|
|
if ($assistantKey == $class::key()) { |
|
73
|
|
|
return $class; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Alternatively, check if the passed argument is the assistant class name |
|
78
|
|
|
if (in_array($assistantKey, $this->assistants)) { |
|
79
|
|
|
return $assistantKey; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.