<commit_before><?php
/**
 * Copyright (c) 2016 <NAME>
 */

namespace AndrewBreksa\Unicorn\App;


use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use League\Container\Container;
use League\Container\ReflectionContainer;
use League\Event\Emitter;
use League\Route\Http\Exception\NotFoundException;
use League\Route\RouteCollection;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\Response;

/**
 * A tiny single class RAD PSR-7 web application "framework"
 *
 * @package Unicorn\App
 * @author <NAME> <<EMAIL>>
 */
class Application implements ContainerInterface {
	/**
	 * The Unicorn version
	 */
	const VERSION = '0';
	/**
	 * Runs on Application->bootstrap()
	 */
	const EVENT_BOOTSTRAP = 'app.bootstrap';
	/**
	 * Runs before dispatch
	 */
	const EVENT_DISPATCH = 'app.dispatch';
	/**
	 * Runs in case of a NotFoundException, listeners get $this and $exceptions as arguments as well as the Event object
	 */
	const EVENT_ROUTE_EXCEPTION = 'app.route.exception';
	/**
	 * Runs in case of an \Exception during dispatch, listeners get $this and $exceptions as arguments as well as the
	 * Event object
	 */
	const EVENT_DISPATCH_EXCEPTION = 'app.dispatch.exception';
	/**
	 * Runs after dispatch. Unicorn includes no default functionality to redner
	 */
	const EVENT_RENDER = 'app.render';
	/**
	 * Runs in case of an \Exception during emitting the PSR-7 response, listeners get $this and $exceptions as
	 * arguments as well as the Event object
	 */
	const EVENT_EMIT_EXCEPTION = 'app.emit.exception';
	/**
	 * Runs after the PSR-7 response has been emitted
	 */
	const EVENT_FINISH = 'app.finish';
	/**
	 * @var Application
	 */
	private static $instance = NULL;
	/**
	 * An array of arbitrary data, could hold user data, non-formatted responses, etc
	 *
	 * @var array
	 */
	public $data = [];
	/**
	 * The application config data
	 *
	 * @var array
	 */
	public $config = [];
	/**
	 * The event emittet
	 *
	 * @var \League\Event\Emitter
	 */
	protected $eventEmitter;
	/**
	 * The DI container
	 *
	 * @var \League\Container\Container
	 */
	protected $container;
	/**
	 * The router
	 *
	 * @var RouteCollection
	 */
	protected $routeCollection;
	/**
	 * Uh, the response
	 *
	 * @var ResponseInterface
	 */
	protected $response;
	/**
	 * Uh-huh, the request
	 *
	 * @var ServerRequestInterface
	 */
	protected $request;

	/**
	 * The root project directory
	 *
	 * @var string
	 */
	protected $basedir;

	/**
	 * Don't call this directly. Instead, call Application::getInstance().
	 *
	 * Application constructor.
	 */
	private function __construct($basedir) {
		$this->basedir = $basedir;
		$this->eventEmitter = new Emitter();
		$this->container = new Container();
		$this->getContainer()->delegate(
			new ReflectionContainer
		);
		$this->getContainer()->delegate($this);
		$this->routeCollection = new RouteCollection($this->getContainer());
		foreach (glob($this->basedir . '/config/autoload/*.php') as $file) {
			$this->config = array_merge($this->config, include($file));
		}
		foreach (glob($this->basedir . '/config/autoload/*.json') as $file) {
			$this->config = array_merge($this->config, json_decode(file_get_contents($file), TRUE));
		}
		$this->getContainer()->share(Application::class, $this);
	}

	/**
	 * Get the league/container container
	 *
	 * @return \League\Container\Container
	 */
	public function getContainer() {
		return $this->container;
	}

	/**
	 * Get the singleton instance of Application
	 *
	 * @param string|null $basedir
	 * @return Application
	 * @throws \InvalidArgumentException if the $basedir is not set on initial creation
	 */
	public static function getInstance($basedir = NULL) {
		if (Application::$instance == NULL) {
			if (is_null($basedir)) {
				throw new \InvalidArgumentException('$basedir is required on initial Application creation');
			}
			Application::$instance = new Application($basedir);
		}

		return Application::$instance;
	}

