Use default view variables

This commit is contained in:
Pierre Rudloff 2020-10-22 00:45:09 +02:00
parent 110bfc9ff1
commit 6bb577bcce
3 changed files with 27 additions and 33 deletions

View File

@ -13,7 +13,6 @@ use Alltube\Locale;
use Alltube\Middleware\CspMiddleware;
use Exception;
use Slim\Http\StatusCode;
use Slim\Http\Uri;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Throwable;
use Psr\Container\ContainerInterface;
@ -60,15 +59,12 @@ class FrontController extends BaseController
$response,
'index.tpl',
[
'config' => $this->config,
'class' => 'index',
'description' => $this->localeManager->t(
'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.'
),
'domain' => $uri->getScheme() . '://' . $uri->getAuthority(),
'canonical' => $this->getCanonicalUrl($request),
'supportedLocales' => $this->localeManager->getSupportedLocales(),
'locale' => $this->localeManager->getLocale(),
]
);
@ -106,14 +102,11 @@ class FrontController extends BaseController
$response,
'extractors.tpl',
[
'config' => $this->config,
'extractors' => $this->downloader->getExtractors(),
'class' => 'extractors',
'title' => $this->localeManager->t('Supported websites'),
'description' => $this->localeManager->t('List of all supported websites from which Alltube Download ' .
'can extract video or audio files'),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
]
);
@ -134,14 +127,11 @@ class FrontController extends BaseController
$response,
'password.tpl',
[
'config' => $this->config,
'class' => 'password',
'title' => $this->localeManager->t('Password prompt'),
'description' => $this->localeManager->t(
'You need a password in order to download this video with Alltube Download'
),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
]
);
@ -195,9 +185,6 @@ class FrontController extends BaseController
'class' => 'info',
'title' => $title,
'description' => $description,
'config' => $this->config,
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
'defaultFormat' => $this->defaultFormat,
]
);
@ -250,12 +237,9 @@ class FrontController extends BaseController
$response,
'error.tpl',
[
'config' => $this->config,
'error' => $message,
'class' => 'video',
'title' => $this->localeManager->t('Error'),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
]
);
@ -323,21 +307,4 @@ class FrontController extends BaseController
return $this->displayError($request, $response, $message);
}
}
/**
* Generate the canonical URL of the current page.
*
* @param Request $request PSR-7 Request
*
* @return string URL
*/
private function getCanonicalUrl(Request $request)
{
/** @var Uri $uri */
$uri = $request->getUri();
return $uri->withBasePath('')
->withHost('alltubedownload.net')
->withScheme('https');
}
}

View File

@ -19,6 +19,23 @@ use SmartyException;
*/
class ViewFactory
{
/**
* Generate the canonical URL of the current page.
*
* @param Request $request PSR-7 Request
*
* @return string URL
*/
private static function getCanonicalUrl(Request $request)
{
/** @var Uri $uri */
$uri = $request->getUri();
return $uri->withBasePath('')
->withHost('alltubedownload.net')
->withScheme('https');
}
/**
* Create Smarty view object.
*
@ -63,6 +80,10 @@ class ViewFactory
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
$view->registerPlugin('block', 't', [$localeManager, 'smartyTranslate']);
$view->offsetSet('canonical', self::getCanonicalUrl($request));
$view->offsetSet('locale', $container->get('locale')->getLocale());
$view->offsetSet('config', $container->get('config'));
return $view;
}
}

View File

@ -6,7 +6,9 @@
namespace Alltube\Test;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Factory\ConfigFactory;
use Alltube\Factory\LocaleManagerFactory;
use Alltube\Factory\SessionFactory;
use Alltube\Factory\ViewFactory;
@ -27,12 +29,14 @@ class ViewFactoryTest extends BaseTest
* @return void
* @throws SmartyException
* @throws DependencyException
* @throws ConfigException
*/
public function testCreate()
{
$container = new Container();
$container['session'] = SessionFactory::create($container);
$container['locale'] = LocaleManagerFactory::create($container);
$container['config'] = ConfigFactory::create($container);
$view = ViewFactory::create($container);
$this->assertInstanceOf(Smarty::class, $view);
}
@ -43,12 +47,14 @@ class ViewFactoryTest extends BaseTest
* @return void
* @throws SmartyException
* @throws DependencyException
* @throws ConfigException
*/
public function testCreateWithXForwardedProto()
{
$container = new Container();
$container['session'] = SessionFactory::create($container);
$container['locale'] = LocaleManagerFactory::create($container);
$container['config'] = ConfigFactory::create($container);
$request = Request::createFromEnvironment(Environment::mock());
$view = ViewFactory::create($container, $request->withHeader('X-Forwarded-Proto', 'https'));
$this->assertInstanceOf(Smarty::class, $view);