Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset path -> data #1073

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
Expand All @@ -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
Expand Down Expand Up @@ -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']) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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']);
}

Expand Down
20 changes: 20 additions & 0 deletions src/Util/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
18 changes: 18 additions & 0 deletions src/Util/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
9 changes: 5 additions & 4 deletions src/Util/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down