Browse Source

Fix #18160, fix #18192: Fixed `registerFile` with argument depends set does not use the position and appendTimestamp argument, also modify the unit view

tags/2.0.39
杨晶旭 4 years ago committed by GitHub
parent
commit
7ff516063d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/CHANGELOG.md
  2. 8
      framework/web/AssetBundle.php
  3. 55
      framework/web/AssetManager.php
  4. 9
      framework/web/View.php
  5. 20
      tests/framework/web/ViewTest.php

1
framework/CHANGELOG.md

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.39 under development 2.0.39 under development
------------------------ ------------------------
- Bug #18160, #18192: Fixed `registerFile` with argument depends set does not use the position and appendTimestamp argument, also modify the unit view (baleeny)
- Bug #18290: Fix response with non-seekable streams (schmunk42) - Bug #18290: Fix response with non-seekable streams (schmunk42)
- Bug #16418: Fixed `yii\data\Pagination::getLinks()` to return links to the first and the last pages regardless of the current page (ptz-nerf, bizley) - Bug #16418: Fixed `yii\data\Pagination::getLinks()` to return links to the first and the last pages regardless of the current page (ptz-nerf, bizley)
- Bug #18297: Replace usage of deprecated `ReflectionParameter::isArray()` method in PHP8 (baletskyi) - Bug #18297: Replace usage of deprecated `ReflectionParameter::isArray()` method in PHP8 (baletskyi)

8
framework/web/AssetBundle.php

@ -153,10 +153,10 @@ class AssetBundle extends BaseObject
if (is_array($js)) { if (is_array($js)) {
$file = array_shift($js); $file = array_shift($js);
$options = ArrayHelper::merge($this->jsOptions, $js); $options = ArrayHelper::merge($this->jsOptions, $js);
$view->registerJsFile($manager->getAssetUrl($this, $file), $options); $view->registerJsFile($manager->getActualAssetUrl($this, $file), $options);
} else { } else {
if ($js !== null) { if ($js !== null) {
$view->registerJsFile($manager->getAssetUrl($this, $js), $this->jsOptions); $view->registerJsFile($manager->getActualAssetUrl($this, $js), $this->jsOptions);
} }
} }
} }
@ -164,10 +164,10 @@ class AssetBundle extends BaseObject
if (is_array($css)) { if (is_array($css)) {
$file = array_shift($css); $file = array_shift($css);
$options = ArrayHelper::merge($this->cssOptions, $css); $options = ArrayHelper::merge($this->cssOptions, $css);
$view->registerCssFile($manager->getAssetUrl($this, $file), $options); $view->registerCssFile($manager->getActualAssetUrl($this, $file), $options);
} else { } else {
if ($css !== null) { if ($css !== null) {
$view->registerCssFile($manager->getAssetUrl($this, $css), $this->cssOptions); $view->registerCssFile($manager->getActualAssetUrl($this, $css), $this->cssOptions);
} }
} }
} }

55
framework/web/AssetManager.php

