<commit_before><?php
/**
 * Created by PhpStorm.
 * User: drzippie
 * Date: 28/05/16
 * Time: 11:19
 */

namespace Ximdex\Setup\Step;


use Ximdex\Setup\Manager;

class System extends Base
{
    public function __construct(Manager $manager)
    {
        parent::__construct($manager);

        $this->label = "System Check";
        $this->template = "system.twig";
        $this->title = "System Requirements";
        $this->vars['title'] = $this->title;
    }

    public function checkErrors()
    {
        parent::checkErrors();

        // file_permisions
        $this->checkPHPVersion( '5.6') ;
        $this->checkRequiredPHPExtensions();
        $this->CheckPermissions() ;
        $this->checkRequiredPackages();

    }

    /**
     * Methods to check
     */
    private function checkPHPVersion( $min  )
    {
        if (phpversion() <= $min) {
            $this->addError(
                sprintf("PHP %s or greater is required", $min),
                sprintf("PHP %s or greater is required. Please upgrade the PHP version", $min),
                "PHP Version"
            );

        }
    }
    private function checkRequiredPHPExtensions(  )
    {
            $current = array_merge( get_loaded_extensions());
            $required = [
                'xsl',
                'curl',
                'gd',
                'mcrypt',
                'PDO',
                'pdo_mysql'
            ];

            foreach( $required as $item ) {

                if (!in_array( $item, $current )) {
                    $this->addError(
                        sprintf("PHP %s extension is required", $item),
                        sprintf("PHP %s extension is required", $item),
                        "PHP Ext"
                    );
                }

            }




    }
    private  function checkRequiredPackages()
    {
        $res = null ;
        @exec('openssl version', $res);
        if (empty($res) ) {
            $tool = 'OpenSSL' ;
            $this->addError(
                sprintf("Tool %s is required", $tool ),
                sprintf("%s package is needed. Please install the package on your system.", $tool  ),
                "Tools"
            );
          }
    }

    private function CheckPermissions()
    {
        $installRoot = $this->manager->getInstallRoot();
        $dirsToCheck = [
            '/conf/',
            '/data',
            '/data/cache',
            '/data/cache/pipelines',
            '/data/files',
            '/data/nodes',
            '/data/sync',
            '/data/sync/serverframes',
            '/data/tmp',
            '/data/tmp/templates_c',
            '/data/tmp/js',
            '/logs',
        ];
        foreach ($dirsToCheck as $dir) {
            $fullDir = $installRoot . $dir;
            if  (!is_dir( $fullDir )) {
                $isValid =  mkdir( $fullDir, true );
                if ( !$isValid) {
                    $this->addError(<commit_msg>change path checkPermissions<commit_after><?php
/**
 * Created by PhpStorm.
 * User: drzippie
 * Date: 28/05/16
 * Time: 11:19
 */

namespace Ximdex\Setup\Step;


use Ximdex\Setup\Manager;

class System extends Base
{
    public function __construct(Manager $manager)
    {
        parent::__construct($manager);

        $this->label = "System Check";
        $this->template = "system.twig";
        $this->title = "System Requirements";
        $this->vars['title'] = $this->title;
    }

    public function checkErrors()
    {
        parent::checkErrors();

        // file_permisions
        $this->checkPHPVersion( '5.6') ;
        $this->checkRequiredPHPExtensions();
        $this->CheckPermissions() ;
        $this->checkRequiredPackages();

    }

    /**
     * Methods to check
     */
    private function checkPHPVersion( $min  )
    {
        if (phpversion() <= $min) {
            $this->addError(
                sprintf("PHP %s or greater is required", $min),
                sprintf("PHP %s or greater is required. Please upgrade the PHP version", $min),
                "PHP Version"
            );

        }
    }
    private function checkRequiredPHPExtensions(  )
    {
            $current = array_merge( get_loaded_extensions());
            $required = [
                'xsl',
                'curl',
                'gd',
                'mcrypt',
                'PDO',
                'pdo_mysql'
            ];

            foreach( $required as $item ) {

                if (!in_array( $item, $current )) {
                    $this->addError(
                        sprintf("PHP %s extension is required", $item),
                        sprintf("PHP %s extension is required", $item),
                        "PHP Ext"
                    );
                }

            }




    }
    private  function checkRequiredPackages()
    {
        $res = null ;
        @exec('openssl version', $res);
        if (empty($res) ) {
            $tool = 'OpenSSL' ;
            $this->addError(
                sprintf("Tool %s is required", $tool ),
                sprintf("%s package is needed. Please install the package on your system.", $tool  ),
                "Tools"
            );
          }
    }

    private function CheckPermissions()
    {
        $installRoot = $this->manager->getInstallRoot();
        $dirsToCheck = [
            '/conf/',
            '/data/',
            '/data/cache/',
            '/data/cache/pipelines/',
            '/data/files/',
            '/data/nodes/',
            '/data/sync/',
            '/data/sync/serverframes/',
            '/data/tmp/',
            '/data/tmp/templates_c/',
            '/data/tmp/js/',
            '/logs/',
        ];
        foreach ($dirsToCheck as $dir) {
            $fullDir = $installRoot . $dir;
            if  (!is_dir( $fullDir )) {
                $isValid =  mkdir( $fullDir, true );
                if ( !$isValid) {
                    $this->addError(
{'fraction_non_alphanumeric': 0.13532187394674755, 'fraction_numerical': 0.004044489383215369, 'mean_word_length': 1.3402996845425867, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, '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': '4017270', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1604, 'n_words': 355}