<commit_before><?php
/**
 * @version		$Id$
 * @category	Koowa
 * @package     Koowa_Controller
 * @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 View Controller Class
 *
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_Controller
 * @uses        KInflector
 */
abstract class KControllerResource extends KControllerAbstract
{
	/**
	 * URL for redirection.
	 *
	 * @var	string
	 */
	protected $_redirect = null;

	/**
	 * Redirect message.
	 *
	 * @var	string
	 */
	protected $_redirect_message = null;

	/**
	 * Redirect message type.
	 *
	 * @var	string
	 */
	protected $_redirect_type = 'message';
	
	/**
	 * View object or identifier (com://APP/COMPONENT.view.NAME.FORMAT)
	 *
	 * @var	string|object
	 */
	protected $_view;
	
	/**
	 * Model object or identifier (com://APP/COMPONENT.model.NAME)
	 *
	 * @var	string|object
	 */
	protected $_model;
	
	/**
	 * Constructor
	 *
	 * @param 	object 	An optional KConfig object with configuration options.
	 */
	public function __construct(KConfig $config)
	{
		parent::__construct($config);

	    // Set the model identifier
	    $this->_model = $config->model;
		
		// Set the view identifier
		$this->_view = $config->view;
		
		//Register display as alias for get
		$this->registerActionAlias('display', 'get');
		
		//Made the executable behavior read-only
		if($config->readonly) {
		    $this->getBehavior('executable')->setReadOnly(true);
		}
	}

	/**
     * Initializes the default configuration 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(
    	    'model'	     => $this->getIdentifier()->name,
        	'view'	     => $this->getIdentifier()->name,
    	    'behaviors'  => array('executable', 'commandable'),
    	    'readonly'   => true, 
    		'request' 	 => array('format' => 'html')
        ));
        
        parent::_initialize($config);
        
        //Force the view to the information found in the request
        if(isset($config->request->view)) {
            $config->view = $config->request->view;
        }
    }
    
	/**
	 * Get the view object attached to the controller
	 * 
	 * This function will check if the view folder exists. If not it will throw
	 * an exception. This is a security measure to make sure we can only explicitly
	 * get data from views the have been physically defined. 
	 *
	 * @throws  KControllerException if the view cannot be found.
	 * @return	KViewAbstract
	 *  
	 */
	public function getView()
	{
	    if(!$this->_view instanceof KViewAbstract)
		{	   
		    //Make sure we have a view identifier
		    if(!($this->_view instanceof KServiceIdentifier)) {
		        $this->setView($this->_view);
			}
			
			//Create the view
			$config = array(
			    'model'  => $this->getModel(),
        	);
        	
			$this->_view = $this->getService($this->_view, $config);
			
			//Set the layout
			if(isset($this->_request->layout)) {
        	    $this->_view->setLayout($this->_request->layout);
        	} 
			
			//Make sure the view exists
		    if(!file_exists(dirname($this->_view->getIdentifier()->filepath))) {
		        throw new KControllerException('View : '.$this->_view->getName().' not found', KHttpResponse::NOT_FOUND);
		    }
		}
		
		return $this->_view;
	}

	/**
	 * Method to set a view object attached to the controller
	 *
	 * @param	mixed	An object that implements KObjectServiceable, KServiceIdentifier object 
	 * 					or valid identifier string
	 * @throws	KControllerException	If the identifier is not a view identifier
	 * @return	object	A KViewAbstract object or a KServiceIdentifier object
	 */
	public function setView($view)
	{
		if(!($view instanceof KViewAbstract))
		{
			if(is_string($view) && strpos($view, '.') === false ) 
		    {
			    $identifier			= clone $this->getIdentifier();
			    $identifier->path	= array('view', $view);
			    $identifier->name	= $this->getRequest()->format;
			}
			else $identifier = $this->getIdentifier($view);
		    
			if($identifier->path[0] != 'view') {
				throw new KControllerException('Identifier: '.$identifier.' is not a view identifier');
			}

			$view = $identifier;
		}
		
		$this->_view = $view;
		
		return $this->_view;
	}
	
	/**
	 * Get the model object attached to the contoller
	 *
	 * @return	KModelAbstract
	 */
	public function getModel()
	{
		if(!$this->_model instanceof KModelAbstract) 
		{
			//Make sure we have a model identifier
		    if(!($this->_model instanceof KServiceIdentifier)) {
		        $this->setModel($this->_model);
			}
		    
		    //@TODO : Pass the state to the model using the options
		    $options = array(
				'state' => $this->getRequest()
            );
		    
		    $this->_model = $this->getService($this->_model)->set($this->getRequest());
		}

		return $this->_model;
	}

