From d02c459379d4e782721e2a0094b4311d267452a8 Mon Sep 17 00:00:00 2001 From: bim-g Date: Sun, 22 Sep 2024 13:45:17 +0200 Subject: [PATCH] [UPD] refactoring to accesse layout path via getter method --- src/Core/Application.php | 38 +++++++++++++++++++++++++++++++------- src/Core/Views/View.php | 11 +++++++++-- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/Core/Application.php b/src/Core/Application.php index 9832787..1f8907b 100644 --- a/src/Core/Application.php +++ b/src/Core/Application.php @@ -38,9 +38,9 @@ class Application */ public static string $LAYOUT_CONTENT; /** - * @var string|null + * @var string */ - public static string $LAYOUT; + private static string $layout; /** * @var string @@ -64,11 +64,14 @@ public function __construct(string $path, AppConfiguration $config) self::$APP_LANG = self::$config_params['lang'] ?? 'fr'; self::$APP_TEMPLATE = self::$config_params['app_template'] ?? ''; self::$LAYOUT_CONTENT = 'layout_content'; - self::$LAYOUT = ''; + self::$layout = ''; self::$VIEW_FOLDER = ''; $this->router = new Router(); } + /** + * @return string + */ public static function getRootDir(): string { return self::$root_dir; @@ -88,32 +91,53 @@ public static function dumper($ex): void } /** - * Set the layout at the top of your application to be available everywhere. + * Define a layout to be used by all pages in the application. + * can be set at the top of your application to be available everywhere. * @param string $layout * @return void */ public static function setLayout(string $layout) { - self::$LAYOUT = self::getRootDir().'/views/'.$layout; + self::$layout = self::getRootDir() . '/views/' . trim($layout, '/'); } + + /** + * @param string $layout_name + * @return void + */ public static function setLayoutContent(string $layout_name) { self::$LAYOUT_CONTENT = $layout_name; } + /** + * @param string $folder_name + * @return void + */ public static function setViewFolder(string $folder_name) { self::$VIEW_FOLDER = $folder_name; } - public static function getLayout() + + /** + * @return string|null + */ + public static function getLayout(): ?string { - return self::$LAYOUT ; + return strlen(trim(self::$layout )) > 0 ? self::$layout : null; } + + /** + * @return string + */ public static function getLayoutContent() { return self::$LAYOUT_CONTENT ; } + /** + * @return string + */ public static function getViewFolder() { return self::$VIEW_FOLDER ; diff --git a/src/Core/Views/View.php b/src/Core/Views/View.php index 97db5f8..0953356 100644 --- a/src/Core/Views/View.php +++ b/src/Core/Views/View.php @@ -77,15 +77,22 @@ public function display(string $view): void { $view_file = $this->buildFilePath($view); $render = $this->renderView($view_file); - if ($this->layout === '' && !$this->reset) { + if (! $this->getLayout() && !$this->reset) { $this->layout = Application::getLayout(); } - if ($this->layout !== '') { + if ($this->getLayout()) { $render = $this->renderLayout($render); } $this->buildAssetHead($render); } + /** + * @return string|null + */ + public function getLayout(): ?string + { + return strlen(trim($this->layout)) > 0 ? $this->layout : null; + } /** * @param class-string $file_name *