<commit_before><?php


namespace app\libraries\routers;

use app\libraries\response\RedirectResponse;
use app\libraries\response\Response;
use app\libraries\response\JsonResponse;
use app\libraries\response\WebResponse;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
use Doctrine\Common\Annotations\AnnotationReader;
use app\libraries\Utils;
use app\libraries\Core;


class WebRouter {
    /** @var Core  */
    protected $core;

    /** @var Request  */
    protected $request;

    /** @var bool */
    protected $logged_in;

    /** @var AnnotationReader */
    protected $reader;

    /** @var array */
    protected $parameters;

    /** @var string the controller to call */
    protected $controller_name;

    /** @var string the method to call */
    protected $method_name;

    private function __construct(Request $request, Core $core, $logged_in) {
        $this->core = $core;
        $this->request = $request;
        $this->logged_in = $logged_in;

        $fileLocator = new FileLocator();
        /** @noinspection PhpUnhandledExceptionInspection */
        $this->reader = new AnnotationReader();
        $annotationLoader = new AnnotatedRouteLoader($this->reader);
        $loader = new AnnotationDirectoryLoader($fileLocator, $annotationLoader);
        $collection = $loader->load(realpath(__DIR__ . "/../../controllers"));
        $context = new RequestContext();

        $matcher = new UrlMatcher($collection, $context->fromRequest($this->request));
        $this->parameters = $matcher->matchRequest($this->request);
    }

    /**
     * @param Request $request
     * @param Core $core
     * @param $logged_in
     * @return Response|mixed should be of type Response only in the future
     */
    static public function getApiResponse(Request $request, Core $core, $logged_in) {
        try {
            $router = new self($request, $core, $logged_in);

            // prevent user that is not logged in from going anywhere except AuthenticationController
            if (!$logged_in &&
                !Utils::endsWith($router->parameters['_controller'], 'AuthenticationController')) {
                return new Response(JsonResponse::getFailResponse("Unauthenticated access. Please log in."));
            }
        }
        catch (ResourceNotFoundException $e) {
            return new Response(JsonResponse::getFailResponse("Endpoint not found."));
        }
        catch (MethodNotAllowedException $e) {
            return new Response(JsonResponse::getFailResponse("Method not allowed."));
        }
        catch (\Exception $e) {
            return new Response(JsonResponse::getErrorResponse($e->getMessage()));
        }

        $core->getOutput()->disableRender();
        $core->disableRedirects();
        return $router->run();
    }

    /**
     * @param Request $request
     * @param Core $core
     * @param $logged_in
     * @return Response|mixed should be of type Response only in the future
     */
    static public function getWebResponse(Request $request, Core $core, $logged_in) {
        try {
            $router = new self($request, $core, $logged_in);

            $course_check_response = $router->loadCourses();
            if ($course_check_response instanceof Response) {
                return $course_check_response;
            }

            $login_check_response = $router->loginCheck();
            if ($login_check_response instanceof Response) {
                return $login_check_response;
            }

            $csrf_check_response = $router->csrfCheck();
            if ($csrf_check_response instanceof Response) {
                return $csrf_check_response;
            }
          
            if (!$router->accessCheck()) {
                return new Response(
                    JsonResponse::getFailResponse("You don't have access to this endpoint."),
                    new WebResponse("Error", "errorPage", "You don't have access to this page.")
                );
            }
        }
        catch (ResourceNotFoundException | MethodNotAllowedException $e) {
            // redirect to login page or home page
            if (!$logged_in) {
                return Response::RedirectOnlyResponse(
                    new RedirectResponse($core->buildNewUrl(['authentication', 'login']))
                );
            }
            else {
                return Response::RedirectOnlyResponse(
                    new RedirectResponse($core->buildNewUrl(['home']))
                );
            }
        }

        return $router->run();
    }