@ -298,30 +298,14 @@ class AssetManager extends Component
*/ */
public function getAssetUrl($bundle, $asset) public function getAssetUrl($bundle, $asset)
{ {
if (($actualAsset = $this->resolveAsset($bundle, $asset)) !== false) { $assetUrl = $this->getActualAssetUrl($bundle, $asset);
if (strncmp($actualAsset, '@web/', 5) === 0) { $assetPath = $this->getAssetPath($bundle, $asset);
$asset = substr($actualAsset, 5);
$basePath = Yii::getAlias('@webroot');
$baseUrl = Yii::getAlias('@web');
} else {
$asset = Yii::getAlias($actualAsset);
$basePath = $this->basePath;
$baseUrl = $this->baseUrl;
}
} else {
$basePath = $bundle->basePath;
$baseUrl = $bundle->baseUrl;
}
if (!Url::isRelative($asset) || strncmp($asset, '/', 1) === 0) { if ($this->appendTimestamp && $assetPath && ($timestamp = @filemtime($assetPath)) > 0) {
return $asset; return "$assetUrl?v=$timestamp";
} }
if ($this->appendTimestamp && ($timestamp = @filemtime("$basePath/$asset")) > 0) { return $assetUrl;
return "$baseUrl/$asset?v=$timestamp";
}
return "$baseUrl/$asset";
} }
/** /**
@ -625,4 +609,33 @@ class AssetManager extends Component
$path = (is_file($path) ? dirname($path) : $path) . filemtime($path); $path = (is_file($path) ? dirname($path) : $path) . filemtime($path);
return sprintf('%x', crc32($path . Yii::getVersion() . '|' . $this->linkAssets)); return sprintf('%x', crc32($path . Yii::getVersion() . '|' . $this->linkAssets));
} }
/**
* Returns the actual URL for the specified asset. Without parameters.
* The actual URL is obtained by prepending either [[AssetBundle::$baseUrl]] or [[AssetManager::$baseUrl]] to the given asset path.
* @param AssetBundle $bundle the asset bundle which the asset file belongs to
* @param string $asset the asset path. This should be one of the assets listed in [[AssetBundle::$js]] or [[AssetBundle::$css]].
* @return string the actual URL for the specified asset.
* @since 2.0.39
*/
public function getActualAssetUrl($bundle, $asset)
{
if (($actualAsset = $this->resolveAsset($bundle, $asset)) !== false) {
if (strncmp($actualAsset, '@web/', 5) === 0) {
$asset = substr($actualAsset, 5);
$baseUrl = Yii::getAlias('@web');
} else {
$asset = Yii::getAlias($actualAsset);
$baseUrl = $this->baseUrl;
}
} else {
$baseUrl = $bundle->baseUrl;
}
if (!Url::isRelative($asset) || strncmp($asset, '/', 1) === 0) {
return $asset;
}
return "$baseUrl/$asset";
}
} }

9
framework/web/View.php

@ -479,15 +479,16 @@ class View extends \yii\base\View
$url = Yii::getAlias($url); $url = Yii::getAlias($url);
$key = $key ?: $url; $key = $key ?: $url;
$depends = ArrayHelper::remove($options, 'depends', []); $depends = ArrayHelper::remove($options, 'depends', []);
$originalOptions = $options;
$position = ArrayHelper::remove($options, 'position', self::POS_END); $position = ArrayHelper::remove($options, 'position', self::POS_END);
try { try {
$asssetManagerAppendTimestamp = $this->getAssetManager()->appendTimestamp; $assetManagerAppendTimestamp = $this->getAssetManager()->appendTimestamp;
} catch (InvalidConfigException $e) { } catch (InvalidConfigException $e) {
$depends = null; // the AssetManager is not available $depends = null; // the AssetManager is not available
$asssetManagerAppendTimestamp = false; $assetManagerAppendTimestamp = false;
} }
$appendTimestamp = ArrayHelper::remove($options, 'appendTimestamp', $asssetManagerAppendTimestamp); $appendTimestamp = ArrayHelper::remove($options, 'appendTimestamp', $assetManagerAppendTimestamp);
if (empty($depends)) { if (empty($depends)) {
// register directly without AssetManager // register directly without AssetManager
@ -504,7 +505,7 @@ class View extends \yii\base\View
'class' => AssetBundle::className(), 'class' => AssetBundle::className(),
'baseUrl' => '', 'baseUrl' => '',
'basePath' => '@webroot', 'basePath' => '@webroot',
(string)$type => [!Url::isRelative($url) ? $url : ltrim($url, '/')], (string)$type => [ArrayHelper::merge([!Url::isRelative($url) ? $url : ltrim($url, '/')], $originalOptions)],
"{$type}Options" => $options, "{$type}Options" => $options,
'depends' => (array)$depends, 'depends' => (array)$depends,
]); ]);

20
tests/framework/web/ViewTest.php

