Skip to content

Instantly share code, notes, and snippets.

@charlesbedrosian
Forked from sj26/php_postgresql.rb
Created May 8, 2012 04:47
Show Gist options
  • Save charlesbedrosian/2632660 to your computer and use it in GitHub Desktop.
Save charlesbedrosian/2632660 to your computer and use it in GitHub Desktop.
Homebrew formula for PHP PostgreSQL extensions
require 'formula'
require 'net/http'
# This is an odd formula which adapts itself to whatever version of PHP
# is installed with the current version of OS X to install the missing
# PostgreSQL extension.
class PhpPostgresql < Formula
homepage 'http://www.php.net/'
# author: Samuel Cochran <sj26@sj26.com>
def self.version; @@version ||= %x[php -v].split(' ', 3)[1]; end
def self.filename; @@filename ||= "php-#{version}.tar.bz2"; end
def self.url; @@url ||= "http://www.php.net/distributions/#{filename}"; end
def self.release_html; @@release_html ||= %x[curl -s #{'http://www.php.net/releases/'}]; end
@@md5s = Hash.new do |md5s, filename|
puts version.inspect, filename.inspect
# Look for a link to the file closely followed by "md5: ..." on the PHP releases page
/<a\s+href="[^"]*\/#{Regexp.quote(filename)}(?:\/[^"]+)?"[^>]*>[^<]*<\/a>\s*<br\s*\/?>\s*<span[^>]*>md5:\s*([a-zA-Z0-9]+)/mi.match(release_html) and $1 or false
end.merge({
# Cached MD5s for PHP versions common on OS X
'php-5.3.1.tar.bz2' => '63e97ad450f0f7259e785100b634c797', # 10.6
'php-5.3.8.tar.bz2' => '704cd414a0565d905e1074ffdc1fadfb', # 10.7
})
def self.md5; @@md5s[filename]; end
def self.standard; @@standard ||= SoftwareSpecification.new(url, nil); end
def php_extensions; lib+'php/extensions'; end
def install
extensions = ['pgsql']
extensions << 'pdo_pgsql' if File.directory? 'ext/pdo_pgsql'
args = ["--prefix=#{prefix}"]
# Weirdly, the Apple-distributed php.h is missing these macros
if version == '5.3.8'
ENV['EXTRA_CFLAGS'] ||= ""
ENV['EXTRA_CFLAGS'] += " -DPHP_FE_END='{ NULL, NULL, NULL, 0, 0 }' -DZEND_MOD_END='{ NULL, NULL, NULL, 0 }'"
end
extensions.each do |extension|
ohai "Compiling #{extension} extension"
Dir.chdir "ext/#{extension}" do
system '/usr/bin/phpize'
system './configure', *args
system '/usr/bin/make'
end
end
php_extensions.mkpath
extensions.each do |extension|
php_extensions.install Dir["ext/#{extension}/modules/#{extension}.so"]
end
end
def caveats
<<-CAVEATS.undent
The extensions are installed into:
#{php_extensions}
To use the module you'll need to include it in your php.ini:
extension=#{php_extensions}/pgsql.so
or use dl in your code:
dl('#{php_extensions}/pgsql.so');
By default, OS X doesn't include a php.ini so you can create one:
cp /etc/php.ini.default /etc/php.ini
This compiles the PostgreSQL extension from the PHP source, attempting to use Mac OS X's version of PHP (currently #{@version}). If you update Mac OS X and this changes the PHP version you may need to uninstall and re-install this forumula.
CAVEATS
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment