Commit inicial powerservice

This commit is contained in:
Gabriel
2026-10-19 12:45:45 -06:00
commit ad85486fe2
10158 changed files with 2736804 additions and 0 deletions
@@ -0,0 +1,62 @@
{
"name": "hidehalo\/nanoid-php",
"type": "library",
"description": "A copy of nanoid in PHP",
"keywords": [
"hidehalo",
"nanoid-php"
],
"homepage": "https:\/\/github.com\/hidehalo\/nanoid-php",
"license": "MIT",
"authors": [
{
"name": "hidehalo",
"email": "tianchen_cc@yeah.net",
"homepage": "https:\/\/github.com\/hidehalo",
"role": "Owner"
}
],
"require": {
"php": "~5.6|~7.0|~8.0",
"paragonie\/random_compat": ">=2.0"
},
"require-dev": {
"phpunit\/phpunit": ">=5.6",
"squizlabs\/php_codesniffer": "^2.3"
},
"archive": {
"exclude": [
"tests\/",
"benchmark\/",
"examples\/",
".gitignore",
".editorconfig",
"*.md",
"*.yml",
"*.xml"
]
},
"autoload": {
"psr-4": {
"JooosiFonDeps\\Hidehalo\\Nanoid\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"JooosiFonDeps\\Hidehalo\\Nanoid\\Test\\": "tests"
}
},
"scripts": {
"test": "phpunit",
"check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests",
"fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests"
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"config": {
"sort-packages": true
}
}
@@ -0,0 +1,110 @@
<?php
namespace JooosiFonDeps\Hidehalo\Nanoid;
class Client
{
/**
* Random mode flags
*
* @const MODE_NORMAL 1
* @const MODEL_DYNAMIC 2
*/
const MODE_NORMAL = 1;
const MODE_DYNAMIC = 2;
/**
* @param string $alphabet Symbols to be used in ID.
* @param integer $size number of symbols in ID.
*/
protected $alphabet;
protected $size;
/**
* @var CoreInterface $core Core dynamic random
*/
private $core;
/**
* @var GeneratorInterface $generator Random Btyes Generator
*/
protected $generator;
/**
* Constructor of Client
*
* @codeCoverageIgnore
* @param integer $size
* @param GeneratorInterface $generator
*/
public function __construct($size = 21, GeneratorInterface $generator = null)
{
$this->size = $size > 0 ? $size : 21;
$this->generator = $generator ?: new Generator();
$this->core = new Core();
$this->alphabet = CoreInterface::SAFE_SYMBOLS;
}
/**
* Generate nanoid via optional modes
*
* @param integer $size
* @param integer $mode Client::MODE_NORMAL|Client::MODE_DYNAMIC
* @return string
*/
public function generateId($size = 0, $mode = self::MODE_NORMAL)
{
$size = $size > 0 ? $size : $this->size;
switch ($mode) {
case self::MODE_DYNAMIC:
return $this->core->random($this->generator, $size, $this->alphabet);
default:
return $this->normalRandom($size);
}
}
/**
* The original API of nanoid. Use it be careful, Please make sure
* you have been implements your custom GeneratorInterface as correctly.
* Otherwise use the build-in default random bytes generator
*
* @param GeneratorInterface $generator
* @param integer $size
* @param string $alphabet default CoreInterface::SAFE_SYMBOLS
* @return string
*/
public function formattedId($alphabet, $size = 0, GeneratorInterface $generator = null)
{
$alphabet = $alphabet ?: CoreInterface::SAFE_SYMBOLS;
$size = $size > 0 ? $size : $this->size;
$generator = $generator ?: $this->generator;
return $this->core->random($generator, $size, $alphabet);
}
/**
* Backwards-compatible method name.
*
* @param string $alphabet
* @param integer $size
* @param GeneratorInterface $generator
*
* @return string
* @since 1.0.0
*/
public function formatedId($alphabet, $size = 0, GeneratorInterface $generator = null)
{
$size = $size > 0 ? $size : $this->size;
return $this->formattedId($alphabet, $size, $generator);
}
/**
* Generate secure URL-friendly unique ID.
* By default, ID will have 21 symbols to have same collisions probability
* as UUID v4.
*
* @see https://github.com/ai/nanoid/blob/master/non-secure/index.js#L19
* @param integer $size
* @return string
*/
private function normalRandom($size)
{
$id = '';
while (1 <= $size--) {
$rand = mt_rand() / (mt_getrandmax() + 1);
$id .= $this->alphabet[intval($rand * 64)];
}
return $id;
}
}
@@ -0,0 +1,32 @@
<?php
namespace JooosiFonDeps\Hidehalo\Nanoid;
class Core implements CoreInterface
{
/**
* @inheritDoc
* @see https://github.com/ai/nanoid/blob/master/async/index.browser.js#L4
*/
public function random(GeneratorInterface $generator, $size, $alphabet = CoreInterface::SAFE_SYMBOLS)
{
$len = strlen($alphabet);
$mask = (2 << (int) (log($len - 1) / \M_LN2)) - 1;
$step = (int) ceil(1.6 * $mask * $size / $len);
$id = '';
while (\true) {
$bytes = $generator->random($step);
// NOTE: `$bytes` maybe not a normal "Array"
// sometimes it's begin from index 1, use iterator please
foreach ($bytes as $byte) {
$byte &= $mask;
if (isset($alphabet[$byte])) {
$id .= $alphabet[$byte];
if (strlen($id) === $size) {
return $id;
}
}
}
}
}
}
@@ -0,0 +1,19 @@
<?php
namespace JooosiFonDeps\Hidehalo\Nanoid;
interface CoreInterface
{
const SAFE_SYMBOLS = '_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const MASKS = [15, 31, 63, 127, 255];
/**
* Secure random string generator with custom alphabet.
* Alphabet must contain 256 symbols or less. Otherwise, the generator
* will not be secure.
*
* @param GeneratorInterface $generator
* @param string $alphabet
* @param integer $size
*/
public function random(GeneratorInterface $generator, $size, $alphabet = CoreInterface::SAFE_SYMBOLS);
}
@@ -0,0 +1,14 @@
<?php
namespace JooosiFonDeps\Hidehalo\Nanoid;
class Generator implements GeneratorInterface
{
/**
* @inheritDoc
*/
public function random($size)
{
return unpack('C*', \random_bytes($size));
}
}
@@ -0,0 +1,14 @@
<?php
namespace JooosiFonDeps\Hidehalo\Nanoid;
interface GeneratorInterface
{
/**
* Return random bytes array
*
* @param integer $size
* @return array
*/
public function random($size);
}