/** * Publishes a file or a directory. * This method will copy the specified asset to a web accessible directory * and return the URL for accessing the published asset. * <ul> * <li>If the asset is a file, its file modification time will be checked * to avoid unnecessary file copying;</li> * <li>If the asset is a directory, all files and subdirectories under it will * be published recursively. Note, the method only checks the * existence of the target directory to avoid repetitive copying.</li> * </ul> * * @param string $path the asset (file or directory) to be published * @param boolean $hashByName whether the published directory should be named as the hashed basename. * If false, the name will be the hash taken from dirname of the path being published and path mtime. * Defaults to false. Set true if the path being published is shared among * different extensions. * @return string an absolute URL to the published asset * @throws Exception if the asset to be published does not exist. */ public function publish($path, $hashByName = false) { if (isset($this->_published[$path])) { return $this->_published[$path]; } else { if (($src = realpath($path)) !== false) { if (is_file($src)) { $dir = $this->hash($hashByName ? basename($src) : dirname($src) . filemtime($src)); $fileName = basename($src); $dstDir = $this->getBasePath() . DIRECTORY_SEPARATOR . $dir; $dstFile = $dstDir . DIRECTORY_SEPARATOR . $fileName; if ($this->linkAssets) { if (!is_file($dstFile)) { if (!is_dir($dstDir)) { mkdir($dstDir); @chmod($dstDir, $this->newDirMode); } symlink($src, $dstFile); } } else { if (@filemtime($dstFile) < @filemtime($src)) { if (!is_dir($dstDir)) { mkdir($dstDir); @chmod($dstDir, $this->newDirMode); } copy($src, $dstFile); @chmod($dstFile, $this->newFileMode); } } return $this->_published[$path] = $this->getBaseUrl() . "/{$dir}/{$fileName}"; } else { if (is_dir($src)) { $dir = $this->hash($hashByName ? basename($src) : $src . filemtime($src)); $dstDir = $this->getBasePath() . DIRECTORY_SEPARATOR . $dir; if ($this->linkAssets) { if (!is_dir($dstDir)) { symlink($src, $dstDir); } } else { if (!is_dir($dstDir)) { $dstF = new \GO\Base\Fs\Folder($dstDir); $folder = new \GO\Base\Fs\Folder($src); $folder->copy($dstF); } } return $this->_published[$path] = $this->getBaseUrl() . '/' . $dir; } } } } throw new \Exception('The asset "' . $path . '" to be published does not exist.'); }