Skip to content

Commit 2046293

Browse files
committed
fixups to enums
1 parent ab024ab commit 2046293

39 files changed

+177
-529
lines changed

examples/generate_phpdoc.rb

+72-48
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88

99
# Regenerate docs with something like:
1010
#
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
2019

2120
# these have hand-written methods, don't autodoc them
2221
$no_generate = %w(
@@ -31,6 +30,16 @@
3130
extract_area
3231
)
3332

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+
3443
# map Ruby type names to PHP type names
3544
$to_php_map = {
3645
"Vips::Image" => "Image",
@@ -49,15 +58,26 @@
4958
"Vips::Blob" => "string",
5059
"gchararray" => "string",
5160
"gpointer" => "string",
52-
"VipsBandFormat" => "BandFormat",
53-
"VipsCoding" => "Coding",
54-
"VipsInterpretation" => "Interpretation",
55-
"VipsDemandStyle" => "DemandStyle",
5661
}
5762

5863
class Vips::Argument
5964
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 ""
6181
end
6282
end
6383

@@ -190,32 +210,35 @@ def generate_class(file, gtype)
190210
* @version GIT:ad44dfdd31056a41cbf217244ce62e7a702e0282
191211
* @link https://github.com/jcupitt/php-vips
192212
*/
213+
EOF
193214

194-
namespace Jcupitt\\Vips;
195-
196-
/**
197-
* %s
198-
*
215+
class_header = <<EOF
199216
* @category Images
200217
* @package Jcupitt\\Vips
201218
* @author John Cupitt <jcupitt@gmail.com>
202219
* @copyright 2016 John Cupitt
203220
* @license https://opensource.org/licenses/MIT MIT
204221
* @version Release:0.1.2
205222
* @link https://github.com/jcupitt/php-vips
206-
*
207223
EOF
208224

209-
# gobject-introspection 3.0.7 crashes a lot if it GCs while doing
210-
# something
211-
GC.disable
212-
213-
Vips::init
214-
215225
# The image class is the most complex
216226
puts "Generating ImageAutodoc.php ..."
217227
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"
219242

220243
generate_class file, GLib::Type["VipsOperation"]
221244

@@ -236,36 +259,37 @@ def generate_class(file, gtype)
236259

237260
file << <<EOF
238261
*/
239-
class ImageAutodoc
262+
abstract class ImageAutodoc
240263
{
241264
}
242265
243266
?>
244267
EOF
245268
end
246269

247-
# do the various constants, like VipsInterpretation
248-
Vips.constants.each do |name|
270+
# generate all the enums
271+
$enums.each do |name|
249272
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"
268289
end
290+
291+
file << "}\n"
292+
file << "?>\n"
269293
end
270294
end
271295

src/Access.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,21 @@
3737
* @link https://github.com/jcupitt/php-vips
3838
*/
3939

40-
namespace Jcupitt\Vips;
40+
namespace Jcupitt\Vips\Enum;
4141

4242
/**
4343
* The Access enum.
44-
*
4544
* @category Images
4645
* @package Jcupitt\Vips
4746
* @author John Cupitt <jcupitt@gmail.com>
4847
* @copyright 2016 John Cupitt
4948
* @license https://opensource.org/licenses/MIT MIT
5049
* @version Release:0.1.2
5150
* @link https://github.com/jcupitt/php-vips
52-
*
5351
*/
54-
5552
abstract class Access {
5653
const RANDOM = 'random';
5754
const SEQUENTIAL = 'sequential';
58-
const SEQUENTIAL-UNBUFFERED = 'sequential-unbuffered';
55+
const SEQUENTIAL_UNBUFFERED = 'sequential_unbuffered';
5956
}
6057
?>

src/Align.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,18 @@
3737
* @link https://github.com/jcupitt/php-vips
3838
*/
3939

40-
namespace Jcupitt\Vips;
40+
namespace Jcupitt\Vips\Enum;
4141

4242
/**
4343
* The Align enum.
44-
*
4544
* @category Images
4645
* @package Jcupitt\Vips
4746
* @author John Cupitt <jcupitt@gmail.com>
4847
* @copyright 2016 John Cupitt
4948
* @license https://opensource.org/licenses/MIT MIT
5049
* @version Release:0.1.2
5150
* @link https://github.com/jcupitt/php-vips
52-
*
5351
*/
54-
5552
abstract class Align {
5653
const LOW = 'low';
5754
const CENTRE = 'centre';

src/Angle.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,18 @@
3737
* @link https://github.com/jcupitt/php-vips
3838
*/
3939

40-
namespace Jcupitt\Vips;
40+
namespace Jcupitt\Vips\Enum;
4141

4242
/**
4343
* The Angle enum.
44-
*
4544
* @category Images
4645
* @package Jcupitt\Vips
4746
* @author John Cupitt <jcupitt@gmail.com>
4847
* @copyright 2016 John Cupitt
4948
* @license https://opensource.org/licenses/MIT MIT
5049
* @version Release:0.1.2
5150
* @link https://github.com/jcupitt/php-vips
52-
*
5351
*/
54-
5552
abstract class Angle {
5653
const D0 = 'd0';
5754
const D90 = 'd90';

src/Angle45.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,18 @@
3737
* @link https://github.com/jcupitt/php-vips
3838
*/
3939

40-
namespace Jcupitt\Vips;
40+
namespace Jcupitt\Vips\Enum;
4141

4242
/**
4343
* The Angle45 enum.
44-
*
4544
* @category Images
4645
* @package Jcupitt\Vips
4746
* @author John Cupitt <jcupitt@gmail.com>
4847
* @copyright 2016 John Cupitt
4948
* @license https://opensource.org/licenses/MIT MIT
5049
* @version Release:0.1.2
5150
* @link https://github.com/jcupitt/php-vips
52-
*
5351
*/
54-
5552
abstract class Angle45 {
5653
const D0 = 'd0';
5754
const D45 = 'd45';

0 commit comments

Comments
 (0)