8
8
9
9
# Regenerate docs with something like:
10
10
#
11
- # ./generate_phpdoc.rb > AutoDocs.php
12
- #
13
- # See https://www.phpdoc.org/docs/latest/references/phpdoc/tags/method.html for
14
- # docs on the @method syntax. We generate something like:
15
- #
16
- # @method [return type] [name]([[type] [parameter]<, ...>]) [<description>]
17
- #
18
- # @method Image embed(Image $in, integer $x, integer $y, integer $width,
19
- # integer $height, array $options = []) embed an image in a larger image
11
+ # cd src
12
+ # ../examples/generate_phpdoc.rb
13
+
14
+ # gobject-introspection 3.0.7 crashes a lot if it GCs while doing
15
+ # something
16
+ GC . disable
17
+
18
+ Vips ::init
20
19
21
20
# these have hand-written methods, don't autodoc them
22
21
$no_generate = %w(
31
30
extract_area
32
31
)
33
32
33
+ # Find all the vips enums
34
+ $enums = [ ]
35
+ Vips . constants . each do |name |
36
+ const = Vips . const_get name
37
+ if const . respond_to? :superclass and
38
+ const . superclass == GLib ::Enum
39
+ $enums << name . to_s
40
+ end
41
+ end
42
+
34
43
# map Ruby type names to PHP type names
35
44
$to_php_map = {
36
45
"Vips::Image" => "Image" ,
49
58
"Vips::Blob" => "string" ,
50
59
"gchararray" => "string" ,
51
60
"gpointer" => "string" ,
52
- "VipsBandFormat" => "BandFormat" ,
53
- "VipsCoding" => "Coding" ,
54
- "VipsInterpretation" => "Interpretation" ,
55
- "VipsDemandStyle" => "DemandStyle" ,
56
61
}
57
62
58
63
class Vips ::Argument
59
64
def to_php
60
- $to_php_map. include? ( type ) ? $to_php_map[ type ] : ""
65
+ return $to_php_map[ type ] if $to_php_map. include? ( type )
66
+ return type if $enums. include? ( type )
67
+
68
+ # is there a "Vips" at the front of the type name? remove it and try the
69
+ # enums again
70
+ trim = type . to_s . tap { |s | s . slice! ( "Vips" ) }
71
+ return "Enum\\ " + trim if $enums. include? ( trim )
72
+
73
+ # is there a "::" at the front of the type name? remove it and try the
74
+ # enums again
75
+ trim . slice! "::"
76
+ return "Enum\\ " + trim if $enums. include? ( trim )
77
+
78
+ # no mapping found
79
+ puts "no mapping found for #{ type } "
80
+ return ""
61
81
end
62
82
end
63
83
@@ -190,32 +210,35 @@ def generate_class(file, gtype)
190
210
* @version GIT:ad44dfdd31056a41cbf217244ce62e7a702e0282
191
211
* @link https://github.com/jcupitt/php-vips
192
212
*/
213
+ EOF
193
214
194
- namespace Jcupitt\\ Vips;
195
-
196
- /**
197
- * %s
198
- *
215
+ class_header = <<EOF
199
216
* @category Images
200
217
* @package Jcupitt\\ Vips
201
218
* @author John Cupitt <jcupitt@gmail.com>
202
219
* @copyright 2016 John Cupitt
203
220
* @license https://opensource.org/licenses/MIT MIT
204
221
* @version Release:0.1.2
205
222
* @link https://github.com/jcupitt/php-vips
206
- *
207
223
EOF
208
224
209
- # gobject-introspection 3.0.7 crashes a lot if it GCs while doing
210
- # something
211
- GC . disable
212
-
213
- Vips ::init
214
-
215
225
# The image class is the most complex
216
226
puts "Generating ImageAutodoc.php ..."
217
227
File . open ( "ImageAutodoc.php" , "w" ) do |file |
218
- file << preamble % "Autodocs for the Image class."
228
+ file << preamble
229
+ file << "\n "
230
+ file << "namespace Jcupitt\\ Vips;\n "
231
+ file << "\n "
232
+
233
+ $enums. each do |name |
234
+ file << "use Jcupitt\\ Vips\\ Enum\\ #{ name } ;\n "
235
+ end
236
+
237
+ file << "\n "
238
+ file << "/**\n "
239
+ file << " * Autodocs for the Image class.\n "
240
+ file << class_header
241
+ file << " *\n "
219
242
220
243
generate_class file , GLib ::Type [ "VipsOperation" ]
221
244
@@ -236,36 +259,37 @@ def generate_class(file, gtype)
236
259
237
260
file << <<EOF
238
261
*/
239
- class ImageAutodoc
262
+ abstract class ImageAutodoc
240
263
{
241
264
}
242
265
243
266
?>
244
267
EOF
245
268
end
246
269
247
- # do the various constants, like VipsInterpretation
248
- Vips . constants . each do |name |
270
+ # generate all the enums
271
+ $enums . each do |name |
249
272
const = Vips . const_get name
250
- if const . respond_to? :superclass and
251
- const . superclass == GLib ::Enum
252
- puts "Generating #{ name } .php ..."
253
- File . open ( "#{ name } .php" , "w" ) do |file |
254
- file << preamble % "The #{ name } enum."
255
- file << <<EOF
256
- */
257
-
258
- abstract class #{ name } {
259
- EOF
260
- const . values . each do |value |
261
- next if value . nick == "last"
262
- file << " const #{ value . nick . upcase } = '#{ value . nick } ';\n "
263
- end
264
- file << <<EOF
265
- }
266
- ?>
267
- EOF
273
+ puts "Generating #{ name } .php ..."
274
+ File . open ( "#{ name } .php" , "w" ) do |file |
275
+ file << preamble
276
+ file << "\n "
277
+ file << "namespace Jcupitt\\ Vips\\ Enum;\n "
278
+ file << "\n "
279
+ file << "/**\n "
280
+ file << " * The #{ name } enum.\n "
281
+ file << class_header
282
+ file << " */\n "
283
+ file << "abstract class #{ name } {\n "
284
+
285
+ const . values . each do |value |
286
+ php_name = value . nick . tr "-" , "_"
287
+ next if value . nick == "last"
288
+ file << " const #{ php_name . upcase } = '#{ php_name } ';\n "
268
289
end
290
+
291
+ file << "}\n "
292
+ file << "?>\n "
269
293
end
270
294
end
271
295
0 commit comments