<commit_before><?php
/**
 * LegacyController - A base controller with legacy API support.
 *
 * @author <NAME> - <EMAIL>
 * @version 3.0
 */

namespace App\Legacy;

use App\Core\Controller as BaseController;
use Language\Language;
use Http\Request;
use Http\Response;
use Routing\Route;

use BadMethodCallException;


abstract class Controller extends BaseController
{
    /**
     * The requested Method by Router.
     *
     * @var string|null
     */
    private $method = null;

    /**
     * The parameters given by Router.
     *
     * @var array
     */
    private $params = array();

    /**
     * Language variable to use the languages class.
     *
     * @var string
     */
    public $language = null;


    /**
     * Create a new Controller instance.
     */
    public function __construct()
    {
        parent::__construct();

        // Initialise the Language object.
        if ($this->language !== false) {
            $this->language = Language::getInstance();
        }

        // Setup the (legacy) Middleware.
        $this->setupMiddleware();
    }

    /**
     *  Setup the (legacy) Middleware.
     *
     * @return void
     + @throw \BadMethodCallException
     */
    private function setupMiddleware()
    {
        $me = $this;

        // Get the Route's parameters and method name, optionally call the Before Middleware.
        $this->beforeFilter(function(Route $route, Request $request) use ($me)
        {
            // Setup the call parameters from the Route instance.
            $me->params = $route->getParams();

            // Setup the called method from the Route instance.
            $action = $route->getAction();

            if (isset($action['controller']) && str_contains($action['controller'], '@')) {
                list(, $method) = explode('@', $action['controller']);

                // Store the method name.
                $me->method = $method;
            } else {
                throw new BadMethodCallException('No valid Controller found on Route action');
            }

            // Execute the Controller's Before Middleware.
            return $me->before();
        });

        // Setup the Controller's After Middleware.
        $this->afterFilter(function(Route $route, Request $request, $response) use ($me)
        {
            $me->after($response);
        });
    }

    /**
     * The (legacy) Middleware called before the Action execution.
     *
     * @return mixed|void
     */
    protected function before()
    {
        //
    }

    /**
     * The (legacy) Middleware called after the Action execution.
     *
     * @param mixed $response
     *
     * @return void
     */
    protected function after($response)
    {
        //
    }

    /**
     * Create from the given result a Response instance and send it.
     *
     * @param mixed  $response
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    protected function processResponse($response)
    {
        // If the response which is returned from the Controller's Action is null and we have
        // View instances on View's Legacy support, we will assume that we are on Legacy Mode.

        if (is_null($response)) {
            return $this->createResponse();
        }

        return parent::processResponse($response);
    }

    /**
     * Create a Response instance from the legacy View API and return it.
     *
     * @return \Http\Response
     */
    protected function createResponse()
    {
        $items = View::getItems();

        $headers = View::getHeaders();

        // Render the View instances to response.
        $response = '';

        foreach ($items as $item) {
            $response .= $item->render();
        }

        // Create a Response instance and return it.
        return new Response($response, 200, $headers);
    }

    /**
     * Return a translated string.<commit_msg>Improve App\Legacy\Controller<commit_after><?php
/**
 * LegacyController - A base controller with legacy API support.
 *
 * @author <NAME> - <EMAIL>
 * @version 3.0
 */

namespace App\Legacy;

use App\Core\Controller as BaseController;
use Language\Language;
use Http\Request;
use Http\Response;
use Routing\Route;

use BadMethodCallException;


abstract class Controller extends BaseController
{
    /**
     * The requested Method by Router.
     *
     * @var string|null
     */
    private $method = null;

    /**
     * The parameters given by Router.
     *
     * @var array
     */
    private $params = array();

    /**
     * Language variable to use the languages class.
     *
     * @var string
     */
    public $language = null;


    /**
     * Create a new Controller instance.
     */
    public function __construct()
    {
        parent::__construct();

        // Initialise the Language object.
        if ($this->language !== false) {
            $this->language = Language::getInstance();
        }

        // Setup the (legacy) Middleware.
        $this->beforeFilter('@callLegacyBefore');

        $this->afterFilter('@callLegacyAfter');
    }

    /**
     * Setup the Controller's parameters and method name.
     *
     * @param \Routing\Route $route
     * @return void
     */
    protected function setupController(Route $route, Request $request)
    {
        $action = $route->getAction();

        if (isset($action['controller'])) {
            list($class, $method) = explode('@', $action['controller']);

            $this->method = $method;
        }

        $this->params = $route->getParams();
    }

    /**
     *  Call the (legacy) Before Middleware.
     *
     * @return mixed
     */
    public function callLegacyBefore(Route $route, Request $request)
    {
        // Setup the Controller instance.
        $this->setupController($route, $request);

        // Execute the Controller's Before Middleware.
        return $this->before();
    }

    /**
     *  Call the (legacy) After Middleware.
     *
     * @return void
     */
    public function callLegacyAfter(Route $route, Request $request, $response)
    {
        // Execute the Controller's After Middleware.
        $this->after($response);
    }

    /**
     * The (legacy) Middleware called before the Action execution.
     *
     * @return mixed|void
     */
    protected function before()
    {
        //
    }

    /**
     * The (legacy) Middleware called after the Action execution.
     *
     * @param mixed $response
     *
     * @return void
     */
    protected function after($response)
    {
        //
    }

    /**
     * Create from the given result a Response instance and send it.
     *
     * @param mixed  $response
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    protected function processResponse($response)
    {
        // If the response which is returned from the Controller's Action is null and we have
        // View instances on View's Legacy support, we will assume that we are on Legacy Mode.

        if (is_null($response)) {
            return $this->createLegacyResponse();
        }

        return parent::processResponse($response);
    }

    /**
     * Create a Response instance from the legacy View API and return it.
     *
     * @return \Http\Response
     */
    protected function createLegacyResponse()
    {
        $items = View::getItems();

        $headers = View::getHeaders();

        // Render the View instances to response.
        $response = '';

        foreach ($items as $item) {
            $response .= $item->render();
        }

        // Create a Response instance and return it.
        return new Response($response, 200, $headers);
    }

    /**
     * Return a translated string.
{'fraction_non_alphanumeric': 0.11750226713304832, 'fraction_numerical': 0.0012955045990413266, 'mean_word_length': 1.9387133612485725, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7556037', 'n_tokens_mistral': 2091, 'n_tokens_neox': 2016, 'n_words': 619}