    private function run() {
        $this->controller_name = $this->parameters['_controller'];
        $this->method_name = $this->parameters['_method'];
        $controller = new $this->controller_name($this->core);

        $arguments = array();
        /** @noinspection PhpUnhandledExceptionInspection */
        $method = new \ReflectionMethod($this->controller_name, $this->method_name);
        foreach ($method->getParameters() as $param) {
            $param_name = $param->getName();
            $arguments[$param_name] = $this->parameters[$param_name] ?? null;
            if (!isset($arguments[$param_name])) {
                $arguments[$param_name] = $this->request->query->get($param_name);
            }
        }
<commit_msg>[Bugfix: Router] Router GET param load default values  (#4222)<commit_after><?php


namespace app\libraries\routers;

use app\libraries\response\RedirectResponse;
use app\libraries\response\Response;
use app\libraries\response\JsonResponse;
use app\libraries\response\WebResponse;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
use Doctrine\Common\Annotations\AnnotationReader;
use app\libraries\Utils;
use app\libraries\Core;


class WebRouter {
    /** @var Core  */
    protected $core;

    /** @var Request  */
    protected $request;

    /** @var bool */
    protected $logged_in;

    /** @var AnnotationReader */
    protected $reader;

    /** @var array */
    protected $parameters;

    /** @var string the controller to call */
    protected $controller_name;

    /** @var string the method to call */
    protected $method_name;

    private function __construct(Request $request, Core $core, $logged_in) {
        $this->core = $core;
        $this->request = $request;
        $this->logged_in = $logged_in;

        $fileLocator = new FileLocator();
        /** @noinspection PhpUnhandledExceptionInspection */
        $this->reader = new AnnotationReader();
        $annotationLoader = new AnnotatedRouteLoader($this->reader);
        $loader = new AnnotationDirectoryLoader($fileLocator, $annotationLoader);
        $collection = $loader->load(realpath(__DIR__ . "/../../controllers"));
        $context = new RequestContext();

        $matcher = new UrlMatcher($collection, $context->fromRequest($this->request));
        $this->parameters = $matcher->matchRequest($this->request);
    }

    /**
     * @param Request $request
     * @param Core $core
     * @param $logged_in
     * @return Response|mixed should be of type Response only in the future
     */
    static public function getApiResponse(Request $request, Core $core, $logged_in) {
        try {
            $router = new self($request, $core, $logged_in);

            // prevent user that is not logged in from going anywhere except AuthenticationController
            if (!$logged_in &&
                !Utils::endsWith($router->parameters['_controller'], 'AuthenticationController')) {
                return new Response(JsonResponse::getFailResponse("Unauthenticated access. Please log in."));
            }
        }
        catch (ResourceNotFoundException $e) {
            return new Response(JsonResponse::getFailResponse("Endpoint not found."));
        }
        catch (MethodNotAllowedException $e) {
            return new Response(JsonResponse::getFailResponse("Method not allowed."));
        }
        catch (\Exception $e) {
            return new Response(JsonResponse::getErrorResponse($e->getMessage()));
        }

        $core->getOutput()->disableRender();
        $core->disableRedirects();
        return $router->run();
    }

    /**
     * @param Request $request
     * @param Core $core
     * @param $logged_in
     * @return Response|mixed should be of type Response only in the future
     */
    static public function getWebResponse(Request $request, Core $core, $logged_in) {
        try {
            $router = new self($request, $core, $logged_in);

            $course_check_response = $router->loadCourses();
            if ($course_check_response instanceof Response) {
                return $course_check_response;
            }

            $login_check_response = $router->loginCheck();
            if ($login_check_response instanceof Response) {
                return $login_check_response;
            }

            $csrf_check_response = $router->csrfCheck();
            if ($csrf_check_response instanceof Response) {
                return $csrf_check_response;
            }
          
            if (!$router->accessCheck()) {
                return new Response(
                    JsonResponse::getFailResponse("You don't have access to this endpoint."),
                    new WebResponse("Error", "errorPage", "You don't have access to this page.")
                );
            }
        }
        catch (ResourceNotFoundException | MethodNotAllowedException $e) {
            // redirect to login page or home page
            if (!$logged_in) {
                return Response::RedirectOnlyResponse(
                    new RedirectResponse($core->buildNewUrl(['authentication', 'login']))
                );
            }
            else {
                return Response::RedirectOnlyResponse(
                    new RedirectResponse($core->buildNewUrl(['home']))
                );
            }
        }

        return $router->run();
    }

    private function run() {
        $this->controller_name = $this->parameters['_controller'];
        $this->method_name = $this->parameters['_method'];
        $controller = new $this->controller_name($this->core);

        $arguments = array();
        /** @noinspection PhpUnhandledExceptionInspection */
        $method = new \ReflectionMethod($this->controller_name, $this->method_name);
        foreach ($method->getParameters() as $param) {
            $param_name = $param->getName();
            $arguments[$param_name] = $this->parameters[$param_name] ?? null;
            if (!isset($arguments[$param_name])) {
                $arguments[$param_name] = $this->request->query->get($param_name);
            }
            if (!isset($arguments[$param_name])) {
                $arguments[$param_name] = $param->getDefaultValue();
            }
        }

{'fraction_non_alphanumeric': 0.12066101250327883, 'fraction_numerical': 0.0003497420652268952, 'mean_word_length': 2.451418225709113, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 80, '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': '399212', 'n_tokens_mistral': 2986, 'n_tokens_neox': 2921, 'n_words': 664}