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;
use Exception;
use Symfony\Component\Yaml\Yaml;
/**
@ -172,7 +173,7 @@ class Config
*/
$options = [];
} 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->file = $yamlfile;

View file

@ -5,6 +5,8 @@
namespace Alltube;
use Locale as PHPLocale;
use Rinvex\Country\Country;
use Teto\HTTP\AcceptLanguage;
/**
@ -55,7 +57,7 @@ class Locale
*/
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.
*
* @return \Rinvex\Country\Country|array
* @return Country|array
*/
public function getCountry()
{

View file

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

View file

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

View file

@ -8,6 +8,7 @@
namespace Alltube;
use Barracuda\ArchiveStream\TarArchive;
use GuzzleHttp\Client;
/**
* 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.
*
* @var \GuzzleHttp\Client
* @var Client
*/
private $client;
@ -65,7 +66,7 @@ class PlaylistArchiveStream extends TarArchive
*/
public function __construct(Config $config = null)
{
$this->client = new \GuzzleHttp\Client();
$this->client = new Client();
$this->download = new VideoDownload($config);
}

View file

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

View file

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

View file

@ -10,11 +10,16 @@ use Alltube\Locale;
use Alltube\LocaleManager;
use Alltube\PasswordException;
use Alltube\VideoDownload;
use Aura\Session\Segment;
use Aura\Session\SessionFactory;
use Exception;
use GuzzleHttp\Client;
use Psr\Container\ContainerInterface;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Stream;
use Slim\Views\Smarty;
/**
* Main controller.
@ -45,14 +50,14 @@ class FrontController
/**
* Session segment used to store session variables.
*
* @var \Aura\Session\Segment
* @var Segment
*/
private $sessionSegment;
/**
* Smarty view.
*
* @var \Slim\Views\Smarty
* @var Smarty
*/
private $view;
@ -88,9 +93,9 @@ class FrontController
$this->container = $container;
$this->view = $this->container->get('view');
$this->localeManager = $this->container->get('locale');
$session_factory = new \Aura\Session\SessionFactory();
$session_factory = new SessionFactory();
$session = $session_factory->newInstance($cookies);
$this->sessionSegment = $session->getSegment('Alltube\Controller\FrontController');
$this->sessionSegment = $session->getSegment(self::class);
if ($this->config->stream) {
$this->defaultFormat = 'best';
}
@ -214,7 +219,7 @@ class FrontController
}
} catch (PasswordException $e) {
return $this->password($request, $response);
} catch (\Exception $e) {
} catch (Exception $e) {
$response = $response->withHeader(
'Content-Disposition',
'attachment; filename="'.
@ -313,11 +318,11 @@ class FrontController
*
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
* @param \Exception $exception Error to display
* @param Exception $exception Error to display
*
* @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(
$response,
@ -366,7 +371,7 @@ class FrontController
$response = $response->withHeader('Content-Type', 'video/'.$video->ext);
$body = new Stream($stream);
} else {
$client = new \GuzzleHttp\Client();
$client = new Client();
$stream = $client->request('GET', $video->url, ['stream' => true]);
$response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type'));
$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)
{
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);
$response = $response->withHeader('Content-Type', 'video/x-matroska');
@ -464,7 +469,7 @@ class FrontController
);
} else {
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]);
@ -533,7 +538,7 @@ class FrontController
return $response->withRedirect(
$this->container->get('router')->pathFor('video').'?url='.urlencode($params['url'])
);
} catch (\Exception $e) {
} catch (Exception $e) {
$response->getBody()->write($e->getMessage());
return $response->withHeader('Content-Type', 'text/plain')->withStatus(500);

View file

@ -58,6 +58,6 @@ $app->get(
try {
$app->run();
} catch (\SmartyException $e) {
} catch (SmartyException $e) {
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\LocaleManager;
use Alltube\ViewFactory;
use Exception;
use PHPUnit\Framework\TestCase;
use Slim\Container;
use Slim\Http\Environment;
@ -348,7 +349,7 @@ class FrontControllerTest extends TestCase
*/
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());
}

View file

@ -27,7 +27,7 @@ class LocaleManagerTest extends TestCase
protected function setUp()
{
$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.
*
* @return void
* @expectedException \Alltube\PasswordException
* @expectedException Alltube\PasswordException
*/
public function testGetURLWithMissingPassword()
{