<commit_before><?php


class Metrofw_Kernel {

	public static $singleton = NULL;

	public $container        = NULL;
	public $serviceList      = array();

	/**
	 * Cache reference
	 */
	public function __construct($container=NULL) {
		$this->container = $container;
		if ($this->container == NULL) {
			$this->container = Metrodi_Container::getContainer();
		}

		ini_set('display_errors', 'on');
		set_exception_handler( array(&$this, 'onException') );
		set_error_handler( array(&$this, 'onError') );
		register_shutdown_function( array(&$this, 'handleFatal') );

		Metrofw_Kernel::$singleton = $this;
	}

	public static function getKernel($container=NULL) {
		if (Metrofw_Kernel::$singleton === NULL) {
			new Metrofw_Kernel($container);
		}
		return Metrofw_Kernel::$singleton;
	}

	/**
	 * Run lifecycles:
	 *  analyze
	 *  resources
	 *  authenticate
	 *  process
	 *  output
	 *  hangup
	 */
	public function runMaster() {
		$this->_runLifecycle('analyze');
		$this->_runLifecycle('resources');
		$this->_runLifecycle('authenticate');
		$this->_runLifecycle('authorize');
		$this->_runLifecycle('process');
		$this->_runLifecycle('output');
		$this->_runLifecycle('hangup');
	}

	/**
	 * Handle signals and slots, which return TRUE to keep the 
	 * event propogating
	 */
	public static function emit($signalName, $source, &$args=array()) {
		$k = Metrofw_Kernel::getKernel();
		$container = $k->container;
		if (empty($args) && count($args) == 0) {
			$args['request']  = $container->make('request');
			$args['response'] = $container->make('response');
		}
		$signal = $container->make('signal');
		$signal->set('source', $source);
		$signal->set('name',   $signalName);
		$continue = true;
		while ($svc = $k->whoCanHandle($signalName.'_pre')) {
			if (is_callable($svc))
			$continue = $svc[0]->{$svc[1]}($signal, $args);
			if (!$continue);break;
		}

		while ($svc = $k->whoCanHandle($signalName)) {
			if (is_callable($svc))
			$continue = $svc[0]->{$svc[1]}($signal, $args);
			if (!$continue);break;
		}

		while ($svc = $k->whoCanHandle($signalName.'_post')) {
			if (is_callable($svc))
			$continue = $svc[0]->{$svc[1]}($signal, $args);
			if (!$continue);break;
		}

		return $continue;
	}

	public static function runLifeCycle($cycle) {
		$k = Metrofw_Kernel::getKernel();
		$k->_runLifecycle($cycle);
/*
		while ($svc = $k->whoCanHandle('master')) {
			$svc[0]->_runLifeCycle($cycle);
		}
*/
	}

	public function _runLifeCycle($cycle) {
		while ($svc = $this->whoCanHandle($cycle)) {

			if (is_object($svc[0]) && ($svc[0] instanceof Closure)) {
				$method = new ReflectionFunction($svc[0]);
			} else {
				if (!is_callable($svc)) {
					continue;
				}
				$method = new ReflectionMethod($svc[0], $svc[1]);
			}
			$params = $method->getParameters();
			$args   = array();
			foreach ($params as $k=>$v) {
				$value = NULL;
				$thing = $v->name; //assume untyped parameter
				if ($v->getClass()) {
					$thing = $v->getClass()->name;
				}
				//try simple get/set flags
				$value = $this->container->get($thing, NULL);
				//try making an object
				if ($value === NULL && $this->container->isThingDefined($thing)) {
					$value = $this->container->make($thing);
				}
				//just get default value defined in function sig<commit_msg>Adding signal(), non-static equivalent of emit()<commit_after><?php


class Metrofw_Kernel {

	public static $singleton = NULL;

	public $container        = NULL;
	public $serviceList      = array();

	/**
	 * Cache reference
	 */
	public function __construct($container=NULL) {
		$this->container = $container;
		if ($this->container == NULL) {
			$this->container = Metrodi_Container::getContainer();
		}

		ini_set('display_errors', 'on');
		set_exception_handler( array(&$this, 'onException') );
		set_error_handler( array(&$this, 'onError') );
		register_shutdown_function( array(&$this, 'handleFatal') );

		Metrofw_Kernel::$singleton = $this;
	}

	public static function getKernel($container=NULL) {
		if (Metrofw_Kernel::$singleton === NULL) {
			new Metrofw_Kernel($container);
		}
		return Metrofw_Kernel::$singleton;
	}

	/**
	 * Run lifecycles:
	 *  analyze
	 *  resources
	 *  authenticate
	 *  process
	 *  output
	 *  hangup
	 */
	public function runMaster() {
		$this->_runLifecycle('analyze');
		$this->_runLifecycle('resources');
		$this->_runLifecycle('authenticate');
		$this->_runLifecycle('authorize');
		$this->_runLifecycle('process');
		$this->_runLifecycle('output');
		$this->_runLifecycle('hangup');
	}

	/**
	 * Handle signals and slots, which return TRUE to keep the 
	 * event propogating
	 */
	public static function emit($signalName, $source, &$args=array()) {
		$k = Metrofw_Kernel::getKernel();
		return $k->signal($signalName, $source, $args);
	}

	public function signal($signalName, $source, &$args=array()) {
		$container = $this->container;
		if (empty($args) && count($args) == 0) {
			$args['request']  = $container->make('request');
			$args['response'] = $container->make('response');
		}
		$signal = $container->make('signal');
		$signal->set('source', $source);
		$signal->set('name',   $signalName);
		$continue = true;
		while ($svc = $this->whoCanHandle($signalName.'_pre')) {
			if (is_callable($svc))
			$continue = $svc[0]->{$svc[1]}($signal, $args);
			if (!$continue);break;
		}

		while ($svc = $this->whoCanHandle($signalName)) {
			if (is_callable($svc))
			$continue = $svc[0]->{$svc[1]}($signal, $args);
			if (!$continue);break;
		}

		while ($svc = $this->whoCanHandle($signalName.'_post')) {
			if (is_callable($svc))
			$continue = $svc[0]->{$svc[1]}($signal, $args);
			if (!$continue);break;
		}

		return $continue;
	}

	public static function runLifeCycle($cycle) {
		$k = Metrofw_Kernel::getKernel();
		$k->_runLifecycle($cycle);
/*
		while ($svc = $k->whoCanHandle('master')) {
			$svc[0]->_runLifeCycle($cycle);
		}
*/
	}

	public function _runLifeCycle($cycle) {
		while ($svc = $this->whoCanHandle($cycle)) {

			if (is_object($svc[0]) && ($svc[0] instanceof Closure)) {
				$method = new ReflectionFunction($svc[0]);
			} else {
				if (!is_callable($svc)) {
					continue;
				}
				$method = new ReflectionMethod($svc[0], $svc[1]);
			}
			$params = $method->getParameters();
			$args   = array();

			foreach ($params as $k=>$v) {
				$value = NULL;
				$thing = $v->name; //assume untyped parameter
				if ($v->getClass()) {
					$thing = $v->getClass()->name;
				}
				//try simple get/set flags
				$value = $this->container->get($thing, NULL);
				//try making an object
				if ($value === NULL && $this->container->isThingDefined($thing)) {
					$value = $this->container->make($thing);
				}
				//just get default value defined in function sig
{'fraction_non_alphanumeric': 0.19587320574162678, 'fraction_numerical': 0.00388755980861244, 'mean_word_length': 4.010486891385768, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 80, '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': '2033808', 'n_tokens_mistral': 2603, 'n_tokens_neox': 2289, 'n_words': 441}