Библиотека для cis, online, cms1

This commit is contained in:
Фёдор Подлеснов 2016-06-29 18:51:32 +03:00
commit 3c2e614d87
269 changed files with 39854 additions and 0 deletions

319
core/path.php Normal file
View file

@ -0,0 +1,319 @@
<?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);
}
}
/**
* Ïîäáèðàåò íîâîå âðåìåííîå èìÿ äëÿ ôàéëà
*
* @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);
}
}
?>