package Cpanel::Easy::PHP5::Opcache;

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

use strict;

use Cpanel::FileUtils ();
use Cpanel::PHPINI    ();

our $easyconfig = {
    'name'          => 'Opcache',
    'note'          => 'Zend Opcode caching extension',
    'verify_on'     => 'Zend Opcache is incompatible with XCache',
    'url'           => 'http://www.php.net/manual/en/ref.opcache.php',
    'when_i_am_off' => sub {
        my ($self) = @_;

        $self->add_to_configure( { '--disable-opcache' => '' }, 'Cpanel::Easy::PHP5' );
        my $ini_file = '/usr/local/lib/php.ini';
        if ( -e $ini_file ) {

            # Delete the line to load the module, comment the lines
            # with opcache directives to preserve any previous
            # configuration
            Cpanel::FileUtils::regex_rep_file(
                $ini_file,
                {
                    qr{^\s*zend_extension(?:_ts)?\s*=\s*"?\S*opcache(?:_TS)?\.so"?\s*.*$}is => q{},
                    qr{^(\s*opcache\..*)$}is                                                => q{; $1},
                },
                {},
            );
        }
    },
    'step' => {
        '0' => {
            'name'    => 'to config options',
            'command' => sub {
                my ($self) = @_;
                $self->add_to_configure( { '--enable-opcache' => '', } );
                return ( 1, 'Ok' );
            },
        },
        '1' => {
            'name'    => 'after build php.ini setup',
            'command' => sub {
                my ($self) = @_;
                return ( 1, 'Ok' ) if $self->get_param('makecpphp');

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

                    my $append  = '';
                    my $ext_dir = Cpanel::PHPINI::get_default_extension_dir('/usr/local');

                    unless ( $self->is_string_in_file( '/usr/local/lib/php.ini', qr/\A\s*(?:;\s*)*zend_extension\s*=\s*"?(?:$ext_dir\/)?opcache\.so"?\s*/ ) ) {
                        $append .= qq(zend_extension="$ext_dir/opcache.so"\n);
                    }

                    unless ( $self->is_string_in_file( '/usr/local/lib/php.ini', qr/\A\s*(?:;\s*)*opcache\./ ) ) {
                        $append .= <<EOF;

; PHP Recommended Opcache settings
; See http://php.net/manual/en/opcache.installation.php

; opcache.memory_consumption=128
; opcache.interned_strings_buffer=8
; opcache.max_accelerated_files=4000
; opcache.revalidate_freq=60
; opcache.fast_shutdown=1
; opcache.enable_cli=1
EOF
                    }

                    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' );
                };

                my ( $v, $spec ) = $self->get_php_version();
                return $self->add_to_modify_later_queue(
                    'Cpanel::Easy::PHP5::' . $spec,
                    {
                        'step' => {
                            '15.50' => {
                                'name'    => 'Opcache to php.ini',
                                'command' => $command,
                            },
                        }
                    },
                );
            },
        },
    },
};

1;

