<commit_before><?php
/**
 * Pagon Framework
 *
 * @package   Pagon
 * @author    <NAME> <<EMAIL>>
 * @copyright (c) 2011 - 2013 Pagon Framework
 */

namespace Pagon;

const VERSION = '0.8.0';

// Depend Fiber
if (!class_exists('Fiber')) {
    require __DIR__ . '/Fiber.php';
}

// Depend Emitter
if (!class_exists('EventEmitter')) {
    require __DIR__ . '/EventEmitter.php';
}

/**
 * App
 * The core of Pagon
 *
 * @package Pagon
 *
 * @property Http\Input|Cli\Input   input        Input of Application
 * @property Http\Output|Cli\Output output       Output of Application
 * @property Router                 router       Router of Application
 * @property array                  locals       The locals variables to used in template
 * @property string                 mode         Current run mode, Default is "develop"
 * @property bool                   debug        Is debug mode enabled?
 * @property string                 views        Views dir
 * @property bool                   error        Handle error
 * @property bool                   buffer       Buffer enabled?
 * @property string                 timezone     Current timezone
 * @property string                 charset      The application charset
 * @property array                  routes       Store the routes for run
 * @property array                  prefixes     The prefixes to mapping the route namespace
 * @property array                  names        Routes names
 * @property string                 autoload     The path to autoload
 * @property array                  namespaces   The namespaces to load with directory path
 * @property array                  alias        The class to alias
 * @property array                  engines      The engines to render template
 * @property array                  errors       Default error handles
 * @property array                  stacks       The middleware stacks to load
 * @property array                  bundles      The bundles to load
 */
class App extends EventEmitter
{
    /**
     * @var Config
     */
    protected $injectors = array(
        'mode'       => 'develop',
        'debug'      => false,
        'views'      => false,
        'error'      => true,
        'routes'     => array(),
        'prefixes'   => array(),
        'names'      => array(),
        'buffer'     => true,
        'timezone'   => 'UTC',
        'charset'    => 'UTF-8',
        'autoload'   => null,
        'alias'      => array(),
        'namespaces' => array(),
        'engines'    => array(
            'jade' => 'Jade'
        ),
        'errors'     => array(
            '404'       => array(404, 'Request path not found'),
            'exception' => array(500, 'Error occurred'),
            'crash'     => array(500, 'Application crashed')
        ),
        'stacks'     => array(),
        'mounts'     => array('/' => ''),
        'bundles'    => array(),
        'locals'     => array(),
        'safe_query' => true,
        'input'      => null,
        'output'     => null,
    );

    /**
     * @var Http\Input|Cli\Input
     */
    public $input;

    /**
     * @var Http\Output|Cli\Output
     */
    public $output;

    /**
     * @var bool Is run?
     */
    private $_run = false;

    /**
     * @var bool Is cli?
     */
    private static $_cli = null;

    /**
     * @var App The top app
     */
    protected static $self;

    /**
     * @var array The file has loads
     */
    protected static $loads = array();

    /**
     * Create app
     *
     * @param array $config
     * @return App
     */
    public static function create($config = array())
    {
        // Is cli
        if (is_null(self::$_cli)) self::$_cli = PHP_SAPI == 'cli';

        $app = new self($config);

        // Set IO depends the run mode
        if (!self::$_cli) {
            $app->input = new Http\Input(array('app' => $app));
            $app->output = new Http\Output(array('app' => $app));
        } else {
            $app->input = new Cli\Input(array('app' => $app));
            $app->output = new Cli\Output(array('app' => $app));
        }

        // Init Route
        $app->router = new Router(array('path' => $app->input->path(), 'app' => $app));

        return $app;
    }

    /**
     * Return current app
     *
     * @throws \RuntimeException
     * @return App
     */
    public static function self()
    {
        if (!self::$self) {
            throw new \RuntimeException("There is no running App exists");
        }

        return self::$self;
    }

    /**
     * App init
     *
     * @param array|string $config
     * @throws \RuntimeException
     * @return App
     */
    public function __construct($config = array())
    {
        // Register shutdown
        register_shutdown_function(array($this, '__shutdown'));

        // Register autoload
        spl_autoload_register(array($this, '__autoload'));

        // Set config
        $this->injectors =
            (!is_array($config) ? Parser::load((string)$config) : $config)
            + (!empty($this->injectors['cli']) ? array('buffer' => false) : array())
            + $this->injectors;

        // Set cli mode
        if (!isset($this->injectors['cli'])) $this->injectors['cli'] = self::$_cli;

        // Set default locals
        $this->injectors['locals']['config'] = & $this->injectors;

        // Set mode
        $this->injectors['mode'] = ($_mode = getenv('PAGON_ENV')) ? $_mode : $this->injectors['mode'];

        // Set pagon root directory
        $this->injectors['mounts']['pagon'] = dirname(dirname(__DIR__));

        $this->input = & $this->injectors['input'];
        $this->output = & $this->injectors['output'];
    }

    /**
     * Get or set id
     *
     * @param string|\Closure $id
     * @return mixed
     */
    public function id($id = null)
    {
        return $this->input->id($id);
    }