	/**
	 * Method to set a model object attached to the controller
	 *
	 * @param	mixed	An object that implements KObjectServiceable, KServiceIdentifier object 
	 * 					or valid identifier string
	 * @throws	KControllerException	If the identifier is not a model identifier
	 * @return	object	A KModelAbstract object or a KServiceIdentifier object
	 */
	public function setModel($model)
	{
		if(!($model instanceof KModelAbstract))
		{
	        if(is_string($model) && strpos($model, '.') === false ) 
		    {
			    // Model names are always plural
			    if(KInflector::isSingular($model)) {
				    $model = KInflector::pluralize($model);
			    } 
		        
			    $identifier			= clone $this->getIdentifier();
			    $identifier->path	= array('model');
			    $identifier->name	= $model;
			}
			else $identifier = $this->getIdentifier($model);
		    
			if($identifier->path[0] != 'model') {
				throw new KControllerException('Identifier: '.$identifier.' is not a model identifier');
			}

			$model = $identifier;
		}
		
		$this->_model = $model;
		
		return $this->_model;
	}
	
	/**
	 * Set a URL for browser redirection.
	 *
	 * @param	string URL to redirect to.
	 * @param	string	Message to display on redirect. Optional, defaults to
	 * 			value set internally by controller, if any.
	 * @param	string	Message type. Optional, defaults to 'message'.
	 * @return	KControllerAbstract
	 */
	public function setRedirect( $url, $msg = null, $type = 'message' )
	{
		$this->_redirect   		 = $url;
		$this->_redirect_message = $msg;
		$this->_redirect_type	 = $type;

		return $this;
	}

	/**
	 * Returns an array with the redirect url, the message and the message type
	 *
	 * @return array	Named array containing url, message and messageType, or null if no redirect was set
	 */
	public function getRedirect()
	{
		$result = array();

		if(!empty($this->_redirect))
		{
			$result = array(
				'url' 		=> JRoute::_($this->_redirect, false),
				'message' 	=> $this->_redirect_message,
				'type' 		=> $this->_redirect_type,
			);
		}

		return $result;
	}
	
	/**
	 * Specialised display function.
	 *
	 * @param	KCommandContext	A command context object
	 * @return 	string|false 	The rendered output of the view or false if something went wrong
	 */
	protected function _actionGet(KCommandContext $context)
	{ 
	    $result = $this->getView()->display();   
	    return $result;
	}
	
	/**
     * Set a request properties
     * 
     * This function also pushes any request changes into the model
     *
     * @param  	string 	The property name.
     * @param 	mixed 	The property value.
     */
 	public function __set($property, $value)
    {
    	parent::__set($property, $value);
    	
    	//Prevent state changes through the parents constructor 
    	if($this->_model instanceof KModelAbstract) {
    	    $this->getModel()->set($property, $value);
    	}
  	}
	
	/**
	 * Supports a simple form Fluent Interfaces. Allows you to set the request 
	 * properties by using the request property name as the method name.
	 *
	 * For example : $controller->view('name')->limit(10)->browse();
	 *
	 * @param	string	Method name
	 * @param	array	Array containing all the arguments for the original call
	 * @return	KControllerBread
	 *
	 * @see http://martinfowler.com/bliki/FluentInterface.html
	 */
	public function __call($method, $args)
	{
	    //Check first if we are calling a mixed in method. 
	    //This prevents the model being loaded durig object instantiation. 
		if(!isset($this->_mixed_methods[$method])) 
        {
            //Check if the method is a state property
			$state = $this->getModel()->getState();
		
			if(isset($state->$method) || in_array($method, array('layout', 'view', 'format'))) 
			{
				$this->$method = $args[0];
				
				if($method == 'view') {
                   $this->_view = $args[0];
                }
				
				return $this;
			}
        }
		
		return parent::__call($method, $args);
	}
}<commit_msg>Removed KControllerBehaviorCommandable and mixin the toolbar mixin.<commit_after><?php
/**
 * @version		$Id$
 * @category	Koowa
 * @package     Koowa_Controller
 * @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 View Controller Class
 *
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_Controller
 * @uses        KInflector
 */
