first commit
This commit is contained in:
84
classes/assets/AbstractAssetManager.php
Normal file
84
classes/assets/AbstractAssetManager.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
|
||||
|
||||
abstract class AbstractAssetManagerCore
|
||||
{
|
||||
protected $directories;
|
||||
protected $configuration;
|
||||
protected $list = [];
|
||||
|
||||
const DEFAULT_MEDIA = 'all';
|
||||
const DEFAULT_PRIORITY = 50;
|
||||
const DEFAULT_JS_POSITION = 'bottom';
|
||||
|
||||
use PrestaShop\PrestaShop\Adapter\Assets\AssetUrlGeneratorTrait;
|
||||
|
||||
public function __construct(array $directories, ConfigurationInterface $configuration)
|
||||
{
|
||||
$this->directories = $directories;
|
||||
$this->configuration = $configuration;
|
||||
|
||||
$this->list = $this->getDefaultList();
|
||||
}
|
||||
|
||||
abstract protected function getDefaultList();
|
||||
|
||||
abstract protected function getList();
|
||||
|
||||
/**
|
||||
* @param string $relativePath
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
public function getFullPath(string $relativePath)
|
||||
{
|
||||
foreach ($this->getDirectories() as $baseDir) {
|
||||
$fullPath = $baseDir . ltrim($relativePath, '/'); // not DIRECTORY_SEPARATOR because, it's path included manually
|
||||
if (file_exists($this->getPathFromUri($fullPath))) {
|
||||
return $fullPath;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function getDirectories()
|
||||
{
|
||||
static $directories;
|
||||
|
||||
if (null === $directories) {
|
||||
foreach ($this->directories as $baseDir) {
|
||||
if (!empty($baseDir)) {
|
||||
$directories[] = $baseDir;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $directories;
|
||||
}
|
||||
}
|
||||
156
classes/assets/CccReducer.php
Normal file
156
classes/assets/CccReducer.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
class CccReducerCore
|
||||
{
|
||||
/** @var string */
|
||||
private $cacheDir;
|
||||
/** @var Filesystem */
|
||||
protected $filesystem;
|
||||
|
||||
use PrestaShop\PrestaShop\Adapter\Assets\AssetUrlGeneratorTrait;
|
||||
|
||||
/**
|
||||
* @param string $cacheDir
|
||||
* @param ConfigurationInterface $configuration
|
||||
* @param Filesystem $filesystem
|
||||
*/
|
||||
public function __construct($cacheDir, ConfigurationInterface $configuration, Filesystem $filesystem)
|
||||
{
|
||||
$this->cacheDir = $cacheDir;
|
||||
$this->configuration = $configuration;
|
||||
$this->filesystem = $filesystem;
|
||||
|
||||
if (!is_dir($this->cacheDir)) {
|
||||
$this->filesystem->mkdir($this->cacheDir);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $cssFileList
|
||||
*
|
||||
* @return array Same list, reduced
|
||||
*/
|
||||
public function reduceCss($cssFileList)
|
||||
{
|
||||
$files = [];
|
||||
foreach ($cssFileList['external'] as $key => &$css) {
|
||||
if ('all' === $css['media'] && 'local' === $css['server']) {
|
||||
$files[] = $this->getPathFromUri($css['path']);
|
||||
unset($cssFileList['external'][$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$version = Configuration::get('PS_CCCCSS_VERSION');
|
||||
$cccFilename = 'theme-' . $this->getFileNameIdentifierFromList($files) . $version . '.css';
|
||||
$destinationPath = $this->cacheDir . $cccFilename;
|
||||
|
||||
if (!$this->filesystem->exists($destinationPath)) {
|
||||
CssMinifier::minify($files, $destinationPath);
|
||||
}
|
||||
if (Tools::hasMediaServer()) {
|
||||
$relativePath = _THEMES_DIR_ . _THEME_NAME_ . '/assets/cache/' . $cccFilename;
|
||||
$destinationUri = Tools::getCurrentUrlProtocolPrefix() . Tools::getMediaServer($relativePath) . $relativePath;
|
||||
} else {
|
||||
$destinationUri = $this->getFQDN() . $this->getUriFromPath($destinationPath);
|
||||
}
|
||||
|
||||
$cssFileList['external']['theme-ccc'] = [
|
||||
'id' => 'theme-ccc',
|
||||
'type' => 'external',
|
||||
'path' => $destinationPath,
|
||||
'uri' => $destinationUri,
|
||||
'media' => 'all',
|
||||
'priority' => StylesheetManager::DEFAULT_PRIORITY,
|
||||
];
|
||||
|
||||
return $cssFileList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $jsFileList
|
||||
*
|
||||
* @return array Same list, reduced
|
||||
*/
|
||||
public function reduceJs($jsFileList)
|
||||
{
|
||||
foreach ($jsFileList as $position => &$list) {
|
||||
$files = [];
|
||||
foreach ($list['external'] as $key => $js) {
|
||||
// We only CCC the file without 'refer' or 'async'
|
||||
if ('' === $js['attribute'] && 'local' === $js['server']) {
|
||||
$files[] = $this->getPathFromUri($js['path']);
|
||||
unset($list['external'][$key]);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($files)) {
|
||||
// No file to CCC
|
||||
continue;
|
||||
}
|
||||
|
||||
$version = Configuration::get('PS_CCCJS_VERSION');
|
||||
$cccFilename = $position . '-' . $this->getFileNameIdentifierFromList($files) . $version . '.js';
|
||||
$destinationPath = $this->cacheDir . $cccFilename;
|
||||
|
||||
if (!$this->filesystem->exists($destinationPath)) {
|
||||
JsMinifier::minify($files, $destinationPath);
|
||||
}
|
||||
if (Tools::hasMediaServer()) {
|
||||
$relativePath = _THEMES_DIR_ . _THEME_NAME_ . '/assets/cache/' . $cccFilename;
|
||||
$destinationUri = Tools::getCurrentUrlProtocolPrefix() . Tools::getMediaServer($relativePath) . $relativePath;
|
||||
} else {
|
||||
$destinationUri = $this->getFQDN() . $this->getUriFromPath($destinationPath);
|
||||
}
|
||||
|
||||
$cccItem = [];
|
||||
$cccItem[$position . '-js-ccc'] = [
|
||||
'id' => $position . '-js-ccc',
|
||||
'type' => 'external',
|
||||
'path' => $destinationPath,
|
||||
'uri' => $destinationUri,
|
||||
'priority' => JavascriptManager::DEFAULT_PRIORITY,
|
||||
'attribute' => '',
|
||||
];
|
||||
$list['external'] = array_merge($cccItem, $list['external']);
|
||||
}
|
||||
|
||||
return $jsFileList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $files
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getFileNameIdentifierFromList(array $files)
|
||||
{
|
||||
return substr(sha1(implode('|', $files)), 0, 6);
|
||||
}
|
||||
}
|
||||
47
classes/assets/CssMinifier.php
Normal file
47
classes/assets/CssMinifier.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use MatthiasMullie\Minify\CSS;
|
||||
|
||||
class CssMinifierCore
|
||||
{
|
||||
/**
|
||||
* @param string[] $files
|
||||
* @param string $destination
|
||||
*
|
||||
* @return string Minified data
|
||||
*/
|
||||
public static function minify(array $files, $destination)
|
||||
{
|
||||
$minifier = new CSS();
|
||||
|
||||
foreach ($files as $file) {
|
||||
$minifier->add($file);
|
||||
}
|
||||
|
||||
return $minifier->minify($destination);
|
||||
}
|
||||
}
|
||||
169
classes/assets/JavascriptManager.php
Normal file
169
classes/assets/JavascriptManager.php
Normal file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
class JavascriptManagerCore extends AbstractAssetManager
|
||||
{
|
||||
protected $list;
|
||||
|
||||
protected $valid_position = ['head', 'bottom'];
|
||||
protected $valid_attribute = ['async', 'defer'];
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getDefaultList()
|
||||
{
|
||||
$default = [];
|
||||
foreach ($this->valid_position as $position) {
|
||||
$default[$position] = [
|
||||
'external' => [],
|
||||
'inline' => [],
|
||||
];
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param string $relativePath
|
||||
* @param string $position
|
||||
* @param int $priority
|
||||
* @param bool $inline
|
||||
* @param string $attribute
|
||||
* @param string $server
|
||||
*/
|
||||
public function register(
|
||||
$id,
|
||||
$relativePath,
|
||||
$position = self::DEFAULT_JS_POSITION,
|
||||
$priority = self::DEFAULT_PRIORITY,
|
||||
$inline = false,
|
||||
$attribute = null,
|
||||
$server = 'local'
|
||||
) {
|
||||
if ('remote' === $server) {
|
||||
$this->add($id, $relativePath, $position, $priority, $inline, $attribute, $server);
|
||||
} elseif ($fullPath = $this->getFullPath($relativePath)) {
|
||||
$this->add($id, $fullPath, $position, $priority, $inline, $attribute, $server);
|
||||
}
|
||||
}
|
||||
|
||||
public function unregisterById($idToRemove)
|
||||
{
|
||||
foreach ($this->valid_position as $position) {
|
||||
foreach ($this->list[$position] as $type => $null) {
|
||||
foreach ($this->list[$position][$type] as $id => $item) {
|
||||
if ($idToRemove === $id) {
|
||||
unset($this->list[$position][$type][$id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param string $fullPath
|
||||
* @param string $position
|
||||
* @param int $priority
|
||||
* @param bool $inline
|
||||
* @param string $attribute
|
||||
* @param string $server
|
||||
*/
|
||||
protected function add($id, $fullPath, $position, $priority, $inline, $attribute, $server)
|
||||
{
|
||||
$priority = is_int($priority) ? $priority : self::DEFAULT_PRIORITY;
|
||||
$position = $this->getSanitizedPosition($position);
|
||||
$attribute = $this->getSanitizedAttribute($attribute);
|
||||
|
||||
if ('remote' === $server) {
|
||||
$uri = $fullPath;
|
||||
$type = 'external';
|
||||
} else {
|
||||
$uri = $this->getFQDN() . $this->getUriFromPath($fullPath);
|
||||
$type = ($inline) ? 'inline' : 'external';
|
||||
}
|
||||
|
||||
$this->list[$position][$type][$id] = [
|
||||
'id' => $id,
|
||||
'type' => $type,
|
||||
'path' => $fullPath,
|
||||
'uri' => $uri,
|
||||
'priority' => $priority,
|
||||
'attribute' => $attribute,
|
||||
'server' => $server,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
$this->sortList();
|
||||
$this->addInlinedJavascriptContent();
|
||||
|
||||
return $this->list;
|
||||
}
|
||||
|
||||
private function addInlinedJavascriptContent()
|
||||
{
|
||||
foreach ($this->valid_position as $position) {
|
||||
foreach ($this->list[$position]['inline'] as &$item) {
|
||||
$item['content'] =
|
||||
'/* ---- ' . $item['id'] . ' @ ' . $item['path'] . ' ---- */' . "\r\n" .
|
||||
file_get_contents($item['path']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getSanitizedPosition($position)
|
||||
{
|
||||
return in_array($position, $this->valid_position, true) ? $position : self::DEFAULT_JS_POSITION;
|
||||
}
|
||||
|
||||
private function getSanitizedAttribute($attribute)
|
||||
{
|
||||
return in_array($attribute, $this->valid_attribute, true) ? $attribute : '';
|
||||
}
|
||||
|
||||
private function sortList()
|
||||
{
|
||||
foreach ($this->valid_position as $position) {
|
||||
foreach ($this->list[$position] as $type => $items) {
|
||||
Tools::uasort($items, function ($a, $b) {
|
||||
if ($a['priority'] === $b['priority']) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ($a['priority'] < $b['priority']) ? -1 : 1;
|
||||
});
|
||||
$this->list[$position][$type] = $items;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
47
classes/assets/JsMinifier.php
Normal file
47
classes/assets/JsMinifier.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use MatthiasMullie\Minify\JS;
|
||||
|
||||
class JsMinifierCore
|
||||
{
|
||||
/**
|
||||
* @param array $files
|
||||
* @param string $destination
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function minify(array $files, $destination)
|
||||
{
|
||||
$minifier = new JS();
|
||||
|
||||
foreach ($files as $file) {
|
||||
$minifier->add($file);
|
||||
}
|
||||
|
||||
return $minifier->minify($destination);
|
||||
}
|
||||
}
|
||||
61
classes/assets/PrestashopAssetsLibraries.php
Normal file
61
classes/assets/PrestashopAssetsLibraries.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
class PrestashopAssetsLibraries
|
||||
{
|
||||
const css = 'registerStylesheet';
|
||||
const js = 'registerJavascript';
|
||||
|
||||
/**
|
||||
* List of libraries available.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $assetsLibraries = [
|
||||
'font-awesome' => [
|
||||
// array of array because a library can have js & css
|
||||
[
|
||||
'id' => 'font-awesome-css',
|
||||
'type' => self::css,
|
||||
'path' => '/themes/_libraries/font-awesome/css/font-awesome.min.css',
|
||||
'params' => ['media' => 'all', 'priority' => 10],
|
||||
],
|
||||
],
|
||||
// can imagine others libraries
|
||||
];
|
||||
|
||||
/**
|
||||
* Get Library files from name.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public static function getAssetsLibraries($name)
|
||||
{
|
||||
return array_key_exists($name, self::$assetsLibraries) ? self::$assetsLibraries[$name] : false;
|
||||
}
|
||||
}
|
||||
163
classes/assets/StylesheetManager.php
Normal file
163
classes/assets/StylesheetManager.php
Normal file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
class StylesheetManagerCore extends AbstractAssetManager
|
||||
{
|
||||
private $valid_media = [
|
||||
'all',
|
||||
'braille',
|
||||
'embossed',
|
||||
'handheld',
|
||||
'print',
|
||||
'projection',
|
||||
'screen',
|
||||
'speech',
|
||||
'tty',
|
||||
'tv',
|
||||
];
|
||||
|
||||
protected function getDefaultList()
|
||||
{
|
||||
return [
|
||||
'external' => [],
|
||||
'inline' => [],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param string $relativePath
|
||||
* @param string $media
|
||||
* @param int $priority
|
||||
* @param bool $inline
|
||||
* @param string $server
|
||||
* @param bool $needRtl
|
||||
*/
|
||||
public function register(
|
||||
$id,
|
||||
$relativePath,
|
||||
$media = self::DEFAULT_MEDIA,
|
||||
$priority = self::DEFAULT_PRIORITY,
|
||||
$inline = false,
|
||||
$server = 'local',
|
||||
$needRtl = true
|
||||
) {
|
||||
$fullPath = $this->getFullPath($relativePath);
|
||||
$rtlFullPath = $this->getFullPath(str_replace('.css', '_rtl.css', $relativePath));
|
||||
$context = Context::getContext();
|
||||
$isRTL = is_object($context->language) && $context->language->is_rtl;
|
||||
if ('remote' === $server) {
|
||||
$this->add($id, $relativePath, $media, $priority, $inline, $server);
|
||||
} elseif ($needRtl && $isRTL && $rtlFullPath) {
|
||||
$this->add($id, $rtlFullPath, $media, $priority, $inline, $server);
|
||||
} elseif ($fullPath) {
|
||||
$this->add($id, $fullPath, $media, $priority, $inline, $server);
|
||||
}
|
||||
}
|
||||
|
||||
public function unregisterById($idToRemove)
|
||||
{
|
||||
foreach ($this->list as $type => $null) {
|
||||
foreach ($this->list[$type] as $id => $item) {
|
||||
if ($idToRemove === $id) {
|
||||
unset($this->list[$type][$id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
$this->sortList();
|
||||
$this->addInlinedStyleContent();
|
||||
|
||||
return $this->list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param string $fullPath
|
||||
* @param string $media
|
||||
* @param int $priority
|
||||
* @param bool $inline
|
||||
* @param string $server
|
||||
*/
|
||||
protected function add($id, $fullPath, $media, $priority, $inline, $server)
|
||||
{
|
||||
$priority = is_int($priority) ? $priority : self::DEFAULT_PRIORITY;
|
||||
$media = $this->getSanitizedMedia($media);
|
||||
|
||||
if ('remote' === $server) {
|
||||
$uri = $fullPath;
|
||||
$type = 'external';
|
||||
} else {
|
||||
$uri = $this->getFQDN() . $this->getUriFromPath($fullPath);
|
||||
$type = ($inline) ? 'inline' : 'external';
|
||||
}
|
||||
|
||||
$this->list[$type][$id] = [
|
||||
'id' => $id,
|
||||
'type' => $type,
|
||||
'path' => $fullPath,
|
||||
'uri' => $uri,
|
||||
'media' => $media,
|
||||
'priority' => $priority,
|
||||
'server' => $server,
|
||||
];
|
||||
}
|
||||
|
||||
private function getSanitizedMedia($media)
|
||||
{
|
||||
return in_array($media, $this->valid_media, true) ? $media : self::DEFAULT_MEDIA;
|
||||
}
|
||||
|
||||
private function sortList()
|
||||
{
|
||||
foreach ($this->list as &$items) {
|
||||
Tools::uasort(
|
||||
$items,
|
||||
function ($a, $b) {
|
||||
if ($a['priority'] === $b['priority']) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ($a['priority'] < $b['priority']) ? -1 : 1;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function addInlinedStyleContent()
|
||||
{
|
||||
foreach ($this->list['inline'] as &$item) {
|
||||
$item['content'] =
|
||||
'/* ---- ' . $item['id'] . ' @ ' . $item['path'] . ' ---- */' . "\r\n" .
|
||||
file_get_contents($item['path']);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user