<commit_before>require 'formula'
require File.join(File.dirname(__FILE__), 'abstract-php-version')

class UnsupportedPhpApiError < RuntimeError
  def initialize
    super "Unsupported PHP API Version"
  end
end

class InvalidPhpizeError < RuntimeError
  def initialize(installed_php_version, required_php_version)
    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(*)
    super

    if build.without? 'homebrew-php'
      installed_php_version = nil
      i = IO.popen("#{phpize} -v")
      out = i.readlines.join("")
      i.close
      { 53 => 20090626, 54 => 20100412, 55 => 20121113 }.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
  end

  def self.init
    depends_on 'autoconf' => :build

    option 'without-homebrew-php', "Ignore homebrew PHP and use default instead"
    option 'without-config-file', "Do not install extension config file"
  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.without? 'homebrew-php'
      "phpize"
    else
      "#{(Formula[php_formula]).bin}/phpize"
    end
  end

  def phpini
    if build.without? 'homebrew-php'
      "php.ini presented by \"php --ini\""
    else
      "#{(Formula[php_formula]).config_path}/php.ini"
    end
  end

  def phpconfig
    if build.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}]<commit_msg>Detect branch and extension names from nested constants<commit_after>require 'formula'
require File.join(File.dirname(__FILE__), 'abstract-php-version')

class UnsupportedPhpApiError < RuntimeError
  def initialize
    super "Unsupported PHP API Version"
  end
end

class InvalidPhpizeError < RuntimeError
  def initialize(installed_php_version, required_php_version)
    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(*)
    super

    if build.without? 'homebrew-php'
      installed_php_version = nil
      i = IO.popen("#{phpize} -v")
      out = i.readlines.join("")
      i.close
      { 53 => 20090626, 54 => 20100412, 55 => 20121113 }.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
  end

  def self.init
    depends_on 'autoconf' => :build

    option 'without-homebrew-php', "Ignore homebrew PHP and use default instead"
    option 'without-config-file', "Do not install extension config file"
  end

  def php_branch
    class_name = self.class.name.split("::").last
    matches = /^Php5([3-9]+)/.match(class_name)
    if matches
      "5." + matches[1]
    else
      raise "Unable to guess PHP branch for #{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.without? 'homebrew-php'
      "phpize"
    else
      "#{(Formula[php_formula]).bin}/phpize"
    end
  end

  def phpini
    if build.without? 'homebrew-php'
      "php.ini presented by \"php --ini\""
    else
      "#{(Formula[php_formula]).config_path}/php.ini"
    end
  end

  def phpconfig
    if build.without? 'homebrew-php'
      ""
    else
      "--with-php-config=#{(Formula[php_formula]).bin}/php-config"
    end
  end

  def extension
    class_name = self.class.name.split("::").last
    matches = /^Php5[3-9](.+)/.match(class_name)
    if matches
      matches[1].downcase
    else
      raise "Unable to guess PHP extension name for #{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}]
{'fraction_non_alphanumeric': 0.11844390413337964, 'fraction_numerical': 0.013546370267453978, 'mean_word_length': 2.8859649122807016, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5811447', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1875, 'n_words': 458}