/** * Create asset instance. * * @param string $alias * @param mixed $source * @param string|array $dependencies * @param string|array $options * @throws Exception * @return Asset */ public function create($alias, $source, $dependencies = [], $options = []) { $assetType = isset($options['type']) ? $options['type'] : ''; if (isset($this->_customTypes[$assetType])) { $assetType = $this->_customTypes[$assetType]; } elseif (is_callable($source)) { $assetType = 'Callback'; } elseif (is_string($source)) { $ext = strtolower(FS::ext($source)); if ($ext === 'js') { $assetType = 'JsFile'; } elseif ($ext === 'css') { $assetType = 'CssFile'; } elseif ($ext === 'less') { $assetType = 'LessFile'; } elseif ($ext === 'jsx') { $assetType = 'JsxFile'; } } elseif (is_array($source)) { $assetType = 'Collection'; } $className = __NAMESPACE__ . '\\Asset\\' . $assetType; if (class_exists($className)) { $options = is_array($options) ? new Data($options) : $options; return new $className($this->getManager(), $alias, $source, $dependencies, $options); } throw new Exception('Undefined asset type: ' . print_r($source, true)); }
/** * Generates URL for given asset file. * * @param array|string $path * @param array $options * @return array|null|string */ public function assetUrl($path, array $options = []) { if (is_array($path)) { return $this->build($path, !empty($options['fullBase'])); } if (empty(FS::ext($path))) { $path .= '.' . $options['ext']; } $Path = $this->_View->getPath(); $relative = $Path->isVirtual($path) ? $Path->get($path) : $Path->get('root:' . $path); if (strpos($path, '://') !== false) { return $path; } if ($relative !== null) { $path = $Path->url($this->_encodeUrl($this->assetTimestamp($relative)), false); if (!empty($options['fullBase'])) { $path = rtrim(Router::fullBaseUrl(), '/') . '/' . ltrim($path, '/'); } return $path; } return null; }
/** * Save image to file * * @param string $filename * @param int $quality * @return bool * * @throws Exception */ protected function _save($filename, $quality) { $quality = $quality ?: $this->_quality; $quality = Helper::quality($quality); $format = strtolower(FS::ext($filename)); if (!Helper::isSupportedFormat($format)) { $format = $this->_mime; } $filename = FS::clean($filename); // Create the image $result = $this->_renderImageByFormat($format, $filename, $quality); $this->loadFile($filename); $this->_quality = $quality; return $result; }
public function testExt() { isSame('png', FS::ext('image.png')); isSame('png', FS::ext('image.jpg.png')); isSame('png', FS::ext('/file/path/image.jpg.png')); isSame('', FS::ext('image')); isSame('', FS::ext('')); isSame('', FS::ext(null)); isSame('', FS::ext(false)); // URl isSame('txt', FS::ext('file.txt?some_var=123456')); isSame('txt', FS::ext('file.txt?some_var=123456?invalid=param')); isSame('php', FS::ext('http://demo.jbzoo.com/sites/phones/smartfony.php?logic=and&exact=0&controller=search&option=com_zoo&task=filter&type=phone&app_id=1&Itemid=101')); isSame('', FS::ext('http://demo.jbzoo.com/sites/phones/smartfony?logic=and&exact=0&controller=search&option=com_zoo&task=filter&type=phone&app_id=1&Itemid=101')); // to lower isSame('png', FS::ext('image.PNG')); isSame('png', FS::ext('image.PnG')); }