abstract class KControllerResource extends KControllerAbstract
{
	/**
	 * URL for redirection.
	 *
	 * @var	string
	 */
	protected $_redirect = null;

	/**
	 * Redirect message.
	 *
	 * @var	string
	 */
	protected $_redirect_message = null;

	/**
	 * Redirect message type.
	 *
	 * @var	string
	 */
	protected $_redirect_type = 'message';
	
	/**
	 * View object or identifier (com://APP/COMPONENT.view.NAME.FORMAT)
	 *
	 * @var	string|object
	 */
	protected $_view;
	
	/**
	 * Model object or identifier (com://APP/COMPONENT.model.NAME)
	 *
	 * @var	string|object
	 */
	protected $_model;
	
	/**
	 * Constructor
	 *
	 * @param 	object 	An optional KConfig object with configuration options.
	 */
	public function __construct(KConfig $config)
	{
		parent::__construct($config);

	    // Set the model identifier
	    $this->_model = $config->model;
		
		// Set the view identifier
		$this->_view = $config->view;
		
		//Register display as alias for get
		$this->registerActionAlias('display', 'get');
		
		// Mixin the toolbar
        $this->mixin(new KMixinToolbar($config->append(array('mixer' => $this))));
		
		//Made the executable behavior read-only
		if($config->readonly) {
		    $this->getBehavior('executable')->setReadOnly(true);
		}
	}
	
	/**
     * Initializes the default configuration 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(
    	    'model'	     => $this->getIdentifier()->name,
        	'view'	     => $this->getIdentifier()->name,
    	    'behaviors'  => array('executable'),
    	    'readonly'   => true, 
    		'request' 	 => array('format' => 'html'),
        ));
        
        parent::_initialize($config);
        
        //Force the view to the information found in the request
        if(isset($config->request->view)) {
            $config->view = $config->request->view;
        }
    }
    
	/**
	 * Get the view object attached to the controller
	 * 
	 * This function will check if the view folder exists. If not it will throw
	 * an exception. This is a security measure to make sure we can only explicitly
	 * get data from views the have been physically defined. 
	 *
	 * @throws  KControllerException if the view cannot be found.
	 * @return	KViewAbstract
	 *  
	 */
	public function getView()
	{
	    if(!$this->_view instanceof KViewAbstract)
		{	   
		    //Make sure we have a view identifier
		    if(!($this->_view instanceof KServiceIdentifier)) {
		        $this->setView($this->_view);
			}
			
			//Create the view
			$config = array(
			    'model'  => $this->getModel(),
        	);
        	
			$this->_view = $this->getService($this->_view, $config);
			
			//Set the layout
			if(isset($this->_request->layout)) {
        	    $this->_view->setLayout($this->_request->layout);
        	} 
			
			//Make sure the view exists
		    if(!file_exists(dirname($this->_view->getIdentifier()->filepath))) {
		        throw new KControllerException('View : '.$this->_view->getName().' not found', KHttpResponse::NOT_FOUND);
		    }
		}
		
		return $this->_view;
	}

	/**
	 * Method to set a view object attached to the controller
	 *
	 * @param	mixed	An object that implements KObjectServiceable, KServiceIdentifier object 
	 * 					or valid identifier string
	 * @throws	KControllerException	If the identifier is not a view identifier
	 * @return	object	A KViewAbstract object or a KServiceIdentifier object
	 */
	public function setView($view)
	{
		if(!($view instanceof KViewAbstract))
		{
			if(is_string($view) && strpos($view, '.') === false ) 
		    {
			    $identifier			= clone $this->getIdentifier();
			    $identifier->path	= array('view', $view);
			    $identifier->name	= $this->getRequest()->format;
			}
			else $identifier = $this->getIdentifier($view);
		    
			if($identifier->path[0] != 'view') {
				throw new KControllerException('Identifier: '.$identifier.' is not a view identifier');
			}

			$view = $identifier;
		}
		
		$this->_view = $view;
		
		return $this->_view;
	}
	
	/**
	 * Get the model object attached to the contoller
	 *
	 * @return	KModelAbstract
	 */
	public function getModel()
	{
		if(!$this->_model instanceof KModelAbstract) 
		{
			//Make sure we have a model identifier
		    if(!($this->_model instanceof KServiceIdentifier)) {
		        $this->setModel($this->_model);
			}
		    
		    //@TODO : Pass the state to the model using the options
		    $options = array(
				'state' => $this->getRequest()
            );
		    
		    $this->_model = $this->getService($this->_model)->set($this->getRequest());
		}

		return $this->_model;
	}

