<commit_before><?php
/**
 *  * Created by mtils on 25.08.19 at 08:49.
 **/

namespace Ems\Skeleton;


use Ems\Console\ConsoleInputConnection;
use Ems\Console\ConsoleOutputConnection;
use Ems\Contracts\Core\ConnectionPool as ConnectionPoolContract;
use Ems\Contracts\Core\InputConnection;
use Ems\Contracts\Core\IO;
use Ems\Contracts\Core\OutputConnection;
use Ems\Contracts\Core\Url;
use Ems\Core\Application;
use Ems\Core\Connection\GlobalsHttpInputConnection;
use Ems\Core\Connection\StdOutputConnection;
use Ems\Core\ConnectionPool;
use Ems\Core\Skeleton\Bootstrapper;
use Ems\Core\Support\StreamLogger;
use function file_exists;
use function getenv;
use function memory_get_peak_usage;
use function php_sapi_name;
use function register_shutdown_function;


class SkeletonBootstrapper extends Bootstrapper
{
    public function bind()
    {
        $this->app->bind(InputConnection::class, function () {
            /** @var ConnectionPoolContract $connectionPool */
            $connectionPool = $this->app->make(ConnectionPoolContract::class);
            return $connectionPool->connection(ConnectionPool::STDIN);
        });

        $this->app->bind(OutputConnection::class, function () {
            /** @var ConnectionPoolContract $connectionPool */
            $connectionPool = $this->app->make(ConnectionPoolContract::class);
            return $connectionPool->connection(ConnectionPool::STDOUT);
        });

        $this->app->afterResolving(ConnectionPool::class, function ($pool) {
            $this->addConnections($pool);
        });

        $this->installBenchmarkPrinter();
    }

    protected function addConnections(ConnectionPool $pool)
    {
        $pool->extend(ConnectionPool::STDIN, function (Url $url) {
            if (!$url->equals(ConnectionPool::STDIN)) {
                return null;
            }

            if (php_sapi_name() == 'cli') {
                return $this->app->make(ConsoleInputConnection::class);
            }

            return $this->app->make(GlobalsHttpInputConnection::class);
        });

        $pool->extend(ConnectionPool::STDOUT, function (Url $url) {
            if (!$url->equals(ConnectionPool::STDOUT)) {
                return null;
            }
            if (php_sapi_name() == 'cli') {
                return $this->app->make(ConsoleOutputConnection::class);
            }
            return $this->app->make(StdOutputConnection::class);
        });

        $pool->extend(ConnectionPool::STDERR, function (Url $url) {
            if ($url->equals(ConnectionPool::STDERR)) {
                return $this->createLogger();
            }
            return null;
        });
    }

    protected function createLogger()
    {
        /** @var Application $app */
        $app = $this->app->make('app');
        if ($app->environment() != 'production') {
            return new StreamLogger('php://stdout');
        }

        $logPath = $this->app->make('app')->path('local/log/app.log');

        if (!file_exists($logPath)) {
            return new StreamLogger('php://stderr');
        }

        return new StreamLogger($logPath);
    }

    protected function installBenchmarkPrinter()
    {
        if(!getenv('EMS_BENCHMARK')) {
            return;
        }

        register_shutdown_function(function () {
            /** @var IO $io */
            $io = $this->app->make(IO::class);
            $usage = memory_get_peak_usage(true);
            $usageOutput = $this->memoryFormat($usage);
            $io->log()->debug("Usage: $usageOutput");
        });
    }

    function memoryFormat($size) {
        $unit=array('B','KB','MB','GB','TB','PB');
        return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
    }
}<commit_msg>Moved binding of connections outside of IO<commit_after><?php
/**
 *  * Created by mtils on 25.08.19 at 08:49.
 **/

namespace Ems\Skeleton;


use Ems\Console\ConsoleInputConnection;
use Ems\Console\ConsoleOutputConnection;
use Ems\Contracts\Core\InputConnection;
use Ems\Contracts\Core\IO;
use Ems\Contracts\Core\OutputConnection;
use Ems\Contracts\Core\Url;
use Ems\Core\Application;
use Ems\Core\Connection\GlobalsHttpInputConnection;
use Ems\Core\Connection\StdOutputConnection;
use Ems\Core\ConnectionPool;
use Ems\Core\Skeleton\Bootstrapper;
use Ems\Core\Support\StreamLogger;
use function file_exists;
use function getenv;
use function memory_get_peak_usage;
use function php_sapi_name;
use function register_shutdown_function;


class SkeletonBootstrapper extends Bootstrapper
{
    public function bind()
    {
        $this->app->bind(InputConnection::class, function () {
            return $this->createInputConnection();
        }, true);

        $this->app->bind(OutputConnection::class, function () {
            return $this->createOutputConnection();
        }, true);

        $this->app->afterResolving(ConnectionPool::class, function ($pool) {
            $this->addConnections($pool);
        });

        $this->installBenchmarkPrinter();
    }

    /**
     * @return InputConnection|object
     */
    protected function createInputConnection()
    {
        if (php_sapi_name() == 'cli') {
            return $this->app->make(ConsoleInputConnection::class);
        }

        return $this->app->make(GlobalsHttpInputConnection::class);
    }

    /**
     * @return OutputConnection|object
     */
    protected function createOutputConnection()
    {
        if (php_sapi_name() == 'cli') {
            return $this->app->make(ConsoleOutputConnection::class);
        }
        return $this->app->make(StdOutputConnection::class);
    }

    /**
     * @param ConnectionPool $pool
     */
    protected function addConnections(ConnectionPool $pool)
    {
        $pool->extend(ConnectionPool::STDIN, function (Url $url) {
            if (!$url->equals(ConnectionPool::STDIN)) {
                return null;
            }
            return $this->app->make(InputConnection::class);
        });

        $pool->extend(ConnectionPool::STDOUT, function (Url $url) {
            if (!$url->equals(ConnectionPool::STDOUT)) {
                return null;
            }
            return $this->app->make(OutputConnection::class);
        });

        $pool->extend(ConnectionPool::STDERR, function (Url $url) {
            if ($url->equals(ConnectionPool::STDERR)) {
                return $this->createLogger();
            }
            return null;
        });
    }

    /**
     * @return StreamLogger
     */
    protected function createLogger()
    {
        /** @var Application $app */
        $app = $this->app->make('app');
        if ($app->environment() != 'production') {
            return new StreamLogger('php://stdout');
        }

        $logPath = $this->app->make('app')->path('local/log/app.log');

        if (!file_exists($logPath)) {
            return new StreamLogger('php://stderr');
        }

        return new StreamLogger($logPath);
    }

    protected function installBenchmarkPrinter()
    {
        if(!getenv('EMS_BENCHMARK')) {
            return;
        }

        register_shutdown_function(function () {
            /** @var IO $io */
            $io = $this->app->make(IO::class);
            $usage = memory_get_peak_usage(true);
            $usageOutput = $this->memoryFormat($usage);
            $io->log()->debug("Usage: $usageOutput");
        });
    }

    protected function memoryFormat($size)
    {
        $unit=['B','KB','MB','GB','TB','PB'];
        return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[(int)$i];
    }
}
{'fraction_non_alphanumeric': 0.14777086916258764, 'fraction_numerical': 0.0050271199894165895, 'mean_word_length': 2.508120649651972, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 83, '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': '4880214', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2129, 'n_words': 362}