XBackBone/app/Factories/ViewFactory.php

72 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2019-11-13 12:02:31 +00:00
<?php
namespace App\Factories;
use App\Web\View;
use Psr\Container\ContainerInterface as Container;
use Slim\Factory\ServerRequestCreatorFactory;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFunction;
class ViewFactory
{
public static function createAppInstance(Container $container)
{
$config = $container->get('config');
$loader = new FilesystemLoader(BASE_DIR.'resources/templates');
2019-11-21 17:00:47 +00:00
$twig = new Environment($loader, [
2019-11-20 17:49:31 +00:00
'cache' => BASE_DIR.'resources/cache/twig',
'autoescape' => 'html',
'debug' => $config['debug'],
2019-11-13 12:02:31 +00:00
'auto_reload' => $config['debug'],
2019-11-21 17:00:47 +00:00
]);
2019-11-13 12:02:31 +00:00
2019-11-19 12:59:17 +00:00
$request = ServerRequestCreatorFactory::determineServerRequestCreator()->createServerRequestFromGlobals();
2019-11-13 12:02:31 +00:00
$twig->addGlobal('config', $config);
$twig->addGlobal('request', $request);
$twig->addGlobal('session', $container->get('session'));
2019-11-13 12:02:31 +00:00
$twig->addGlobal('current_lang', $container->get('lang')->getLang());
$twig->addGlobal('maxUploadSize', stringToBytes(ini_get('post_max_size')));
2019-11-13 12:02:31 +00:00
$twig->addGlobal('PLATFORM_VERSION', PLATFORM_VERSION);
$twig->addFunction(new TwigFunction('route', 'route'));
$twig->addFunction(new TwigFunction('lang', 'lang'));
$twig->addFunction(new TwigFunction('urlFor', 'urlFor'));
$twig->addFunction(new TwigFunction('asset', 'asset'));
$twig->addFunction(new TwigFunction('mime2font', 'mime2font'));
$twig->addFunction(new TwigFunction('queryParams', 'queryParams'));
$twig->addFunction(new TwigFunction('isDisplayableImage', 'isDisplayableImage'));
2019-11-19 12:59:17 +00:00
$twig->addFunction(new TwigFunction('inPath', 'inPath'));
2020-02-29 22:35:43 +00:00
$twig->addFunction(new TwigFunction('humanFileSize', 'humanFileSize'));
$twig->addFunction(new TwigFunction('param', 'param'));
2021-08-18 06:57:15 +00:00
$twig->addFunction(new TwigFunction('glue', 'glue'));
2019-11-13 12:02:31 +00:00
return new View($twig);
}
2019-11-20 17:49:31 +00:00
public static function createInstallerInstance(Container $container)
{
2019-11-13 12:02:31 +00:00
$config = $container->get('config');
2019-11-21 17:00:47 +00:00
$loader = new FilesystemLoader([BASE_DIR.'install/templates', BASE_DIR.'resources/templates']);
2019-11-13 12:02:31 +00:00
2019-11-21 17:00:47 +00:00
$twig = new Environment($loader, [
2019-11-20 17:49:31 +00:00
'cache' => false,
'autoescape' => 'html',
'debug' => $config['debug'],
2019-11-13 12:02:31 +00:00
'auto_reload' => $config['debug'],
2019-11-21 17:00:47 +00:00
]);
2019-11-13 12:02:31 +00:00
2019-11-19 12:59:17 +00:00
$request = ServerRequestCreatorFactory::determineServerRequestCreator()->createServerRequestFromGlobals();
2019-11-13 12:02:31 +00:00
$twig->addGlobal('config', $config);
$twig->addGlobal('request', $request);
2019-11-19 11:32:58 +00:00
$twig->addGlobal('session', $container->get('session'));
2019-11-13 12:02:31 +00:00
$twig->addGlobal('PLATFORM_VERSION', PLATFORM_VERSION);
return new View($twig);
}
2019-11-20 17:49:31 +00:00
}