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 Alltube\Middleware\CspMiddleware;
use Exception; use Exception;
use Slim\Http\StatusCode; use Slim\Http\StatusCode;
use Slim\Http\Uri;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer; use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Throwable; use Throwable;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
@ -60,15 +59,12 @@ class FrontController extends BaseController
$response, $response,
'index.tpl', 'index.tpl',
[ [
'config' => $this->config,
'class' => 'index', 'class' => 'index',
'description' => $this->localeManager->t( 'description' => $this->localeManager->t(
'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.' 'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.'
), ),
'domain' => $uri->getScheme() . '://' . $uri->getAuthority(), 'domain' => $uri->getScheme() . '://' . $uri->getAuthority(),
'canonical' => $this->getCanonicalUrl($request),
'supportedLocales' => $this->localeManager->getSupportedLocales(), 'supportedLocales' => $this->localeManager->getSupportedLocales(),
'locale' => $this->localeManager->getLocale(),
] ]
); );
@ -106,14 +102,11 @@ class FrontController extends BaseController
$response, $response,
'extractors.tpl', 'extractors.tpl',
[ [
'config' => $this->config,
'extractors' => $this->downloader->getExtractors(), 'extractors' => $this->downloader->getExtractors(),
'class' => 'extractors', 'class' => 'extractors',
'title' => $this->localeManager->t('Supported websites'), 'title' => $this->localeManager->t('Supported websites'),
'description' => $this->localeManager->t('List of all supported websites from which Alltube Download ' . 'description' => $this->localeManager->t('List of all supported websites from which Alltube Download ' .
'can extract video or audio files'), 'can extract video or audio files'),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
] ]
); );
@ -134,14 +127,11 @@ class FrontController extends BaseController
$response, $response,
'password.tpl', 'password.tpl',
[ [
'config' => $this->config,
'class' => 'password', 'class' => 'password',
'title' => $this->localeManager->t('Password prompt'), 'title' => $this->localeManager->t('Password prompt'),
'description' => $this->localeManager->t( 'description' => $this->localeManager->t(
'You need a password in order to download this video with Alltube Download' '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', 'class' => 'info',
'title' => $title, 'title' => $title,
'description' => $description, 'description' => $description,
'config' => $this->config,
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
'defaultFormat' => $this->defaultFormat, 'defaultFormat' => $this->defaultFormat,
] ]
); );
@ -250,12 +237,9 @@ class FrontController extends BaseController
$response, $response,
'error.tpl', 'error.tpl',
[ [
'config' => $this->config,
'error' => $message, 'error' => $message,
'class' => 'video', 'class' => 'video',
'title' => $this->localeManager->t('Error'), '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); 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 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. * Create Smarty view object.
* *
@ -63,6 +80,10 @@ class ViewFactory
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']); $view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
$view->registerPlugin('block', 't', [$localeManager, 'smartyTranslate']); $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; return $view;
} }
} }

View file

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