Skip to content

Commit

Permalink
Refactoring of the Website Controller, some configurations that were …
Browse files Browse the repository at this point in the history
…in the admin were moved to website. Now the static generation of files works
  • Loading branch information
ramsesmoreno committed Oct 11, 2021
1 parent 5e9f4f4 commit ee18326
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 40 deletions.
8 changes: 7 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php

return [
'langs' => ['en']
"langs" => ["en"],
"static_generation" => "lazy", // (TBD) lazy | eager | none Lazy: The entities wait to be called to get processed. Eager: Entities views get processed on save. None: No cache.
"static_storage" => [
'drive' => 'public',
'folder' => ''
],
"formats" => ["html"]
];
1 change: 0 additions & 1 deletion routes/website.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@
Route::middleware('web')
->group( function () {
Route::get('/{path}', [WebsiteController::class, 'any'])->where('path', '.*');
Route::post('/{path}', [WebsiteController::class, 'any'])->where('path', '.*');
});
66 changes: 28 additions & 38 deletions src/Http/Controllers/WebsiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Kusikusi\Models\Website;
use Kusikusi\Models\Entity;
use Illuminate\Support\Facades\Validator;
use Mimey\MimeTypes;
use Illuminate\Http\Testing\MimeType;

class WebsiteController extends Controller
{
Expand All @@ -33,36 +33,32 @@ public function __construct()
* @param $request \Illuminate\Http\Request
* @return \Illuminate\Http\Response
*/
public function any(Request $request)
public function any(Request $request, $path)
{
$path = $request->path() == '/' ? '/' : '/' . $request->path();
$originalExtension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
$format = strtolower(pathinfo($path, PATHINFO_EXTENSION));

if ($format === '') {
$format = 'html';
$static_path = Str::finish($path, '/').'index.html';
} else {
$path = substr($path, 0, strrpos($path, "."));
$static_path = $path.'.'.$format;
$originalFormat = $format;
if ($format === '') $format = 'html';
if (!in_array($format, Config::get('kusikusi_website.formats', ['html']))) {
$controller = new HtmlController();
return ($controller->error($request, 404));
}
$path = preg_replace('/\/index$/', '', $path);
if ($path === '') $path = '/';
$filename = strtolower(pathinfo($path, PATHINFO_FILENAME));
$path = "/".Str::beforeLast($path, '.');
$filename = Str::afterLast($path, '/');
$staticPath = "$path.$format";

// Send the file stored in the static folder if it exist
/* if ($exists = Storage::disk('views_processed')->exists($static_path) && (!env('APP_DEBUG', false) || $format !== 'html')) {
$mimes = new MimeTypes;
$mimeType = $mimes->getMimeType($format);
$staticStorage = Config::get('kusikusi_website.static_storage.drive');
if (!env('APP_DEBUG', false) && Storage::disk($staticStorage)->exists($staticPath)) {
$mimeType = MimeType::from($staticPath);
$headers = ['Content-Type' => $mimeType];
return Storage::disk('views_processed')->response($static_path, null, $headers);
} */
return Storage::disk($staticStorage)->response($staticPath, null, $headers);
}

// Search for the entity is being called by its url, ignore inactive and soft deleted.
$defaultLang = config('cms.langs', [''])[0];
App::setLocale($defaultLang);
$searchRouteResult = EntityRoute::where('path', $path)->first();
if (!$searchRouteResult) {
$defaultLang = config('kusikusi_website.langs', [''])[0];
App::setLocale($defaultLang);
$request->lang = $defaultLang;
$controller = new HtmlController();
return ($controller->error($request, 404));
Expand All @@ -75,7 +71,7 @@ public function any(Request $request)
->where('lang', $searchRouteResult->lang)
->where('kind', 'main')
->first();
return redirect($redirect->path . ($originalExtension !== '' ? '.'.$originalExtension : ''), $status || 301);
return redirect($redirect->path . ($originalFormat !== '' ? '.'.$originalFormat : ''), $status || 301);
break;
case 'alias':
case 'main':
Expand All @@ -87,7 +83,7 @@ public function any(Request $request)

// Get the model class name from App or Kusikusi
$modelClassName = Entity::getEntityClassName($searchRouteResult->entity_model);

$entity = $modelClassName::select("*")
->where("id", $searchRouteResult->entity_id)
->isPublished()
Expand All @@ -107,26 +103,20 @@ public function any(Request $request)
$controllerClassName = "App\\Http\\Controllers\\" . ucfirst($format) . 'Controller';
if(!class_exists($controllerClassName)) {
$controller = new HtmlController;
return ($controller->error($request, 404));
return ($controller->error($request, 501));
}
$controller = new $controllerClassName;
if (method_exists($controller, $model_name)) {
$view = $controller->$model_name($request, $entity, $lang);
/* if (Config::get('cms.static_generation', 'lazy') === 'lazy') {
$modelInstance = new $modelClassName;
if ($modelInstance->getCacheViewsAs()) {
$render = $view->render();
if ($modelInstance->getCacheViewsAs() === 'directory' || $path === '/' || $path === '') {
$cachePath = "$path/index.$format";
if ($path !== '/' && $path !== '') {
Storage::disk('views_processed')->put($path.'.'.$format, $render);
}
} else {
$cachePath = "$path.$format";
}
Storage::disk('views_processed')->put($cachePath, $render);
if (Config::get('kusikusi_website.static_generation', 'none') === 'lazy') {
$render = $view->render();
if ($path !== '/' || $path === '') {
Storage::disk($staticStorage)->put($staticPath, $render);
}
} */
if ($format === 'html') {
Storage::disk($staticStorage)->put("$path/index.$format", $render);
}
}
return $view;
} else {
return ($controller->error($request, 501));
Expand Down

0 comments on commit ee18326

Please sign in to comment.