Commit 8b70a55b by Carsten Brandt

Improved global configuration of assetmanager

Added `beforeCopy`, `afterCopy`, `forceCopy` properties to AssetManager fixes #4209
parent ce609218
...@@ -135,6 +135,7 @@ Yii Framework 2 Change Log ...@@ -135,6 +135,7 @@ Yii Framework 2 Change Log
- Enh #4080: Added proper handling and support of the symlinked directories in `FileHelper`, added $options parameter in `FileHelper::removeDirectory()` (resurtm) - Enh #4080: Added proper handling and support of the symlinked directories in `FileHelper`, added $options parameter in `FileHelper::removeDirectory()` (resurtm)
- Enh #4086: changedAttributes of afterSave Event now contain old values (dizews) - Enh #4086: changedAttributes of afterSave Event now contain old values (dizews)
- Enh #4114: Added `Security::generateRandomBytes()`, improved tests (samdark) - Enh #4114: Added `Security::generateRandomBytes()`, improved tests (samdark)
- Enh #4209: Added `beforeCopy`, `afterCopy`, `forceCopy` properties to AssetManager (cebe)
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
- Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue) - Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
- Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue) - Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue)
......
...@@ -110,6 +110,7 @@ class AssetBundle extends Object ...@@ -110,6 +110,7 @@ class AssetBundle extends Object
*/ */
public $publishOptions = []; public $publishOptions = [];
/** /**
* @param View $view * @param View $view
* @return static the registered asset bundle instance * @return static the registered asset bundle instance
......
...@@ -94,6 +94,33 @@ class AssetManager extends Component ...@@ -94,6 +94,33 @@ class AssetManager extends Component
* but read-only for other users. * but read-only for other users.
*/ */
public $dirMode = 0775; public $dirMode = 0775;
/**
* @var callback a PHP callback that is called before copying each sub-directory or file.
* This option is used only when publishing a directory. If the callback returns false, the copy
* operation for the sub-directory or file will be cancelled.
*
* The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or
* file to be copied from, while `$to` is the copy target.
*
* This is passed as a parameter `beforeCopy` to [[\yii\helpers\FileHelper::copyDirectory()]].
*/
public $beforeCopy;
/**
* @var callback a PHP callback that is called after a sub-directory or file is successfully copied.
* This option is used only when publishing a directory. The signature of the callback is the same as
* for [[beforeCopy]].
* This is passed as a parameter `afterCopy` to [[\yii\helpers\FileHelper::copyDirectory()]].
*/
public $afterCopy;
/**
* @var boolean whether the directory being published should be copied even if
* it is found in the target directory. This option is used only when publishing a directory.
* You may want to set this to be `true` during the development stage to make sure the published
* directory is always up-to-date. Do not set this to true on production servers as it will
* significantly degrade the performance.
*/
public $forceCopy = false;
/** /**
* Initializes the component. * Initializes the component.
...@@ -211,18 +238,13 @@ class AssetManager extends Component ...@@ -211,18 +238,13 @@ class AssetManager extends Component
* The following options are supported: * The following options are supported:
* *
* - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. * - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file.
* This option is used only when publishing a directory. If the callback returns false, the copy * This overrides [[beforeCopy]] if set.
* operation for the sub-directory or file will be cancelled.
* The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or
* file to be copied from, while `$to` is the copy target.
* - afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied. * - afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied.
* This option is used only when publishing a directory. The signature of the callback is similar to that * This overrides [[afterCopy]] if set.
* of `beforeCopy`.
* - forceCopy: boolean, whether the directory being published should be copied even if * - forceCopy: boolean, whether the directory being published should be copied even if
* it is found in the target directory. This option is used only when publishing a directory. * it is found in the target directory. This option is used only when publishing a directory.
* You may want to set this to be true during the development stage to make sure the published * This overrides [[forceCopy]] if set.
* directory is always up-to-date. Do not set this to true on production servers as it will *
* significantly degrade the performance.
* @return array the path (directory or file path) and the URL that the asset is published as. * @return array the path (directory or file path) and the URL that the asset is published as.
* @throws InvalidParamException if the asset to be published does not exist. * @throws InvalidParamException if the asset to be published does not exist.
*/ */
...@@ -267,13 +289,15 @@ class AssetManager extends Component ...@@ -267,13 +289,15 @@ class AssetManager extends Component
if (!is_dir($dstDir)) { if (!is_dir($dstDir)) {
symlink($src, $dstDir); symlink($src, $dstDir);
} }
} elseif (!is_dir($dstDir) || !empty($options['forceCopy'])) { } elseif (!is_dir($dstDir) || !empty($options['forceCopy']) || (!isset($options['forceCopy']) && $this->forceCopy)) {
$opts = [ $opts = [
'dirMode' => $this->dirMode, 'dirMode' => $this->dirMode,
'fileMode' => $this->fileMode, 'fileMode' => $this->fileMode,
]; ];
if (isset($options['beforeCopy'])) { if (isset($options['beforeCopy'])) {
$opts['beforeCopy'] = $options['beforeCopy']; $opts['beforeCopy'] = $options['beforeCopy'];
} elseif ($this->beforeCopy !== null) {
$opts['beforeCopy'] = $this->beforeCopy;
} else { } else {
$opts['beforeCopy'] = function ($from, $to) { $opts['beforeCopy'] = function ($from, $to) {
return strncmp(basename($from), '.', 1) !== 0; return strncmp(basename($from), '.', 1) !== 0;
...@@ -281,6 +305,8 @@ class AssetManager extends Component ...@@ -281,6 +305,8 @@ class AssetManager extends Component
} }
if (isset($options['afterCopy'])) { if (isset($options['afterCopy'])) {
$opts['afterCopy'] = $options['afterCopy']; $opts['afterCopy'] = $options['afterCopy'];
} elseif ($this->afterCopy !== null) {
$opts['afterCopy'] = $this->afterCopy;
} }
FileHelper::copyDirectory($src, $dstDir, $opts); FileHelper::copyDirectory($src, $dstDir, $opts);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment