Pass the response to Joomla

@param   KDispatcherContextInterface $context A command context object
@return 	bool
17607
protected function _beforeTerminate(KDispatcherContextInterface $context)
    {
        $response = $context->getResponse();

        //Pass back to Joomla
        if(!$response->isRedirect() && !$response->isDownloadable() && $this->getDecorator() == 'joomla')
        {
            // #237 - Since Joomla will wrap the page we do not know the actual content-length and should not send it
            $response->getHeaders()->remove('Content-length');
            header_remove('Content-Length');

            //Contenttype
            JFactory::getDocument()->setMimeEncoding($response->getContentType());

            //Set messages for any request method
            $messages = $response->getMessages();
            foreach($messages as $type => $group)
            {
                if ($type === 'success') {
                    $type = 'message';
                }

                foreach($group as $message) {
                    JFactory::getApplication()->enqueueMessage($message, $type);
                }
            }

            //Set the cache state
            JFactory::getApplication()->allowCache($context->getRequest()->isCacheable());

            //Do not flush the response
            return false;
        }
    }
80570
protected function render(IComponentResponse $response, IContext $context)
    {
        $content = $response->getContent();

        if ($content instanceof IDisplayModel) {
            $view = $this->getView();

            if ($view instanceof IContextAware) {
                $view->setContext($context);
            }

            try {
                $content = $view->render($content->getTemplate(), $content->getVariables());
            } catch (\Exception $e) {
                throw new RuntimeException(
                    $this->translate(
                        'Cannot render template "{template}".',
                        [
                            'template' => $content->getTemplate()
                        ]
                    ),
                    0,
                    $e
                );
            }

            $response->setContent($content);

            if ($view instanceof IContextAware) {
                $view->clearContext();
            }
        }

        return $response;
    }
72644
protected function _actionOptions(AnCommandContext $context)
    {
        $context->response->status = AnHttpResponse::OK;
        $context->response->set('Content-Length', 0);
    }
118405
protected function _actionForward(AnCommandContext $context)
    {
        $response = $this->getController()->getResponse();

        if (! $response->getContent()) {
            if (in_array($response->getStatusCode(), array(201, 205))) {
                $view = $this->getController()->getIdentifier()->name;
                $response->setContent($this->getController()->view($view)->execute('display', $context));

                if ($response->getStatusCode() == 205) {
                    $response->setStatus(200);
                }
            }
        }

        return $context->result;
    }
1022025
protected function _actionAdd(KControllerContextInterface $context)
    {
        if(!$context->result instanceof KModelEntityInterface) {
            $entity = $this->getModel()->create($context->request->data->toArray());
        } else {
            $entity = $context->result;
        }

        //Only throw an error if the action explicitly failed.
        if($entity->save() === false)
        {
            $error = $entity->getStatusMessage();
            throw new KControllerExceptionActionFailed($error ? $error : 'Add Action Failed');
        }
        else
        {
            if ($entity instanceof KModelEntityInterface)
            {
                $url = clone $context->request->getUrl();

                if ($this->getModel()->getState()->isUnique())
                {
                    $states = $this->getModel()->getState()->getValues(true);

                    foreach ($states as $key => $value) {
                        $url->query[$key] = $entity->getProperty($key);
                    }
                }
                else $url->query[$entity->getIdentityKey()] = $entity->getProperty($entity->getIdentityKey());

                $context->response->headers->set('Location', (string) $url);
                $context->response->setStatus(KHttpResponse::CREATED);
            }
        }

        return $entity;
    }
695046
protected function _actionSend(DispatcherContext $context)
    {
        //Send the response
        $context->response->send(false);

        //Terminate the response
        $this->terminate($context);
    }
717653
protected function _actionDelete($context)
    {
        $context->response->status = AnHttpResponse::NO_CONTENT;
        $this->getItem()->delete();
        $context->response->setRedirect(route($this->getItem()->owner->getURL()));
    }
29377
protected function _actionFail(KDispatcherContextInterface $context)
    {
        //Check an exception was passed
        if(!isset($context->param) && !$context->param instanceof KException)
        {
            throw new InvalidArgumentException(
                "Action parameter 'exception' [KException] is required"
            );
        }

        //Get the exception object
        if($context->param instanceof KEventException) {
            $exception = $context->param->getException();
        } else {
            $exception = $context->param;
        }

        //If the error code does not correspond to a status message, use 500
        $code = $exception->getCode();
        if(!isset(KHttpResponse::$status_messages[$code])) {
            $code = '500';
        }

        //Get the error message
        $message = KHttpResponse::$status_messages[$code];

        //Set the response status
        $context->response->setStatus($code , $message);

        //Send the response
        $this->send($context);
    }
