aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/static.php
blob: f28c7e446716f04fb82836d2596c334e2316737e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

use Digitigrade\HttpResponseStatus\NotFound;
use Digitigrade\Router;

Router::getInstance()->mount('/static/:path', function (array $args) {
    // TODO: support nested paths (needs changes in router i think)
    $realPath = __DIR__ . '/../static/' . $args['path'];
    if (!is_file($realPath)) {
        throw new NotFound('static resource ' . $args['path'] . ' does not exist');
    }
    $fileExtension = explode('.', basename($realPath));
    $fileExtension = $fileExtension[count($fileExtension) - 1];
    header('Cache-Control: max-age=604800'); // 1 week
    header('Content-Type: ' . match ($fileExtension) {
        'css' => 'text/css',
        'js' => 'application/javascript',
        'svg' => 'image/svg+xml',
        default => mime_content_type($realPath)
    });
    echo file_get_contents($realPath);
});