<commit_before><?php
namespace CLIFramework\Command;
use CLIFramework\Command;
use CLIFramework\Autoload\ComposerAutoloadGenerator;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use RuntimeException;
use Exception;
use Phar;
use CodeGen\Block;
use CodeGen\Expr\NewObjectExpr;
use CodeGen\Statement\UseStatement;
use CodeGen\Statement\FunctionCallStatement;
use CodeGen\Statement\AssignStatement;
use CodeGen\Statement\MethodCallStatement;
use CLIFramework\PharKit\PharGenerator;
use ReflectionClass;
use SplFileInfo;

function GetClassPath($class) {
    $refclass = new ReflectionClass($class);
    $path = $refclass->getFilename();
    return ltrim(str_replace(getcwd(), '', $path ), DIRECTORY_SEPARATOR);
}

/**
 * Build phar file from composer.json
 */
class BuildPharCommand extends Command
{
    public function brief()
    {
        return 'Build executable phar file from composer.json';
    }

    public function options($opts)
    {

        // append executable (bootstrap scripts, if it's not defined, it's just a library phar file.
        $opts->add('bootstrap?','bootstrap or executable php file')
            ;

        $opts->add('executable','make the phar file executable')
            ->isa('bool')
            ->defaultValue(true)
            ;

        $opts->add('c|compress?', 'compress type: gz, bz2')
            ->defaultValue('gz')
            ->validValues([ 'gz', 'bz2' ])
            ;

        $opts->add('no-compress', 'do not compress phar file.');

        $opts->add('add+', 'add a path respectively');

        $opts->add('exclude+' , 'exclude pattern');
        /*

        // optional classloader script (use Universal ClassLoader by default 
        $opts->add('classloader?','embed a classloader in phar file');


        $opts->add('lib+','library path');



        $opts->add('output:','output');
         */
    }

    public function arguments($args)
    {
        $args->add('composer-config')->isa('file');
        $args->add('phar-file');
    }

    public function execute($composerConfigFile, $pharFile = 'output.phar')
    {
        if (!extension_loaded('json')) {
            throw new RuntimeException('json extension is required.');
        }





        $pharGenerator = new PharGenerator($this->logger, $pharFile);
        $phar = $pharGenerator->getPhar();
        ini_set('phar.readonly', 0);
        $this->logger->info("Creating phar file $pharFile...");

        $phar->startBuffering();

        $stubs = [];
        if ($this->options->executable) {
            $this->logger->debug( 'Add shell bang...' );
            $stubs[] = "#!/usr/bin/env php";
        }
        // prepend open tag
        $stubs[] = '<?php';

        $this->logger->info("Setting up stub..." );
        $stubs[] = "Phar::mapPhar('$pharFile');";


        // Generate class loader stub
        $interfacePath = GetClassPath('Universal\\ClassLoader\\ClassLoader');
        $psr0path = GetClassPath('Universal\\ClassLoader\\Psr0ClassLoader');
        $psr4path = GetClassPath('Universal\\ClassLoader\\Psr4ClassLoader');
        $fileinfo = new SplFileInfo($psr0path);

        // $phar->buildFromDirectory($fileinfo->getPath());

        $phar->buildFromIterator(
            new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fileinfo->getPath())),
            getcwd()
        );

        $stubs[] = "require 'phar://$pharFile/$interfacePath';";
        $stubs[] = "require 'phar://$pharFile/$psr0path';";
        $stubs[] = "require 'phar://$pharFile/$psr4path';";

        if ($bootstrap = $this->options->bootstrap) {
            $this->logger->info("Add $bootstrap");
            $content = php_strip_whitespace($bootstrap);
            $content = preg_replace('{^#!/usr/bin/env\s+php\s*}', '', $content);
            $phar->addFromString($bootstrap, $content);

            $this->logger->info( "Adding bootstrap script: $bootstrap" );
            $stubs[] = "require 'phar://$pharFile/$bootstrap';";
        }

        $generator = new ComposerAutoloadGenerator;
        echo $generator->generate($composerConfigFile, $pharFile);

        $stubs[] = $generator->generate($composerConfigFile, $pharFile);


        $stubs[] = '__HALT_COMPILER();';
        $phar->setStub(join("\n",$stubs));


        // Include files in phar's root
        if ($adds = $this->options->add) {
            foreach ($adds as $add) {
                $phar->buildFromIterator(
                    new RecursiveIteratorIterator(new RecursiveDirectoryIterator($add)),
                    getcwd()
                );
            }
        }






        // Finish building...
        $phar->stopBuffering();

        $compressType = Phar::GZ;
        if ($this->options->{'no-compress'} ) {
            $compressType = null;
        } else if ($type = $this->options->compress) {
            switch ($type) {
            case 'gz':
                $compressType = Phar::GZ;
                break;
            case 'bz2':
                $compressType = Phar::BZ2;
                break;
            default:
                throw new Exception("Phar compression: $type is not supported, valid values are gz, bz2");
                break;
            }
        }
        if ($compressType) {
            $this->logger->info( "Compressing phar ..." );
            $phar->compressFiles($compressType);
        }

        $this->logger->info('Done');
    }

}



