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

Use Media Server for assets (CSS, JS, Images) #16180

Merged
merged 9 commits into from Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions classes/Tools.php
Expand Up @@ -2364,7 +2364,10 @@ public static function replaceByAbsoluteURL($matches)

protected static $_cache_nb_media_servers = null;

public static function getMediaServer($filename)
/**
* @return bool
*/
public static function hasMediaServer(): bool
{
if (self::$_cache_nb_media_servers === null && defined('_MEDIA_SERVER_1_') && defined('_MEDIA_SERVER_2_') && defined('_MEDIA_SERVER_3_')) {
if (_MEDIA_SERVER_1_ == '') {
Expand All @@ -2378,7 +2381,17 @@ public static function getMediaServer($filename)
}
}

if ($filename && self::$_cache_nb_media_servers && ($id_media_server = (abs(crc32($filename)) % self::$_cache_nb_media_servers + 1))) {
return self::$_cache_nb_media_servers > 0;
}

/**
* @param string $filename
*
* @return string
*/
public static function getMediaServer($filename)
Progi1984 marked this conversation as resolved.
Show resolved Hide resolved
{
if ($filename && self::hasMediaServer() && ($id_media_server = (abs(crc32($filename)) % self::$_cache_nb_media_servers + 1))) {
PierreRambaud marked this conversation as resolved.
Show resolved Hide resolved
PierreRambaud marked this conversation as resolved.
Show resolved Hide resolved
return constant('_MEDIA_SERVER_' . $id_media_server . '_');
}

Expand Down
9 changes: 7 additions & 2 deletions classes/assets/AbstractAssetManager.php
Expand Up @@ -50,10 +50,15 @@ abstract protected function getDefaultList();

abstract protected function getList();

protected function getFullPath($relativePath)
/**
* @param string $relativePath
*
* @return bool|string
*/
public function getFullPath($relativePath)
Progi1984 marked this conversation as resolved.
Show resolved Hide resolved
{
foreach ($this->getDirectories() as $baseDir) {
$fullPath = $baseDir . ltrim($relativePath, '/'); // not DIRECTORY_SEPARATOR because, it's path included manualy
$fullPath = $baseDir . ltrim($relativePath, '/'); // not DIRECTORY_SEPARATOR because, it's path included manually
if (file_exists($this->getPathFromUri($fullPath))) {
return $fullPath;
}
Expand Down
14 changes: 12 additions & 2 deletions classes/assets/CccReducer.php
Expand Up @@ -58,16 +58,21 @@ public function reduceCss($cssFileList)
$version = Configuration::get('PS_CCCCSS_VERSION');
$cccFilename = 'theme-' . $this->getFileNameIdentifierFromList($files) . $version . '.css';
$destinationPath = $this->cacheDir . $cccFilename;
$destinationUri = $this->getFQDN() . $this->getUriFromPath($destinationPath);

if (!$this->filesystem->exists($destinationPath)) {
CssMinifier::minify($files, $destinationPath);
}
if (Tools::hasMediaServer()) {
Progi1984 marked this conversation as resolved.
Show resolved Hide resolved
$relativePath = _THEMES_DIR_ . _THEME_NAME_ . '/assets/cache/' . $cccFilename;
$destinationPath = $destinationUri = Tools::getCurrentUrlProtocolPrefix() . Tools::getMediaServer($relativePath) . $relativePath;
Progi1984 marked this conversation as resolved.
Show resolved Hide resolved
}

$cssFileList['external']['theme-ccc'] = [
'id' => 'theme-ccc',
'type' => 'external',
'path' => $destinationPath,
'uri' => $this->getFQDN() . $this->getUriFromPath($destinationPath),
'uri' => $destinationUri,
'media' => 'all',
'priority' => StylesheetManager::DEFAULT_PRIORITY,
];
Expand Down Expand Up @@ -95,17 +100,22 @@ public function reduceJs($jsFileList)
$version = Configuration::get('PS_CCCJS_VERSION');
$cccFilename = $position . '-' . $this->getFileNameIdentifierFromList($files) . $version . '.js';
$destinationPath = $this->cacheDir . $cccFilename;
$destinationUri = $this->getFQDN() . $this->getUriFromPath($destinationPath);

if (!$this->filesystem->exists($destinationPath)) {
JsMinifier::minify($files, $destinationPath);
}
if (Tools::hasMediaServer()) {
Progi1984 marked this conversation as resolved.
Show resolved Hide resolved
$relativePath = _THEMES_DIR_ . _THEME_NAME_ . '/assets/cache/' . $cccFilename;
$destinationPath = $destinationUri = Tools::getCurrentUrlProtocolPrefix() . Tools::getMediaServer($relativePath) . $relativePath;
Progi1984 marked this conversation as resolved.
Show resolved Hide resolved
}

$cccItem = [];
$cccItem[$position . '-js-ccc'] = [
'id' => $position . '-js-ccc',
'type' => 'external',
'path' => $destinationPath,
'uri' => $this->getFQDN() . $this->getUriFromPath($destinationPath),
'uri' => $destinationUri,
'priority' => JavascriptManager::DEFAULT_PRIORITY,
'attribute' => '',
];
Expand Down
26 changes: 17 additions & 9 deletions classes/controller/FrontController.php
Expand Up @@ -1052,10 +1052,10 @@ public function removeMedia($media_uri, $css_media_type = null, $check_path = tr
*/
}

public function registerStylesheet($id, $relativePath, $params = array())
public function registerStylesheet($id, $relativePath, $params = [])
{
if (!is_array($params)) {
$params = array();
$params = [];
}

$default_params = [
Expand All @@ -1064,9 +1064,13 @@ public function registerStylesheet($id, $relativePath, $params = array())
'inline' => false,
'server' => 'local',
];

$params = array_merge($default_params, $params);

if (Tools::hasMediaServer() && !Configuration::get('PS_CSS_THEME_CACHE')) {
$relativePath = Tools::getCurrentUrlProtocolPrefix() . Tools::getMediaServer($relativePath)
Progi1984 marked this conversation as resolved.
Show resolved Hide resolved
. $this->stylesheetManager->getFullPath($relativePath) ?? $relativePath;
$params['server'] = 'remote';
}
$this->stylesheetManager->register($id, $relativePath, $params['media'], $params['priority'], $params['inline'], $params['server']);
}

Expand All @@ -1075,10 +1079,10 @@ public function unregisterStylesheet($id)
$this->stylesheetManager->unregisterById($id);
}

public function registerJavascript($id, $relativePath, $params = array())
public function registerJavascript($id, $relativePath, $params = [])
{
if (!is_array($params)) {
$params = array();
$params = [];
}

$default_params = [
Expand All @@ -1088,9 +1092,13 @@ public function registerJavascript($id, $relativePath, $params = array())
'attributes' => null,
'server' => 'local',
];

$params = array_merge($default_params, $params);

if (Tools::hasMediaServer() && !Configuration::get('PS_JS_THEME_CACHE')) {
$relativePath = Tools::getCurrentUrlProtocolPrefix() . Tools::getMediaServer($relativePath)
. $this->javascriptManager->getFullPath($relativePath) ?? $relativePath;
$params['server'] = 'remote';
}
Progi1984 marked this conversation as resolved.
Show resolved Hide resolved
$this->javascriptManager->register($id, $relativePath, $params['position'], $params['priority'], $params['inline'], $params['attributes'], $params['server']);
}

Expand Down Expand Up @@ -1605,9 +1613,9 @@ public function getTemplateVarShop()
'long' => Configuration::get('PS_STORES_CENTER_LONG'),
'lat' => Configuration::get('PS_STORES_CENTER_LAT'),

'logo' => (Configuration::get('PS_LOGO')) ? _PS_IMG_ . Configuration::get('PS_LOGO') : '',
'stores_icon' => (Configuration::get('PS_STORES_ICON')) ? _PS_IMG_ . Configuration::get('PS_STORES_ICON') : '',
'favicon' => (Configuration::get('PS_FAVICON')) ? _PS_IMG_ . Configuration::get('PS_FAVICON') : '',
'logo' => (Configuration::get('PS_LOGO')) ? Configuration::get('PS_LOGO') : '',
Progi1984 marked this conversation as resolved.
Show resolved Hide resolved
'stores_icon' => (Configuration::get('PS_STORES_ICON')) ? Configuration::get('PS_STORES_ICON') : '',
'favicon' => (Configuration::get('PS_FAVICON')) ? Configuration::get('PS_FAVICON') : '',
'favicon_update_time' => Configuration::get('PS_IMG_UPDATE_TIME'),

'address' => array(
Expand Down
4 changes: 2 additions & 2 deletions themes/classic/templates/_partials/head.tpl
Expand Up @@ -51,8 +51,8 @@
{/block}

{block name='head_icons'}
<link rel="icon" type="image/vnd.microsoft.icon" href="{$shop.favicon}?{$shop.favicon_update_time}">
<link rel="shortcut icon" type="image/x-icon" href="{$shop.favicon}?{$shop.favicon_update_time}">
<link rel="icon" type="image/vnd.microsoft.icon" href="{$urls.img_ps_url}{$shop.favicon}?{$shop.favicon_update_time}">
<link rel="shortcut icon" type="image/x-icon" href="{$urls.img_ps_url}{$shop.favicon}?{$shop.favicon_update_time}">
{/block}

{block name='stylesheets'}
Expand Down
4 changes: 2 additions & 2 deletions themes/classic/templates/_partials/header.tpl
Expand Up @@ -62,12 +62,12 @@
{if $page.page_name == 'index'}
<h1>
<a href="{$urls.base_url}">
<img class="logo img-responsive" src="{$shop.logo}" alt="{$shop.name}">
<img class="logo img-responsive" src="{$urls.img_ps_url}{$shop.logo}" alt="{$shop.name}">
</a>
</h1>
{else}
<a href="{$urls.base_url}">
<img class="logo img-responsive" src="{$shop.logo}" alt="{$shop.name}">
<img class="logo img-responsive" src="{$urls.img_ps_url}{$shop.logo}" alt="{$shop.name}">
</a>
{/if}
</div>
Expand Down
2 changes: 1 addition & 1 deletion themes/classic/templates/checkout/_partials/header.tpl
Expand Up @@ -29,7 +29,7 @@
<div class="row">
<div class="col-md-6 hidden-sm-down" id="_desktop_logo">
<a href="{$urls.base_url}">
<img class="logo img-responsive" src="{$shop.logo}" alt="{$shop.name} {l s='logo' d='Shop.Theme.Global'}">
<img class="logo img-responsive" src="{$urls.img_ps_url}{$shop.logo}" alt="{$shop.name} {l s='logo' d='Shop.Theme.Global'}">
</a>
</div>
<div class="col-md-6 text-xs-right hidden-sm-down">
Expand Down
2 changes: 1 addition & 1 deletion themes/classic/templates/errors/maintenance.tpl
Expand Up @@ -31,7 +31,7 @@
{block name='page_header_container'}
<header class="page-header">
{block name='page_header_logo'}
<div class="logo"><img src="{$shop.logo}" alt="logo"></div>
<div class="logo"><img src="{$urls.img_ps_url}{$shop.logo}" alt="logo"></div>
{/block}

{block name='hook_maintenance'}
Expand Down
2 changes: 1 addition & 1 deletion themes/classic/templates/errors/restricted-country.tpl
Expand Up @@ -30,7 +30,7 @@

{block name='page_header_container'}
<header class="page-header">
<div class="logo"><img src="{$shop.logo}" alt="logo"></div>
<div class="logo"><img src="{$urls.img_ps_url}{$shop.logo}" alt="logo"></div>
{block name='page_header'}
<h1>{block name='page_title'}{$shop.name}{/block}</h1>
{/block}
Expand Down