style: Declare imported classes at the top of the file

This commit is contained in:
Pierre Rudloff 2018-02-05 16:48:58 +01:00
parent 33530eff4d
commit 04fe43a1ca
12 changed files with 81 additions and 63 deletions

View file

@ -5,6 +5,7 @@
namespace Alltube; namespace Alltube;
use Exception;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
/** /**
@ -172,7 +173,7 @@ class Config
*/ */
$options = []; $options = [];
} else { } else {
throw new \Exception("Can't find config file at ".$yamlPath); throw new Exception("Can't find config file at ".$yamlPath);
} }
self::$instance = new self($options); self::$instance = new self($options);
self::$instance->file = $yamlfile; self::$instance->file = $yamlfile;

View file

@ -5,6 +5,8 @@
namespace Alltube; namespace Alltube;
use Locale as PHPLocale;
use Rinvex\Country\Country;
use Teto\HTTP\AcceptLanguage; use Teto\HTTP\AcceptLanguage;
/** /**
@ -55,7 +57,7 @@ class Locale
*/ */
public function getFullName() public function getFullName()
{ {
return \Locale::getDisplayName($this->getIso15897(), $this->getIso15897()); return PHPLocale::getDisplayName($this->getIso15897(), $this->getIso15897());
} }
/** /**
@ -91,7 +93,7 @@ class Locale
/** /**
* Get country information from locale. * Get country information from locale.
* *
* @return \Rinvex\Country\Country|array * @return Country|array
*/ */
public function getCountry() public function getCountry()
{ {

View file

@ -5,6 +5,8 @@
namespace Alltube; namespace Alltube;
use Aura\Session\Segment;
use Aura\Session\SessionFactory;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
/** /**
@ -29,7 +31,7 @@ class LocaleManager
/** /**
* Session segment used to store session variables. * Session segment used to store session variables.
* *
* @var \Aura\Session\Segment * @var Segment
*/ */
private $sessionSegment; private $sessionSegment;
@ -40,9 +42,9 @@ class LocaleManager
*/ */
public function __construct(array $cookies = []) public function __construct(array $cookies = [])
{ {
$session_factory = new \Aura\Session\SessionFactory(); $session_factory = new SessionFactory();
$session = $session_factory->newInstance($cookies); $session = $session_factory->newInstance($cookies);
$this->sessionSegment = $session->getSegment('Alltube\LocaleManager'); $this->sessionSegment = $session->getSegment(self::class);
$cookieLocale = $this->sessionSegment->get('locale'); $cookieLocale = $this->sessionSegment->get('locale');
if (isset($cookieLocale)) { if (isset($cookieLocale)) {
$this->setLocale(new Locale($cookieLocale)); $this->setLocale(new Locale($cookieLocale));

View file

@ -5,9 +5,11 @@
namespace Alltube; namespace Alltube;
use Exception;
/** /**
* Exception thrown when a video requires a password. * Exception thrown when a video requires a password.
*/ */
class PasswordException extends \Exception class PasswordException extends Exception
{ {
} }

View file

@ -8,6 +8,7 @@
namespace Alltube; namespace Alltube;
use Barracuda\ArchiveStream\TarArchive; use Barracuda\ArchiveStream\TarArchive;
use GuzzleHttp\Client;
/** /**
* Class used to create a Tar archive from playlists and stream it to the browser. * Class used to create a Tar archive from playlists and stream it to the browser.
@ -33,7 +34,7 @@ class PlaylistArchiveStream extends TarArchive
/** /**
* Guzzle client. * Guzzle client.
* *
* @var \GuzzleHttp\Client * @var Client
*/ */
private $client; private $client;
@ -65,7 +66,7 @@ class PlaylistArchiveStream extends TarArchive
*/ */
public function __construct(Config $config = null) public function __construct(Config $config = null)
{ {
$this->client = new \GuzzleHttp\Client(); $this->client = new Client();
$this->download = new VideoDownload($config); $this->download = new VideoDownload($config);
} }

View file

@ -5,7 +5,9 @@
namespace Alltube; namespace Alltube;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use Slim\Router; use Slim\Router;
/** /**
@ -43,8 +45,8 @@ class UglyRouter extends Router
* @param array $data Named argument replacement data * @param array $data Named argument replacement data
* @param array $queryParams Optional query string parameters * @param array $queryParams Optional query string parameters
* *
* @throws \RuntimeException If named route does not exist * @throws RuntimeException If named route does not exist
* @throws \InvalidArgumentException If required data not provided * @throws InvalidArgumentException If required data not provided
* *
* @return string * @return string
*/ */

View file

@ -5,6 +5,8 @@
namespace Alltube; namespace Alltube;
use Exception;
use stdClass;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
/** /**
@ -24,8 +26,8 @@ class VideoDownload
* *
* @param Config $config Config instance. * @param Config $config Config instance.
* *
* @throws \Exception If youtube-dl is missing * @throws Exception If youtube-dl is missing
* @throws \Exception If Python is missing * @throws Exception If Python is missing
*/ */
public function __construct(Config $config = null) public function __construct(Config $config = null)
{ {
@ -39,9 +41,9 @@ class VideoDownload
so they will always go to the logs. so they will always go to the logs.
*/ */
if (!is_file($this->config->youtubedl)) { if (!is_file($this->config->youtubedl)) {
throw new \Exception("Can't find youtube-dl at ".$this->config->youtubedl); throw new Exception("Can't find youtube-dl at ".$this->config->youtubedl);
} elseif (!$this->checkCommand([$this->config->python, '--version'])) { } elseif (!$this->checkCommand([$this->config->python, '--version'])) {
throw new \Exception("Can't find Python at ".$this->config->python); throw new Exception("Can't find Python at ".$this->config->python);
} }
} }
@ -82,8 +84,8 @@ class VideoDownload
* @param string $password Video password * @param string $password Video password
* *
* @throws PasswordException If the video is protected by a password and no password was specified * @throws PasswordException If the video is protected by a password and no password was specified
* @throws \Exception If the password is wrong * @throws Exception If the password is wrong
* @throws \Exception If youtube-dl returns an error * @throws Exception If youtube-dl returns an error
* *
* @return string * @return string
*/ */
@ -111,9 +113,9 @@ class VideoDownload
if ($errorOutput == 'ERROR: This video is protected by a password, use the --video-password option') { if ($errorOutput == 'ERROR: This video is protected by a password, use the --video-password option') {
throw new PasswordException($errorOutput); throw new PasswordException($errorOutput);
} elseif (substr($errorOutput, 0, 21) == 'ERROR: Wrong password') { } elseif (substr($errorOutput, 0, 21) == 'ERROR: Wrong password') {
throw new \Exception(_('Wrong password')); throw new Exception(_('Wrong password'));
} else { } else {
throw new \Exception($errorOutput); throw new Exception($errorOutput);
} }
} else { } else {
return trim($process->getOutput()); return trim($process->getOutput());
@ -209,7 +211,7 @@ class VideoDownload
* *
* @return array Arguments * @return array Arguments
*/ */
private function getRtmpArguments(\stdClass $video) private function getRtmpArguments(stdClass $video)
{ {
$arguments = []; $arguments = [];
@ -260,14 +262,14 @@ class VideoDownload
* @param string $filetype Filetype of the converted file * @param string $filetype Filetype of the converted file
* @param bool $audioOnly True to return an audio-only file * @param bool $audioOnly True to return an audio-only file
* *
* @throws \Exception If avconv/ffmpeg is missing * @throws Exception If avconv/ffmpeg is missing
* *
* @return Process Process * @return Process Process
*/ */
private function getAvconvProcess(\stdClass $video, $audioBitrate, $filetype = 'mp3', $audioOnly = true) private function getAvconvProcess(stdClass $video, $audioBitrate, $filetype = 'mp3', $audioOnly = true)
{ {
if (!$this->checkCommand([$this->config->avconv, '-version'])) { if (!$this->checkCommand([$this->config->avconv, '-version'])) {
throw(new \Exception(_('Can\'t find avconv or ffmpeg.'))); throw(new Exception(_('Can\'t find avconv or ffmpeg.')));
} }
if ($video->protocol == 'rtmp') { if ($video->protocol == 'rtmp') {
@ -314,8 +316,8 @@ class VideoDownload
* @param string $format Format to use for the video * @param string $format Format to use for the video
* @param string $password Video password * @param string $password Video password
* *
* @throws \Exception If your try to convert and M3U8 video * @throws Exception If your try to convert and M3U8 video
* @throws \Exception If the popen stream was not created correctly * @throws Exception If the popen stream was not created correctly
* *
* @return resource popen stream * @return resource popen stream
*/ */
@ -323,7 +325,7 @@ class VideoDownload
{ {
$video = $this->getJSON($url, $format, $password); $video = $this->getJSON($url, $format, $password);
if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) { if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) {
throw(new \Exception(_('Conversion of M3U8 files is not supported.'))); throw(new Exception(_('Conversion of M3U8 files is not supported.')));
} }
$avconvProc = $this->getAvconvProcess($video, $this->config->audioBitrate); $avconvProc = $this->getAvconvProcess($video, $this->config->audioBitrate);
@ -331,7 +333,7 @@ class VideoDownload
$stream = popen($avconvProc->getCommandLine(), 'r'); $stream = popen($avconvProc->getCommandLine(), 'r');
if (!is_resource($stream)) { if (!is_resource($stream)) {
throw new \Exception(_('Could not open popen stream.')); throw new Exception(_('Could not open popen stream.'));
} }
return $stream; return $stream;
@ -340,17 +342,17 @@ class VideoDownload
/** /**
* Get video stream from an M3U playlist. * Get video stream from an M3U playlist.
* *
* @param \stdClass $video Video object returned by getJSON * @param stdClass $video Video object returned by getJSON
* *
* @throws \Exception If avconv/ffmpeg is missing * @throws Exception If avconv/ffmpeg is missing
* @throws \Exception If the popen stream was not created correctly * @throws Exception If the popen stream was not created correctly
* *
* @return resource popen stream * @return resource popen stream
*/ */
public function getM3uStream(\stdClass $video) public function getM3uStream(stdClass $video)
{ {
if (!$this->checkCommand([$this->config->avconv, '-version'])) { if (!$this->checkCommand([$this->config->avconv, '-version'])) {
throw(new \Exception(_('Can\'t find avconv or ffmpeg.'))); throw(new Exception(_('Can\'t find avconv or ffmpeg.')));
} }
$process = new Process( $process = new Process(
@ -368,7 +370,7 @@ class VideoDownload
$stream = popen($process->getCommandLine(), 'r'); $stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) { if (!is_resource($stream)) {
throw new \Exception(_('Could not open popen stream.')); throw new Exception(_('Could not open popen stream.'));
} }
return $stream; return $stream;
@ -379,7 +381,7 @@ class VideoDownload
* *
* @param array $urls URLs of the video ($urls[0]) and audio ($urls[1]) files * @param array $urls URLs of the video ($urls[0]) and audio ($urls[1]) files
* *
* @throws \Exception If the popen stream was not created correctly * @throws Exception If the popen stream was not created correctly
* *
* @return resource popen stream * @return resource popen stream
*/ */
@ -401,7 +403,7 @@ class VideoDownload
$stream = popen($process->getCommandLine(), 'r'); $stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) { if (!is_resource($stream)) {
throw new \Exception(_('Could not open popen stream.')); throw new Exception(_('Could not open popen stream.'));
} }
return $stream; return $stream;
@ -410,13 +412,13 @@ class VideoDownload
/** /**
* Get video stream from an RTMP video. * Get video stream from an RTMP video.
* *
* @param \stdClass $video Video object returned by getJSON * @param stdClass $video Video object returned by getJSON
* *
* @throws \Exception If the popen stream was not created correctly * @throws Exception If the popen stream was not created correctly
* *
* @return resource popen stream * @return resource popen stream
*/ */
public function getRtmpStream(\stdClass $video) public function getRtmpStream(stdClass $video)
{ {
$process = new Process( $process = new Process(
array_merge( array_merge(
@ -434,7 +436,7 @@ class VideoDownload
); );
$stream = popen($process->getCommandLine(), 'r'); $stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) { if (!is_resource($stream)) {
throw new \Exception(_('Could not open popen stream.')); throw new Exception(_('Could not open popen stream.'));
} }
return $stream; return $stream;
@ -446,11 +448,11 @@ class VideoDownload
* @param object $video Video object returned by youtube-dl * @param object $video Video object returned by youtube-dl
* @param string $format Requested format * @param string $format Requested format
* *
* @throws \Exception If the popen stream was not created correctly * @throws Exception If the popen stream was not created correctly
* *
* @return resource * @return resource
*/ */
public function getPlaylistArchiveStream(\stdClass $video, $format) public function getPlaylistArchiveStream(stdClass $video, $format)
{ {
$playlistItems = []; $playlistItems = [];
foreach ($video->entries as $entry) { foreach ($video->entries as $entry) {
@ -458,7 +460,7 @@ class VideoDownload
} }
$stream = fopen('playlist://'.implode(';', $playlistItems).'/'.$format, 'r'); $stream = fopen('playlist://'.implode(';', $playlistItems).'/'.$format, 'r');
if (!is_resource($stream)) { if (!is_resource($stream)) {
throw new \Exception(_('Could not open fopen stream.')); throw new Exception(_('Could not open fopen stream.'));
} }
return $stream; return $stream;
@ -473,8 +475,8 @@ class VideoDownload
* @param string $filetype Filetype of the converted file * @param string $filetype Filetype of the converted file
* @param string $password Video password * @param string $password Video password
* *
* @throws \Exception If your try to convert and M3U8 video * @throws Exception If your try to convert and M3U8 video
* @throws \Exception If the popen stream was not created correctly * @throws Exception If the popen stream was not created correctly
* *
* @return resource popen stream * @return resource popen stream
*/ */
@ -482,7 +484,7 @@ class VideoDownload
{ {
$video = $this->getJSON($url, $format, $password); $video = $this->getJSON($url, $format, $password);
if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) { if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) {
throw(new \Exception(_('Conversion of M3U8 files is not supported.'))); throw(new Exception(_('Conversion of M3U8 files is not supported.')));
} }
$avconvProc = $this->getAvconvProcess($video, $audioBitrate, $filetype, false); $avconvProc = $this->getAvconvProcess($video, $audioBitrate, $filetype, false);
@ -490,7 +492,7 @@ class VideoDownload
$stream = popen($avconvProc->getCommandLine(), 'r'); $stream = popen($avconvProc->getCommandLine(), 'r');
if (!is_resource($stream)) { if (!is_resource($stream)) {
throw new \Exception(_('Could not open popen stream.')); throw new Exception(_('Could not open popen stream.'));
} }
return $stream; return $stream;

View file

@ -10,11 +10,16 @@ use Alltube\Locale;
use Alltube\LocaleManager; use Alltube\LocaleManager;
use Alltube\PasswordException; use Alltube\PasswordException;
use Alltube\VideoDownload; use Alltube\VideoDownload;
use Aura\Session\Segment;
use Aura\Session\SessionFactory;
use Exception;
use GuzzleHttp\Client;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Slim\Container; use Slim\Container;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
use Slim\Http\Stream; use Slim\Http\Stream;
use Slim\Views\Smarty;
/** /**
* Main controller. * Main controller.
@ -45,14 +50,14 @@ class FrontController
/** /**
* Session segment used to store session variables. * Session segment used to store session variables.
* *
* @var \Aura\Session\Segment * @var Segment
*/ */
private $sessionSegment; private $sessionSegment;
/** /**
* Smarty view. * Smarty view.
* *
* @var \Slim\Views\Smarty * @var Smarty
*/ */
private $view; private $view;
@ -88,9 +93,9 @@ class FrontController
$this->container = $container; $this->container = $container;
$this->view = $this->container->get('view'); $this->view = $this->container->get('view');
$this->localeManager = $this->container->get('locale'); $this->localeManager = $this->container->get('locale');
$session_factory = new \Aura\Session\SessionFactory(); $session_factory = new SessionFactory();
$session = $session_factory->newInstance($cookies); $session = $session_factory->newInstance($cookies);
$this->sessionSegment = $session->getSegment('Alltube\Controller\FrontController'); $this->sessionSegment = $session->getSegment(self::class);
if ($this->config->stream) { if ($this->config->stream) {
$this->defaultFormat = 'best'; $this->defaultFormat = 'best';
} }
@ -214,7 +219,7 @@ class FrontController
} }
} catch (PasswordException $e) { } catch (PasswordException $e) {
return $this->password($request, $response); return $this->password($request, $response);
} catch (\Exception $e) { } catch (Exception $e) {
$response = $response->withHeader( $response = $response->withHeader(
'Content-Disposition', 'Content-Disposition',
'attachment; filename="'. 'attachment; filename="'.
@ -313,11 +318,11 @@ class FrontController
* *
* @param Request $request PSR-7 request * @param Request $request PSR-7 request
* @param Response $response PSR-7 response * @param Response $response PSR-7 response
* @param \Exception $exception Error to display * @param Exception $exception Error to display
* *
* @return Response HTTP response * @return Response HTTP response
*/ */
public function error(Request $request, Response $response, \Exception $exception) public function error(Request $request, Response $response, Exception $exception)
{ {
$this->view->render( $this->view->render(
$response, $response,
@ -366,7 +371,7 @@ class FrontController
$response = $response->withHeader('Content-Type', 'video/'.$video->ext); $response = $response->withHeader('Content-Type', 'video/'.$video->ext);
$body = new Stream($stream); $body = new Stream($stream);
} else { } else {
$client = new \GuzzleHttp\Client(); $client = new Client();
$stream = $client->request('GET', $video->url, ['stream' => true]); $stream = $client->request('GET', $video->url, ['stream' => true]);
$response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type')); $response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type'));
$response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length')); $response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length'));
@ -397,7 +402,7 @@ class FrontController
private function getRemuxStream(array $urls, $format, Response $response, Request $request) private function getRemuxStream(array $urls, $format, Response $response, Request $request)
{ {
if (!$this->config->remux) { if (!$this->config->remux) {
throw new \Exception(_('You need to enable remux mode to merge two formats.')); throw new Exception(_('You need to enable remux mode to merge two formats.'));
} }
$stream = $this->download->getRemuxStream($urls); $stream = $this->download->getRemuxStream($urls);
$response = $response->withHeader('Content-Type', 'video/x-matroska'); $response = $response->withHeader('Content-Type', 'video/x-matroska');
@ -464,7 +469,7 @@ class FrontController
); );
} else { } else {
if (empty($videoUrls[0])) { if (empty($videoUrls[0])) {
throw new \Exception(_("Can't find URL of video.")); throw new Exception(_("Can't find URL of video."));
} }
return $response->withRedirect($videoUrls[0]); return $response->withRedirect($videoUrls[0]);
@ -533,7 +538,7 @@ class FrontController
return $response->withRedirect( return $response->withRedirect(
$this->container->get('router')->pathFor('video').'?url='.urlencode($params['url']) $this->container->get('router')->pathFor('video').'?url='.urlencode($params['url'])
); );
} catch (\Exception $e) { } catch (Exception $e) {
$response->getBody()->write($e->getMessage()); $response->getBody()->write($e->getMessage());
return $response->withHeader('Content-Type', 'text/plain')->withStatus(500); return $response->withHeader('Content-Type', 'text/plain')->withStatus(500);

View file

@ -58,6 +58,6 @@ $app->get(
try { try {
$app->run(); $app->run();
} catch (\SmartyException $e) { } catch (SmartyException $e) {
die('Smarty could not compile the template file: '.$e->getMessage()); die('Smarty could not compile the template file: '.$e->getMessage());
} }

View file

@ -9,6 +9,7 @@ use Alltube\Config;
use Alltube\Controller\FrontController; use Alltube\Controller\FrontController;
use Alltube\LocaleManager; use Alltube\LocaleManager;
use Alltube\ViewFactory; use Alltube\ViewFactory;
use Exception;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Slim\Container; use Slim\Container;
use Slim\Http\Environment; use Slim\Http\Environment;
@ -348,7 +349,7 @@ class FrontControllerTest extends TestCase
*/ */
public function testError() public function testError()
{ {
$result = $this->controller->error($this->request, $this->response, new \Exception('foo')); $result = $this->controller->error($this->request, $this->response, new Exception('foo'));
$this->assertTrue($result->isServerError()); $this->assertTrue($result->isServerError());
} }

View file

@ -27,7 +27,7 @@ class LocaleManagerTest extends TestCase
protected function setUp() protected function setUp()
{ {
$this->localeManager = new LocaleManager(); $this->localeManager = new LocaleManager();
$_SESSION['Alltube\LocaleManager']['locale'] = 'foo_BAR'; $_SESSION[LocaleManager::class]['locale'] = 'foo_BAR';
} }
/** /**

View file

@ -126,7 +126,7 @@ class VideoDownloadTest extends TestCase
* Test getURL function with a protected video and no password. * Test getURL function with a protected video and no password.
* *
* @return void * @return void
* @expectedException \Alltube\PasswordException * @expectedException Alltube\PasswordException
*/ */
public function testGetURLWithMissingPassword() public function testGetURLWithMissingPassword()
{ {