<commit_msg>try making php 5.3 compatbiel<commit_after><?php
namespace CLIFramework\Command;
use CLIFramework\Command;
use CLIFramework\Autoload\ComposerAutoloadGenerator;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use RuntimeException;
use Exception;
use Phar;
use CodeGen\Block;
use CodeGen\Expr\NewObjectExpr;
use CodeGen\Statement\UseStatement;
use CodeGen\Statement\FunctionCallStatement;
use CodeGen\Statement\AssignStatement;
use CodeGen\Statement\MethodCallStatement;
use CLIFramework\PharKit\PharGenerator;
use ReflectionClass;
use SplFileInfo;

function GetClassPath($class) {
    $refclass = new ReflectionClass($class);
    $path = $refclass->getFilename();
    return ltrim(str_replace(getcwd(), '', $path ), DIRECTORY_SEPARATOR);
}

/**
 * Build phar file from composer.json
 */
class BuildPharCommand extends Command
{
    public function brief()
    {
        return 'Build executable phar file from composer.json';
    }

    public function options($opts)
    {

        // append executable (bootstrap scripts, if it's not defined, it's just a library phar file.
        $opts->add('bootstrap?','bootstrap or executable php file')
            ;

        $opts->add('executable','make the phar file executable')
            ->isa('bool')
            ->defaultValue(true)
            ;

        $opts->add('c|compress?', 'compress type: gz, bz2')
            ->defaultValue('gz')
            ->validValues(array( 'gz', 'bz2'))
            ;

        $opts->add('no-compress', 'do not compress phar file.');

        $opts->add('add+', 'add a path respectively');

        $opts->add('exclude+' , 'exclude pattern');
        /*

        // optional classloader script (use Universal ClassLoader by default 
        $opts->add('classloader?','embed a classloader in phar file');


        $opts->add('lib+','library path');



        $opts->add('output:','output');
         */
    }

    public function arguments($args)
    {
        $args->add('composer-config')->isa('file');
        $args->add('phar-file');
    }

    public function execute($composerConfigFile, $pharFile = 'output.phar')
    {
        if (!extension_loaded('json')) {
            throw new RuntimeException('json extension is required.');
        }





        $pharGenerator = new PharGenerator($this->logger, $pharFile);
        $phar = $pharGenerator->getPhar();
        ini_set('phar.readonly', 0);
        $this->logger->info("Creating phar file $pharFile...");

        $phar->startBuffering();

        $stubs = array();
        if ($this->options->executable) {
            $this->logger->debug( 'Add shell bang...' );
            $stubs[] = "#!/usr/bin/env php";
        }
        // prepend open tag
        $stubs[] = '<?php';

        $this->logger->info("Setting up stub..." );
        $stubs[] = "Phar::mapPhar('$pharFile');";


        // Generate class loader stub
        $interfacePath = GetClassPath('Universal\\ClassLoader\\ClassLoader');
        $psr0path = GetClassPath('Universal\\ClassLoader\\Psr0ClassLoader');
        $psr4path = GetClassPath('Universal\\ClassLoader\\Psr4ClassLoader');
        $fileinfo = new SplFileInfo($psr0path);

        // $phar->buildFromDirectory($fileinfo->getPath());

        $phar->buildFromIterator(
            new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fileinfo->getPath())),
            getcwd()
        );

        $stubs[] = "require 'phar://$pharFile/$interfacePath';";
        $stubs[] = "require 'phar://$pharFile/$psr0path';";
        $stubs[] = "require 'phar://$pharFile/$psr4path';";

        if ($bootstrap = $this->options->bootstrap) {
            $this->logger->info("Add $bootstrap");
            $content = php_strip_whitespace($bootstrap);
            $content = preg_replace('{^#!/usr/bin/env\s+php\s*}', '', $content);
            $phar->addFromString($bootstrap, $content);

            $this->logger->info( "Adding bootstrap script: $bootstrap" );
            $stubs[] = "require 'phar://$pharFile/$bootstrap';";
        }

        $generator = new ComposerAutoloadGenerator;
        echo $generator->generate($composerConfigFile, $pharFile);

        $stubs[] = $generator->generate($composerConfigFile, $pharFile);


        $stubs[] = '__HALT_COMPILER();';
        $phar->setStub(join("\n",$stubs));


        // Include files in phar's root
        if ($adds = $this->options->add) {
            foreach ($adds as $add) {
                $phar->buildFromIterator(
                    new RecursiveIteratorIterator(new RecursiveDirectoryIterator($add)),
                    getcwd()
                );
            }
        }






        // Finish building...
        $phar->stopBuffering();

        $compressType = Phar::GZ;
        if ($this->options->{'no-compress'} ) {
            $compressType = null;
        } else if ($type = $this->options->compress) {
            switch ($type) {
            case 'gz':
                $compressType = Phar::GZ;
                break;
            case 'bz2':
                $compressType = Phar::BZ2;
                break;
            default:
                throw new Exception("Phar compression: $type is not supported, valid values are gz, bz2");
                break;
            }
        }
        if ($compressType) {
            $this->logger->info( "Compressing phar ..." );
            $phar->compressFiles($compressType);
        }

        $this->logger->info('Done');
    }

}




{'fraction_non_alphanumeric': 0.15245192750023, 'fraction_numerical': 0.0025761339589658663, 'mean_word_length': 2.3009413908290313, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 113, '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': '2641849', 'n_tokens_mistral': 3256, 'n_tokens_neox': 3092, 'n_words': 623}