PHP Classes

File: src/Jaxon.php

Recommend this page to a friend!
  Classes of Thierry Feuzeu   Jaxon for Symfony   src/Jaxon.php   Download  
File: src/Jaxon.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Jaxon for Symfony
Jaxon integration for the Symfony framework
Author: By
Last change: Use the $_SERVER data only when available.
Changed the default js url and dir.
Added a gateway to the Symfony service container.
Added logger setup.
Added a service container wrapper

But it is disabled. We first need to be able to get a class instance from the service container using the class name instead of the identifier.
Changed the Jaxon service d├ęclaration.
Removed unused argument.
Updated the plugin and the controller.
Renamed the bootstrap methods.
Adapted to the new class hierarchy is jaxon-core.
Date: 1 year ago
Size: 3,480 bytes
 

Contents

Class file image Download
<?php

namespace Jaxon\AjaxBundle;

use
Symfony\Component\HttpFoundation\Response as HttpResponse;
use
Symfony\Component\HttpKernel\KernelInterface;
use
Twig\Environment as TemplateEngine;
use
Psr\Log\LoggerInterface;

class
Jaxon
{
    use \
Jaxon\Features\App;

   
/**
     * The template engine
     *
     * @var EngineInterface
     */
    // protected $template;

    /**
     * Create a new Jaxon instance.
     *
     * @param KernelInterface $kernel
     * @param LoggerInterface $logger
     * @param TemplateEngine $template
     * @param array $config
     */
   
public function __construct(KernelInterface $kernel,
       
LoggerInterface $logger, TemplateEngine $template, array $config)
    {
       
// The application URL
       
$sJsUrl = \array_key_exists('SERVER_NAME', $_SERVER) ?
           
'//' . $_SERVER['SERVER_NAME'] . '/jaxon/js' : '/jaxon/js';
       
// The application web dir
       
$sJsDir = \array_key_exists('DOCUMENT_ROOT', $_SERVER) ?
           
'//' . $_SERVER['DOCUMENT_ROOT'] . '/jaxon/js' :
            \
rtrim($kernel->getProjectDir(), '/') . '/public/jaxon/js';
       
// Export and minify options
       
$bExportJs = $bMinifyJs = !$kernel->isDebug();

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

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

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

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

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

       
$this->bootstrap()
            ->
lib($config['lib'])
            ->
app($config['app'])
           
// ->uri($sUri)
           
->js($bExportJs, $sJsUrl, $sJsDir, $bMinifyJs)
            ->
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 Symfony HTTP response
       
$httpResponse = new HttpResponse();
       
$httpResponse->headers->set('Content-Type', $jaxonResponse->getContentType());
       
$httpResponse->setCharset($jaxonResponse->getCharacterEncoding());
       
$httpResponse->setStatusCode($code);
       
$httpResponse->setContent($jaxonResponse->getOutput());
        return
$httpResponse;
    }

   
/**
     * 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();
    }
}