phplibrary/src/path.php
2017-02-09 14:57:40 +03:00

317 lines
8.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*.
require_module 'standard';
.*/
/**
* Класс для работы с папками и путями
* Для итерации над файлами возможно лучше использовать SPL
*
* @package utils
*/
class Path
{
const SEPARATOR = "/";
protected $path;
public function __construct ($path)
{
assert(is_string($path));
$this->path = $this->fromString($path);
$this->optimize();
}
static function factory($path) {
return new Path($path);
}
/**
* Возвращает расширение файла
*
* @param string $fileName Полное имя файла
*
* @return string
*/
static function getExtension ($fileName)
{
assert(is_string($fileName));
return pathinfo($fileName, PATHINFO_EXTENSION);
}
/**
* Полное имя файла без расширения
*
* @param string $fileName Имя файла
*
* @return string
*/
static function skipExtension ($fileName)
{
assert(is_string($fileName));
$path = pathinfo($fileName);
if ($path['dirname'] == ".") {
return $path['filename'];
} else {
return self::join($path['dirname'], $path['filename']);
}
}
/**
* Возвращает имя файла без расширения
*
* @param string $fileName Полное имя файла
*
* @return string
*/
static function getFileName ($fileName)
{
assert(is_string($fileName));
return pathinfo($fileName, PATHINFO_FILENAME);
}
/**
* Список файлов в директории
*
* @param array $allow массив расширений для файлов
* @param array $ignore массив имен пааок которые не нужно обрабатывать
*
* @return array
*/
public function getContent ($allow = null, $ignore = array())
{
$ignore = array_merge(array (".", "..", $ignore));
return self::fileList($this->__toString(), $allow, $ignore);
}
// Использовать SPL ???
protected function fileList ($base, &$allow, &$ignore)
{
$result = array ();
if ($handle = opendir($base)) {
while (false !== ($file = readdir($handle))) {
if (! in_array ($file, $ignore)) {
$isDir = is_dir (Path::join ($base, $file));
if ($isDir || ($allow == null) || in_array (self::getExtension($file), $allow)) {
$result[] = $file;
}
}
}
closedir($handle);
}
return $result;
}
protected function fileListAll (&$result, $base, &$allow, &$ignore)
{
$files = self::fileList($base, $allow, $ignore);
foreach ($files as $name) {
$fullname = self::join($base, $name);
if (is_dir($fullname)) {
self::fileListAll($result, $fullname, $allow, $ignore);
} else {
array_push ($result, $fullname);
}
}
}
/**
* Список файлов в директориии и ее поддиректорий
*
* @param array $allow массив расширений разрешеных для файлов
* @param array $ignore массив имен пааок которые не нужно обрабатывать
*
* @return array
*/
function getContentRec ($allow = null, $ignore = array())
{
$result = array ();
$ignore = array_merge(array (".", ".."), $ignore);
self::fileListAll($result, $this->__toString(), $allow, $ignore);
return $result;
}
/**
* Рекурсивно копирует директорию
*
* @param string $source Папка из которой копируется
* @param string $target Папка в которую копируется
*/
public static function copy ($source, $target)
{
if (is_dir($source)) {
if (! file_exists($target)) mkdir ($target);
$path = new Path($source);
$files = $path->getContent();
foreach ($files as $file) {
$entry = self::join($source, $file);
if (is_dir($entry)) {
self::copy($entry, Path::join($target, $file));
} else {
copy($entry, Path::join($target, $file));
}
}
}
}
/**
* Рекурсивно удаляет директорию
*
* @param string $path Папка
*/
public static function delete ($path)
{
assert(is_string($path));
if (is_dir($path)) {
foreach(glob($path . '/*') as $sf) {
if (is_dir($sf) && !is_link($sf)) {
self::delete($sf);
} else {
unlink($sf);
}
}
rmdir($path);
}
}
/**
* Преобразует строку путя в массив
*
* @param string $path Путь
*
* @return array
*/
public function fromString ($path)
{
assert(is_string($path));
$list = preg_split("/[\/\\\\]/", $path);
return $list;
}
/**
* Преобразует относительный путь в абсолютный
*/
public function optimize ()
{
$result = array(current($this->path));
while (next($this->path) !== false) {
$n = current ($this->path);
switch ($n) {
case "": break;
case ".": break;
case "..": if (count($result) > 0) array_pop($result); break;
default:
array_push($result, $n);
}
}
reset($this->path);
$this->path = $result;
}
/**
* Преобразует путь в строку
*
* @return string
*/
public function __toString ()
{
$result = implode($this->path, self::SEPARATOR);
return $result;
}
/**
* Проверяет является ли папка родительской для другой папки
*
* @parma Path $path
*
* @return boolean
*/
public function isParent ($path)
{
if (count($path->path) > count($this->path)) {
for ($i = 0; $i < count($this->path); $i++) {
if ($path->path[$i] != $this->path[$i]) {
return false;
}
}
return true;
}
return false;
}
/**
* Находит путь относительно текущего путя
*
* @param string $name Полный путь к файлу
*
* @return string Относительный путь к файлу
*/
public function relPath ($name)
{
$path = new Path ($name);
foreach ($this->path as $n) {
array_shift($path->path);
}
return $path->__toString();
}
public function append ($path)
{
$base = $this->__toString();
return self::join($base, $path);
}
/**
* Создает недастающие папки для записи файла
*
* @param string $dst Полное имя файла
*
* @return void
*/
static function prepare ($dst)
{
$path_dst = pathinfo($dst, PATHINFO_DIRNAME);
if (! file_exists($path_dst)) {
mkdir($path_dst, 0700, true);
}
}
/**
* Подбирает новое временное имя для файла
*
* @param string $dst Предпологаемое имя файла
*
* @return string Новое имя файла
*/
static function resolveFile ($dst)
{
$i = 0;
$file = self::skipExtension($dst);
$suffix = self::getExtension($dst);
$temp = $dst;
while (file_exists($temp)) {
$i ++;
$temp = $file . "." . $i . "." . $suffix;
}
return $temp;
}
/**
* Обьединяет строки в путь соединяя необходимым разделителем
*
* @return string
*/
static function join ()
{
$args = func_get_args();
return implode(self::SEPARATOR, $args);
}
}