From a772dc867de7c1cc92c328308e5f193bd9e86ccb Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Fri, 29 Nov 2019 00:24:51 -0700 Subject: [PATCH] Reorganized and cleaned up index and ViewComposer a bit --- app/Bootstrap/ViewComposer.php | 21 +++++++++++++++++---- index.php | 6 ++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/Bootstrap/ViewComposer.php b/app/Bootstrap/ViewComposer.php index 6258d04..cb4b70c 100644 --- a/app/Bootstrap/ViewComposer.php +++ b/app/Bootstrap/ViewComposer.php @@ -33,10 +33,25 @@ class ViewComposer */ public function __invoke(): void { + $this->twig->getEnvironment()->setCache( + $this->config->get('view_cache', 'app/cache/views') + ); + $this->twig->getEnvironment()->getExtension(CoreExtension::class)->setDateFormat( $this->config->get('date_format', 'Y-m-d H:i:s'), '%d days' ); + $this->registerGlobalFunctions(); + $this->registerThemeFunctions(); + } + + /** + * Register global Twig functions. + * + * @return void + */ + public function registerGlobalFunctions(): void + { $this->twig->getEnvironment()->addFunction( new TwigFunction('asset', function (string $path) { return "/app/themes/{$this->config->get('theme', 'defualt')}/{$path}"; @@ -51,18 +66,16 @@ class ViewComposer return sprintf('%.2f', $bytes / pow(1024, $factor)) . $sizes[$factor]; }) ); - - $this->registerThemeFunctions(); } /** - * Register theme Twig functions. + * Register theme specific Twig functions. * * @return void */ public function registerThemeFunctions(): void { - $themeConfigPath = "app/themes/{$this->config->get('theme')}/config.php"; + $themeConfigPath = "{$this->twig->getLoader()->getPaths()[0]}/config.php"; if (file_exists($themeConfigPath)) { $themeConfig = include $themeConfigPath; diff --git a/index.php b/index.php index b63f3f3..713aee7 100644 --- a/index.php +++ b/index.php @@ -2,8 +2,8 @@ use App\Bootstrap\ViewComposer; use App\Controllers; -use DI\Container; use DI\Bridge\Slim\Bridge; +use DI\Container; use Dotenv\Dotenv; use PHLAK\Config\Config; use Slim\Views\Twig; @@ -22,9 +22,7 @@ $container = new Container(); /** Register dependencies */ $container->set(Config::class, new Config('app/config')); $container->set(Twig::class, function (Config $config) { - return new Twig("app/themes/{$config->get('theme')}", [ - 'cache' => $config->get('view_cache', 'app/cache/views') - ]); + return new Twig("app/themes/{$config->get('theme', 'default')}"); }); /** Configure the view handler */