2017-04-26 00:50:19 +02:00
|
|
|
<?php
|
2019-10-03 21:24:12 +02:00
|
|
|
|
2017-04-26 00:50:19 +02:00
|
|
|
/**
|
|
|
|
* ViewFactory class.
|
|
|
|
*/
|
2017-04-26 00:52:05 +02:00
|
|
|
|
2020-10-20 23:29:50 +02:00
|
|
|
namespace Alltube\Factory;
|
2017-04-26 00:50:19 +02:00
|
|
|
|
2020-10-20 23:29:50 +02:00
|
|
|
use Alltube\LocaleManager;
|
2021-02-07 00:03:37 +01:00
|
|
|
use Junker\DebugBar\Bridge\SmartyCollector;
|
2017-04-26 01:08:42 +02:00
|
|
|
use Psr\Container\ContainerInterface;
|
2017-04-26 00:50:19 +02:00
|
|
|
use Slim\Http\Request;
|
2020-10-19 22:24:59 +02:00
|
|
|
use Slim\Http\Uri;
|
2017-04-26 00:50:19 +02:00
|
|
|
use Slim\Views\Smarty;
|
|
|
|
use Slim\Views\SmartyPlugins;
|
2020-05-13 21:18:32 +02:00
|
|
|
use SmartyException;
|
2017-04-26 00:50:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create Smarty view object.
|
|
|
|
*/
|
|
|
|
class ViewFactory
|
|
|
|
{
|
2020-10-22 00:45:09 +02:00
|
|
|
/**
|
|
|
|
* Generate the canonical URL of the current page.
|
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 Request
|
|
|
|
*
|
|
|
|
* @return string URL
|
|
|
|
*/
|
2020-12-17 22:43:05 +01:00
|
|
|
private static function getCanonicalUrl(Request $request): string
|
2020-10-22 00:45:09 +02:00
|
|
|
{
|
|
|
|
/** @var Uri $uri */
|
|
|
|
$uri = $request->getUri();
|
|
|
|
|
|
|
|
return $uri->withBasePath('')
|
|
|
|
->withHost('alltubedownload.net')
|
|
|
|
->withScheme('https');
|
|
|
|
}
|
|
|
|
|
2017-04-26 00:50:19 +02:00
|
|
|
/**
|
|
|
|
* Create Smarty view object.
|
|
|
|
*
|
2017-11-12 16:36:44 +01:00
|
|
|
* @param ContainerInterface $container Slim dependency container
|
2020-09-27 15:53:53 +02:00
|
|
|
* @param Request|null $request PSR-7 request
|
2017-04-26 00:50:19 +02:00
|
|
|
*
|
|
|
|
* @return Smarty
|
2020-05-13 21:18:32 +02:00
|
|
|
* @throws SmartyException
|
2017-04-26 00:50:19 +02:00
|
|
|
*/
|
2020-12-17 22:43:05 +01:00
|
|
|
public static function create(ContainerInterface $container, Request $request = null): Smarty
|
2017-04-26 00:50:19 +02:00
|
|
|
{
|
|
|
|
if (!isset($request)) {
|
2020-05-13 22:28:05 +02:00
|
|
|
$request = $container->get('request');
|
2017-04-26 00:50:19 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 23:29:50 +02:00
|
|
|
$view = new Smarty(__DIR__ . '/../../templates/');
|
2020-10-19 20:18:03 +00:00
|
|
|
|
2020-10-19 22:24:59 +02:00
|
|
|
/** @var Uri $uri */
|
2020-10-19 20:18:03 +00:00
|
|
|
$uri = $request->getUri();
|
2017-05-14 21:53:57 +02:00
|
|
|
if (in_array('https', $request->getHeader('X-Forwarded-Proto'))) {
|
2020-10-19 20:18:03 +00:00
|
|
|
$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'))) {
|
2020-10-19 20:18:03 +00:00
|
|
|
$uri = $uri->withHost($host);
|
|
|
|
}
|
|
|
|
|
2020-10-19 22:24:59 +02:00
|
|
|
if ($port = current($request->getHeader('X-Forwarded-Port'))) {
|
2020-10-19 20:18:03 +00:00
|
|
|
$uri = $uri->withPort(intVal($port));
|
|
|
|
}
|
|
|
|
|
2020-10-19 22:24:59 +02:00
|
|
|
if ($path = current($request->getHeader('X-Forwarded-Path'))) {
|
2020-10-19 20:18:03 +00:00
|
|
|
$uri = $uri->withBasePath($path);
|
2017-05-14 21:53:57 +02:00
|
|
|
}
|
2017-04-26 00:50:19 +02:00
|
|
|
|
2020-05-13 22:28:05 +02:00
|
|
|
/** @var LocaleManager $localeManager */
|
|
|
|
$localeManager = $container->get('locale');
|
2019-11-27 23:15:49 +01:00
|
|
|
|
2020-10-19 22:24:59 +02:00
|
|
|
$smartyPlugins = new SmartyPlugins($container->get('router'), $uri->withUserInfo(''));
|
2017-04-26 00:50:19 +02:00
|
|
|
$view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
|
|
|
|
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
|
2019-11-27 23:15:49 +01:00
|
|
|
$view->registerPlugin('block', 't', [$localeManager, 'smartyTranslate']);
|
2017-04-26 00:50:19 +02:00
|
|
|
|
2020-10-22 00:45:09 +02:00
|
|
|
$view->offsetSet('canonical', self::getCanonicalUrl($request));
|
2020-11-21 14:20:01 +01:00
|
|
|
$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');
|
|
|
|
|
|
|
|
$debugBar->addCollector(new SmartyCollector($view->getSmarty()));
|
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-26 00:50:19 +02:00
|
|
|
return $view;
|
|
|
|
}
|
|
|
|
}
|