<commit_before><?php
/**
 * @version		$Id$
 * @category	Koowa
 * @package		Koowa_Dispatcher
 * @copyright	Copyright (C) 2007 - 2010 <NAME>. All rights reserved.
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link     	http://www.nooku.org
 */

/**
 * Abstract controller dispatcher
 *
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_Dispatcher
 * @uses		KMixinClass
 * @uses        KObject
 * @uses        KFactory
 */
abstract class KDispatcherAbstract extends KControllerAbstract
{
	/**
	 * Controller object or identifier (APP::com.COMPONENT.controller.NAME)
	 *
	 * @var	string|object
	 */
	protected $_controller;
	
	/**
	 * Default controller name
	 *
	 * @var	string
	 */
	protected $_controller_default;
	
	/**
	 * The request data
	 * 
	 * @var KConfig
	 */
	protected $_request;
	
	/**
	 * The request persistency
	 * 
	 * @var boolean
	 */
	protected $_request_persistent;

	/**
	 * Constructor.
	 *
	 * @param 	object 	An optional KConfig object with configuration options.
	 */
	public function __construct(KConfig $config)
	{
		parent::__construct($config);
		
		//Set the request
		$this->_request = $config->request;
		
		//Set the request persistency
		$this->_request_persistent = $config->request_persistent;
		
		//Set the controller default
		$this->_controller_default = $config->controller_default;
		
		if($config->controller !== null) {
			$this->setController($config->controller);
		}

		if(KRequest::method() != 'GET') 
		{
			$this->registerCallback('before.dispatch', array($this, 'authorize'));
			$this->registerCallback('after.dispatch' , array($this, 'forward'));
	  	}

	    $this->registerCallback('after.dispatch', array($this, 'render'));
	}

    /**
     * Initializes the options for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param 	object 	An optional KConfig object with configuration options.
     * @return 	void
     */
    protected function _initialize(KConfig $config)
    {
    	$config->append(array(
        	'controller'			=> null,
    		'controller_default'	=> $this->_identifier->package,
    		'request'				=> KRequest::get('get', 'string'),
    		'request_persistent' 	=> false
        ));

        parent::_initialize($config);
    }

	/**
	 * Method to get a controller identifier
	 *
	 * @return	object	The controller.
	 */
	public function getController()
	{
		if(!$this->_controller)
		{
			$application 	= $this->_identifier->application;
			$package 		= $this->_identifier->package;

			//Get the controller name
			$controller = KRequest::get('get.view', 'cmd', $this->_controller_default);
			
			//In case we are loading a subview, we use the first part of the name as controller name
			if(strpos($controller, '.') !== false)
			{
				$result = explode('.', $controller);

				//Set the controller based on the parent
				$controller = $result[0];
			}

			// Controller names are always singular
			if(KInflector::isPlural($controller)) {
				$controller = KInflector::singularize($controller);
			}
			
			$config = array(
        		'request' 	   => $this->_request,
        		'persistent'   => $this->_request_persistent,
        		'auto_display' => true
        	);

			$this->_controller = KFactory::get($application.'::com.'.$package.'.controller.'.$controller, $config);
		}

		return $this->_controller;
	}

	/**
	 * Method to set a controller object attached to the dispatcher
	 *
	 * @param	mixed	An object that implements KObjectIdentifiable, an object that
	 *                  implements KIndentifierInterface or valid identifier string<commit_msg>Removed unused auto_display config option<commit_after><?php
/**
 * @version		$Id$
 * @category	Koowa
 * @package		Koowa_Dispatcher
 * @copyright	Copyright (C) 2007 - 2010 <NAME>. All rights reserved.
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link     	http://www.nooku.org
 */

/**
 * Abstract controller dispatcher
 *
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_Dispatcher
 * @uses		KMixinClass
 * @uses        KObject
 * @uses        KFactory
 */
abstract class KDispatcherAbstract extends KControllerAbstract
{
	/**
	 * Controller object or identifier (APP::com.COMPONENT.controller.NAME)
	 *
	 * @var	string|object
	 */
	protected $_controller;
	
	/**
	 * Default controller name
	 *
	 * @var	string
	 */
	protected $_controller_default;
	
	/**
	 * The request data
	 * 
	 * @var KConfig
	 */
	protected $_request;
	
	/**
	 * The request persistency
	 * 
	 * @var boolean
	 */
	protected $_request_persistent;

	/**
	 * Constructor.
	 *
	 * @param 	object 	An optional KConfig object with configuration options.
	 */
	public function __construct(KConfig $config)
	{
		parent::__construct($config);
		
		//Set the request
		$this->_request = $config->request;
		
		//Set the request persistency
		$this->_request_persistent = $config->request_persistent;
		
		//Set the controller default
		$this->_controller_default = $config->controller_default;
		
		if($config->controller !== null) {
			$this->setController($config->controller);
		}

		if(KRequest::method() != 'GET') 
		{
			$this->registerCallback('before.dispatch', array($this, 'authorize'));
			$this->registerCallback('after.dispatch' , array($this, 'forward'));
	  	}

	    $this->registerCallback('after.dispatch', array($this, 'render'));
	}

    /**
     * Initializes the options for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param 	object 	An optional KConfig object with configuration options.
     * @return 	void
     */
    protected function _initialize(KConfig $config)
    {
    	$config->append(array(
        	'controller'			=> null,
    		'controller_default'	=> $this->_identifier->package,
    		'request'				=> KRequest::get('get', 'string'),
    		'request_persistent' 	=> false
        ));

        parent::_initialize($config);
    }

	/**
	 * Method to get a controller identifier
	 *
	 * @return	object	The controller.
	 */
	public function getController()
	{
		if(!$this->_controller)
		{
			$application 	= $this->_identifier->application;
			$package 		= $this->_identifier->package;

			//Get the controller name
			$controller = KRequest::get('get.view', 'cmd', $this->_controller_default);
			
			//In case we are loading a subview, we use the first part of the name as controller name
			if(strpos($controller, '.') !== false)
			{
				$result = explode('.', $controller);

				//Set the controller based on the parent
				$controller = $result[0];
			}

			// Controller names are always singular
			if(KInflector::isPlural($controller)) {
				$controller = KInflector::singularize($controller);
			}
			
			$config = array(
        		'request' 	   => $this->_request,
        		'persistent'   => $this->_request_persistent
        	);

			$this->_controller = KFactory::get($application.'::com.'.$package.'.controller.'.$controller, $config);
		}

		return $this->_controller;
	}

	/**
	 * Method to set a controller object attached to the dispatcher
	 *
	 * @param	mixed	An object that implements KObjectIdentifiable, an object that
	 *                  implements KIndentifierInterface or valid identifier string
{'fraction_non_alphanumeric': 0.13753778510579828, 'fraction_numerical': 0.0027480076944215444, 'mean_word_length': 3.057413600891862, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1779042', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2135, 'n_words': 535}