Reorganized and cleaned up index and ViewComposer a bit

This commit is contained in:
Chris Kankiewicz
2019-11-29 00:24:51 -07:00
parent 95c38554c0
commit a772dc867d
2 changed files with 19 additions and 8 deletions

View File

@@ -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;

View File

@@ -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 */