-
Notifications
You must be signed in to change notification settings - Fork 4
/
extwriter.php
263 lines (224 loc) · 8.53 KB
/
extwriter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* extwriter.php - G\Generator\ExtWriter application class
*
* This is released under the MIT, see LICENSE for details
*
* @author Elizabeth M Smith <auroraeosrose@php.net>
* @copyright Elizabeth M Smith (c) 2012
* @link http://gtkforphp.net
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @since Php 5.4.0
* @package gig
* @subpackage lib
*/
/**
* Namespace for the tool
*/
namespace G\Generator;
/**
* ExtWriter - handles setup and running for the extension writer ggen tool
*
* uses command line variables and a configuration file to do generation of code
*/
class ExtWriter extends Cli {
/**
* version string in major.minor.bug-dev/alpha/beta format
* @var string
*/
const VERSION = '0.1.0-dev';
/**
* Does setup work
* parses options, etc
*
* @return void
*/
public function __construct($argv, $argc) {
parent::__construct($argv, $argc);
$this->opt->setOptions(
array(
array('h', 'help', 'Usage instructions'),
array('v', 'verbose', 'Show version information'),
array('q', 'quiet', 'Suppress all output'),
array('l:', 'log:', 'Log to file provided --log=logfile'),
array('c', 'check', 'Lint check config and specification files'),
array('u', 'update', 'update the extension, do not overwrite existing files this is implicit if the location specified in the config is not empty'),
array('n', 'new', 'ignore existing files and create a new extension this is implicit if the location specified in the config is empty'),
)
);
}
/**
* Runs the application
*
* @return void
*/
public function run() {
$options = $this->opt->getOptions();
$this->handleOptions($options);
// get all our other parameters
$params = $this->opt->getParameters();
// no params? ack! trigger fatal - bad bad
if(count($params) < 1) {
trigger_error('Config filename was not provided', E_USER_ERROR);
}
// filename should be the last param provided
$filename = array_pop($params);
// load our config - maybe
$config = $this->loadConfig($filename);
// load our spec file
$spec = $this->loadSpec($config);
// load our output mechanism
$output = $this->loadWriter($config);
$output->writeModule($spec);
}
/**
* Walks through our options and handles them
*
* @param array $options options to handle
* @return void
*/
protected function handleOptions($options) {
foreach($options as $name => $value) {
switch($name) {
case 'h':
case 'help':
$this->showHeader();
return $this->opt->showHelp(' config_filename.type');
case 'v':
case 'verbose':
$this->messages = 2;
break;
case 'q':
case 'quiet':
if ($this->messages > 1) {
$this->showHeader();
trigger_error('You cannot use --verbose and --quiet at the same time', E_USER_ERROR);
}
$this->messages = 0;
break;
}
}
$this->showHeader();
foreach($options as $name => $value) {
switch($name) {
case 'l':
case 'log':
// TODO: making logging work... $this->setLogFile($value);
break;
case 'c':
case 'check':
// TODO: do the lint
break;
case 'u':
case 'update':
// TODO: force update
break;
case 'n':
case 'new':
// TODO: force new
break;
}
}
}
/**
* Attempts to load in our module configuration
* the last .blah is used for the name of the config parser
*
* @param string $filename name of config file to load
* @return array configuration
*/
protected function loadConfig($filename) {
// look for file as absolute path
$this->printMessage('Attempting to load filename ' . $filename, 2);
$path = realpath($filename);
$this->printMessage('Looking in ' . $path, 2);
if (!file_exists($path)) {
// look for file in cwd registered
$path = realpath($this->cwd . DIRECTORY_SEPARATOR . $filename);
$this->printMessage('Looking in ' . $path, 2);
if (!file_exists($path)) {
// look for file in include path using stream_resolve_include_path
$path = stream_resolve_include_path($filename);
$this->printMessage('Looking in ' . $path, 2);
if (!file_exists($path)) {
trigger_error('Config file ' . $filename . ' could not be loaded', E_USER_ERROR);
}
}
}
// path should now be a shiny absolute path to a file
$this->printMessage('Loading configuration from ' . $path);
// parser type detection
$ext = pathinfo ($filename, PATHINFO_EXTENSION);
$this->printMessage('Config detected as type ' . $ext, 2);
// Parser for config should be in parsers/config/$ext.php
$parser = realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR . 'parsers' . DIRECTORY_SEPARATOR . 'config'
. DIRECTORY_SEPARATOR . strtolower($ext) . '.php';
$this->printMessage('Trying to load parser ' . $parser, 2);
if (!file_exists($parser)) {
trigger_error('Configuration parser for ' . $ext . ' could not be loaded', E_USER_ERROR);
}
$class = 'G\Generator\ConfigParser\\' . ucfirst(strtolower($ext));
$this->printMessage('Trying to load class ' . $class, 2);
include $parser;
if (!class_exists($class)) {
trigger_error('Configuration parser class ' . $class . ' could not be found', E_USER_ERROR);
}
$this->printMessage('Parser loaded for config type ' . $ext);
$config = new $class($path);
return $config->parse($filename);
}
/**
* Attempts to create and load a spec parser for the requested module
*
* @param string $filename name of config file to load
* @return object instanceof specparser
*/
protected function loadSpec($config) {
// parser type detection
if (isset($config['specification']['type'])) {
$type = $config['specification']['type'];
} else {
trigger_error('Specification type missing from configuration file', E_USER_ERROR);
}
$this->printMessage('Specification detected as type ' . $type, 2);
// Parser for spec should be in parsers/spec/$type.php
$parser = realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR . 'parsers' . DIRECTORY_SEPARATOR . 'spec'
. DIRECTORY_SEPARATOR . strtolower($type) . '.php';
$this->printMessage('Trying to load parser ' . $parser, 2);
if (!file_exists($parser)) {
trigger_error('Specification parser for ' . $type . ' could not be loaded', E_USER_ERROR);
}
$class = 'G\Generator\SpecParser\\' . ucfirst(strtolower($type));
$this->printMessage('Trying to load class ' . $class, 2);
include $parser;
if (!class_exists($class)) {
trigger_error('Specification parser class ' . $class . ' could not be found', E_USER_ERROR);
}
$this->printMessage('Parser loaded for spec type ' . $type);
$spec = new $class($config['specification']);
return $spec->parse();
}
/**
* Starts up a writer instance to use to output our parsed information
*
* @param array $config configuration values
* @return object instanceof writer
*/
protected function loadWriter($config) {
// writer configuration information
if (!isset($config['output'])) {
trigger_error('Writer output settings missing from configuration file', E_USER_ERROR);
}
return new Output($config['output']);
}
/**
* Echoes out version information to console
*
* @return void
*/
protected function showHeader() {
if($this->messages > 0) {
echo 'ggen ' . self::VERSION . ', Copyright (c) 2012 Elizabeth M Smith', PHP_EOL, PHP_EOL;
}
}
}