<commit_before><?php
/**
 * Koowa Framework - http://developer.joomlatools.com/koowa
 *
 * @copyright	Copyright (C) 2007 - 2013 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link		http://github.com/joomlatools/koowa for the canonical source repository
 */

/**
 * Abstract Controller
 *
 * Note: Concrete controllers must have a singular name
 *
 * @author  <NAME> <https://github.com/johanjanssens>
 * @package Koowa\Library\Controller
 */
abstract class KControllerAbstract extends KObject implements KControllerInterface
{
    /**
     * The class actions
     *
     * @var array
     */
    protected $_actions = array();

    /**
     * Response object or identifier
     *
     * @var	string|object
     */
    protected $_response;

    /**
     * Request object or identifier
     *
     * @var	string|object
     */
    protected $_request;

    /**
     * User object or identifier
     *
     * @var	string|object
     */
    protected $_user;

    /**
     * Has the controller been dispatched
     *
     * @var boolean
     */
    protected $_dispatched;

    //Status codes
    const STATUS_SUCCESS   = KHttpResponse::OK;
    const STATUS_CREATED   = KHttpResponse::CREATED;
    const STATUS_ACCEPTED  = KHttpResponse::ACCEPTED;
    const STATUS_UNCHANGED = KHttpResponse::NO_CONTENT;
    const STATUS_RESET     = KHttpResponse::RESET_CONTENT;

    /**
     * Constructor.
     *
     * @param   KObjectConfig $config Configuration options.
     */
    public function __construct( KObjectConfig $config)
    {
        parent::__construct($config);

        //Set the dispatched state
        $this->_dispatched = $config->dispatched;

        // Set the model identifier
        $this->_request = $config->request;

        // Set the view identifier
        $this->_response = $config->response;

        // Set the user identifier
        $this->_user = $config->user;

        //Set the query in the request
        if(!empty($config->query)) {
            $this->getRequest()->query->add(KObjectConfig::unbox($config->query));
        }

        // Mixin the behavior interface
        $this->mixin('koowa:behavior.mixin', $config);
    }

    /**
     * Initializes the default configuration for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param   KObjectConfig $config Configuration options.
     * @return void
     */
    protected function _initialize(KObjectConfig $config)
    {
        $config->append(array(
            'command_chain'     => 'koowa:command.chain',
            'event_publisher'   => 'event.publisher',
            'enable_events'     => true,
            'enable_callbacks'  => true,
            'dispatched'		=> false,
            'request'           => 'koowa:controller.request',
            'response'          => 'koowa:controller.response',
            'user'              => 'koowa:user',
            'behaviors'         => array('permissible'),
            'query'             => array()
        ));

        parent::_initialize($config);
    }

	/**
     * Has the controller been dispatched
     *
     * @return  boolean	Returns true if the controller has been dispatched
     */
    public function isDispatched()
    {
        return $this->_dispatched;
    }

    /**
     * Execute an action by triggering a method in the derived class.
     *
     * @param   string                      $action  The action to execute
     * @param   KControllerContextInterface $context A command context object
     * @throws  Exception
     * @throws  BadMethodCallException
     * @return  mixed|bool The value returned by the called method, false in error case.
     */
    public function execute($action, KControllerContextInterface $context)
    {
        $action  = strtolower($action);

        //Set the context subject
        $context_subject = $context->getSubject();
        $context->setSubject($this);

        //Set the context action
        $context->action  = $action;

        //Execute the action
        if($this->invokeCommand('before.'.$action, $context, false) !== false)
        {
            $method = '_action' . ucfirst($action);

            if (!method_exists($this, $method))
            {
                if (isset($this->_mixed_methods[$action]))
                {
                    $context->setName('action.' . $action);
                    $context->result = $this->_mixed_methods[$action]->execute($context);
                }
                else
                {
                    throw new KControllerExceptionNotImplemented(
                        "Can't execute '$action', method: '$method' does not exist"
                    );
                }
            }
            else  $context->result = $this->$method($context);

            $this->invokeCommand('after.'.$action, $context);
        }

        //Reset the context subject
        $context->setSubject($context_subject);

        return $context->result;
    }

