initial commit; version 22.5.12042

This commit is contained in:
2022-12-12 23:28:25 -05:00
commit af1b03d79f
17653 changed files with 22692970 additions and 0 deletions

View File

@ -0,0 +1,165 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
declare(strict_types=1);
namespace Phpfastcache\Util;
use ArrayAccess;
use Countable;
use Iterator;
/**
* Class ArrayObject
* @package phpFastCache\Util
*/
class ArrayObject implements ArrayAccess, Iterator, Countable
{
/**
* @var array
*/
private $array = [];
/**
* @var int
*/
private $position = 0;
/**
* @param $args
* ArrayObject constructor.
*/
public function __construct(...$args)
{
$this->array = (count($args) === 1 && is_array($args[0]) ? $args[0] : $args);
}
/**
* @return mixed
*/
public function current()
{
return $this->array[$this->position];
}
/**
*
*/
public function next()
{
++$this->position;
}
/**
* @return int
*/
public function key(): int
{
return $this->position;
}
/**
* @return bool
*/
public function valid(): bool
{
return $this->offsetExists($this->position);
}
/**
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset): bool
{
return array_key_exists($offset, $this->array);
}
/**
*
*/
public function rewind()
{
$this->position = 0;
}
/**
* @return int
*/
public function count(): int
{
return count($this->array);
}
/**
* @param mixed $offset
* @return mixed|null
*/
public function offsetGet($offset)
{
return $this->array[$offset] ?? null;
}
/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
// NOTE: THIS IS THE FIX FOR THE ISSUE "Indirect modification of overloaded element of SplFixedArray has no effect"
// NOTE: WHEN APPENDING AN ARRAY (E.G. myArr[] = 5) THE KEY IS NULL, SO WE TEST FOR THIS CONDITION BELOW, AND VOILA
if ($offset === null) {
$this->array[] = $value;
} else {
$this->array[$offset] = $value;
}
}
/**
* @param mixed $offset
*/
public function offsetUnset($offset)
{
unset($this->array[$offset]);
}
/**
* @return array|mixed
*/
public function toArray()
{
return $this->array;
}
/**
* @param array $array
* @return self
*/
public function mergeArray($array): self
{
$this->array = array_merge($this->array, $array);
return $this;
}
/**
* @return array|mixed
*/
protected function &getArray()
{
return $this->array;
}
}

View File

@ -0,0 +1,34 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
declare(strict_types=1);
namespace Phpfastcache\Util;
/**
* Interface ClassNamespaceResolverInterface
* @package Phpfastcache\Util
*/
interface ClassNamespaceResolverInterface
{
/**
* @return string
*/
public function getClassNamespace(): string;
/**
* @return string
*/
public function getClassName(): string;
}

View File

@ -0,0 +1,175 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
declare(strict_types=1);
namespace Phpfastcache\Util;
use Iterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Traversable;
/**
* Trait ClassNamespaceResolverTrait
* @package phpFastCache\Core
*/
trait ClassNamespaceResolverTrait
{
/**
* @var string
*/
protected $namespace;
/**
* Iterate over all files in the given directory searching for classes.
*
* NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they
* deprecated the whole component as of SF4. Our thanks to them.
*
* @param Iterator|string|array $dir The directory to search in or an iterator
*
* @return array A class map array
*/
protected static function createClassMap($dir): array
{
if (\is_string($dir)) {
$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
}
$map = [];
if (\is_iterable($dir)) {
foreach ($dir as $file) {
if (!$file->isFile()) {
continue;
}
$path = $file->getRealPath() ?: $file->getPathname();
if ('php' !== pathinfo($path, PATHINFO_EXTENSION)) {
continue;
}
$classes = self::findClasses($path);
if (PHP_VERSION_ID >= 70000) {
// PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
gc_mem_caches();
}
foreach ($classes as $class) {
$map[$class] = $path;
}
}
}
return $map;
}
/**
* Extract the classes in the given file.
*
* NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they
* deprecated the whole component as of SF4. Our thanks to them.
*
* @param string $path The file to check
*
* @return array The found classes
*/
protected static function findClasses(string $path): array
{
$contents = file_get_contents($path);
$tokens = token_get_all($contents);
$classes = [];
$namespace = '';
for ($i = 0; isset($tokens[$i]); ++$i) {
$token = $tokens[$i];
if (!isset($token[1])) {
continue;
}
$class = '';
switch ($token[0]) {
case T_NAMESPACE:
$namespace = '';
// If there is a namespace, extract it (PHP 8 test)
if(\defined('T_NAME_QUALIFIED')){
while (isset($tokens[++$i][1])) {
if ($tokens[$i][0] === T_NAME_QUALIFIED) {
$namespace = $tokens[$i][1];
break;
}
}
}else{
while (isset($tokens[++$i][1])) {
if (\in_array($tokens[$i][0], [T_STRING, T_NS_SEPARATOR], true)) {
$namespace .= $tokens[$i][1];
}
}
}
$namespace .= '\\';
break;
case T_CLASS:
case T_INTERFACE:
case T_TRAIT:
// Skip usage of ::class constant
$isClassConstant = false;
for ($j = $i - 1; $j > 0; --$j) {
if (!isset($tokens[$j][1])) {
break;
}
if (T_DOUBLE_COLON === $tokens[$j][0]) {
$isClassConstant = true;
break;
} elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) {
break;
}
}
if ($isClassConstant) {
break;
}
// Find the classname
while (isset($tokens[++$i][1])) {
$t = $tokens[$i];
if (T_STRING === $t[0]) {
$class .= $t[1];
} elseif ('' !== $class && T_WHITESPACE === $t[0]) {
break;
}
}
$classes[] = ltrim($namespace . $class, '\\');
break;
default:
break;
}
}
return $classes;
}
/**
* @return string
*/
public function getClassNamespace(): string
{
if (!$this->namespace) {
$this->namespace = substr(static::class, 0, strrpos(static::class, '\\'));
}
return $this->namespace;
}
/**
* @return string
*/
public function getClassName(): string
{
return static::class;
}
}

