Skip to content

Commit

Permalink
Added Env class and new controller: home, global
Browse files Browse the repository at this point in the history
  • Loading branch information
puleeno committed Oct 10, 2023
1 parent d7ce1c4 commit 93f50ab
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .env-example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DEBUG=false
ENV=prod
VIEW_DEBUG=false
BATCH_DATA_SIZE=50
BATCH_DATA_SIZE=50
REACT_FRONTEND=false
26 changes: 26 additions & 0 deletions app/Core/Env.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Core;

final class Env
{
public static function get($key, $defaultValue = null)
{
$envVar = getenv($key);
if (is_null($envVar)) {
return $defaultValue;
}

if (in_array($envVar, ["true", "false"])) {
return boolval($envVar);
}
if (is_numeric($envVar)) {
if (strpos($envVar, ',') !== false) {
$envVar = str_replace(',', '', $envVar);
}

return strpos($envVar, '.') === false ? intval($envVar) : floatval($envVar);
}
return $envVar;
}
}
13 changes: 13 additions & 0 deletions app/Http/Controllers/GlobalController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Http\Controllers;

use Psr\Http\Message\ResponseInterface;

class GlobalController extends Controller
{
public function handle(ResponseInterface $response)
{
return $response;
}
}
17 changes: 17 additions & 0 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Http\Controllers;

use App\Core\HookManager;
use Psr\Http\Message\ResponseInterface;

class HomeController extends Controller
{
public function index(ResponseInterface $response): ResponseInterface
{
return view(
HookManager::applyFilters('home_template', 'pages/home'),
HookManager::applyFilters('home_data', [])
);
}
}
21 changes: 17 additions & 4 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Core\AssetManager;
use App\Core\Assets\AssetStylesheetOptions;
use App\Core\Assets\AssetUrl;
use App\Http\Controllers\GlobalController;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Core\ExtensionManager;
Expand Down Expand Up @@ -181,6 +182,11 @@ public function boot()
$this->loadComposer();
$this->setupEnvironment();
$this->setup();

$this->writeErrorLogs(
$this->setupHttpErrorHandle()
);

$this->initAssets();
$this->initExtensions();
$this->loadExtensions();
Expand Down Expand Up @@ -220,14 +226,21 @@ protected function setupAssets()
HookManager::addAction('footer', [$assetManager, 'executeFooterScripts'], 99);
}

protected function run()

/**
* This method use to register actions to URL has format `/pagepath`
*/
protected function registerGlobalController()
{
$this->writeErrorLogs(
$this->setupHttpErrorHandle()
);
$this->app->any('/{pagePath:/?.+}', [GlobalController::class, 'handle']);
}

protected function run()
{
$this->setupAssets();

$this->registerGlobalController();

// Run App & Emit Response
$response = $this->app->handle($this->request);

Expand Down
12 changes: 11 additions & 1 deletion configs/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
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\Http\Controllers\StaticFileController;
use App\Core\HookManager;
use App\Http\Controllers\HomeController;

return function (Application $app) {
$app->any('/extensions/{extensionName:/?.+}/assets/{pagePath:/?.+}', StaticFileController::class);
Expand All @@ -17,4 +18,13 @@
$group->get('', ListUsersAction::class);
$group->get('/{id}', ViewUserAction::class);
});


$app->any(
'/',
HookManager::applyFilters(
'home_controller',
[HomeController::class, 'index']
)
);
};
2 changes: 1 addition & 1 deletion extensions/layout
2 changes: 1 addition & 1 deletion extensions/react
Submodule react updated from 2347ff to e5fb33

0 comments on commit 93f50ab

Please sign in to comment.