	/**
	 * Optionally takes a string and returns a stream open to php://temp at r+
	 *
	 * @param string|null $string
	 * @return StreamInterface
	 */
	public static function newTempStream($string = NULL) {
		$stream = new \Zend\Diactoros\Stream(fopen('php://temp', 'r+'));
		if (!is_null($string)) {<commit_msg>remove mandatory baseDir on getInstance, now config/autoload is only checked and parsed if it is provided<commit_after><?php
/**
 * Copyright (c) 2016 <NAME>
 */

namespace AndrewBreksa\Unicorn\App;


use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use League\Container\Container;
use League\Container\ReflectionContainer;
use League\Event\Emitter;
use League\Route\Http\Exception\NotFoundException;
use League\Route\RouteCollection;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\Response;

/**
 * A tiny single class RAD PSR-7 web application "framework"
 *
 * @package Unicorn\App
 * @author <NAME> <<EMAIL>>
 */
class Application implements ContainerInterface {
	/**
	 * The Unicorn version
	 */
	const VERSION = '0';
	/**
	 * Runs on Application->bootstrap()
	 */
	const EVENT_BOOTSTRAP = 'app.bootstrap';
	/**
	 * Runs before dispatch
	 */
	const EVENT_DISPATCH = 'app.dispatch';
	/**
	 * Runs in case of a NotFoundException, listeners get $this and $exceptions as arguments as well as the Event object
	 */
	const EVENT_ROUTE_EXCEPTION = 'app.route.exception';
	/**
	 * Runs in case of an \Exception during dispatch, listeners get $this and $exceptions as arguments as well as the
	 * Event object
	 */
	const EVENT_DISPATCH_EXCEPTION = 'app.dispatch.exception';
	/**
	 * Runs after dispatch. Unicorn includes no default functionality to redner
	 */
	const EVENT_RENDER = 'app.render';
	/**
	 * Runs in case of an \Exception during emitting the PSR-7 response, listeners get $this and $exceptions as
	 * arguments as well as the Event object
	 */
	const EVENT_EMIT_EXCEPTION = 'app.emit.exception';
	/**
	 * Runs after the PSR-7 response has been emitted
	 */
	const EVENT_FINISH = 'app.finish';
	/**
	 * @var Application
	 */
	private static $instance = NULL;
	/**
	 * An array of arbitrary data, could hold user data, non-formatted responses, etc
	 *
	 * @var array
	 */
	public $data = [];
	/**
	 * The application config data
	 *
	 * @var array
	 */
	public $config = [];
	/**
	 * The event emittet
	 *
	 * @var \League\Event\Emitter
	 */
	protected $eventEmitter;
	/**
	 * The DI container
	 *
	 * @var \League\Container\Container
	 */
	protected $container;
	/**
	 * The router
	 *
	 * @var RouteCollection
	 */
	protected $routeCollection;
	/**
	 * Uh, the response
	 *
	 * @var ResponseInterface
	 */
	protected $response;
	/**
	 * Uh-huh, the request
	 *
	 * @var ServerRequestInterface
	 */
	protected $request;

	/**
	 * The root project directory
	 *
	 * @var string
	 */
	protected $basedir;

	/**
	 * Don't call this directly. Instead, call Application::getInstance().
	 *
	 * Application constructor.
	 */
	private function __construct($basedir = NULL) {
		$this->basedir = $basedir;
		$this->eventEmitter = new Emitter();
		$this->container = new Container();
		$this->getContainer()->delegate(
			new ReflectionContainer
		);
		$this->getContainer()->delegate($this);
		$this->routeCollection = new RouteCollection($this->getContainer());
		if(!is_null($basedir)) {
			foreach (glob($this->basedir . '/config/autoload/*.php') as $file) {
				$this->config = array_merge($this->config, include($file));
			}
			foreach (glob($this->basedir . '/config/autoload/*.json') as $file) {
				$this->config = array_merge($this->config, json_decode(file_get_contents($file), TRUE));
			}
		}
		$this->getContainer()->share(Application::class, $this);
	}

	/**
	 * Get the league/container container
	 *
	 * @return \League\Container\Container
	 */
	public function getContainer() {
		return $this->container;
	}

	/**
	 * Get the singleton instance of Application
	 *
	 * @param string|null $basedir
	 * @return Application
	 * @throws \InvalidArgumentException if the $basedir is not set on initial creation
	 */
	public static function getInstance($basedir = NULL) {
		if (Application::$instance == NULL) {
			Application::$instance = new Application($basedir);
		}

		return Application::$instance;
	}

	/**
	 * Optionally takes a string and returns a stream open to php://temp at r+
	 *
	 * @param string|null $string
	 * @return StreamInterface
	 */
	public static function newTempStream($string = NULL) {
		$stream = new \Zend\Diactoros\Stream(fopen('php://temp', 'r+'));
		if (!is_null($string)) {
{'fraction_non_alphanumeric': 0.1348714416896235, 'fraction_numerical': 0.0018365472910927456, 'mean_word_length': 3.725054229934924, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 49, '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': '3738823', 'n_tokens_mistral': 2823, 'n_tokens_neox': 2562, 'n_words': 738}