This method will copy the specified file or directory to [[basePath]] so that
it can be accessed via the Web server.
If the asset is a file, its file modification time will be checked to avoid
unnecessary file copying.
If the asset is a directory, all files and subdirectories under it will be published recursively.
Note, in case $forceCopy is false the method only checks the existence of the target
directory to avoid repetitive copying (which is very expensive).
By default, when publishing a directory, subdirectories and files whose name starts with a dot "."
will NOT be published. If you want to change this behavior, you may specify the "beforeCopy" option
as explained in the $options parameter.
Note: On rare scenario, a race condition can develop that will lead to a
one-time-manifestation of a non-critical problem in the creation of the directory
that holds the published assets. This problem can be avoided altogether by 'requesting'
in advance all the resources that are supposed to trigger a 'publish()' call, and doing
that in the application deployment phase, before system goes live. See more in the following
discussion: http://code.google.com/p/yii/issues/detail?id=2579
public publish ( string $path, array $options = [] ) : array | ||
$path | string | the asset (file or directory) to be published |
$options | array | the options to be applied when publishing a directory. The following options are supported: - only: array, list of patterns that the file paths should match if they want to be copied. - except: array, list of patterns that the files or directories should match if they want to be excluded from being copied. - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true. - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. This overrides [[beforeCopy]] if set. - afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied. This overrides [[afterCopy]] if set. - 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. This overrides [[forceCopy]] if set. |
Результат | array | the path (directory or file path) and the URL that the asset is published as. |
/** * Publishes the asset bundle if its source code is not under Web-accessible directory. * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding * CSS or JS files using [[AssetManager::converter|asset converter]]. * @param \yii\web\AssetManager $am the asset manager to perform the asset publishing */ public function publish($am) { if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) { list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions); } $converter = $am->getConverter(); foreach ($this->js as $i => $value) { if (is_array($value)) { if (isset($value['file'])) { $js = $value['file']; } else { continue; } } else { $js = $value; } if (strpos($js, '/') !== 0 && strpos($js, '://') === false) { if (isset($this->basePath, $this->baseUrl)) { $js = $converter->convert($js, $this->basePath); } else { $js = '/' . $js; } if (is_array($value)) { $value['file'] = $js; } else { $value = $js; } $this->js[$i] = $value; } } foreach ($this->css as $i => $value) { if (is_array($value)) { if (isset($value['file'])) { $css = $value['file']; } else { continue; } } else { $css = $value; } if (strpos($css, '/') !== 0 && strpos($css, '://') === false) { if (isset($this->basePath, $this->baseUrl)) { $css = $converter->convert($css, $this->basePath); } else { $css = '/' . $css; } if (is_array($value)) { $value['file'] = $css; } else { $value = $css; } $this->css[$i] = $value; } } }
/** * Publishes the asset bundle if its source code is not under Web-accessible directory. * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding * CSS or JS files using [[AssetManager::converter|asset converter]]. * @param AssetManager $am the asset manager to perform the asset publishing */ public function publish($am) { if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) { list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions); } if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) { foreach ($this->js as $i => $js) { if (is_array($js)) { $file = array_shift($js); if (Url::isRelative($file)) { $js = ArrayHelper::merge($this->jsOptions, $js); array_unshift($js, $converter->convert($file, $this->basePath)); $this->js[$i] = $js; } } elseif (Url::isRelative($js)) { $this->js[$i] = $converter->convert($js, $this->basePath); } } foreach ($this->css as $i => $css) { if (is_array($css)) { $file = array_shift($css); if (Url::isRelative($file)) { $css = ArrayHelper::merge($this->cssOptions, $css); array_unshift($css, $converter->convert($file, $this->basePath)); $this->css[$i] = $css; } } elseif (Url::isRelative($css)) { $this->css[$i] = $converter->convert($css, $this->basePath); } } } }
/** * Publishes the asset bundle if its source code is not under Web-accessible directory. * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding * CSS or JS files using [[AssetManager::converter|asset converter]]. * @param AssetManager $am the asset manager to perform the asset publishing */ public function publish($am) { if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) { list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions); } if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) { foreach ($this->js as $i => $js) { if (Url::isRelative($js)) { $this->js[$i] = $converter->convert($js, $this->basePath); } } foreach ($this->css as $i => $css) { if (Url::isRelative($css)) { $this->css[$i] = $converter->convert($css, $this->basePath); } } } }
/** * Publishes the asset bundle if its source code is not under Web-accessible directory. * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding * CSS or JS files using [[AssetManager::converter|asset converter]]. * @param AssetManager $am the asset manager to perform the asset publishing */ public function publish($am) { if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) { list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions); } $converter = $am->getConverter(); foreach ($this->js as $i => $js) { if (strpos($js, '/') !== 0 && strpos($js, '://') === false) { if (isset($this->basePath, $this->baseUrl)) { $this->js[$i] = $converter->convert($js, $this->basePath); } else { $this->js[$i] = '/' . $js; } } } foreach ($this->css as $i => $css) { if (strpos($css, '/') !== 0 && strpos($css, '://') === false) { if (isset($this->basePath, $this->baseUrl)) { $this->css[$i] = $converter->convert($css, $this->basePath); } else { $this->css[$i] = '/' . $css; } } } }
public function registerAssetFiles($view) { parent::registerAssetFiles($view); $m = new AssetManager(); $m->publish('@webroot/downloads/images/'); }