<commit_before><?php
/*
 * Created by <NAME> on 2011-03-06.
 * Copyright (c) 2011 <NAME>.
 * All rights reserved.
 */

namespace Inject\Core;

/**
 * Base class for the application
 */
abstract class Engine
{
	/**
	 * List of loaded engines.
	 * 
	 * @var array(\Inject\Core\Engine)
	 */
	private static $engines = array();
	
	// ------------------------------------------------------------------------

	/**
	 * Instantiates the engine, or returns an instance if already instantiated.
	 * 
	 * @return \Inject\Core\Engine
	 */
	public static function instance()
	{
		$class = get_called_class();
		
		return isset(self::$engines[$class]) ? self::$engines[$class] : self::$engines[$class] = new $class();
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Returns an array containing the loaded engines.
	 * 
	 * @return array(\Inject\Core\Engine)
	 */
	public static function getLoadedEngines()
	{
		return self::$engines;
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * The root directory of this application engine.
	 * 
	 * @var string
	 */
	protected $engine_root = '';
	
	/**
	 * A list of paths to different resources associated with this Engine instance.
	 * 
	 * @var array(string => string)
	 */
	public $paths = array();
	
	/**
	 * The configuration for this Engine.
	 * 
	 * @var array(string => mixed)
	 */
	public $config = array();
	
	/**
	 * The list of initialized middleware, cached result from initMiddleware().
	 * 
	 * @var array(\Inject\Core\Middleware\MiddlewareInterface)
	 */
	protected $middleware = array();
	
	/**
	 * Cache of the endpoint from initEndpoint().
	 * 
	 * @var callback
	 */
	protected $endpoint = null;
	
	// ------------------------------------------------------------------------
	
	/**
	 * Creates a new application instance.
	 */
	protected function __construct()
	{
		self::$engines[]   = $this;
		
		$this->engine_root = $this->registerRootDir();
		$this->paths       = $this->initPaths();
		$this->config      = $this->initConfig();
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Should return the root directory for the application managed by this
	 * engine instance.
	 * 
	 * @return string
	 */
	abstract protected function registerRootDir();
	
	// ------------------------------------------------------------------------

	/**
	 * Initializes the paths for the different assets related to the application.
	 * 
	 * Controllers and Models are not included as their loading is governed
	 * by the autoloader.
	 * 
	 * Default paths:
	 * - assets: ENGINE_ROOT/Resources/Assets/
	 * - cache:  ENGINE_ROOT/Resources/Cache/
	 * - config: ENGINE_ROOT/Resources/Config/
	 * - engine: ENGINE_ROOT/
	 * - lang:   ENGINE_ROOT/Resources/I18n/
	 * - views:  ENGINE_ROOT/Resources/Views/
	 * 
	 * @return array(string => string)
	 */
	protected function initPaths()
	{
		return array(
				'assets' => $this->engine_root.'/Resources/Assets/',
				'cache'  => $this->engine_root.'/Resources/Cache/',
				'config' => $this->engine_root.'/Resources/Config/',
				'engine' => $this->engine_root.'/',
				'lang'   => $this->engine_root.'/Resources/I18n/',
				'views'  => $this->engine_root.'/Resources/Views/'
			);
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Initializes the engine configuration, default configuration is loaded
	 * from the return value of $this->paths['config'].'Config.php'.
	 * 
	 * @return array(string => mixed)
	 */
	public function initConfig()
	{
		if(file_exists($this->paths['config'].'Config.php'))
		{
			 return include $this->paths['config'].'Config.php';
		}
		else
		{
			// TODO: Are these sane defaults?
			return array(
					'debug' => true
				);
		}
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Initializes the middleware for this engine.
	 * 
	 * @return array(\Inject\Core\Middleware\MiddlewareInterface)
	 */
	protected function initMiddleware()
	{
		return array();
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Initializes the endpoint callback for this engine.
	 * 
	 * @return callback
	 */
	protected function initEndpoint()
	{
		return null;
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Creates the application stack and returns it.
	 * 
	 * @return \Inject\Core\MiddlewareStack
	 */
	public function stack()
	{
		return new MiddlewareStack(
				empty($this->middleware) ? $this->middleware = $this->initMiddleware() : $this->middleware,
				empty($this->endpoint)   ? $this->endpoint   = $this->initEndpoint()   : $this->endpoint
			);
	}
	
	// ------------------------------------------------------------------------<commit_msg>Core\Engine: initConfig() is now protected<commit_after><?php
/*
 * Created by <NAME> on 2011-03-06.
 * Copyright (c) 2011 <NAME>.
 * All rights reserved.
 */

namespace Inject\Core;

/**
 * Base class for the application
 */
abstract class Engine
{
	/**
	 * List of loaded engines.
	 * 
	 * @var array(\Inject\Core\Engine)
	 */
	private static $engines = array();
	
	// ------------------------------------------------------------------------

	/**
	 * Instantiates the engine, or returns an instance if already instantiated.
	 * 
	 * @return \Inject\Core\Engine
	 */
	public static function instance()
	{
		$class = get_called_class();
		
		return isset(self::$engines[$class]) ? self::$engines[$class] : self::$engines[$class] = new $class();
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Returns an array containing the loaded engines.
	 * 
	 * @return array(\Inject\Core\Engine)
	 */
	public static function getLoadedEngines()
	{
		return self::$engines;
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * The root directory of this application engine.
	 * 
	 * @var string
	 */
	protected $engine_root = '';
	
	/**
	 * A list of paths to different resources associated with this Engine instance.
	 * 
	 * @var array(string => string)
	 */
	public $paths = array();
	
	/**
	 * The configuration for this Engine.
	 * 
	 * @var array(string => mixed)
	 */
	public $config = array();
	
	/**
	 * The list of initialized middleware, cached result from initMiddleware().
	 * 
	 * @var array(\Inject\Core\Middleware\MiddlewareInterface)
	 */
	protected $middleware = array();
	
	/**
	 * Cache of the endpoint from initEndpoint().
	 * 
	 * @var callback
	 */
	protected $endpoint = null;
	
	// ------------------------------------------------------------------------
	
	/**
	 * Creates a new application instance.
	 */
	protected function __construct()
	{
		self::$engines[]   = $this;
		
		$this->engine_root = $this->registerRootDir();
		$this->paths       = $this->initPaths();
		$this->config      = $this->initConfig();
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Should return the root directory for the application managed by this
	 * engine instance.
	 * 
	 * @return string
	 */
	abstract protected function registerRootDir();
	
	// ------------------------------------------------------------------------

	/**
	 * Initializes the paths for the different assets related to the application.
	 * 
	 * Controllers and Models are not included as their loading is governed
	 * by the autoloader.
	 * 
	 * Default paths:
	 * - assets: ENGINE_ROOT/Resources/Assets/
	 * - cache:  ENGINE_ROOT/Resources/Cache/
	 * - config: ENGINE_ROOT/Resources/Config/
	 * - engine: ENGINE_ROOT/
	 * - lang:   ENGINE_ROOT/Resources/I18n/
	 * - views:  ENGINE_ROOT/Resources/Views/
	 * 
	 * @return array(string => string)
	 */
	protected function initPaths()
	{
		return array(
				'assets' => $this->engine_root.'/Resources/Assets/',
				'cache'  => $this->engine_root.'/Resources/Cache/',
				'config' => $this->engine_root.'/Resources/Config/',
				'engine' => $this->engine_root.'/',
				'lang'   => $this->engine_root.'/Resources/I18n/',
				'views'  => $this->engine_root.'/Resources/Views/'
			);
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Initializes the engine configuration, default configuration is loaded
	 * from the return value of $this->paths['config'].'Config.php'.
	 * 
	 * @return array(string => mixed)
	 */
	protected function initConfig()
	{
		if(file_exists($this->paths['config'].'Config.php'))
		{
			 return include $this->paths['config'].'Config.php';
		}
		else
		{
			// TODO: Are these sane defaults?
			return array(
					'debug' => true
				);
		}
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Initializes the middleware for this engine.
	 * 
	 * @return array(\Inject\Core\Middleware\MiddlewareInterface)
	 */
	protected function initMiddleware()
	{
		return array();
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Initializes the endpoint callback for this engine.
	 * 
	 * @return callback
	 */
	protected function initEndpoint()
	{
		return null;
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Creates the application stack and returns it, used to call this Engine
	 * by calling the stack instance's run($env) method.
	 * 
	 * @return \Inject\Core\MiddlewareStack
	 */
	public function stack()
	{
		return new MiddlewareStack(
				empty($this->middleware) ? $this->middleware = $this->initMiddleware() : $this->middleware,
				empty($this->endpoint)   ? $this->endpoint   = $this->initEndpoint()   : $this->endpoint
			);
	}
	
	// ------------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.2887820840576746, 'fraction_numerical': 0.003272318232948154, 'mean_word_length': 1.6325706594885598, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 75, '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': '496692', 'n_tokens_mistral': 2891, 'n_tokens_neox': 2502, 'n_words': 646}