1373554
protected function _beforeControllerGet(AnCommandContext $context)
    {
        if ($this->_mixer->getRequest()->get == 'accesstoken') {
            $this->_mixer->execute('getaccesstoken', $context);
            return false;
        } else {
            $this->api = $this->getAPI();
        }
    }
1059537
protected function execute(Context $context)
    {
        $this->logger->debug('kernel.execute: execute logic');

        // prepare params
        $params = $context->params;
        $params[] = $context;

        // execute logic method
        $result = call_user_func_array($context->logic->method, $params);
        $this->logger->debug('kernel.execute: #' . $context->logic->name . ' executed');

        // user response
        if($result instanceof Response) {
            $context->response = $result;
            $this->logger->debug('kernel.execute: response provided as result of execution');
        }
        // raw response
        else {
            $context->response->raw = true;
            $context->response->content = $result;
            $this->logger->debug('kernel.execute: raw content provided as result of execution');
        }

        return $context;
    }
344962
public function authenticateRequest(KDispatcherContextInterface $context)
    {
        if(!$context->user->isAuthentic() && $username = $this->getUsername()) {
            return $this->_loginUser($username);
        }

        return false;
    }
603693
protected function _afterReset(ModelContextInterface $context)
    {
        if($context->modified == 'sort' && strpos($context->state->sort, ',') !== false)
        {
            $context->state->sort = explode(',', $context->state->sort);

            foreach($context->state->sort as $key => $value)
            {
                if(strtoupper($value) == 'DESC' || strtoupper($value) == 'ASC')
                {
                    unset($context->state->sort[$key]);
                    $context->state->direction = $value;
                }
            }
        }
    }
1219600
public function getContext()
    {
        $context = new KDispatcherContext();

        $context->setSubject($this);
        $context->setRequest($this->getRequest());
        $context->setResponse($this->getResponse());
        $context->setUser($this->getUser());

        return $context;
    }
1094944
protected function _afterEdit(KControllerContextInterface $context)
    {
        $name   = $this->getMixer()->getIdentifier()->name;

        foreach ($context->result as $entity)
        {
            if ($entity->getStatus() !== KDatabase::STATUS_FAILED)
            {
                if ($name === $this->_category_entity)
                {
                    if (($entity->old_enabled !== $entity->enabled) || ($entity->old_access !== $entity->access))
                    {
                        $category_context = 'com_'.$this->_package.'.'.$this->_category_entity;

                        $this->_getDispatcher()->trigger('onFinderAfterSave', array($category_context, $entity, false));
                    }
                } else {
                    $this->_getDispatcher()->trigger('onFinderAfterSave', array($this->_event_context, $entity, false));
                }
            }
        }
    }
911674
protected function _actionDelete(AnCommandContext $context)
    {
        $context->response->status = AnHttpResponse::NO_CONTENT;
        $entity = $this->getItem()->delete();

        return $entity;
    }
73096
protected function _actionDispatch(AnCommandContext $context)
    {
        dispatch_plugin('system.onBeforeDispatch', array( $context ));
        
        $name = 'com_'.$this->getComponent()->getIdentifier()->package;

        define('ANPATH_COMPONENT', ANPATH_BASE.DS.'components'.DS.$name);

        if (! file_exists(ANPATH_COMPONENT)) {
            throw new LibBaseControllerExceptionNotFound(
                'Component '.$name.' not found',
                AnHttpResponse::INTERNAL_SERVER_ERROR
            );
        }

        $app = AnService::get('repos:settings.app')->find(array('package' => $name));

        if (isset($app) && $app->enabled != 1) {
            throw new LibBaseControllerExceptionForbidden(
                'Component '.$name.' is disabled',
                AnHttpResponse::INTERNAL_SERVER_ERROR
            );
        }

        $this->getComponent()->dispatch($context);

        dispatch_plugin('system.onAfterDispatch', array( $context ));

        $location = $context->response->getHeader('Location');
        $isHtml = $context->request->getFormat() == 'html';
        $isAjax = $context->request->isAjax();

        if (! $location && $isHtml && !$isAjax) {
            $this->_render($context);
        }

        $this->send($context);
    }
656279
protected function _actionSave2new(KControllerContextInterface $context)
    {
        // Cache and lock the referrer since _ActionSave would unset it
        $referrer = $this->getReferrer($context);
        $this->_lockReferrer($context);

        $entity = $this->save($context);

        // Re-set the referrer
        $cookie = $this->getObject('lib:http.cookie', array(
            'name'   => $this->_cookie_name,
            'value'  => (string) $referrer,
            'path'   => $this->_cookie_path
        ));

        $context->response->headers->addCookie($cookie);

        $identifier = $this->getMixer()->getIdentifier();
        $view       = KStringInflector::singularize($identifier->name);
        $url        = sprintf('index.php?option=com_%s&view=%s', $identifier->package, $view);

        $context->response->setRedirect($this->getObject('lib:http.url',array('url' => $url)));

        return $entity;
    }