PHP Classes

File: src/Module.php

Recommend this page to a friend!
  Classes of Thierry Feuzeu   Jaxon for Yii   src/Module.php   Download  
File: src/Module.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Jaxon for Yii
Yi plugin to call PHP from with AJAX
Author: By
Last change: The Container class now implements the PSR-11 ContainerInterface.
Added a Logger class.
Added a gateway to the DI container.
Removed unused argument.
Updated the plugin and the controller.
Renamed the bootstrap methods.
Adapted to the new class hierarchy is jaxon-core.
Updated the Armada setup.
Date: 3 years ago
Size: 3,447 bytes
 

Contents

Class file image Download
<?php

namespace Jaxon\Yii;

use
Yii;
use
yii\web\Response;

class
Module extends \yii\base\Module
{
    use \
Jaxon\Features\App;

   
/**
     * Default route for this package
     *
     * @var string
     */
   
public $defaultRoute = 'jaxon';

   
/**
     * Namespace of the controllers in this package
     *
     * @var string
     */
   
public $controllerNamespace = 'Jaxon\\Yii\\Controllers';

   
/**
     * Create a new Jaxon instance.
     *
     * @return void
     */
   
public function __construct()
    {
       
// Call the parent contructor after member initialisation
       
parent::__construct('jaxon');
    }

   
/**
     * Initialise the Jaxon module.
     *
     * @return void
     */
   
public function init()
    {
       
$bIsDebug = ((YII_ENV_DEV) ? true : false);
       
$sAppPath = rtrim(Yii::getAlias('@app'), '/');
       
$sJsUrl = rtrim(Yii::getAlias('@web'), '/') . '/jaxon/js';
       
$sJsDir = rtrim(Yii::getAlias('@webroot'), '/') . '/jaxon/js';

       
$jaxon = jaxon();
       
$di = $jaxon->di();

       
// Read the config options.
       
$aOptions = $jaxon->config()->read($sAppPath . '/config/jaxon.php');
       
$aLibOptions = key_exists('lib', $aOptions) ? $aOptions['lib'] : [];
       
$aAppOptions = key_exists('app', $aOptions) ? $aOptions['app'] : [];

       
$viewManager = $di->getViewManager();
       
// Set the default view namespace
       
$viewManager->addNamespace('default', '', '', 'yii');
       
// Add the view renderer
       
$viewManager->addRenderer('yii', function() {
            return new
View();
        });

       
// Set the session manager
       
$di->setSessionManager(function() {
            return new
Session();
        });

       
// Set the framework service container wrapper
       
$di->setAppContainer(new Container());

       
// Set the logger
       
$this->setLogger(new Logger());

       
$this->bootstrap()
            ->
lib($aLibOptions)
            ->
app($aAppOptions)
           
// ->uri($sUri)
           
->js(!$bIsDebug, $sJsUrl, $sJsDir, !$bIsDebug)
            ->
run();

       
// Prevent the Jaxon library from sending the response or exiting
       
$jaxon->setOption('core.response.send', false);
       
$jaxon->setOption('core.process.exit', false);
    }

   
/**
     * Get the HTTP response
     *
     * @param string $code The HTTP response code
     *
     * @return mixed
     */
   
public function httpResponse($code = '200')
    {
       
$jaxon = jaxon();
       
// Get the reponse to the request
       
$jaxonResponse = $jaxon->di()->getResponseManager()->getResponse();
        if(!
$jaxonResponse)
        {
           
$jaxonResponse = $jaxon->getResponse();
        }

       
// Create and return a Yii HTTP response
       
header('Content-Type: ' . $jaxonResponse->getContentType() .
           
'; charset=' . $jaxonResponse->getCharacterEncoding());
       
Yii::$app->response->format = Response::FORMAT_JSON;
       
Yii::$app->response->statusCode = $code;
       
Yii::$app->response->content = $jaxonResponse->getOutput();
        return
Yii::$app->response;
    }

   
/**
     * Process an incoming Jaxon request, and return the response.
     *
     * @return mixed
     */
   
public function processRequest()
    {
       
// Process the jaxon request
       
jaxon()->processRequest();

       
// Return the reponse to the request
       
return $this->httpResponse();
    }
}