<commit_before><?php

declare(strict_types=1);

namespace Cortex\Foundation\Overrides\Illuminate\Http;

use Throwable;
use Illuminate\Support\Str;
use Illuminate\Http\Request as BaseRequest;

class Request extends BaseRequest
{
    /**
     * Get the URL (no query string) for the request.
     *
     * @return string
     */
    public function url(): string
    {
        return parent::url().(config('cortex.foundation.route.trailing_slash') ? '/' : '');
    }

    /**
     * Check if this is an API request.
     *
     * @return boolean
     */
    public function isApi(): bool
    {
        try {
            if ($route = $this->route()) {
                // 1. Route matched through name or uri, and is an API request (ex. /api/users)
                if ($segment = $route->getName()) {
                    $match = Str::before($segment, '.');
                } else {
                    $segment = $route->uri();
                    $match = Str::before($segment, '/');
                }

                if ($match !== 'api') {
                    // 2. Route matched through middleware, and is an API request (ex. /api/users)
                    $match = collect($route->gatherMiddleware())->first(function ($middleware) {
                        return Str::contains($middleware, 'api:');
                    });
                }
            }

            // 3. Catch other use cases:
            // 3.1. Route NOT an API request
            // 3.2. Route NOT matched / Wrong URL (ex. 404 error)
            // 3.3. Route matched but NOT a valid accessarea (could happen if route is mistakenly named, make sure route names contain valid accessarea prefix)
            return isset($match) && $match === 'api';
        } catch (Throwable $e) {
            return false;
        }
    }

    /**
     * Get access area for current request.
     *
     * @return string
     */
    public function getAccessArea(): string
    {
        try {
            if ($route = $this->route()) {
                // 1. Route matched and is an accessarea request (ex. /adminarea/users)
                if ($segment = $route->getName()) {
                    $area = Str::before($segment, '.');
                } else {
                    $segment = $route->uri();
                    $area = Str::before($segment, '/');
                }

                if (! array_key_exists($area, config('cortex.foundation.route.prefix'))) {
                    // 2. Route matched and is an API request (ex. /api/users)
                    $middleware = collect($route->gatherMiddleware())->first(function ($middleware) {
                        return Str::contains($middleware, 'api:');
                    });

                    if ($middlewareGuard = Str::after($middleware, 'api:')) {
                        $area = $middlewareGuard.'area';
                    }
                }
            }

            // 3. Catch other use cases:
            // 3.1. Route NOT matched / Wrong URL (ex. 404 error)
            // 3.2. Route matched but NOT a valid accessarea (could happen if route is mistakenly named, make sure route names contain valid accessarea prefix)
            return isset($area) && array_key_exists($area, config('cortex.foundation.route.prefix')) ? $area : 'frontarea';
        } catch (Throwable $e) {
            // We can't afford any kind of exceptions here, as this is used in the exception handler itself!
            // Imagine if the exception handler, thrown an exception! How, who, and where else to catch?!
            return 'frontarea';
        }
    }
}<commit_msg>Cache accessacrea value for current request<commit_after><?php

declare(strict_types=1);

namespace Cortex\Foundation\Overrides\Illuminate\Http;

use Throwable;
use Illuminate\Support\Str;
use Illuminate\Http\Request as BaseRequest;

class Request extends BaseRequest
{
    /**
     * The access area name.
     *
     * @var string
     */
    protected $accessarea;

    /**
     * Get the URL (no query string) for the request.
     *
     * @return string
     */
    public function url(): string
    {
        return parent::url().(config('cortex.foundation.route.trailing_slash') ? '/' : '');
    }

    /**
     * Check if this is an API request.
     *
     * @return boolean
     */
    public function isApi(): bool
    {
        try {
            if ($route = $this->route()) {
                // 1. Route matched through name or uri, and is an API request (ex. /api/users)
                if ($segment = $route->getName()) {
                    $match = Str::before($segment, '.');
                } else {
                    $segment = $route->uri();
                    $match = Str::before($segment, '/');
                }

                if ($match !== 'api') {
                    // 2. Route matched through middleware, and is an API request (ex. /api/users)
                    $match = collect($route->gatherMiddleware())->first(function ($middleware) {
                        return Str::contains($middleware, 'api:');
                    });
                }
            }

            // 3. Catch other use cases:
            // 3.1. Route NOT an API request
            // 3.2. Route NOT matched / Wrong URL (ex. 404 error)
            // 3.3. Route matched but NOT a valid api name (could happen if route is mistakenly named, make sure route names contain valid api prefix)
            return isset($match) && $match === 'api';
        } catch (Throwable $e) {
            return false;
        }
    }

    /**
     * Get access area for current request.
     *
     * @return string
     */
    public function accessarea(): string
    {
        if (! is_null($this->accessarea)) {
            return $this->accessarea;
        }

        try {
            if ($route = $this->route()) {
                // 1. Route matched and is an accessarea request (ex. /adminarea/users)
                if ($segment = $route->getName()) {
                    $area = Str::before($segment, '.');
                } else {
                    $segment = $route->uri();
                    $area = Str::before($segment, '/');
                }

                if (! array_key_exists($area, config('cortex.foundation.route.prefix'))) {
                    // 2. Route matched and is an API request (ex. /api/users)
                    $middleware = collect($route->gatherMiddleware())->first(function ($middleware) {
                        return Str::contains($middleware, 'api:');
                    });

                    if ($middlewareGuard = Str::after($middleware, 'api:')) {
                        $area = $middlewareGuard.'area';
                    }
                }
            }

            // 3. Catch other use cases:
            // 3.1. Route NOT matched / Wrong URL (ex. 404 error)
            // 3.2. Route matched but NOT a valid accessarea (could happen if route is mistakenly named, make sure route names contain valid accessarea prefix)
            return $this->accessarea = isset($area) && array_key_exists($area, config('cortex.foundation.route.prefix')) ? $area : 'frontarea';
        } catch (Throwable $e) {
            // We can't afford any kind of exceptions here, as this is used in the exception handler itself!
            // Imagine if the exception handler, thrown an exception! How, who, and where else to catch?!
            return $this->accessarea = 'frontarea';
        }
    }
}
{'fraction_non_alphanumeric': 0.12580645161290321, 'fraction_numerical': 0.006182795698924731, 'mean_word_length': 1.5552884615384615, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '6876269', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1890, 'n_words': 596}