    /**
     * Mixin an object
     *<commit_msg>re #91 : KControllerAbstract now extends from KCommandInvokerAbstract and mixes the KEventMixinInterface. Events are automatically being published through the event invoker.<commit_after><?php
/**
 * Koowa Framework - http://developer.joomlatools.com/koowa
 *
 * @copyright	Copyright (C) 2007 - 2013 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link		http://github.com/joomlatools/koowa for the canonical source repository
 */

/**
 * Abstract Controller
 *
 * Note: Concrete controllers must have a singular name
 *
 * @author  <NAME> <https://github.com/johanjanssens>
 * @package Koowa\Library\Controller
 */
abstract class KControllerAbstract extends KCommandInvokerAbstract implements KControllerInterface
{
    /**
     * The class actions
     *
     * @var array
     */
    protected $_actions = array();

    /**
     * Response object or identifier
     *
     * @var	string|object
     */
    protected $_response;

    /**
     * Request object or identifier
     *
     * @var	string|object
     */
    protected $_request;

    /**
     * User object or identifier
     *
     * @var	string|object
     */
    protected $_user;

    /**
     * Has the controller been dispatched
     *
     * @var boolean
     */
    protected $_dispatched;

    //Status codes
    const STATUS_SUCCESS   = KHttpResponse::OK;
    const STATUS_CREATED   = KHttpResponse::CREATED;
    const STATUS_ACCEPTED  = KHttpResponse::ACCEPTED;
    const STATUS_UNCHANGED = KHttpResponse::NO_CONTENT;
    const STATUS_RESET     = KHttpResponse::RESET_CONTENT;

    /**
     * Constructor.
     *
     * @param   KObjectConfig $config Configuration options.
     */
    public function __construct( KObjectConfig $config)
    {
        parent::__construct($config);

        //Set the dispatched state
        $this->_dispatched = $config->dispatched;

        // Set the model identifier
        $this->_request = $config->request;

        // Set the view identifier
        $this->_response = $config->response;

        // Set the user identifier
        $this->_user = $config->user;

        //Set the query in the request
        if(!empty($config->query)) {
            $this->getRequest()->query->add(KObjectConfig::unbox($config->query));
        }

        // Mixin the behavior (and command) interface
        $this->mixin('koowa:behavior.mixin', $config);

        // Mixin the event interface
        $this->mixin('koowa:event.mixin', $config);
    }

    /**
     * Initializes the default configuration for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param   KObjectConfig $config Configuration options.
     * @return void
     */
    protected function _initialize(KObjectConfig $config)
    {
        $config->append(array(
            'command_chain'     => 'koowa:command.chain',
            'command_invokers'  => array('koowa:command.invoker.event'),
            'dispatched'		=> false,
            'request'           => 'koowa:controller.request',
            'response'          => 'koowa:controller.response',
            'user'              => 'koowa:user',
            'behaviors'         => array('permissible'),
            'query'             => array()
        ));

        parent::_initialize($config);
    }

	/**
     * Has the controller been dispatched
     *
     * @return  boolean	Returns true if the controller has been dispatched
     */
    public function isDispatched()
    {
        return $this->_dispatched;
    }

    /**
     * Execute an action by triggering a method in the derived class.
     *
     * @param   string                      $action  The action to execute
     * @param   KControllerContextInterface $context A command context object
     * @throws  Exception
     * @throws  BadMethodCallException
     * @return  mixed|bool The value returned by the called method, false in error case.
     */
    public function execute($action, KControllerContextInterface $context)
    {
        $action  = strtolower($action);

        //Set the context subject
        $context_subject = $context->getSubject();
        $context->setSubject($this);

        //Set the context action
        $context->action  = $action;

        //Execute the action
        if($this->invokeCommand('before.'.$action, $context) !== false)
        {
            $method = '_action' . ucfirst($action);

            if (!method_exists($this, $method))
            {
                if (isset($this->_mixed_methods[$action]))
                {
                    $context->setName('action.' . $action);
                    $context->result = $this->_mixed_methods[$action]->execute($context);
                }
                else
                {
                    throw new KControllerExceptionNotImplemented(
                        "Can't execute '$action', method: '$method' does not exist"
                    );
                }
            }
            else  $context->result = $this->$method($context);

            $this->invokeCommand('after.'.$action, $context);
        }

        //Reset the context subject
        $context->setSubject($context_subject);

        return $context->result;
    }

    /**
     * Mixin an object
     *
{'fraction_non_alphanumeric': 0.11649089165867689, 'fraction_numerical': 0.0019175455417066154, 'mean_word_length': 1.984549356223176, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 90, 'https://': 2, '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': '5447034', 'n_tokens_mistral': 2879, 'n_tokens_neox': 2750, 'n_words': 690}