yii\web\AssetManager::publishDirectory PHP Метод

publishDirectory() защищенный Метод

Publishes a directory.
protected publishDirectory ( string $src, array $options ) : string[]
$src string the asset 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.
Результат string[] the path directory and the URL that the asset is published as.
    protected function publishDirectory($src, $options)
    {
        $dir = $this->hash($src);
        $dstDir = $this->basePath . DIRECTORY_SEPARATOR . $dir;
        if ($this->linkAssets) {
            if (!is_dir($dstDir)) {
                FileHelper::createDirectory(dirname($dstDir), $this->dirMode, true);
                symlink($src, $dstDir);
            }
        } elseif (!empty($options['forceCopy']) || $this->forceCopy && !isset($options['forceCopy']) || !is_dir($dstDir)) {
            $opts = array_merge($options, ['dirMode' => $this->dirMode, 'fileMode' => $this->fileMode]);
            if (!isset($opts['beforeCopy'])) {
                if ($this->beforeCopy !== null) {
                    $opts['beforeCopy'] = $this->beforeCopy;
                } else {
                    $opts['beforeCopy'] = function ($from, $to) {
                        return strncmp(basename($from), '.', 1) !== 0;
                    };
                }
            }
            if (!isset($opts['afterCopy']) && $this->afterCopy !== null) {
                $opts['afterCopy'] = $this->afterCopy;
            }
            FileHelper::copyDirectory($src, $dstDir, $opts);
        }
        return [$dstDir, $this->baseUrl . '/' . $dir];
    }

Usage Example

Пример #1
0
 /**
  * (non-PHPdoc)
  * EXTRA: 
  * @see \yii\web\AssetManager::publishDirectory()
  * 
  *  [const-dir] contain directory name or empty if copy current asset directly to base assets' dir 
  */
 public function publishDirectory($src, $options)
 {
     throw new \yii\base\Exception('works but block support until it will be required. skip testing purpose');
     // default behavior with hashed dir
     if (!isset($options['const-dir'])) {
         return parent::publishDirectory($src, $options);
     }
     //
     // my custom : don't generate random dir, instead, use custom if set
     //
     $dstDir = $this->basePath . (!empty($options['const-dir']) ? '/' . $options['const-dir'] : '');
     //dont copy if already was copied
     // TODO: add datetime checks
     if (file_exists($dstDir)) {
         return [$dstDir, $this->baseUrl];
     }
     // A. copy only subdirs if set
     if (!empty($options['sub-dirs']) && is_array($options['sub-dirs'])) {
         foreach ($options['sub-dirs'] as $subdir) {
             if (is_dir($src . '/' . $subdir)) {
                 FileHelper::copyDirectory($src . '/' . $subdir, $dstDir . '/' . $subdir, ['dirMode' => $this->dirMode, 'fileMode' => $this->fileMode, 'beforeCopy' => @$options['beforeCopy'], 'afterCopy' => @$options['afterCopy'], 'forceCopy' => @$options['forceCopy']]);
             }
             //TODO: else write error log
         }
     } else {
         //copy whole dir
         FileHelper::copyDirectory($src, $dstDir, ['dirMode' => $this->dirMode, 'fileMode' => $this->fileMode, 'beforeCopy' => @$options['beforeCopy'], 'afterCopy' => @$options['afterCopy'], 'forceCopy' => @$options['forceCopy']]);
     }
     return [$dstDir, $this->baseUrl];
 }