Undeclared properties

This commit is contained in:
Pierre Rudloff 2017-05-30 23:49:38 +02:00
parent 433a580d64
commit 3ead8dd458
4 changed files with 46 additions and 17 deletions

View file

@ -24,6 +24,13 @@ class LocaleManager
*/ */
private $curLocale; private $curLocale;
/**
* Session segment used to store session variables.
*
* @var \Aura\Session\Segment
*/
private $sessionSegment;
/** /**
* LocaleManager constructor. * LocaleManager constructor.
* *

View file

@ -15,6 +15,14 @@ use Teto\HTTP\AcceptLanguage;
*/ */
class LocaleMiddleware class LocaleMiddleware
{ {
/**
* LocaleManager instance.
*
* @var LocaleManager
*/
private $localeManager;
/** /**
* LocaleMiddleware constructor. * LocaleMiddleware constructor.
* *
@ -22,7 +30,7 @@ class LocaleMiddleware
*/ */
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container)
{ {
$this->locale = $container->get('locale'); $this->localeManager = $container->get('locale');
} }
/** /**
@ -34,7 +42,7 @@ class LocaleMiddleware
*/ */
public function testLocale(array $proposedLocale) public function testLocale(array $proposedLocale)
{ {
foreach ($this->locale->getSupportedLocales() as $locale) { foreach ($this->localeManager->getSupportedLocales() as $locale) {
$parsedLocale = AcceptLanguage::parse($locale); $parsedLocale = AcceptLanguage::parse($locale);
if (isset($proposedLocale['language']) if (isset($proposedLocale['language'])
&& $parsedLocale[1]['language'] == $proposedLocale['language'] && $parsedLocale[1]['language'] == $proposedLocale['language']
@ -57,14 +65,14 @@ class LocaleMiddleware
public function __invoke(Request $request, Response $response, callable $next) public function __invoke(Request $request, Response $response, callable $next)
{ {
$headers = $request->getHeader('Accept-Language'); $headers = $request->getHeader('Accept-Language');
$curLocale = $this->locale->getLocale(); $curLocale = $this->localeManager->getLocale();
if (!isset($curLocale)) { if (!isset($curLocale)) {
if (isset($headers[0])) { if (isset($headers[0])) {
$this->locale->setLocale( $this->localeManager->setLocale(
AcceptLanguage::detect([$this, 'testLocale'], new Locale('en_US'), $headers[0]) AcceptLanguage::detect([$this, 'testLocale'], new Locale('en_US'), $headers[0])
); );
} else { } else {
$this->locale->setLocale(new Locale('en_US')); $this->localeManager->setLocale(new Locale('en_US'));
} }
} }

View file

@ -62,6 +62,13 @@ class FrontController
*/ */
private $defaultFormat = 'best[protocol^=http]'; private $defaultFormat = 'best[protocol^=http]';
/**
* LocaleManager instance.
*
* @var LocaleManager
*/
private $localeManager;
/** /**
* FrontController constructor. * FrontController constructor.
* *
@ -79,7 +86,7 @@ class FrontController
$this->download = new VideoDownload(); $this->download = new VideoDownload();
$this->container = $container; $this->container = $container;
$this->view = $this->container->get('view'); $this->view = $this->container->get('view');
$this->locale = $this->container->get('locale'); $this->localeManager = $this->container->get('locale');
$session_factory = new \Aura\Session\SessionFactory(); $session_factory = new \Aura\Session\SessionFactory();
$session = $session_factory->newInstance($cookies); $session = $session_factory->newInstance($cookies);
$this->sessionSegment = $session->getSegment('Alltube\Controller\FrontController'); $this->sessionSegment = $session->getSegment('Alltube\Controller\FrontController');
@ -108,8 +115,8 @@ class FrontController
'description' => 'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.', 'description' => 'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.',
'domain' => $uri->getScheme().'://'.$uri->getAuthority(), 'domain' => $uri->getScheme().'://'.$uri->getAuthority(),
'canonical' => $this->getCanonicalUrl($request), 'canonical' => $this->getCanonicalUrl($request),
'locales' => $this->locale->getSupportedLocales(), 'locales' => $this->localeManager->getSupportedLocales(),
'locale' => $this->locale->getLocale(), 'locale' => $this->localeManager->getLocale(),
] ]
); );
@ -127,7 +134,7 @@ class FrontController
*/ */
public function locale(Request $request, Response $response, array $data) public function locale(Request $request, Response $response, array $data)
{ {
$this->locale->setLocale(new Locale($data['locale'])); $this->localeManager->setLocale(new Locale($data['locale']));
return $response->withRedirect($this->container->get('router')->pathFor('index')); return $response->withRedirect($this->container->get('router')->pathFor('index'));
} }
@ -152,7 +159,7 @@ class FrontController
'description' => 'List of all supported websites from which Alltube Download '. 'description' => '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), 'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->locale->getLocale(), 'locale' => $this->localeManager->getLocale(),
] ]
); );
@ -177,7 +184,7 @@ class FrontController
'title' => 'Password prompt', 'title' => 'Password prompt',
'description' => 'You need a password in order to download this video with Alltube Download', 'description' => 'You need a password in order to download this video with Alltube Download',
'canonical' => $this->getCanonicalUrl($request), 'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->locale->getLocale(), 'locale' => $this->localeManager->getLocale(),
] ]
); );
@ -268,7 +275,7 @@ class FrontController
'protocol' => $protocol, 'protocol' => $protocol,
'config' => $this->config, 'config' => $this->config,
'canonical' => $this->getCanonicalUrl($request), 'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->locale->getLocale(), 'locale' => $this->localeManager->getLocale(),
] ]
); );
@ -320,7 +327,7 @@ class FrontController
'class' => 'video', 'class' => 'video',
'title' => 'Error', 'title' => 'Error',
'canonical' => $this->getCanonicalUrl($request), 'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->locale->getLocale(), 'locale' => $this->localeManager->getLocale(),
] ]
); );

View file

@ -22,14 +22,21 @@ class LocaleMiddlewareTest extends \PHPUnit_Framework_TestCase
* *
* @var string * @var string
*/ */
private $locale; private $origlocale;
/**
* LocaleMiddleware instance.
*
* @var LocaleMiddleware
*/
private $middleware;
/** /**
* Prepare tests. * Prepare tests.
*/ */
protected function setUp() protected function setUp()
{ {
$this->locale = getenv('LANG'); $this->origlocale = getenv('LANG');
$container = new Container(); $container = new Container();
$container['locale'] = new LocaleManager(); $container['locale'] = new LocaleManager();
$this->middleware = new LocaleMiddleware($container); $this->middleware = new LocaleMiddleware($container);
@ -42,8 +49,8 @@ class LocaleMiddlewareTest extends \PHPUnit_Framework_TestCase
*/ */
protected function tearDown() protected function tearDown()
{ {
putenv('LANG='.$this->locale); putenv('LANG='.$this->origlocale);
setlocale(LC_ALL, $this->locale); setlocale(LC_ALL, $this->origlocale);
} }
/** /**