View File

@ -0,0 +1,155 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
declare(strict_types=1);
namespace Phpfastcache\Util;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
/**
* Class Directory
* @package phpFastCache\Util
*/
class Directory
{
/**
* Get the directory size
* @param string $directory
* @param bool $includeDirAllocSize
* @return integer
*/
public static function dirSize(string $directory, bool $includeDirAllocSize = false): int
{
$size = 0;
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) {
/**
* @var SplFileInfo $file
*/
if ($file->isFile()) {
$size += filesize($file->getRealPath());
} else {
if ($includeDirAllocSize) {
$size += $file->getSize();
}
}
}
return $size;
}
/**
* @param string $path
* @return int
*/
public static function getFileCount(string $path): int
{
$count = 0;
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach ($objects as $object) {
/**
* @var SplFileInfo $object
*/
if ($object->isFile()) {
$count++;
}
}
return $count;
}
/**
* Recursively delete a directory and all of it's contents - e.g.the equivalent of `rm -r` on the command-line.
* Consistent with `rmdir()` and `unlink()`, an E_WARNING level error will be generated on failure.
*
* @param string $source absolute path to directory or file to delete.
* @param bool $removeOnlyChildren set to true will only remove content inside directory.
*
* @return bool true on success; false on failure
*/
public static function rrmdir(string $source, bool $removeOnlyChildren = false): bool
{
if (empty($source) || file_exists($source) === false) {
return false;
}
if (is_file($source) || is_link($source)) {
clearstatcache(true, $source);
return unlink($source);
}
$files = new RecursiveIteratorIterator
(
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
/**
* @var SplFileInfo $fileinfo
*/
$realpath = $fileinfo->getRealPath();
if($realpath){
if ($fileinfo->isDir()) {
if (self::rrmdir($fileinfo->getRealPath()) === false) {
return false;
}
} elseif (unlink($realpath) === false) {
return false;
}
}
else{
return false;
}
}
if ($removeOnlyChildren === false) {
return rmdir($source);
}
return true;
}
/**
* Alias of realpath() but work
* on non-existing files
*
* @param string $path
* @return string
*/
public static function getAbsolutePath(string $path): string
{
$parts = preg_split('~[/\\\\]+~', $path, 0, PREG_SPLIT_NO_EMPTY);
$absolutes = [];
foreach ($parts as $part) {
if ('.' === $part) {
continue;
}
if ('..' === $part) {
array_pop($absolutes);
} else {
$absolutes[] = $part;
}
}
/**
* Allows to dereference char
*/
$__FILE__ = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__);// remove file protocols such as "phar://" etc.
$prefix = $__FILE__[0] === DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : '';
return $prefix . implode(DIRECTORY_SEPARATOR, $absolutes);
}
}

View File

@ -0,0 +1,60 @@
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
declare(strict_types=1);
namespace Phpfastcache\Util;
/**
* Trait MemcacheDriverCollisionDetectorTrait
* @package phpFastCache\Core
*/
trait MemcacheDriverCollisionDetectorTrait
{
/**
* @var string
*/
protected static $driverUsed;
/**
* @param $driverName
* @return bool
*/
public static function checkCollision($driverName): bool
{
$CONSTANT_NAME = __NAMESPACE__ . '\MEMCACHE_DRIVER_USED';
if ($driverName && is_string($driverName)) {
if (!defined($CONSTANT_NAME)) {
define($CONSTANT_NAME, $driverName);
return true;
} else {
if (constant($CONSTANT_NAME) !== $driverName) {
trigger_error(
'Memcache collision detected, you used both Memcache and Memcached driver in your script, this may leads to unexpected behaviours',
E_USER_WARNING
);
return false;
}
}
return true;
}
return false;
}
}