-
Notifications
You must be signed in to change notification settings - Fork 4
/
output.php
195 lines (169 loc) · 4.68 KB
/
output.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
<?php
/**
* output.php - G\Generator\Output 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 g\generator
* @subpackage lib
*/
/**
* Namespace for the tool
*/
namespace G\Generator;
/**
* Takes information about a module and writes it out using the template
* chosen
*/
class Output {
/**
* our repository
* @var object
*/
protected $repository;
/**
* namespace to modularize
* @var string
*/
protected $namespace;
/**
* version to modularize
* @var string
*/
protected $version;
/**
* module name
* @var string
*/
protected $module;
/**
* authors
* @var array
*/
protected $authors = array();
/**
* Checks for the extension and attempts to require the proper typelib
*
* @return void
*/
public function __construct($config) {
// module name
if (!isset($config['module'])) {
trigger_error('Module name missing in configuration file', E_USER_ERROR);
}
$this->module = $config['module'];
// template location
if (!isset($config['templates'])) {
trigger_error('Template location missing in configuration file', E_USER_ERROR);
}
$templates = realpath($config['templates']);
if (!file_exists($templates)) {
$templates = realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR . 'templates'
. DIRECTORY_SEPARATOR . $config['templates'] . DIRECTORY_SEPARATOR;
if (!file_exists($templates)) {
trigger_error('Template ' . $templates . ' does not exist', E_USER_ERROR);
}
}
$this->templates = $templates;
// output location
if (!isset($config['location'])) {
trigger_error('Output location missing in configuration file', E_USER_ERROR);
}
$location = $config['location'];
$this->deleteDir($location);
mkdir($location);
$location = realpath($location);
// for now we always generate new
trigger_error('Output will write to ' . $location, E_USER_WARNING);
$this->location = $location . DIRECTORY_SEPARATOR;
if (isset($config['authors'])) {
$this->authors = $config['authors'];
}
}
/**
* Writes out our module
*
* @return void
*/
public function writeModule($spec) {
$classes = array();
// each class gets it's own file class.c
// each class.c file has a boilerplate template with an init
// check for enums
if (isset($spec['enums'])) {
$this->writeEnumFile($spec['enums']);
$classes[] = 'Enums';
}
// finally write out module
$this->writeModuleFile($classes);
}
/**
* Enum File
*
* @return void
*/
protected function writeEnumFile($enums) {
$authors = $this->authors;
$module = strtolower($this->module);
$module_uc = strtoupper($this->module);
ob_start();
include $this->templates . 'enum.c.tpl';
$file = ob_get_clean();
$filename = $this->location . 'enums.c';
file_put_contents($filename, $file);
}
/**
* Module File
*
* @return void
*/
protected function writeModuleFile($raw_classes) {
$year = date('Y');
$authors = $this->authors;
$module = strtolower($this->module);
$module_uc = strtoupper($this->module);
$module_pretty = $this->module;
$generator_version = \G\Generator\ExtWriter::VERSION;
$classes = array();
foreach($raw_classes as $name => $def) {
$classes[] = $this->mangleClassName($name);
}
unset($raw_classes, $name, $def);
ob_start();
include $this->templates . 'module.c.tpl';
$file = ob_get_clean();
$filename = $this->location . 'php_' . $module . '.c';
file_put_contents($filename, $file);
}
/**
* Cleans up our environment
*
* @return void
*/
protected function deleteDir($dir) {
if (!file_exists($dir)) {
return;
}
foreach(glob($dir . '/*') as $file) {
if(is_dir($file)) {
$this->deleteDir($file);
} else {
unlink($file);
}
}
rmdir($dir);
}
/**
* TODO: make this do something
*
* @return void
*/
protected function mangleClassname($name) {
return $name;
}
}