Skip to content

Commit

Permalink
Support load static files
Browse files Browse the repository at this point in the history
  • Loading branch information
puleeno committed Oct 1, 2023
1 parent a42eafd commit 3ce3843
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
53 changes: 53 additions & 0 deletions app/Http/Controllers/StaticFileController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Http\Controllers;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class StaticFileController
{
protected function getContentTypeFromFileName($fileName)
{
$fileNameArr = explode('.', $fileName);
$extension = end($fileNameArr);

switch ($extension) {
case 'js':
return 'text/javascript';
case 'css':
return 'text/css';
case 'json':
return 'application/json';
case 'jsonld':
return 'application/ld+json';
case 'svg':
return 'image/svg+xml';
case 'png':
return 'image/png';
case 'jpg':
case 'jpeg':
return 'image/jpeg';
case 'gif':
return 'image/gif';
case 'ico':
return 'image/vnd.microsoft.icon';
case 'htm':
case 'html':
return 'text/html';
}

return 'text/plain';
}

public function __invoke(RequestInterface $request, ResponseInterface $response, $args = [])
{
$pagePath = $request->getUri()->getPath();
$assetFile = get_path('root') . str_replace('/', DIRECTORY_SEPARATOR, $pagePath);

$response = $response->withHeader('Content-Type', $this->getContentTypeFromFileName(basename($pagePath)));
$response->getBody()->write(file_get_contents($assetFile));

return $response;
}
}
3 changes: 0 additions & 3 deletions app/Http/Handlers/HttpErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ protected function respond(): Response
$error->setDescription($exception->getMessage());
}

var_dump($exception);
die;

$payload = new ActionPayload($statusCode, null, $error);
$encodedPayload = json_encode($payload, JSON_PRETTY_PRINT);

Expand Down
9 changes: 7 additions & 2 deletions configs/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

declare(strict_types=1);

use Slim\Interfaces\RouteCollectorProxyInterface as Group;

use App\Core\Application;
use App\Application\Actions\User\ListUsersAction;
use App\Application\Actions\User\ViewUserAction;
use App\Core\Application;
use Slim\Interfaces\RouteCollectorProxyInterface as Group;
use App\Http\Controllers\StaticFileController;

return function (Application $app) {
$app->any('/extensions/{extensionName:/?.+}/assets/{pagePath:/?.+}', StaticFileController::class);
$app->any('/themes/{themeName:/?.+}/assets/{pagePath:/?.+}', StaticFileController::class);

$app->group('/users', function (Group $group) {
$group->get('', ListUsersAction::class);
$group->get('/{id}', ViewUserAction::class);
Expand Down

0 comments on commit 3ce3843

Please sign in to comment.