alltube/classes/ViewFactory.php

51 lines
1.4 KiB
PHP
Raw Normal View History

<?php
/**
* ViewFactory class.
*/
2017-04-26 00:52:05 +02:00
namespace Alltube;
use Psr\Container\ContainerInterface;
use Slim\Http\Request;
use Slim\Views\Smarty;
use Slim\Views\SmartyPlugins;
2020-05-13 21:18:32 +02:00
use SmartyException;
/**
* Create Smarty view object.
*/
class ViewFactory
{
/**
* Create Smarty view object.
*
2017-11-12 16:36:44 +01:00
* @param ContainerInterface $container Slim dependency container
2020-05-13 21:18:32 +02:00
* @param Request $request PSR-7 request
*
* @return Smarty
2020-05-13 21:18:32 +02:00
* @throws SmartyException
*/
public static function create(ContainerInterface $container, Request $request = null)
{
if (!isset($request)) {
2020-05-13 22:28:05 +02:00
$request = $container->get('request');
}
$view = new Smarty(__DIR__ . '/../templates/');
if (in_array('https', $request->getHeader('X-Forwarded-Proto'))) {
$request = $request->withUri($request->getUri()->withScheme('https')->withPort(443));
}
2020-05-13 22:28:05 +02:00
/** @var LocaleManager $localeManager */
$localeManager = $container->get('locale');
2020-05-13 22:28:05 +02:00
$smartyPlugins = new SmartyPlugins($container->get('router'), $request->getUri()->withUserInfo(null));
$view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
$view->registerPlugin('block', 't', [$localeManager, 'smartyTranslate']);
return $view;
}
}