Skip to content

Commit

Permalink
source abstraction (rethink)
Browse files Browse the repository at this point in the history
  • Loading branch information
backbone87 committed May 21, 2012
1 parent 50ea1e4 commit e9df2e5
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 54 deletions.
@@ -0,0 +1,45 @@
<?php

class AbstractMultimediaVideoSource implements MultimediaVideoSource {

private $strType;

private $strURL;

protected function __construct($strURL, $strType) {
$this->strURL = $strURL;
$this->strType = $strType;
}

public function serialize() {
return serialize(array(
$this->strType,
$this->strURL,
));
}

public function unserialize($strSerialized) {
list(
$this->strType,
$this->strURL,
) = unserialize($strSerialized);
}

public function getType() {
return $this->strType;
}

public function getURL() {
return $this->strURL;
}

public function isValid() {
try {
$this->validate();
return true;
} catch(Exception $e) {
return false;
}
}

}
22 changes: 12 additions & 10 deletions TL_ROOT/system/modules/backboneit_multimedia/MultimediaDCA.php
Expand Up @@ -80,33 +80,35 @@ public function submitVideo($objDC) {

public function loadVideoSourcesLocal($varValue, $objDC) {
$arrSources = array();
foreach($this->getMultimedia($objDC)->getLocalSources() as $arrSource) {
$arrSources[] = $arrSource['url'];
foreach($this->getMultimedia($objDC)->getSourceByClass('MultimediaVideoLocalSource') as $objSource) {
$arrSources[] = $objSource->getLocalPath();
}
return $arrSources;
}

public function loadVideoSourcesExternal($varValue, $objDC) {
return $this->getMultimedia($objDC)->getExternalSources();
$arrSources = array();
foreach($this->getMultimedia($objDC)->getSourceByClass('MultimediaVideoHTTPSource') as $objSource) {
$arrSources[] = $objSource->getURL();
}
return $arrSources;
}

public function saveVideoSourcesLocal($varValue, $objDC) {
$arrSources = array();
foreach(deserialize($varValue, true) as $strSource) {
$arrSources[] = array('url' => $strSource, 'local' => true);
foreach(deserialize($varValue, true) as $strURL) {
$arrSources[] = new MultimediaVideoLocalSource($strURL);
}
$this->getMultimedia($objDC)->replaceLocalSources($arrSources);
$this->getMultimedia($objDC)->replaceSourceByClass($arrSources);
return null;
}

public function saveVideoSourcesExternal($varValue, $objDC) {
$arrSources = array();
foreach(deserialize($varValue, true) as $arrSource) {
if(strlen($arrSource['url'])) {
$arrSources[] = $arrSource;
}
strlen($arrSource['url']) && $arrSources[] = new MultimediaVideoHTTPSource($arrSource['url']);
}
$this->getMultimedia($objDC)->replaceExternalSources($arrSources);
$this->getMultimedia($objDC)->replaceSourceByClass($arrSources);
return null;
}

Expand Down
Expand Up @@ -29,64 +29,57 @@ public function getSource() {
return $this->arrData['video_source'];
}

public function validateSources() {
$this->arrData['video_source'] = deserialize($this->arrData['video_source'], true);
$arrInvalid = array();
foreach($this->arrData['video_source'] as &$arrSource) {
try {
$arrSource['mime'] = self::fetchMIME($arrSource);
if(!isset(self::$arrMIMEs[$arrSource['mime']])) {
$arrInvalid[] = $arrSource;
}
} catch(Exception $e) {
$arrInvalid[] = $arrSource;
}
}

return $arrInvalid;
}

public function setSource(array $arrSources) {
$this->arrData['video_source'] = $arrSources;
}

public function replaceLocalSources(array $arrSources) {
$this->setSource(array_merge($arrSources, $this->getExternalSources()));
}

public function replaceExternalSources(array $arrSources) {
$this->setSource(array_merge($arrSources, $this->getLocalSources()));
}

public function getLocalSources() {
return array_filter($this->getSource(), array(__CLASS__, 'isLocalSource'));
}

public function getExternalSources() {
return array_filter($this->getSource(), array(__CLASS__, 'isExternalSource'));
public function validateSource() {
$arrInvalid = array();
foreach($this->getSource() as $objSource) {
$objSource->isValid() || $arrInvalid[] = $objSource;
}
return $arrInvalid;
}

public static function fetchMIME(array &$arrSource) {
$strURL = self::isLocalSource($arrSource) ? Environment::getInstance()->base . $arrSource['url'] : $arrSource['url'];
public function getSourceByType() {
$arrTypes = array_flip(func_get_args());

// $objReq = new RequestExtendedCached(60 * 60); // bugged see #2991
$objReq = new RequestExtended();
$objReq->send($strURL, false, 'HEAD');
if(!$arrTypes) {
return $this->getSource();
}

if($objReq->hasError()) {
throw new Exception(sprintf('Source request responded with error: [%s]', $objReq->error), $objReq->code);
$arrSources = array();
foreach($this->getSource() as $objSource) {
isset($arrTypes[$objSource->getType()]) && $arrSources[] = $objSource;
}

list($strMIME) = explode(';', $objReq->headers['Content-Type'], 2);
return $strMIME ? $strMIME : 'application/octet-stream';
return $arrSources;
}

public static function isLocalSource(array $arrSource) {
return $arrSource['local'];
public function getSourceByClass() {
$arrClasses = array_flip(func_get_args());

if(!$arrClasses) {
return $this->getSource();
}

$arrSources = array();
foreach($this->getSource() as $objSource) {
isset($arrClasses[get_class($objSource)]) && $arrSources[] = $objSource;
}

return $arrSources;
}

public static function isExternalSource(array $arrSource) {
return !$arrSource['local'];
public function replaceSourceByClass(array $arrSources) {
$arrTypes = array();
foreach($arrSources as $objSource) {
$arrTypes[get_class($objSource)] = true;
}
foreach($this->getSource() as $objSource) {
isset($arrTypes[get_class($objSource)]) || $arrSources[] = $objSource;
}
$this->setSource($arrSources);
}

}
@@ -0,0 +1,57 @@
<?php

class MultimediaVideoHTTPSource implements AbstractMultimediaVideoSource {

private static $arrMIMEs = array(
'application/ogg' => true,
'video/ogg' => true,
'video/webm' => true,
'video/mp4' => true,
'video/x-flv' => true,
);

private $strMIME;

public function __construct($strURL, $strType = 'http') {
parent::__construct($strURL, $strType);
}

public function serialize() {
return serialize(array(
parent::serialize(),
$this->strMIME,
));
}

public function unserialize($strSerialized) {
list(
$strParent,
$this->strMIME,
) = unserialize($strSerialized);
parent::unserialize($strParent);
}

public function validate() {
if(!isset(self::$arrMIMEs[$this->getMIME()])) {
throw new Exception(sprintf('[%s] does not respond with a valid internet video MIME', $this->getURL()));
}
}

public function getMIME() {
return $this->strMIME ? $this->strMIME : $this->strMIME = $this->fetchMIME();
}

protected function fetchMIME() {
// $objReq = new RequestExtendedCached(60 * 60); // bugged see #2991
$objReq = new RequestExtended();
$objReq->send($this->getURL(), false, 'HEAD');

if($objReq->hasError()) {
throw new Exception(sprintf('Source request responded with error: [%s]', $objReq->error), $objReq->code);
}

list($strMIME) = explode(';', $objReq->headers['Content-Type'], 2);
return $strMIME ? $strMIME : 'application/octet-stream';
}

}
@@ -0,0 +1,17 @@
<?php

class MultimediaVideoLocalSource implements MultimediaVideoHTTPSource {

public function __construct($strURL) {
parent::__construct($strURL, 'local');
}

public function getURL() {
return Environment::getInstance()->base . parent::getURL();
}

public function getLocalPath() {
return parent::getURL();
}

}
@@ -0,0 +1,13 @@
<?php

interface MultimediaVideoSource extends Serializable {

public function getType();

public function getURL();

public function isValid();

public function validate();

}

0 comments on commit e9df2e5

Please sign in to comment.