/** * Get the absolute path for a file * @param File $file * @return string */ public function getAbsolutePath(File $file) { $path = $file->getPath(); $phar = $file->isInPhar(); if ($phar) { $pharFile = new File(substr($path, strlen($phar->getPath()) + 7)); $path = $phar->getPath() . File::DIRECTORY_SEPARATOR . $pharFile->getPath(); } $hasPharProtocol = $file->hasPharProtocol($path); if ($hasPharProtocol) { $path = substr($path, 7); } if (!$this->isAbsolute($file)) { $path = str_replace('\\', File::DIRECTORY_SEPARATOR, getcwd()) . File::DIRECTORY_SEPARATOR . $path; } $drivePrefix = $this->getDrivePrefix($path); $path = substr($path, strlen($drivePrefix)); $parts = explode(File::DIRECTORY_SEPARATOR, $path); $absolutePath = array(); foreach ($parts as $part) { if ($part == '' || $part == '.') { continue; } if ($part == '..') { array_pop($absolutePath); continue; } array_push($absolutePath, $part); } $absolutePath = $drivePrefix . implode(File::DIRECTORY_SEPARATOR, $absolutePath); if ($phar || $hasPharProtocol) { $file = new File($absolutePath); $absolutePath = $file->getPath(); if ($hasPharProtocol && !$file->hasPharProtocol() && $file->isPhar()) { $absolutePath = 'phar://' . $absolutePath; } } return $absolutePath; }
/** * Construct a file object * @param string|File $path * @param string|File $child file in the provided path (optional) * @return null * @throws zibo\library\filesystem\exception\FileSystemException when the path is empty * @throws zibo\library\filesystem\exception\FileSystemException when the child is absolute */ public function __construct($path, $child = null) { $this->isRootPath = false; $this->path = self::retrievePath($path, $this->isRootPath); if ($child != null) { $child = new File($child); if ($child->isAbsolute()) { throw new FileSystemException('Child ' . $child->getPath() . ' cannot be absolute'); } $childPath = $child->getPath(); if ($child->hasPharProtocol()) { $childPath = substr($childPath, 7); } if (!$this->isRootPath) { $this->path .= self::DIRECTORY_SEPARATOR; } $this->path .= $childPath; } if ($this->isInPhar() && !$this->hasPharProtocol()) { $this->path = 'phar://' . $this->path; } }
/** * Get the relative file in the Zibo file structure for a given absolute file. * @param zibo\library\filesystem\File $file absolute file to get the relative file from * @return zibo\library\filesystem\File relative file in the Zibo file structure if located in the root of the Zibo installation * @throws zibo\ZiboException when the provided file is not in the root path * @throws zibo\ZiboException when the provided file is part of the Zibo file system structure */ public function getRelativeFile(File $file) { $absoluteFile = $file->getAbsolutePath(); $rootPath = $this->getRootPath(); $isPhar = $file->hasPharProtocol(); $file = str_replace($rootPath->getPath() . File::DIRECTORY_SEPARATOR, '', $absoluteFile); if ($file == $absoluteFile) { throw new ZiboException($file . ' is not in the root path'); } if ($isPhar) { $file = substr($file, 7); } $tokens = explode(File::DIRECTORY_SEPARATOR, $file); $token = array_shift($tokens); if ($token == self::DIRECTORY_APPLICATION || $token == self::DIRECTORY_SYSTEM) { $token = array_pop($tokens); return new File(implode(File::DIRECTORY_SEPARATOR, $tokens), $token); } if ($token !== self::DIRECTORY_MODULES || count($tokens) < 2) { throw new ZiboException($file . ' is not in the Zibo file system structure (' . $token . ')'); } array_shift($tokens); $token = array_pop($tokens); return new File(implode(File::DIRECTORY_SEPARATOR, $tokens), $token); }