|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Telegram\Bot\Answers; |
|
4
|
|
|
|
|
5
|
|
|
use Telegram\Bot\Api; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class AnswerBus |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class AnswerBus |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Api |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $telegram; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Handle calls to missing methods. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $method |
|
21
|
|
|
* @param array $parameters |
|
22
|
|
|
* |
|
23
|
|
|
* @return mixed |
|
24
|
|
|
* |
|
25
|
|
|
* @throws \BadMethodCallException |
|
26
|
|
|
*/ |
|
27
|
10 |
|
public function __call($method, $parameters) |
|
28
|
|
|
{ |
|
29
|
10 |
|
if (method_exists($this, $method)) { |
|
30
|
10 |
|
return call_user_func_array([$this, $method], $parameters); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
throw new \BadMethodCallException("Method [$method] does not exist."); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return Api |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getTelegram() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->telegram; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Use PHP Reflection and Laravel Container to instantiate the answer with type hinted dependencies. |
|
46
|
|
|
* |
|
47
|
|
|
* @param $answerClass |
|
48
|
|
|
* |
|
49
|
|
|
* @return object |
|
50
|
|
|
*/ |
|
51
|
2 |
|
protected function buildDependencyInjectedAnswer($answerClass) |
|
52
|
|
|
{ |
|
53
|
|
|
// check if the command has a constructor |
|
54
|
2 |
|
if (!method_exists($answerClass, '__construct')) { |
|
55
|
|
|
return new $answerClass(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// get constructor params |
|
59
|
2 |
|
$constructorReflector = new \ReflectionMethod($answerClass, '__construct'); |
|
60
|
2 |
|
$params = $constructorReflector->getParameters(); |
|
61
|
|
|
|
|
62
|
|
|
// if no params are needed proceed with normal instantiation |
|
63
|
2 |
|
if (empty($params)) { |
|
64
|
|
|
return new $answerClass(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// otherwise fetch each dependency out of the container |
|
68
|
2 |
|
$container = $this->telegram->getContainer(); |
|
69
|
2 |
|
$dependencies = []; |
|
70
|
2 |
|
foreach ($params as $param) { |
|
71
|
2 |
|
$dependencies[] = $container->make($param->getClass()->name); |
|
72
|
2 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
// and instantiate the object with dependencies through ReflectionClass |
|
75
|
2 |
|
$classReflector = new \ReflectionClass($answerClass); |
|
76
|
|
|
|
|
77
|
2 |
|
return $classReflector->newInstanceArgs($dependencies); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|