    /**
     * Check if cli
     *
     * @return bool
     */
    public function cli()
    {<commit_msg>Reduce the router injector call<commit_after><?php
/**
 * Pagon Framework
 *
 * @package   Pagon
 * @author    <NAME> <<EMAIL>>
 * @copyright (c) 2011 - 2013 Pagon Framework
 */

namespace Pagon;

const VERSION = '0.8.0';

// Depend Fiber
if (!class_exists('Fiber')) {
    require __DIR__ . '/Fiber.php';
}

// Depend Emitter
if (!class_exists('EventEmitter')) {
    require __DIR__ . '/EventEmitter.php';
}

/**
 * App
 * The core of Pagon
 *
 * @package Pagon
 *
 * @property Http\Input|Cli\Input   input        Input of Application
 * @property Http\Output|Cli\Output output       Output of Application
 * @property Router                 router       Router of Application
 * @property array                  locals       The locals variables to used in template
 * @property string                 mode         Current run mode, Default is "develop"
 * @property bool                   debug        Is debug mode enabled?
 * @property string                 views        Views dir
 * @property bool                   error        Handle error
 * @property bool                   buffer       Buffer enabled?
 * @property string                 timezone     Current timezone
 * @property string                 charset      The application charset
 * @property array                  routes       Store the routes for run
 * @property array                  prefixes     The prefixes to mapping the route namespace
 * @property array                  names        Routes names
 * @property string                 autoload     The path to autoload
 * @property array                  namespaces   The namespaces to load with directory path
 * @property array                  alias        The class to alias
 * @property array                  engines      The engines to render template
 * @property array                  errors       Default error handles
 * @property array                  stacks       The middleware stacks to load
 * @property array                  bundles      The bundles to load
 */
class App extends EventEmitter
{
    /**
     * @var Config
     */
    protected $injectors = array(
        'mode'       => 'develop',
        'debug'      => false,
        'views'      => false,
        'error'      => true,
        'routes'     => array(),
        'prefixes'   => array(),
        'names'      => array(),
        'buffer'     => true,
        'timezone'   => 'UTC',
        'charset'    => 'UTF-8',
        'autoload'   => null,
        'alias'      => array(),
        'namespaces' => array(),
        'engines'    => array(
            'jade' => 'Jade'
        ),
        'errors'     => array(
            '404'       => array(404, 'Request path not found'),
            'exception' => array(500, 'Error occurred'),
            'crash'     => array(500, 'Application crashed')
        ),
        'stacks'     => array(),
        'mounts'     => array('/' => ''),
        'bundles'    => array(),
        'locals'     => array(),
        'safe_query' => true,
        'input'      => null,
        'output'     => null,
        'router'     => null,
    );

    /**
     * @var Http\Input|Cli\Input
     */
    public $input;

    /**
     * @var Http\Output|Cli\Output
     */
    public $output;

    /**
     * @var Router
     */
    public $router;

    /**
     * @var bool Is run?
     */
    private $_run = false;

    /**
     * @var bool Is cli?
     */
    private static $_cli = null;

    /**
     * @var App The top app
     */
    protected static $self;

    /**
     * @var array The file has loads
     */
    protected static $loads = array();

    /**
     * Create app
     *
     * @param array $config
     * @return App
     */
    public static function create($config = array())
    {
        // Is cli
        if (is_null(self::$_cli)) self::$_cli = PHP_SAPI == 'cli';

        $app = new self($config);

        // Set IO depends the run mode
        if (!self::$_cli) {
            $app->input = new Http\Input(array('app' => $app));
            $app->output = new Http\Output(array('app' => $app));
        } else {
            $app->input = new Cli\Input(array('app' => $app));
            $app->output = new Cli\Output(array('app' => $app));
        }

        // Init Route
        $app->router = new Router(array('path' => $app->input->path(), 'app' => $app));

        return $app;
    }

    /**
     * Return current app
     *
     * @throws \RuntimeException
     * @return App
     */
    public static function self()
    {
        if (!self::$self) {
            throw new \RuntimeException("There is no running App exists");
        }

        return self::$self;
    }

    /**
     * App init
     *
     * @param array|string $config
     * @throws \RuntimeException
     * @return App
     */
    public function __construct($config = array())
    {
        // Register shutdown
        register_shutdown_function(array($this, '__shutdown'));

        // Register autoload
        spl_autoload_register(array($this, '__autoload'));

        // Set config
        $this->injectors =
            (!is_array($config) ? Parser::load((string)$config) : $config)
            + (!empty($this->injectors['cli']) ? array('buffer' => false) : array())
            + $this->injectors;

        // Set cli mode
        if (!isset($this->injectors['cli'])) $this->injectors['cli'] = self::$_cli;

        // Set default locals
        $this->injectors['locals']['config'] = & $this->injectors;

        // Set mode
        $this->injectors['mode'] = ($_mode = getenv('PAGON_ENV')) ? $_mode : $this->injectors['mode'];

        // Set pagon root directory
        $this->injectors['mounts']['pagon'] = dirname(dirname(__DIR__));

        $this->input = & $this->injectors['input'];
        $this->output = & $this->injectors['output'];
        $this->router = & $this->injectors['router'];
    }

    /**
     * Get or set id
     *
     * @param string|\Closure $id
     * @return mixed
     */
    public function id($id = null)
    {
        return $this->input->id($id);
    }

    /**
     * Check if cli
     *
     * @return bool
     */
    public function cli()
    {
{'fraction_non_alphanumeric': 0.1387973640856672, 'fraction_numerical': 0.003953871499176277, 'mean_word_length': 1.640495867768595, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 126, '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': '3779898', 'n_tokens_mistral': 3478, 'n_tokens_neox': 3356, 'n_words': 932}