alltube/classes/Factory/ViewFactory.php

130 lines
3.7 KiB
PHP
Raw Normal View History

<?php
/**
* ViewFactory class.
*/
2017-04-26 00:52:05 +02:00
2020-10-20 23:29:50 +02:00
namespace Alltube\Factory;
2020-10-20 23:29:50 +02:00
use Alltube\LocaleManager;
2021-02-07 00:03:37 +01:00
use Junker\DebugBar\Bridge\SmartyCollector;
use Psr\Container\ContainerInterface;
use Slim\Http\Request;
2020-10-19 22:24:59 +02:00
use Slim\Http\Uri;
use Slim\Views\Smarty;
use Slim\Views\SmartyPlugins;
2020-05-13 21:18:32 +02:00
use SmartyException;
/**
* Create Smarty view object.
*/
class ViewFactory
{
2020-10-22 00:45:09 +02:00
/**
* @param Uri $uri
* @return Uri
*/
private static function cleanBasePath(Uri $uri): Uri
{
$basePath = $uri->getBasePath();
if (str_ends_with($basePath, 'index.php')) {
$basePath = dirname($basePath);
if ($basePath == '/') {
/*
* Calling withBasePath('/') does nothing,
* we have to use an empty string instead.
*/
$basePath = '';
}
/*
* When the base path ends with index.php,
* routing works correctly, but it breaks the URL of static assets using {base_url}.
* So we alter the base path but only in the URI used by SmartyPlugins.
*/
$uri = $uri->withBasePath($basePath);
}
return $uri;
}
/**
* Create a URI suitable for templates.
*
* @param Request $request
* @return Uri
*/
public static function prepareUri(Request $request): Uri
{
2020-10-19 22:24:59 +02:00
/** @var Uri $uri */
$uri = $request->getUri();
if (in_array('https', $request->getHeader('X-Forwarded-Proto'))) {
$uri = $uri->withScheme('https')->withPort(443);
}
// set values from X-Forwarded-* headers
2020-10-19 22:24:59 +02:00
if ($host = current($request->getHeader('X-Forwarded-Host'))) {
$uri = $uri->withHost($host);
}
2020-10-19 22:24:59 +02:00
if ($port = current($request->getHeader('X-Forwarded-Port'))) {
$uri = $uri->withPort(intVal($port));
}
2020-10-19 22:24:59 +02:00
if ($path = current($request->getHeader('X-Forwarded-Path'))) {
$uri = $uri->withBasePath($path);
}
return self::cleanBasePath($uri);
}
/**
* Create Smarty view object.
*
* @param ContainerInterface $container Slim dependency container
* @param Request|null $request PSR-7 request
*
* @return Smarty
* @throws SmartyException
*/
public static function create(ContainerInterface $container, Request $request = null): Smarty
{
if (!isset($request)) {
$request = $container->get('request');
}
$view = new Smarty($container->get('root_path') . '/templates/');
$uri = self::prepareUri($request);
2020-05-13 22:28:05 +02:00
/** @var LocaleManager $localeManager */
$localeManager = $container->get('locale');
2020-10-19 22:24:59 +02:00
$smartyPlugins = new SmartyPlugins($container->get('router'), $uri->withUserInfo(''));
$view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
$view->registerPlugin('block', 't', [$localeManager, 'smartyTranslate']);
$view->offsetSet('locale', $container->get('locale'));
2020-10-22 00:45:09 +02:00
$view->offsetSet('config', $container->get('config'));
2020-10-22 21:40:20 +02:00
$view->offsetSet('domain', $uri->withBasePath('')->getBaseUrl());
2020-10-22 00:45:09 +02:00
2021-02-06 15:00:26 +01:00
if ($container->has('debugbar')) {
2021-02-07 00:03:37 +01:00
$debugBar = $container->get('debugbar');
2022-01-27 00:15:05 +01:00
$collector = new SmartyCollector($view->getSmarty());
$collector->useHtmlVarDumper();
$debugBar->addCollector($collector);
2021-02-07 00:03:37 +01:00
2021-02-06 15:00:26 +01:00
$view->offsetSet(
'debug_render',
2021-02-07 00:03:37 +01:00
$debugBar->getJavascriptRenderer(
$uri->getBaseUrl() . '/vendor/maximebf/debugbar/src/DebugBar/Resources/'
)
2021-02-06 15:00:26 +01:00
);
}
return $view;
}
}