package Cpanel::Easy::PHP5::PDO;

# cpanel - Cpanel/Easy/PHP5/PDO.pm                Copyright(c) 2014 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use Cpanel::Version::Compare ();

our $easyconfig = {
    'name' => 'PDO',
    'note' => q{Includes SQLite support. MySQL support is available.},

    # Cpanel::Easy::PHP5::SQLite3 is "reverse" opt mod so it's value must be 0 in order to have this functionality
    'depends'       => { 'optmods'                     => { 'Cpanel::Easy::PHP5::SQLite3' => 0, }, },
    'implies'       => { 'Cpanel::Easy::PHP5::SQLite3' => 0, },
    'url'           => 'http://us.php.net/manual/en/book.pdo.php',
    'when_i_am_off' => sub {
        my $self = shift;

        $self->add_to_configure( { '--disable-pdo' => '' } );

        if ( !$self->get_param('makecpphp') ) {
            if ( -e '/usr/local/lib/php.ini' ) {
                Cpanel::FileUtils::regex_rep_file(
                    '/usr/local/lib/php.ini',
                    { qr{^\s*extension\s*=\s*"?pdo\.so"?\s*$}is => q{}, qr{^\s*extension\s*=\s*"?pdo_sqlite\.so"?\s*$}is => q{}, qr{^\s*extension\s*=\s*"?sqlite\.so"?\s*$}is => q{} }, {},
                );
            }
        }
    },
    'step' => {
        '0' => {
            'name'    => 'to config options',
            'command' => sub {
                my ($self) = @_;

                # can't be shared if versioning is in use
                $self->{'__'}{'shared'} = $self->get_ns_value_from_profile('Cpanel::Easy::PHP5::Versioning') ? '' : 'shared';
                $self->add_to_configure( { '--enable-pdo' => $self->{'__'}{'shared'}, '--with-pdo-sqlite' => $self->{'__'}{'shared'} } );
                my ( $v, $spec ) = $self->get_php_version();
                if ( Cpanel::Version::Compare::compare( $v, '<', '5.4' ) ) {
                    $self->add_to_configure( { '--with-sqlite' => $self->{'__'}{'shared'} } );
                }
                return ( 1, 'ok' );
            },
        },
        '1' => {
            'name'    => 'after build php.ini setup',
            'command' => sub {
                my ($self) = @_;
                return ( 1, 'Ok' ) if $self->get_param('makecpphp');

                # dont activate extensions if compiled static
                return ( 1, 'Ok' ) unless $self->{'__'}{'shared'} eq 'shared';

                my ( $v, $spec ) = $self->get_php_version();

                my $command = sub {
                    my ($self) = @_;

                    # add extension=pdo.so and extension=pdo_sqlite.so extension=sqlite.so to php.ini
                    # sqlite.so not required if PHP version >= 5.4

                    my $append = '';
                    $self->set_phpini_object_key_from('/usr/local/lib/php.ini');

                    my @so = qw(pdo.so pdo_sqlite.so);
                    push @so, 'sqlite.so' if Cpanel::Version::Compare::compare( $v, '<', '5.4' );

                  EXT:
                    for my $new (@so) {
                        next EXT if grep / \A \Q$new\E \z /xms, @{ $self->{'_'}{'php_ini'}{'extension'} };
                        $append .= "extension=$new\n";
                    }

                    if ($append) {
                        if ( open my $ini_fh, '>>', '/usr/local/lib/php.ini' ) {
                            print {$ini_fh} $append;
                            close $ini_fh;
                        }
                        else {
                            $self->print_alert( q{Could not append '[_1]' with '[_2]': [_3]}, '/usr/local/lib/php.ini', $append, $! );
                        }
                    }

                    return ( 1, 'ok' );
                };

                return $self->add_to_modify_later_queue(
                    'Cpanel::Easy::PHP5::' . $spec,
                    {
                        'step' => {
                            '15.40' => {
                                'name'    => 'PDO to php.ini',
                                'command' => $command,
                            },
                        }
                    },
                );
            },
        },
    },
};

1;