@ -211,7 +211,7 @@ class ViewTest extends TestCase
$this->assertRegExp($pattern, $html); $this->assertRegExp($pattern, $html);
// with alias but wo timestamp // with alias but wo timestamp
// The timestamp setting won't be redefined because global AssetManager is used // redefine AssetManager timestamp setting
$view = new View(); $view = new View();
$view->registerJsFile('@web/assetSources/js/jquery.js', $view->registerJsFile('@web/assetSources/js/jquery.js',
[ [
@ -219,7 +219,7 @@ class ViewTest extends TestCase
'depends' => 'yii\web\AssetBundle', 'depends' => 'yii\web\AssetBundle',
]); // <script src="/assetSources/js/jquery.js"></script> ]); // <script src="/assetSources/js/jquery.js"></script>
$html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']);
$this->assertRegExp($pattern, $html); $this->assertNotRegExp($pattern, $html);
// wo depends == wo AssetManager // wo depends == wo AssetManager
$view = new View(); $view = new View();
@ -268,15 +268,15 @@ class ViewTest extends TestCase
$html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']);
$this->assertRegExp($pattern, $html); $this->assertRegExp($pattern, $html);
// The timestamp setting won't be redefined because global AssetManager is used // redefine AssetManager timestamp setting
$view = new View(); $view = new View();
$view->registerJsFile('/assetSources/js/jquery.js', $view->registerJsFile('/assetSources/js/jquery.js',
[ [
'appendTimestamp' => true, 'appendTimestamp' => true,
'depends' => 'yii\web\AssetBundle', 'depends' => 'yii\web\AssetBundle',
]); // <script src="/assetSources/js/jquery.js"></script> ]); // <script src="/assetSources/js/jquery.js?v=1602294572"></script>
$html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']);
$this->assertNotRegExp($pattern, $html); $this->assertRegExp($pattern, $html);
$view = new View(); $view = new View();
$view->registerJsFile('/assetSources/js/jquery.js', $view->registerJsFile('/assetSources/js/jquery.js',
@ -357,7 +357,7 @@ class ViewTest extends TestCase
$this->assertRegExp($pattern, $html); $this->assertRegExp($pattern, $html);
// with alias but wo timestamp // with alias but wo timestamp
// The timestamp setting won't be redefined because global AssetManager is used // redefine AssetManager timestamp setting
$view = new View(); $view = new View();
$view->registerCssFile('@web/assetSources/css/stub.css', $view->registerCssFile('@web/assetSources/css/stub.css',
[ [
@ -365,7 +365,7 @@ class ViewTest extends TestCase
'depends' => 'yii\web\AssetBundle', 'depends' => 'yii\web\AssetBundle',
]); // <link href="/assetSources/css/stub.css" rel="stylesheet" > ]); // <link href="/assetSources/css/stub.css" rel="stylesheet" >
$html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']);
$this->assertRegExp($pattern, $html); $this->assertNotRegExp($pattern, $html);
// wo depends == wo AssetManager // wo depends == wo AssetManager
$view = new View(); $view = new View();
@ -414,15 +414,15 @@ class ViewTest extends TestCase
$html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']);
$this->assertRegExp($pattern, $html); $this->assertRegExp($pattern, $html);
// The timestamp setting won't be redefined because global AssetManager is used // redefine AssetManager timestamp setting
$view = new View(); $view = new View();
$view->registerCssFile('/assetSources/css/stub.css', $view->registerCssFile('/assetSources/css/stub.css',
[ [
'appendTimestamp' => true, 'appendTimestamp' => true,
'depends' => 'yii\web\AssetBundle', 'depends' => 'yii\web\AssetBundle',
]); // <link href="/assetSources/css/stub.css" rel="stylesheet" > ]); // <link href="/assetSources/css/stub.css?v=1602294572" rel="stylesheet" >
$html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']);
$this->assertNotRegExp($pattern, $html); $this->assertRegExp($pattern, $html);
$view = new View(); $view = new View();
$view->registerCssFile('/assetSources/css/stub.css', $view->registerCssFile('/assetSources/css/stub.css',

Loading…
Cancel
Save