<commit_before><?php

/*
 * This file is part of the PHP Bench package
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

require_once __DIR__ . '/../lib/Container.php';

use PhpBench\Container;

$configPaths = array();
$container = new Container();

foreach ($argv as $arg) {
    if (0 === strpos($arg, '--config=')) {
        $configFile = substr($arg, 9);
        if (!file_exists($configFile)) {
            echo sprintf('Config file "%s" does not exist', $configFile) . PHP_EOL;
            exit(1);
        }
        $configPaths = array($configFile);
    }
}

if (empty($configPaths)) {
    $configPaths = array(
        getcwd() . '/phpbench.json',
        getcwd() . '/phpbench.dist.json',
    );
}

$hasBootstrap = false;
$config = array();
foreach ($configPaths as $configPath) {
    if (!file_exists($configPath)) {
        continue;
    }

    $configDir = dirname($configPath);
    $config = json_decode(file_get_contents($configPath), true);

    if (null === $config) {
        echo sprintf('Could not decode configuration file into JSON "%s"',
            $configPath
        );
        exit(1);
    }

    if (isset($config['path'])) {
        // prepend config dir to path if it is non-relative
        if (substr($config['path'], 0, 1) !== '/') {
            $config['path'] = $configDir . DIRECTORY_SEPARATOR . $config['path'];
        }
    }

    $config['config_path'] = $configPath;

    if (isset($config['bootstrap'])) {
        $bootstrap = realpath($configDir . DIRECTORY_SEPARATOR . $config['bootstrap']);
        if (!file_exists($bootstrap)) {
            echo sprintf('Bootstrap file "%s" was not found',
                $bootstrap
            );
            exit(1);
        }
        require_once($bootstrap);
        $hasBootstrap = true;
    }
    break;
}

if (false === $hasBootstrap) {
    $bootstrapPath = getcwd() . '/vendor/autoload.php';

    if (!file_exists($bootstrapPath)) {
        echo sprintf('Autoload file "%s" does not exist. Maybe you need to do a composer install?', $bootstrapPath) . PHP_EOL;
        exit(1);
    }

    require_once $bootstrapPath;
}

$container->configure();
$container->mergeParameters($config);
$container->build($config);
$container->get('console.application')->run();<commit_msg>Use seld/jsonlint to validate the JSON file<commit_after><?php

/*
 * This file is part of the PHP Bench package
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

require_once __DIR__ . '/../lib/Container.php';

use PhpBench\Container;

$configPaths = array();
$container = new Container();

foreach ($argv as $arg) {
    if (0 === strpos($arg, '--config=')) {
        $configFile = substr($arg, 9);
        if (!file_exists($configFile)) {
            echo sprintf('Config file "%s" does not exist', $configFile) . PHP_EOL;
            exit(1);
        }
        $configPaths = array($configFile);
    }
}

if (empty($configPaths)) {
    $configPaths = array(
        getcwd() . '/phpbench.json',
        getcwd() . '/phpbench.dist.json',
    );
}

$hasBootstrap = false;
$invalidJson = false;

$config = array();
foreach ($configPaths as $configPath) {
    if (!file_exists($configPath)) {
        continue;
    }

    $configDir = dirname($configPath);
    $configBody = file_get_contents($configPath);
    $config = json_decode($configBody, true);

    if (null === $config) {
        $invalidJson = true;
        break;
    }

    if (null === $config) {
        echo sprintf('Could not decode configuration file into JSON "%s"',
            $configPath
        );
        exit(1);
    }

    if (isset($config['path'])) {
        // prepend config dir to path if it is non-relative
        if (substr($config['path'], 0, 1) !== '/') {
            $config['path'] = $configDir . DIRECTORY_SEPARATOR . $config['path'];
        }
    }

    $config['config_path'] = $configPath;

    if (isset($config['bootstrap'])) {
        $bootstrap = realpath($configDir . DIRECTORY_SEPARATOR . $config['bootstrap']);
        if (!file_exists($bootstrap)) {
            echo sprintf('Bootstrap file "%s" was not found',
                $bootstrap
            );
            exit(1);
        }
        require_once($bootstrap);
        $hasBootstrap = true;
    }
    break;
}

if (false === $hasBootstrap) {
    $bootstrapPath = getcwd() . '/vendor/autoload.php';

    if (!file_exists($bootstrapPath)) {
        echo sprintf('Autoload file "%s" does not exist. Maybe you need to do a composer install?', $bootstrapPath) . PHP_EOL;
        exit(1);
    }

    require_once $bootstrapPath;
}

if ($invalidJson) {
    // if we failed to parse the configuration file, attempt to parse it
    try {
        $parser = new Seld\JsonLint\JsonParser();
        $parser->parse($configBody);
    } catch (Seld\JsonLint\ParsingException $e) {
        echo 'Error parsing config file:' . PHP_EOL . PHP_EOL;
        echo $e->getMessage();
        exit(1);
    }
}

$container->configure();
$container->mergeParameters($config);
$container->build();
$container->get('console.application')->run();
{'fraction_non_alphanumeric': 0.15658295281582954, 'fraction_numerical': 0.0032343987823439876, 'mean_word_length': 2.598220396988364, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '5583402', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1583, 'n_words': 405}