From 11e8243443adaa52cbcbab5373733e52c2ad4529 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Fri, 8 Apr 2016 19:06:41 +0200 Subject: [PATCH] Don't use static functions --- classes/VideoDownload.php | 40 +++++++++++++++---------------- controllers/FrontController.php | 42 ++++++++++++++++++--------------- index.php | 15 +++++++----- tests/VideoDownloadTest.php | 19 +++++++++------ 4 files changed, 64 insertions(+), 52 deletions(-) diff --git a/classes/VideoDownload.php b/classes/VideoDownload.php index ea0ca2c..7c0588c 100644 --- a/classes/VideoDownload.php +++ b/classes/VideoDownload.php @@ -27,17 +27,21 @@ use Symfony\Component\Process\Process; * */ class VideoDownload { + public function __construct() + { + $this->config = Config::getInstance(); + } + /** * Get the user agent used youtube-dl * * @return string UA * */ - public static function getUA() + public function getUA() { - $config = Config::getInstance(); $cmd = escapeshellcmd( - $config->python.' '.escapeshellarg($config->youtubedl). - ' '.$config->params + $this->config->python.' '.escapeshellarg($this->config->youtubedl). + ' '.$this->config->params ); $process = new Process($cmd.' --dump-user-agent'); $process->run(); @@ -49,12 +53,11 @@ class VideoDownload * * @return array Extractors * */ - public static function listExtractors() + public function listExtractors() { - $config = Config::getInstance(); $cmd = escapeshellcmd( - $config->python.' '.escapeshellarg($config->youtubedl). - ' '.$config->params + $this->config->python.' '.escapeshellarg($this->config->youtubedl). + ' '.$this->config->params ); $process = new Process($cmd.' --list-extractors'); $process->run(); @@ -69,12 +72,11 @@ class VideoDownload * * @return string Filename * */ - public static function getFilename($url, $format = null) + public function getFilename($url, $format = null) { - $config = Config::getInstance(); $cmd = escapeshellcmd( - $config->python.' '.escapeshellarg($config->youtubedl). - ' '.$config->params + $this->config->python.' '.escapeshellarg($this->config->youtubedl). + ' '.$this->config->params ); if (isset($format)) { $cmd .= ' -f '.escapeshellarg($format); @@ -93,12 +95,11 @@ class VideoDownload * * @return string JSON * */ - public static function getJSON($url, $format = null) + public function getJSON($url, $format = null) { - $config = Config::getInstance(); $cmd = escapeshellcmd( - $config->python.' '.escapeshellarg($config->youtubedl). - ' '.$config->params + $this->config->python.' '.escapeshellarg($this->config->youtubedl). + ' '.$this->config->params ); if (isset($format)) { $cmd .= ' -f '.escapeshellarg($format); @@ -121,12 +122,11 @@ class VideoDownload * * @return string URL of video * */ - public static function getURL($url, $format = null) + public function getURL($url, $format = null) { - $config = Config::getInstance(); $cmd = escapeshellcmd( - $config->python.' '.escapeshellarg($config->youtubedl). - ' '.$config->params + $this->config->python.' '.escapeshellarg($this->config->youtubedl). + ' '.$this->config->params ); if (isset($format)) { $cmd .= ' -f '.escapeshellarg($format); diff --git a/controllers/FrontController.php b/controllers/FrontController.php index 06f8188..0f5846a 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -28,6 +28,11 @@ use Alltube\Config; * */ class FrontController { + public function __construct() + { + $this->config = Config::getInstance(); + $this->download = new VideoDownload(); + } /** * Display index page @@ -37,10 +42,9 @@ class FrontController * * @return void */ - public static function index($request, $response) + public function index($request, $response) { global $container; - $config = Config::getInstance(); $container->view->render( $response, 'head.tpl', @@ -56,7 +60,7 @@ class FrontController $response, 'index.tpl', array( - 'convert'=>$config->convert + 'convert'=>$this->config->convert ) ); $container->view->render($response, 'footer.tpl'); @@ -70,7 +74,7 @@ class FrontController * * @return void */ - public static function extractors($request, $response) + public function extractors($request, $response) { global $container; $container->view->render( @@ -86,7 +90,7 @@ class FrontController $response, 'extractors.tpl', array( - 'extractors'=>VideoDownload::listExtractors() + 'extractors'=>$this->download->listExtractors() ) ); $container->view->render($response, 'footer.tpl'); @@ -100,17 +104,17 @@ class FrontController * * @return void */ - public static function video($request, $response) + public function video($request, $response) { global $container; - $config = Config::getInstance(); + $this->config = Config::getInstance(); if (isset($_GET["url"])) { if (isset($_GET['audio'])) { try { - $video = VideoDownload::getJSON($_GET["url"]); + $video = $this->download->getJSON($_GET["url"]); //Vimeo needs a correct user-agent - $UA = VideoDownload::getUA(); + $UA = $this->download->getUA(); ini_set( 'user_agent', $UA @@ -122,7 +126,7 @@ class FrontController 'Content-Disposition: attachment; filename="'. html_entity_decode( pathinfo( - VideoDownload::getFilename( + $this->download->getFilename( $video->webpage_url ), PATHINFO_FILENAME @@ -134,7 +138,7 @@ class FrontController header("Content-Type: audio/mpeg"); passthru( '/usr/bin/rtmpdump -q -r '.escapeshellarg($video->url). - ' | '.$config->avconv. + ' | '.$this->config->avconv. ' -v quiet -i - -f mp3 -vn pipe:1' ); exit; @@ -144,7 +148,7 @@ class FrontController 'Content-Disposition: attachment; filename="'. html_entity_decode( pathinfo( - VideoDownload::getFilename( + $this->download->getFilename( $video->webpage_url ), PATHINFO_FILENAME @@ -155,10 +159,10 @@ class FrontController ); header("Content-Type: audio/mpeg"); passthru( - 'curl '.$config->curl_params. + 'curl '.$this->config->curl_params. ' --user-agent '.escapeshellarg($UA). ' '.escapeshellarg($video->url). - ' | '.$config->avconv. + ' | '.$this->config->avconv. ' -v quiet -i - -f mp3 -vn pipe:1' ); exit; @@ -168,7 +172,7 @@ class FrontController } } else { try { - $video = VideoDownload::getJSON($_GET["url"]); + $video = $this->download->getJSON($_GET["url"]); $container->view->render( $response, 'head.tpl', @@ -216,12 +220,12 @@ class FrontController * * @return void */ - public static function redirect($request, $response) + public function redirect($request, $response) { global $app; if (isset($_GET["url"])) { try { - $video = VideoDownload::getURL($_GET["url"]); + $video = $this->download->getURL($_GET["url"]); return $response->withRedirect($video['url']); } catch (\Exception $e) { echo $e->getMessage().PHP_EOL; @@ -238,12 +242,12 @@ class FrontController * * @return void */ - public static function json($request, $response) + public function json($request, $response) { global $app; if (isset($_GET["url"])) { try { - $video = VideoDownload::getJSON($_GET["url"]); + $video = $this->download->getJSON($_GET["url"]); return $response->withJson($video); } catch (\Exception $e) { return $response->withJson( diff --git a/index.php b/index.php index 468a685..959e9d7 100644 --- a/index.php +++ b/index.php @@ -15,6 +15,7 @@ require_once __DIR__.'/vendor/autoload.php'; require_once __DIR__.'/vendor/rudloff/smarty-plugin-noscheme/modifier.noscheme.php'; use Alltube\VideoDownload; +use Alltube\Controller\FrontController; $app = new \Slim\App(); $container = $app->getContainer(); @@ -22,30 +23,32 @@ $container['view'] = function ($c) { $view = new \Slim\Views\Smarty(__DIR__.'/templates/'); $view->addSlimPlugins($c['router'], $c['request']->getUri()); - $view->registerPlugin('modifier', 'noscheme', 'Smarty_Modifier_noscheme'); + $view->registerPlugin('modifier', 'noscheme', 'Smarty_Modifier_noscheme'); return $view; }; +$controller = new FrontController(); + $app->get( '/', - array('Alltube\Controller\FrontController', 'index') + array($controller, 'index') ); $app->get( '/extractors', - array('Alltube\Controller\FrontController', 'extractors') + array($controller, 'extractors') )->setName('extractors'); $app->get( '/video', - array('Alltube\Controller\FrontController', 'video') + array($controller, 'video') )->setName('video'); $app->get( '/redirect', - array('Alltube\Controller\FrontController', 'redirect') + array($controller, 'redirect') ); $app->get( '/json', - array('Alltube\Controller\FrontController', 'json') + array($controller, 'json') ); $app->run(); diff --git a/tests/VideoDownloadTest.php b/tests/VideoDownloadTest.php index 9f0c7b4..35b632d 100644 --- a/tests/VideoDownloadTest.php +++ b/tests/VideoDownloadTest.php @@ -27,6 +27,11 @@ use Alltube\VideoDownload; * */ class VideoDownloadTest extends \PHPUnit_Framework_TestCase { + protected function setUp() + { + $this->download = new VideoDownload(); + } + /** * Test getUA function * @@ -34,7 +39,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase */ public function testGetUA() { - $this->assertStringStartsWith('Mozilla/', VideoDownload::getUA()); + $this->assertStringStartsWith('Mozilla/', $this->download->getUA()); } /** @@ -44,7 +49,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase */ public function testListExtractors() { - $extractors = VideoDownload::listExtractors(); + $extractors = $this->download->listExtractors(); $this->assertContains('youtube', $extractors); } @@ -59,7 +64,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase */ public function testGetURL($url, $format) { - $videoURL = VideoDownload::getURL($url, $format); + $videoURL = $this->download->getURL($url, $format); $this->assertArrayHasKey('success', $videoURL); $this->assertArrayHasKey('url', $videoURL); } @@ -75,7 +80,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase */ public function testGetURLError($url) { - $videoURL = VideoDownload::getURL($url); + $videoURL = $this->download->getURL($url); } /** @@ -126,7 +131,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase */ public function testGetFilename($url, $format, $result) { - $filename = VideoDownload::getFilename($url, $format); + $filename = $this->download->getFilename($url, $format); $this->assertEquals($filename, $result); } @@ -141,7 +146,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase */ public function testGetJSON($url, $format) { - $info = VideoDownload::getJSON($url, $format); + $info = $this->download->getJSON($url, $format); $this->assertObjectHasAttribute('webpage_url', $info); $this->assertObjectHasAttribute('url', $info); $this->assertObjectHasAttribute('ext', $info); @@ -161,6 +166,6 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase */ public function testGetJSONError($url) { - $videoURL = VideoDownload::getJSON($url); + $videoURL = $this->download->getJSON($url); } }