Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser;
|
Chris@0
|
4
|
Chris@0
|
5 use PhpParser\Builder;
|
Chris@0
|
6 use PhpParser\Node\Stmt\Use_;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * The following methods use reserved keywords, so their implementation is defined with an underscore and made available
|
Chris@0
|
10 * with the reserved name through __call() magic.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @method Builder\Namespace_ namespace(string $name) Creates a namespace builder.
|
Chris@0
|
13 * @method Builder\Class_ class(string $name) Creates a class builder.
|
Chris@0
|
14 * @method Builder\Interface_ interface(string $name) Creates an interface builder.
|
Chris@0
|
15 * @method Builder\Trait_ trait(string $name) Creates a trait builder.
|
Chris@0
|
16 * @method Builder\Function_ function(string $name) Creates a function builder.
|
Chris@0
|
17 * @method Builder\Use_ use(string $name) Creates a namespace/class use builder.
|
Chris@0
|
18 */
|
Chris@0
|
19 class BuilderFactory
|
Chris@0
|
20 {
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Creates a namespace builder.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @param null|string|Node\Name $name Name of the namespace
|
Chris@0
|
25 *
|
Chris@0
|
26 * @return Builder\Namespace_ The created namespace builder
|
Chris@0
|
27 */
|
Chris@0
|
28 protected function _namespace($name) {
|
Chris@0
|
29 return new Builder\Namespace_($name);
|
Chris@0
|
30 }
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Creates a class builder.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @param string $name Name of the class
|
Chris@0
|
36 *
|
Chris@0
|
37 * @return Builder\Class_ The created class builder
|
Chris@0
|
38 */
|
Chris@0
|
39 protected function _class($name) {
|
Chris@0
|
40 return new Builder\Class_($name);
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Creates an interface builder.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @param string $name Name of the interface
|
Chris@0
|
47 *
|
Chris@0
|
48 * @return Builder\Interface_ The created interface builder
|
Chris@0
|
49 */
|
Chris@0
|
50 protected function _interface($name) {
|
Chris@0
|
51 return new Builder\Interface_($name);
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * Creates a trait builder.
|
Chris@0
|
56 *
|
Chris@0
|
57 * @param string $name Name of the trait
|
Chris@0
|
58 *
|
Chris@0
|
59 * @return Builder\Trait_ The created trait builder
|
Chris@0
|
60 */
|
Chris@0
|
61 protected function _trait($name) {
|
Chris@0
|
62 return new Builder\Trait_($name);
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Creates a method builder.
|
Chris@0
|
67 *
|
Chris@0
|
68 * @param string $name Name of the method
|
Chris@0
|
69 *
|
Chris@0
|
70 * @return Builder\Method The created method builder
|
Chris@0
|
71 */
|
Chris@0
|
72 public function method($name) {
|
Chris@0
|
73 return new Builder\Method($name);
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Creates a parameter builder.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @param string $name Name of the parameter
|
Chris@0
|
80 *
|
Chris@0
|
81 * @return Builder\Param The created parameter builder
|
Chris@0
|
82 */
|
Chris@0
|
83 public function param($name) {
|
Chris@0
|
84 return new Builder\Param($name);
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * Creates a property builder.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @param string $name Name of the property
|
Chris@0
|
91 *
|
Chris@0
|
92 * @return Builder\Property The created property builder
|
Chris@0
|
93 */
|
Chris@0
|
94 public function property($name) {
|
Chris@0
|
95 return new Builder\Property($name);
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 /**
|
Chris@0
|
99 * Creates a function builder.
|
Chris@0
|
100 *
|
Chris@0
|
101 * @param string $name Name of the function
|
Chris@0
|
102 *
|
Chris@0
|
103 * @return Builder\Function_ The created function builder
|
Chris@0
|
104 */
|
Chris@0
|
105 protected function _function($name) {
|
Chris@0
|
106 return new Builder\Function_($name);
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * Creates a namespace/class use builder.
|
Chris@0
|
111 *
|
Chris@0
|
112 * @param string|Node\Name Name to alias
|
Chris@0
|
113 *
|
Chris@0
|
114 * @return Builder\Use_ The create use builder
|
Chris@0
|
115 */
|
Chris@0
|
116 protected function _use($name) {
|
Chris@0
|
117 return new Builder\Use_($name, Use_::TYPE_NORMAL);
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 public function __call($name, array $args) {
|
Chris@0
|
121 if (method_exists($this, '_' . $name)) {
|
Chris@0
|
122 return call_user_func_array(array($this, '_' . $name), $args);
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 throw new \LogicException(sprintf('Method "%s" does not exist', $name));
|
Chris@0
|
126 }
|
Chris@0
|
127 }
|