<commit_before><?php

namespace Routing\Assets;

use Config\Config;
use Http\Response;
use Routing\Assets\DispatcherInterface;
use Support\Str;

use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;

use Carbon\Carbon;

use LogicException;


class DefaultDispatcher implements DispatcherInterface
{
    /**
     * The valid Vendor paths.
     * @var array
     */
    protected $paths = array();

    /**
     * The currently accepted encodings for Response content compression.
     *
     * @var array
     */
    protected static $algorithms = array('gzip', 'deflate');


    /**
     * Create a new Default Dispatcher instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->paths = Config::get('routing.assets.paths', array());
    }

    /**
     * Dispatch a Assets File Response.
     *
     * @return \Symfony\Component\HttpFoundation\Response|null
     */
    public function dispatch(SymfonyRequest $request)
    {
        // For proper Assets serving, the file URI should be either of the following:
        //
        // /templates/default/assets/css/style.css
        // /modules/blog/assets/css/style.css
        // /assets/css/style.css

        $method = $request->method();

        $uri = $request->path();

        // Calculate the Asset File path, if it is a valid one.
        $path = null;

        if (preg_match('#^(templates|modules)/([^/]+)/assets/(.*)$#i', $uri, $matches)) {
            $folderName = Str::studly($matches[2]);

            $filePath = str_replace('/', DS, $matches[3]);

            $baseFolder = (strtolower($matches[1]) == 'modules') ? 'Modules' : 'Templates';

            $path = APPDIR .$baseFolder .DS .$folderName .DS .'Assets' .DS .$filePath;
        } else if (preg_match('#^(assets|vendor)/(.*)$#i', $uri, $matches)) {
            $filePath = $matches[2];

            if (strtolower($matches[1]) == 'assets') {
                // The Asset File is located on root 'assets' folder.
                $path = ROOTDIR .'assets' .DS .str_replace('/', DS, $filePath);
            } else if (Str::startsWith($filePath, $this->paths)) {
                // The Asset File is located on one of the valid Vendor paths.
                $path = ROOTDIR .'vendor' .DS .str_replace('/', DS, $filePath);
            }
        }

        if (! is_null($path) && in_array($method, array('GET', 'HEAD'))) {
            $response = $this->serve($path, $request);

            // Prepare the Response instance.
            $response->prepare($request);

            return $response;
        }
    }

    /**
     * Serve a File.<commit_msg>Improve Routin\Assets\DefaultDispatcher<commit_after><?php

namespace Routing\Assets;

use Config\Config;
use Http\Response;
use Routing\Assets\DispatcherInterface;
use Support\Str;

use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;

use Carbon\Carbon;

use LogicException;


class DefaultDispatcher implements DispatcherInterface
{
    /**
     * The valid Vendor paths.
     * @var array
     */
    protected $paths = array();

    /**
     * The currently accepted encodings for Response content compression.
     *
     * @var array
     */
    protected static $algorithms = array('gzip', 'deflate');


    /**
     * Create a new Default Dispatcher instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->paths = Config::get('routing.assets.paths', array());
    }

    /**
     * Dispatch a Assets File Response.
     *
     * @return \Symfony\Component\HttpFoundation\Response|null
     */
    public function dispatch(SymfonyRequest $request)
    {
        // For proper Assets serving, the file URI should be either of the following:
        //
        // /templates/default/assets/css/style.css
        // /modules/blog/assets/css/style.css
        // /assets/css/style.css

        $method = $request->method();

        $uri = $request->path();

        // Calculate the Asset File path, if it is a valid one.
        $path = null;

        if (preg_match('#^(templates|modules)/([^/]+)/assets/(.*)$#i', $uri, $matches)) {
            $baseFolder = (strtolower($matches[1]) == 'modules') ? 'Modules' : 'Templates';

            $basePath = APPDIR .$baseFolder .DS .Str::studly($matches[2]) .DS .'Assets' .DS;

            $path = $basePath .str_replace('/', DS, $matches[3]);
        } else if (preg_match('#^(assets|vendor)/(.*)$#i', $uri, $matches)) {
            $uriPath = $matches[2];

            //
            $baseFolder = strtolower($matches[1]);

            $basePath = ROOTDIR .$baseFolder .DS;

            if (($baseFolder == 'assets') || Str::startsWith($uriPath, $this->paths)) {
                $path = $basePath .str_replace('/', DS, $uriPath);
            }
        }

        if (! is_null($path) && in_array($method, array('GET', 'HEAD'))) {
            $response = $this->serve($path, $request);

            // Prepare the Response instance.
            $response->prepare($request);

            return $response;
        }
    }

    /**
     * Serve a File.
{'fraction_non_alphanumeric': 0.1465154563746113, 'fraction_numerical': 0.0018291567587342234, 'mean_word_length': 2.315949060036386, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '4813390', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1579, 'n_words': 380}