|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of byrokrat\autogiro. |
|
4
|
|
|
* |
|
5
|
|
|
* byrokrat\autogiro is free software: you can redistribute it and/or |
|
6
|
|
|
* modify it under the terms of the GNU General Public License as published |
|
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* byrokrat\autogiro is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
* |
|
18
|
|
|
* Copyright 2016 Hannes Forsgård |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
declare(strict_types = 1); |
|
22
|
|
|
|
|
23
|
|
|
namespace byrokrat\autogiro\Visitor; |
|
24
|
|
|
|
|
25
|
|
|
use byrokrat\autogiro\Tree\Node; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Generic node visitor |
|
29
|
|
|
* |
|
30
|
|
|
* Will read the node type (eg. SomeNode) and dispatch a node specific method |
|
31
|
|
|
* if defined in visitor (eg. beforeSomeNode or afterSomeNode). By convention |
|
32
|
|
|
* such visitor methods should type hint the specific node type |
|
33
|
|
|
* (eg. beforeSomeNode(SomeNode $node)). |
|
34
|
|
|
*/ |
|
35
|
|
|
class Visitor |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* Generic method for visiting a node before its children |
|
39
|
|
|
*/ |
|
40
|
|
|
public function visitBefore(Node $node) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->dispatch('before' . $node->getType(), $node); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Generic method for visiting a node after its children |
|
47
|
|
|
*/ |
|
48
|
|
|
public function visitAfter(Node $node) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->dispatch('after' . $node->getType(), $node); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Dispatch to method if method exists |
|
55
|
|
|
*/ |
|
56
|
|
|
private function dispatch(string $method, Node $node) |
|
57
|
|
|
{ |
|
58
|
|
|
if (method_exists($this, $method)) { |
|
59
|
|
|
$this->$method($node); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|