forked from bobthecow/homebrew-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract-php-extension.rb
245 lines (204 loc) · 6.01 KB
/
abstract-php-extension.rb
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
require 'formula'
require File.join(File.dirname(__FILE__), 'abstract-php-version')
class UnsupportedPhpApiError < RuntimeError
attr :name
def initialize name
@name = name
super "Unsupported PHP API Version"
end
end
class InvalidPhpizeError < RuntimeError
attr :name
def initialize (installed_php_version, required_php_version)
@name = name
super <<-EOS.undent
Version of phpize (PHP#{installed_php_version}) in $PATH does not support building this extension
version (PHP#{required_php_version}). Consider installing with the `--without-homebrew-php` flag.
EOS
end
end
class AbstractPhpExtension < Formula
def initialize name='__UNKNOWN__', path=nil
begin
raise "One does not simply install an AbstractPhpExtension" if name == "abstract-php-extension"
sup = super
if build.include? 'without-homebrew-php'
installed_php_version = nil
i = IO.popen("#{phpize} -v")
out = i.readlines.join("")
i.close
{ 53 => 20090626, 54 => 20100412 }.each do |v, api|
installed_php_version = v.to_s if out.match(/#{api}/)
end
raise UnsupportedPhpApiError.new if installed_php_version.nil?
required_php_version = php_branch.sub('.', '').to_s
unless installed_php_version == required_php_version
raise InvalidPhpizeError.new(installed_php_version, required_php_version)
end
end
sup
rescue Exception => e
# Hack so that we pass all brew doctor tests
reraise = e.backtrace.select { |l| l.match(/(doctor|cleanup|leaves|uses)\.rb/) }
raise e if reraise.empty?
end
end
# Hack to allow 'brew uses' to work, which requires deps, version, and requirements
%w(deps requirements version).each do |method|
define_method(method) do
if defined?(active_spec) && active_spec.respond_to?(method)
active_spec.send(method)
else
method === 'version' ? 'abstract' : []
end
end
end
def self.init
depends_on 'autoconf' => :build
option 'without-homebrew-php', "Ignore homebrew PHP and use default instead"
end
def php_branch
matches = /^Php5([3-9]+)/.match(self.class.name)
if matches
"5." + matches[1]
else
raise "Unable to guess PHP branch for #{self.class.name}"
end
end
def php_formula
'php' + php_branch.sub('.', '')
end
def safe_phpize
cmd = ''
cmd << "PHP_AUTOCONF=\"#{Formula['autoconf'].opt_prefix}/bin/autoconf\" "
cmd << "PHP_AUTOHEADER=\"#{Formula['autoconf'].opt_prefix}/bin/autoheader\" "
cmd << phpize
system cmd
end
def phpize
if build.include? 'without-homebrew-php'
"phpize"
else
"#{(Formula[php_formula]).bin}/phpize"
end
end
def phpini
if build.include? 'without-homebrew-php'
"php.ini presented by \"php --ini\""
else
"#{(Formula[php_formula]).config_path}/php.ini"
end
end
def phpconfig
if build.include? 'without-homebrew-php'
""
else
"--with-php-config=#{(Formula[php_formula]).bin}/php-config"
end
end
def extension
matches = /^Php5[3-9](.+)/.match(self.class.name)
if matches
matches[1].downcase
else
raise "Unable to guess PHP extension name for #{self.class.name}"
end
end
def extension_type
# extension or zend_extension
"extension"
end
def module_path
prefix / "#{extension}.so"
end
def config_file
begin
<<-EOS.undent
[#{extension}]
#{extension_type}="#{module_path}"
EOS
rescue Exception => e
nil
end
end
def caveats
caveats = [ "To finish installing #{extension} for PHP #{php_branch}:" ]
if build.include? "without-config-file"
caveats << " * Add the following line to #{phpini}:\n"
caveats << config_file
else
caveats << " * #{config_scandir_path}/#{config_filename} was created,"
caveats << " do not forget to remove it upon extension removal."
end
caveats << <<-EOS
* Validate installation via one of the following methods:
*
* Using PHP from a webserver:
* - Restart your webserver.
* - Write a PHP page that calls "phpinfo();"
* - Load it in a browser and look for the info on the #{extension} module.
* - If you see it, you have been successful!
*
* Using PHP from the command line:
* - Run "php -i" (command-line "phpinfo()")
* - Look for the info on the #{extension} module.
* - If you see it, you have been successful!
EOS
caveats.join("\n")
end
def config_path
etc / "php" / php_branch
end
def config_scandir_path
config_path / "conf.d"
end
def config_filename
"ext-" + extension + ".ini"
end
def config_filepath
config_scandir_path / config_filename
end
def write_config_file
if config_filepath.file?
inreplace config_filepath do |s|
s.gsub!(/^(zend_)?extension=.+$/, "#{extension_type}=\"#{module_path}\"")
end
elsif config_file
config_scandir_path.mkpath
config_filepath.write(config_file)
end
end
def options
options = []
options << ["--without-config-file", "Do not add #{config_filename} to #{config_scandir_path}"] if config_file
options
end
end
class AbstractPhp53Extension < AbstractPhpExtension
include AbstractPhpVersion::Php53Defs
def self.init opts=[]
super()
depends_on "php53" => opts unless build.include?('without-homebrew-php')
end
end
class AbstractPhp54Extension < AbstractPhpExtension
include AbstractPhpVersion::Php54Defs
def self.init opts=[]
super()
depends_on "php54" => opts unless build.include?('without-homebrew-php')
end
end
class AbstractPhp55Extension < AbstractPhpExtension
include AbstractPhpVersion::Php55Defs
def self.init opts=[]
super()
depends_on "php55" => opts unless build.include?('without-homebrew-php')
end
end
class AbstractPhp56Extension < AbstractPhpExtension
include AbstractPhpVersion::Php56Defs
def self.init opts=[]
super()
depends_on "php56" => opts unless build.include?('without-homebrew-php')
end
end