PHP Classes

File: src/Di/Traits/ResponseTrait.php

Recommend this page to a friend!
  Packages of Thierry Feuzeu   Jaxon   src/Di/Traits/ResponseTrait.php   Download  
File: src/Di/Traits/ResponseTrait.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Jaxon
Call PHP classes from JavaScript using AJAX
Author: By
Last change:
Date: 1 year ago
Size: 1,947 bytes
 

Contents

Class file image Download
<?php

namespace Jaxon\Di\Traits;

use Jaxon\App\Config\ConfigManager;
use Jaxon\App\I18n\Translator;
use Jaxon\Di\Container;
use Jaxon\Plugin\Manager\PluginManager;
use Jaxon\Response\Response;
use Jaxon\Response\NodeResponse;
use Jaxon\Response\Manager\ResponseManager;
use Jaxon\Script\Call\JxnCall;

use function trim;

trait ResponseTrait
{
    /**
     * Register the values into the container
     *
     * @return void
     */
    private function registerResponses(): void
    {
        // Global Response
        $this->set(Response::class, function($di) {
            return new Response($di->g(ResponseManager::class), $di->g(PluginManager::class));
        });
        // Response Manager
        $this->set(ResponseManager::class, function($di) {
            $sEncoding = trim($di->g(ConfigManager::class)->getOption('core.encoding', ''));
            return new ResponseManager($di->g(Container::class), $di->g(Translator::class), $sEncoding);
        });
    }

    /**
     * Get the response manager
     *
     * @return ResponseManager
     */
    public function getResponseManager(): ResponseManager
    {
        return $this->g(ResponseManager::class);
    }

    /**
     * Get the global Response object
     *
     * @return Response
     */
    public function getResponse(): Response
    {
        return $this->g(Response::class);
    }

    /**
     * Create a new Jaxon response
     *
     * @return Response
     */
    public function newResponse(): Response
    {
        return new Response($this->g(ResponseManager::class), $this->g(PluginManager::class));
    }

    /**
     * Create a new reponse for a Jaxon component
     *
     * @param JxnCall $xJxnCall
     *
     * @return NodeResponse
     */
    public function newNodeResponse(JxnCall $xJxnCall): NodeResponse
    {
        return new NodeResponse($this->g(ResponseManager::class),
            $this->g(PluginManager::class), $xJxnCall);
    }
}