|
21 | 21 | abstract class CompanionGenerator
|
22 | 22 | {
|
23 | 23 |
|
24 |
| - private $namingStrategy; |
25 |
| - private $outputPath; |
26 |
| - private $tmpl; |
27 |
| - |
28 |
| - /** |
29 |
| - * Create a new generator that outputs to the given path. The given output |
30 |
| - * path is used as a base path. The generation logic will append a PSR-0 |
31 |
| - * compliant path to the specified path, taking into account both the value |
32 |
| - * of getCompanionNamespace() and the name of the companion as generated by |
33 |
| - * the NamingStrategy. |
34 |
| - * |
35 |
| - * # Example |
36 |
| - * For a class my\ns\Definition, the DefaultNamingStrategy and the following |
37 |
| - * implementation: |
38 |
| - * |
39 |
| - * class MyGenerator extends AbstractGenerator { |
40 |
| - * protected static $actorNamespace = 'my\dynamic\ns'; |
41 |
| - * |
42 |
| - * public function __construct() { |
43 |
| - * parent::__construct('/path/to/site/target'); |
44 |
| - * } |
45 |
| - * |
46 |
| - * protected function getCompanionNamespace() { |
47 |
| - * return 'my\dynamic\ns'; |
48 |
| - * } |
49 |
| - * |
50 |
| - * // ... |
51 |
| - * } |
52 |
| - * |
53 |
| - * The companion will be my\dynamic\ns\my_ns_Definition and will output at |
54 |
| - * /path/to/site/target/my/dynamic/ns/my/ns/Definition.php |
55 |
| - * |
56 |
| - * @param string $outputPath The target path for the generated companion. |
57 |
| - */ |
58 |
| - public function __construct($outputPath) |
59 |
| - { |
60 |
| - $this->outputPath = rtrim($outputPath, '/'); |
61 |
| - } |
62 |
| - |
63 |
| - /** |
64 |
| - * Generate the code. This method delegates to the implementation for the |
65 |
| - * acutal generation then outputs to the specified path. |
66 |
| - * |
67 |
| - * @param string $defClass The definition for which to generate an actor. |
68 |
| - */ |
69 |
| - public function generate($defClass) { |
70 |
| - $this->init(); |
71 |
| - |
72 |
| - $templatePath = $this->getTemplatePath($defClass); |
73 |
| - |
74 |
| - $companionNs = $this->getCompanionNamespace($defClass); |
75 |
| - $companionClass = $this->namingStrategy->getCompanionClassName( |
76 |
| - $defClass |
77 |
| - ); |
78 |
| - $targetDir = $this->outputPath . '/' . $this->toPath($companionNs); |
79 |
| - $fileName = $this->toPath($companionClass); |
80 |
| - $outPath = $targetDir . '/' . $fileName . '.php'; |
81 |
| - |
82 |
| - $values = $this->getValues($defClass); |
83 |
| - $values['companionNs'] = $companionNs; |
84 |
| - $values['companionClass'] = $companionClass; |
85 |
| - $values['model'] = $defClass; |
86 |
| - $values['modelStr'] = str_replace('\\', '\\\\', $defClass); |
87 |
| - |
88 |
| - $resolver = new TemplateResolver(); |
89 |
| - $resolver->resolve($templatePath, $outPath, $values); |
90 |
| - } |
91 |
| - |
92 |
| - /** |
93 |
| - * Getter for the generator's naming strategy. Access is useful when |
94 |
| - * a companion class name needs to be determined outside of normal companion |
95 |
| - * generation and loading. |
96 |
| - * |
97 |
| - * @return NamingStrategy |
98 |
| - */ |
99 |
| - public function getNamingStrategy() { |
100 |
| - return $this->namingStrategy; |
101 |
| - } |
102 |
| - |
103 |
| - /** |
104 |
| - * This method is responsible for returning the base namespace in which |
105 |
| - * companion object reside. |
106 |
| - * |
107 |
| - * @param string $defClass The name of the class for which a companion is |
108 |
| - * being generated. This allows implementations to return different base |
109 |
| - * namespaces for a possible hierarchy of definitions. |
110 |
| - * @return string |
111 |
| - */ |
112 |
| - protected abstract function getCompanionNamespace($defClass); |
113 |
| - |
114 |
| - /** |
115 |
| - * This method is responsible for returning the path to the template that is |
116 |
| - * used to generate the actor. |
117 |
| - * |
118 |
| - * @param string $defClass The name of the class for which a companion is |
119 |
| - * being generated. This allows implementations to return different |
120 |
| - * templates for different definitions. |
121 |
| - * @return string |
122 |
| - */ |
123 |
| - protected abstract function getTemplatePath($defClass); |
124 |
| - |
125 |
| - /** |
126 |
| - * This method is responsible for actually generating the substitution values |
127 |
| - * for generating the actor for the specified definition class. These values |
128 |
| - * will be substituted into the template specified by getTemplatePath(). |
129 |
| - * |
130 |
| - * @param string $defClass The name of the definition class. |
131 |
| - * @return array Substitution values for generating the actor. |
132 |
| - */ |
133 |
| - protected abstract function getValues($defClass); |
134 |
| - |
135 |
| - /* |
136 |
| - * ========================================================================= |
137 |
| - * Dependency setters |
138 |
| - * ========================================================================= |
139 |
| - */ |
140 |
| - |
141 |
| - public function setNamingStrategy(NamingStrategy $namingStrategy) |
142 |
| - { |
143 |
| - $this->namingStrategy = $namingStrategy; |
144 |
| - } |
145 |
| - |
146 |
| - /* |
147 |
| - * ========================================================================= |
148 |
| - * Private helpers |
149 |
| - * ========================================================================= |
150 |
| - */ |
151 |
| - |
152 |
| - /* Ensure dependency are initialized */ |
153 |
| - private function init() { |
154 |
| - // Ensure that injectable dependencies have an implementation available. |
155 |
| - if ($this->namingStrategy === null) { |
156 |
| - $this->namingStrategy = new DefaultNamingStrategy(); |
157 |
| - } |
158 |
| - |
159 |
| - // If the template hasn't been set then initialize this instance |
160 |
| - if ($this->tmpl === null) { |
161 |
| - } |
162 |
| - } |
163 |
| - |
164 |
| - /* Convert the given namespace/classname to a path fragment. */ |
165 |
| - private function toPath($name) { |
166 |
| - return str_replace(array('\\', '_'), '/', $name); |
167 |
| - } |
| 24 | + private $namingStrategy; |
| 25 | + private $outputPath; |
| 26 | + private $tmpl; |
| 27 | + |
| 28 | + /** |
| 29 | + * Create a new generator that outputs to the given path. The given output |
| 30 | + * path is used as a base path. The generation logic will append a PSR-0 |
| 31 | + * compliant path to the specified path, taking into account both the value |
| 32 | + * of getCompanionNamespace() and the name of the companion as generated by |
| 33 | + * the NamingStrategy. |
| 34 | + * |
| 35 | + * # Example |
| 36 | + * For a class my\ns\Definition, the DefaultNamingStrategy and the following |
| 37 | + * implementation: |
| 38 | + * |
| 39 | + * class MyGenerator extends AbstractGenerator { |
| 40 | + * protected static $actorNamespace = 'my\dynamic\ns'; |
| 41 | + * |
| 42 | + * public function __construct() { |
| 43 | + * parent::__construct('/path/to/site/target'); |
| 44 | + * } |
| 45 | + * |
| 46 | + * protected function getCompanionNamespace() { |
| 47 | + * return 'my\dynamic\ns'; |
| 48 | + * } |
| 49 | + * |
| 50 | + * // ... |
| 51 | + * } |
| 52 | + * |
| 53 | + * The companion will be my\dynamic\ns\my_ns_Definition and will output at |
| 54 | + * /path/to/site/target/my/dynamic/ns/my/ns/Definition.php |
| 55 | + * |
| 56 | + * @param string $outputPath The target path for the generated companion. |
| 57 | + */ |
| 58 | + public function __construct($outputPath) |
| 59 | + { |
| 60 | + $this->outputPath = rtrim($outputPath, '/'); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Generate the code. This method delegates to the implementation for the |
| 65 | + * acutal generation then outputs to the specified path. |
| 66 | + * |
| 67 | + * @param string $defClass The definition for which to generate an actor. |
| 68 | + */ |
| 69 | + public function generate($defClass) { |
| 70 | + $this->init(); |
| 71 | + |
| 72 | + $templatePath = $this->getTemplatePath($defClass); |
| 73 | + |
| 74 | + $companionNs = $this->getCompanionNamespace($defClass); |
| 75 | + $companionClass = $this->namingStrategy->getCompanionClassName( |
| 76 | + $defClass |
| 77 | + ); |
| 78 | + $targetDir = $this->outputPath . '/' . $this->toPath($companionNs); |
| 79 | + $fileName = $this->toPath($companionClass); |
| 80 | + $outPath = $targetDir . '/' . $fileName . '.php'; |
| 81 | + |
| 82 | + $values = $this->getValues($defClass); |
| 83 | + $values['companionNs'] = $companionNs; |
| 84 | + $values['companionClass'] = $companionClass; |
| 85 | + $values['model'] = $defClass; |
| 86 | + $values['modelStr'] = str_replace('\\', '\\\\', $defClass); |
| 87 | + |
| 88 | + $resolver = new TemplateResolver(); |
| 89 | + $resolver->resolve($templatePath, $outPath, $values); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Getter for the generator's naming strategy. Access is useful when |
| 94 | + * a companion class name needs to be determined outside of normal companion |
| 95 | + * generation and loading. |
| 96 | + * |
| 97 | + * @return NamingStrategy |
| 98 | + */ |
| 99 | + public function getNamingStrategy() { |
| 100 | + return $this->namingStrategy; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * This method is responsible for returning the base namespace in which |
| 105 | + * companion object reside. |
| 106 | + * |
| 107 | + * @param string $defClass The name of the class for which a companion is |
| 108 | + * being generated. This allows implementations to return different base |
| 109 | + * namespaces for a possible hierarchy of definitions. |
| 110 | + * @return string |
| 111 | + */ |
| 112 | + protected abstract function getCompanionNamespace($defClass); |
| 113 | + |
| 114 | + /** |
| 115 | + * This method is responsible for returning the path to the template that is |
| 116 | + * used to generate the actor. |
| 117 | + * |
| 118 | + * @param string $defClass The name of the class for which a companion is |
| 119 | + * being generated. This allows implementations to return different |
| 120 | + * templates for different definitions. |
| 121 | + * @return string |
| 122 | + */ |
| 123 | + protected abstract function getTemplatePath($defClass); |
| 124 | + |
| 125 | + /** |
| 126 | + * This method is responsible for actually generating the substitution values |
| 127 | + * for generating the actor for the specified definition class. These values |
| 128 | + * will be substituted into the template specified by getTemplatePath(). |
| 129 | + * |
| 130 | + * @param string $defClass The name of the definition class. |
| 131 | + * @return array Substitution values for generating the actor. |
| 132 | + */ |
| 133 | + protected abstract function getValues($defClass); |
| 134 | + |
| 135 | + /* |
| 136 | + * ========================================================================= |
| 137 | + * Dependency setters |
| 138 | + * ========================================================================= |
| 139 | + */ |
| 140 | + |
| 141 | + public function setNamingStrategy(NamingStrategy $namingStrategy) |
| 142 | + { |
| 143 | + $this->namingStrategy = $namingStrategy; |
| 144 | + } |
| 145 | + |
| 146 | + /* |
| 147 | + * ========================================================================= |
| 148 | + * Private helpers |
| 149 | + * ========================================================================= |
| 150 | + */ |
| 151 | + |
| 152 | + /* Ensure dependency are initialized */ |
| 153 | + private function init() { |
| 154 | + // Ensure that injectable dependencies have an implementation available. |
| 155 | + if ($this->namingStrategy === null) { |
| 156 | + $this->namingStrategy = new DefaultNamingStrategy(); |
| 157 | + } |
| 158 | + |
| 159 | + // If the template hasn't been set then initialize this instance |
| 160 | + if ($this->tmpl === null) { |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /* Convert the given namespace/classname to a path fragment. */ |
| 165 | + private function toPath($name) { |
| 166 | + return str_replace(array('\\', '_'), '/', $name); |
| 167 | + } |
168 | 168 | }
|
0 commit comments