1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
namespace gossi\codegen\generator; |
18
|
|
|
|
19
|
|
|
use gossi\codegen\model\GenerateableInterface; |
20
|
|
|
use gossi\codegen\visitor\GeneratorNavigator; |
21
|
|
|
use gossi\codegen\visitor\GeneratorVisitor; |
22
|
|
|
use gossi\codegen\visitor\GeneratorVisitorInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The default generator strategy. |
26
|
|
|
* |
27
|
|
|
* This strategy allows to change the order in which methods, properties and |
28
|
|
|
* constants are sorted. |
29
|
|
|
* |
30
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class GeneratorStrategy { |
33
|
|
|
|
34
|
|
|
private $navigator; |
35
|
|
|
|
36
|
|
|
private $visitor; |
37
|
|
|
|
38
|
18 |
|
public function __construct(GeneratorVisitorInterface $visitor = null) { |
39
|
18 |
|
$this->navigator = new GeneratorNavigator(); |
40
|
18 |
|
$this->visitor = $visitor ?: new GeneratorVisitor(); |
41
|
18 |
|
} |
42
|
|
|
|
43
|
2 |
|
public function setConstantSortFunc(\Closure $func = null) { |
44
|
2 |
|
$this->navigator->setConstantSortFunc($func); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
2 |
|
public function setMethodSortFunc(\Closure $func = null) { |
48
|
2 |
|
$this->navigator->setMethodSortFunc($func); |
49
|
2 |
|
} |
50
|
|
|
|
51
|
2 |
|
public function setPropertySortFunc(\Closure $func = null) { |
52
|
2 |
|
$this->navigator->setPropertySortFunc($func); |
53
|
2 |
|
} |
54
|
|
|
|
55
|
|
|
public function setUseStatementSortFunc(\Closure $func = null) { |
56
|
|
|
$this->navigator->setUseStatementSortFunc($func); |
57
|
|
|
} |
58
|
|
|
|
59
|
18 |
|
public function generate(GenerateableInterface $class) { |
|
|
|
|
60
|
18 |
|
$this->visitor->reset(); |
61
|
18 |
|
$this->navigator->accept($this->visitor, $class); |
62
|
|
|
|
63
|
18 |
|
return $this->visitor->getContent(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.