From 3f83b5569a4bfe3b5a97e34007db56fa3ed243a5 Mon Sep 17 00:00:00 2001 From: Maciej Klemarczyk Date: Fri, 27 Jul 2018 01:01:41 +0200 Subject: [PATCH] Name was changed to proper FluidPlayer, additional sourceOptions were implemented. Source is now being checked before use. Script options were added for configuration. --- src/helpers/HtmlVideo.php | 18 ++---------- src/models/VideoSource.php | 1 + .../{FluidPalyer.php => FluidPlayer.php} | 28 +++++++++++++------ ...idPalyerAsset.php => FluidPlayerAsset.php} | 2 +- 4 files changed, 25 insertions(+), 24 deletions(-) rename src/widgets/{FluidPalyer.php => FluidPlayer.php} (62%) rename src/widgets/{FluidPalyerAsset.php => FluidPlayerAsset.php} (92%) diff --git a/src/helpers/HtmlVideo.php b/src/helpers/HtmlVideo.php index bfa54df..ccb9851 100644 --- a/src/helpers/HtmlVideo.php +++ b/src/helpers/HtmlVideo.php @@ -16,30 +16,18 @@ * @author Maciej Klemarczyk * @since 1.0 */ -class FluidPlayerWidget extends \yii\helpers\BaseHtml +class HtmlVideo extends \yii\helpers\BaseHtml { - public static function beginForm($id, $options = []) + public static function beginVideo($id, $options = []) { $options['id'] = $id; return static::beginTag('video', $options); } - public static function source($src, $type = 'SD', $options = []) + public static function source($src, $options = []) { $options['src'] = Url::to($src); - if (isset($options['srcset']) && is_array($options['srcset'])) { - $srcset = []; - foreach ($options['srcset'] as $descriptor => $url) { - $srcset[] = Url::to($url) . ' ' . $descriptor; - } - $options['srcset'] = implode(',', $srcset); - } - - if (!isset($options['alt'])) { - $options['alt'] = ''; - } - return static::tag('source', '', $options); } diff --git a/src/models/VideoSource.php b/src/models/VideoSource.php index 3f1a359..ddd6afb 100644 --- a/src/models/VideoSource.php +++ b/src/models/VideoSource.php @@ -20,6 +20,7 @@ class VideoSource extends Model public $title; public $type; public $quality; + public $sourceOptions; public function rules() { diff --git a/src/widgets/FluidPalyer.php b/src/widgets/FluidPlayer.php similarity index 62% rename from src/widgets/FluidPalyer.php rename to src/widgets/FluidPlayer.php index 5396ea8..b503347 100644 --- a/src/widgets/FluidPalyer.php +++ b/src/widgets/FluidPlayer.php @@ -9,6 +9,7 @@ namespace insma\player\widgets; use yii\base\Widget; +use yii\helpers\Json; use yii\web\View; use insma\player\helpers\HtmlVideo; @@ -30,18 +31,25 @@ class FluidPlayer extends Widget */ public $videoOptions; + /** + * @var string + */ + public $playerOptions; + public function init() { parent::init(); - $this->registerAssetBundle() + $this->registerAssetBundle(); $this->registerPlayerScript(); } public function run() { - $htmlView = HtmlVideo::beginVideo($this->id, $videoOptions); - foreach ($sources as $key => $value) { - $htmlView .= self::prepareSourceTag($value); + $htmlView = HtmlVideo::beginVideo($this->id, $this->videoOptions); + foreach ($this->sources as $key => $value) { + if ($value instanceof VideoSource && $value->validate()) { + $htmlView .= self::prepareSourceTag($value); + } } $htmlView .= HtmlVideo::endVideo(); return $htmlView; @@ -51,12 +59,15 @@ protected function prepareSourceTag($source) { $options = []; - $options['source'] = $source->source; + if (is_array($source->sourceOptions)) { + $options = $source->sourceOptions; + } + $options['title'] = $source->title; $options['type'] = $source->type; $options['quality'] = $source->quality; - return HtmlVideo::source($src, $options); + return HtmlVideo::source($source->source, $options); } /** @@ -64,12 +75,13 @@ protected function prepareSourceTag($source) */ protected function registerAssetBundle() { - FluidPalyerAsset::register($this->getView()); + FluidPlayerAsset::register($this->getView()); } protected function registerPlayerScript() { - $jsScript = ''; + $options = Json::encode($this->playerOptions); + $jsScript = 'fluidPlayer("' . $this->id . '",' . $options . ');'; $this->getView()->registerJs($jsScript, View::POS_READY, $this->id); } } diff --git a/src/widgets/FluidPalyerAsset.php b/src/widgets/FluidPlayerAsset.php similarity index 92% rename from src/widgets/FluidPalyerAsset.php rename to src/widgets/FluidPlayerAsset.php index c3fbe64..18bec08 100644 --- a/src/widgets/FluidPalyerAsset.php +++ b/src/widgets/FluidPlayerAsset.php @@ -12,7 +12,7 @@ * @author Maciej Klemarczyk * @since 1.0 */ -class FluidPalyerAsset extends \yii\web\AssetBundle +class FluidPlayerAsset extends \yii\web\AssetBundle { public $sourcePath = '@vendor/insma/fluid-player';