|
1
|
|
|
<?php |
|
2
|
|
|
namespace rtens\domin\reflection; |
|
3
|
|
|
|
|
4
|
|
|
use rtens\domin\ActionRegistry; |
|
5
|
|
|
use rtens\domin\reflection\types\TypeFactory; |
|
6
|
|
|
|
|
7
|
|
|
class MethodActionGenerator { |
|
8
|
|
|
|
|
9
|
|
|
const SEPERATOR = '~'; |
|
10
|
|
|
|
|
11
|
|
|
private $actions; |
|
12
|
|
|
private $types; |
|
13
|
|
|
private $parser; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(ActionRegistry $actions, TypeFactory $types, CommentParser $parser) { |
|
16
|
|
|
$this->actions = $actions; |
|
17
|
|
|
$this->types = $types; |
|
18
|
|
|
$this->parser = $parser; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function fromObject($object) { |
|
22
|
|
|
$class = new \ReflectionClass($object); |
|
23
|
|
|
|
|
24
|
|
|
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
25
|
|
|
if ($method->isStatic() || substr($method->name, 0, 1) == '_') { |
|
26
|
|
|
continue; |
|
27
|
|
|
} |
|
28
|
|
|
$this->actions->add(self::getId($method), |
|
29
|
|
|
new GenericMethodAction($object, $method->name, $this->types, $this->parser)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function configure($object, $method, callable $callback) { |
|
36
|
|
|
$callback($this->get($object, $method)); |
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function get($object, $method) { |
|
41
|
|
|
$method = new \ReflectionMethod(get_class($object), $method); |
|
42
|
|
|
return $this->actions->getAction(self::getId($method)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private static function getId(\ReflectionMethod $method) { |
|
46
|
|
|
return $method->getDeclaringClass()->getShortName() . self::SEPERATOR . $method->name; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function actionId($class, $method) { |
|
50
|
|
|
return self::getId(new \ReflectionMethod($class, $method)); |
|
51
|
|
|
} |
|
52
|
|
|
} |