	/**
	 * Method to set a model object attached to the controller
	 *
	 * @param	mixed	An object that implements KObjectServiceable, KServiceIdentifier object 
	 * 					or valid identifier string
	 * @throws	KControllerException	If the identifier is not a model identifier
	 * @return	object	A KModelAbstract object or a KServiceIdentifier object
	 */
	public function setModel($model)
	{
		if(!($model instanceof KModelAbstract))
		{
	        if(is_string($model) && strpos($model, '.') === false ) 
		    {
			    // Model names are always plural
			    if(KInflector::isSingular($model)) {
				    $model = KInflector::pluralize($model);
			    } 
		        
			    $identifier			= clone $this->getIdentifier();
			    $identifier->path	= array('model');
			    $identifier->name	= $model;
			}
			else $identifier = $this->getIdentifier($model);
		    
			if($identifier->path[0] != 'model') {
				throw new KControllerException('Identifier: '.$identifier.' is not a model identifier');
			}

			$model = $identifier;
		}
		
		$this->_model = $model;
		
		return $this->_model;
	}
	
	/**
	 * Set a URL for browser redirection.
	 *
	 * @param	string URL to redirect to.
	 * @param	string	Message to display on redirect. Optional, defaults to
	 * 			value set internally by controller, if any.
	 * @param	string	Message type. Optional, defaults to 'message'.
	 * @return	KControllerAbstract
	 */
	public function setRedirect( $url, $msg = null, $type = 'message' )
	{
		$this->_redirect   		 = $url;
		$this->_redirect_message = $msg;
		$this->_redirect_type	 = $type;

		return $this;
	}

	/**
	 * Returns an array with the redirect url, the message and the message type
	 *
	 * @return array	Named array containing url, message and messageType, or null if no redirect was set
	 */
	public function getRedirect()
	{
		$result = array();

		if(!empty($this->_redirect))
		{
			$result = array(
				'url' 		=> JRoute::_($this->_redirect, false),
				'message' 	=> $this->_redirect_message,
				'type' 		=> $this->_redirect_type,
			);
		}

		return $result;
	}
	
	/**
	 * Specialised display function.
	 *
	 * @param	KCommandContext	A command context object
	 * @return 	string|false 	The rendered output of the view or false if something went wrong
	 */
	protected function _actionGet(KCommandContext $context)
	{ 
	    $result = $this->getView()->display();   
	    return $result;
	}
	
	/**
     * Set a request properties
     * 
     * This function also pushes any request changes into the model
     *
     * @param  	string 	The property name.
     * @param 	mixed 	The property value.
     */
 	public function __set($property, $value)
    {
    	parent::__set($property, $value);
    	
    	//Prevent state changes through the parents constructor 
    	if($this->_model instanceof KModelAbstract) {
    	    $this->getModel()->set($property, $value);
    	}
  	}
	
	/**
	 * Supports a simple form Fluent Interfaces. Allows you to set the request 
	 * properties by using the request property name as the method name.
	 *
	 * For example : $controller->view('name')->limit(10)->browse();
	 *
	 * @param	string	Method name
	 * @param	array	Array containing all the arguments for the original call
	 * @return	KControllerBread
	 *
	 * @see http://martinfowler.com/bliki/FluentInterface.html
	 */
	public function __call($method, $args)
	{
	    //Check first if we are calling a mixed in method. 
	    //This prevents the model being loaded durig object instantiation. 
		if(!isset($this->_mixed_methods[$method])) 
        {
            //Check if the method is a state property
			$state = $this->getModel()->getState();
		
			if(isset($state->$method) || in_array($method, array('layout', 'view', 'format'))) 
			{
				$this->$method = $args[0];
				
				if($method == 'view') {
                   $this->_view = $args[0];
                }
				
				return $this;
			}
        }
		
		return parent::__call($method, $args);
	}
}
{'fraction_non_alphanumeric': 0.13255712255384278, 'fraction_numerical': 0.0016398819285011479, 'mean_word_length': 2.6996966632962587, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 210, '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': '3390536', 'n_tokens_mistral': 6141, 'n_tokens_neox': 5350, 'n_words': 1541}