diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index ce878a774..07c5c0063 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -34,7 +34,7 @@ class Asset implements \ArrayAccess protected $minified = false; /** - * Creates an Asset from file(s) path. + * Creates an Asset from a source. * * $options[ * 'fingerprint' => true, @@ -43,15 +43,15 @@ class Asset implements \ArrayAccess * 'ignore_missing' => false, * ]; * - * @param Builder $builder - * @param string|array $path - * @param array|null $options + * @param Builder $builder + * @param mixed $source + * @param array|null $options */ - public function __construct(Builder $builder, $path, array $options = null) + public function __construct(Builder $builder, $source, array $options = null) { $this->builder = $builder; $this->config = $builder->getConfig(); - $path = is_array($path) ? $path : [$path]; + $source = is_array($source) ? $source : [$source]; // handles options $fingerprint = (bool) $this->config->get('assets.fingerprint.enabled'); @@ -60,16 +60,26 @@ public function __construct(Builder $builder, $path, array $options = null) $ignore_missing = false; extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); - // loads file(s) + // loads $file = []; $prevType = ''; $prevExt = ''; - foreach ($path as $p) { - $file = $this->loadFile($p, $ignore_missing); - if ($file['missing']) { - $this->data['path'] = ''; + foreach ($source as $s) { + // determines source type + switch (true) { + case Util\Str::isBinary(): + //return $this->initFromBinary($this->data); + case Util\Url::isUrl(): + //return $this->initFromUrl($this->data); + case Util\File::isFilePath(): + $file = $this->loadFile($s, $ignore_missing); + if ($file['missing']) { + $this->data['path'] = ''; - continue; + break; + } + default: + throw new Exception('Asset source not readable'); } // bundle: same type only @@ -100,7 +110,7 @@ public function __construct(Builder $builder, $path, array $options = null) $prevExt = $file['ext']; } // bundle: define path - if (count($path) > 1) { + if (count($source) > 1) { $this->data['path'] = $filename; if (empty($filename)) { switch ($this->data['ext']) { @@ -321,6 +331,9 @@ public function getIntegrity(string $algo = 'sha384'): string */ public function getWidth() { + if ($this->data['type'] != 'image') { + throw new Exception(\sprintf('Can\'t get width of a non image asset ("%s")', $this->data['path'])); + } if (false === $size = $this->getImageSize()) { return false; } @@ -335,6 +348,9 @@ public function getWidth() */ public function getHeight() { + if ($this->data['type'] != 'image') { + throw new Exception(\sprintf('Can\'t get height of a non image asset ("%s")', $this->data['path'])); + } if (false === $size = $this->getImageSize()) { return false; } @@ -351,6 +367,10 @@ public function getHeight() */ public function getAudio(): Mp3Info { + if ($this->data['subtype'] != 'audio/mpeg') { + throw new Exception(\sprintf('Can\'t get audio infos of a non MP3 asset ("%s")', $this->data['path'])); + } + return new Mp3Info($this->data['file']); } diff --git a/src/Util/File.php b/src/Util/File.php index 4668bd530..c6b32b2ce 100644 --- a/src/Util/File.php +++ b/src/Util/File.php @@ -77,4 +77,24 @@ public static function getMimeType(string $filename): array $subtype, ]; } + + /** + * Determines if data is file path. + * + * @param mixed $data + * + * @return bool + */ + public static function isFilePath($data): bool + { + if (is_string($data)) { + try { + return is_file($data); + } catch (\Exception $e) { + return false; + } + } + + return false; + } } diff --git a/src/Util/Str.php b/src/Util/Str.php index 586640745..e3b14a7af 100644 --- a/src/Util/Str.php +++ b/src/Util/Str.php @@ -57,4 +57,22 @@ public static function combineArrayToString( return substr($string, 0, -2); } + + /** + * Determines if data is binary. + * + * @param mixed $data + * + * @return bool + */ + public static function isBinary($data): bool + { + if (is_string($data)) { + $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data); + + return $mime != 'application/x-empty'; + } + + return false; + } } diff --git a/src/Util/Url.php b/src/Util/Url.php index 9fd37597a..42fecdf1d 100644 --- a/src/Util/Url.php +++ b/src/Util/Url.php @@ -13,15 +13,16 @@ class Url { /** - * Tests if a string is an URL. + * Tests if data is a valid URL. * - * @param string $url + * @param mixed $data * * @return bool */ - public static function isUrl(string $url): bool + public static function isUrl($data): bool { - return (bool) preg_match('~^(?:f|ht)tps?://~i', $url); + //return (bool) preg_match('~^(?:f|ht)tps?://~i', $data); + return (bool) filter_var($data, FILTER_VALIDATE_URL); } /**