Skip to content

Commit

Permalink
style: Declare imported classes at the top of the file
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudloff committed Feb 5, 2018
1 parent 33530ef commit 04fe43a
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 63 deletions.
3 changes: 2 additions & 1 deletion classes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Alltube;

use Exception;
use Symfony\Component\Yaml\Yaml;

/**
Expand Down Expand Up @@ -172,7 +173,7 @@ public static function getInstance($yamlfile = 'config/config.yml')
*/
$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;
Expand Down
6 changes: 4 additions & 2 deletions classes/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Alltube;

use Locale as PHPLocale;
use Rinvex\Country\Country;
use Teto\HTTP\AcceptLanguage;

/**
Expand Down Expand Up @@ -55,7 +57,7 @@ public function __toString()
*/
public function getFullName()
{
return \Locale::getDisplayName($this->getIso15897(), $this->getIso15897());
return PHPLocale::getDisplayName($this->getIso15897(), $this->getIso15897());
}

/**
Expand Down Expand Up @@ -91,7 +93,7 @@ public function getIso3166()
/**
* Get country information from locale.
*
* @return \Rinvex\Country\Country|array
* @return Country|array
*/
public function getCountry()
{
Expand Down
8 changes: 5 additions & 3 deletions classes/LocaleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Alltube;

use Aura\Session\Segment;
use Aura\Session\SessionFactory;
use Symfony\Component\Process\Process;

/**
Expand All @@ -29,7 +31,7 @@ class LocaleManager
/**
* Session segment used to store session variables.
*
* @var \Aura\Session\Segment
* @var Segment
*/
private $sessionSegment;

Expand All @@ -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));
Expand Down
4 changes: 3 additions & 1 deletion classes/PasswordException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

namespace Alltube;

use Exception;

/**
* Exception thrown when a video requires a password.
*/
class PasswordException extends \Exception
class PasswordException extends Exception
{
}
5 changes: 3 additions & 2 deletions classes/PlaylistArchiveStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,7 +34,7 @@ class PlaylistArchiveStream extends TarArchive
/**
* Guzzle client.
*
* @var \GuzzleHttp\Client
* @var Client
*/
private $client;

Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 4 additions & 2 deletions classes/UglyRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

namespace Alltube;

use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use Slim\Router;

/**
Expand Down Expand Up @@ -43,8 +45,8 @@ public function dispatch(ServerRequestInterface $request)
* @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
*/
Expand Down
72 changes: 37 additions & 35 deletions classes/VideoDownload.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Alltube;

use Exception;
use stdClass;
use Symfony\Component\Process\Process;

/**
Expand All @@ -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)
{
Expand All @@ -39,9 +41,9 @@ public function __construct(Config $config = null)
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);
}
}

Expand Down Expand Up @@ -82,8 +84,8 @@ public function listExtractors()
* @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
*/
Expand Down Expand Up @@ -111,9 +113,9 @@ private function getProp($url, $format = null, $prop = 'dump-json', $password =
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());
Expand Down Expand Up @@ -209,7 +211,7 @@ public function getAudioFilename($url, $format = null, $password = null)
*
* @return array Arguments
*/
private function getRtmpArguments(\stdClass $video)
private function getRtmpArguments(stdClass $video)
{
$arguments = [];

Expand Down Expand Up @@ -260,14 +262,14 @@ private function checkCommand(array $command)
* @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') {
Expand Down Expand Up @@ -314,24 +316,24 @@ private function getAvconvProcess(\stdClass $video, $audioBitrate, $filetype = '
* @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
*/
public function getAudioStream($url, $format, $password = null)
{
$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);

$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;
Expand All @@ -340,17 +342,17 @@ public function getAudioStream($url, $format, $password = null)
/**
* 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(
Expand All @@ -368,7 +370,7 @@ public function getM3uStream(\stdClass $video)

$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;
Expand All @@ -379,7 +381,7 @@ public function getM3uStream(\stdClass $video)
*
* @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
*/
Expand All @@ -401,7 +403,7 @@ public function getRemuxStream(array $urls)

$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;
Expand All @@ -410,13 +412,13 @@ public function getRemuxStream(array $urls)
/**
* 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(
Expand All @@ -434,7 +436,7 @@ public function getRtmpStream(\stdClass $video)
);
$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;
Expand All @@ -446,19 +448,19 @@ public function getRtmpStream(\stdClass $video)
* @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) {
$playlistItems[] = urlencode($entry->url);
}
$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;
Expand All @@ -473,24 +475,24 @@ public function getPlaylistArchiveStream(\stdClass $video, $format)
* @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
*/
public function getConvertedStream($url, $format, $audioBitrate, $filetype, $password = null)
{
$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);

$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;
Expand Down
Loading

0 comments on commit 04fe43a

Please sign in to comment.