Compare commits
141 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
13c76efcef | ||
|
|
f59c325b05 | ||
|
|
aac5157173 | ||
|
|
abc2705c64 | ||
|
|
e2fff45b16 | ||
|
|
b6ae3f99d9 | ||
|
|
a377ab4b61 | ||
|
|
ec9275d8d3 | ||
|
|
10ed5b3ea0 | ||
|
|
5f290419c6 | ||
|
|
0152af6e65 | ||
|
|
4adb9fba30 | ||
|
|
672931ee67 | ||
|
|
373a9aff2f | ||
|
|
4e3ceecac8 | ||
|
|
8bf694b2b2 | ||
|
|
caf03421a7 | ||
|
|
facf8adaf7 | ||
|
|
c8158c8224 | ||
|
|
7b9a0043d3 | ||
|
|
6517747fef | ||
|
|
fb7071adc4 | ||
|
|
d96c92f9ef | ||
|
|
d7c0054ea0 | ||
|
|
80d2215f2b | ||
|
|
b18bf937d3 | ||
|
|
4b6c5df74f | ||
|
|
89e3888bf8 | ||
|
|
c46125aef1 | ||
|
|
a8ca5d7a2b | ||
|
|
ae2d6580a2 | ||
|
|
97e1b2b63d | ||
|
|
d632a2599e | ||
|
|
2f81009a97 | ||
|
|
e9799f888b | ||
|
|
94b24b4eae | ||
|
|
a304d29698 | ||
|
|
c4c809be66 | ||
|
|
a3bf38f6cf | ||
|
|
c1b906e701 | ||
|
|
32f8e1440d | ||
|
|
2f8f98642b | ||
|
|
7ab03913ac | ||
|
|
a7853171e0 | ||
|
|
dfdfcf87f2 | ||
|
|
6218e0a6a8 | ||
|
|
a84a745be2 | ||
|
|
312762e92e | ||
|
|
fa4c1f0597 | ||
|
|
438a75af23 | ||
|
|
afd8ff3e31 | ||
|
|
4343af7937 | ||
|
|
d06769c48b | ||
|
|
4c0390f082 | ||
|
|
95bee3dc8c | ||
|
|
65132d8a4a | ||
|
|
b2243cd06d | ||
|
|
8f09b67d86 | ||
|
|
fe459aec80 | ||
|
|
de0ed058ca | ||
|
|
f90bd193d9 | ||
|
|
0e31180868 | ||
|
|
68c9164eaa | ||
|
|
c2389db1c9 | ||
|
|
f9558cd3aa | ||
|
|
ae3011fe22 | ||
|
|
9b9dfeac69 | ||
|
|
33cb05a002 | ||
|
|
ec110499a8 | ||
|
|
09839f3451 | ||
|
|
067e0aed5d | ||
|
|
545de5c4a1 | ||
|
|
2fe37bf92a | ||
|
|
cd81d15d9a | ||
|
|
8a33ee5c15 | ||
|
|
46e1419ef5 | ||
|
|
c441063437 | ||
|
|
5290d5f351 | ||
|
|
2635ccb82b | ||
|
|
4f2ac2ed1b | ||
|
|
5b8e4e4e3e | ||
|
|
53192da571 | ||
|
|
f29e915068 | ||
|
|
46bc5f2da6 | ||
|
|
d70182a84e | ||
|
|
7243f69826 | ||
|
|
1fc144e178 | ||
|
|
c383a7b7b7 | ||
|
|
69077c384c | ||
|
|
cfd49947ad | ||
|
|
6985dc4e9d | ||
|
|
5f2668b011 | ||
|
|
eba1ef9c59 | ||
|
|
8497144053 | ||
|
|
2006798388 | ||
|
|
bf63d850ca | ||
|
|
53e267ce24 | ||
|
|
1754ecf2ee | ||
|
|
3c37899a48 | ||
|
|
0436cfe3da | ||
|
|
3606de1a00 | ||
|
|
3081439eda | ||
|
|
7af0e74b85 | ||
|
|
7748b83a6b | ||
|
|
f83293ff1a | ||
|
|
9c3be2942e | ||
|
|
ee62bd98ee | ||
|
|
02e9610fad | ||
|
|
8a41db4649 | ||
|
|
e27ea3dc9f | ||
|
|
ec4bf54d81 | ||
|
|
ec3ca787fa | ||
|
|
86acbbb85b | ||
|
|
8e0af7a5f7 | ||
|
|
b022662dfc | ||
|
|
3039ebf913 | ||
|
|
e2e080c404 | ||
|
|
4671143d1c | ||
|
|
b492558cca | ||
|
|
64e76530d4 | ||
|
|
0b93f9f146 | ||
|
|
4c6fe1cd6c | ||
|
|
83ba48f598 | ||
|
|
62c6de8244 | ||
|
|
1c2f9f0c2c | ||
|
|
30bb0e8895 | ||
|
|
75c4c98de8 | ||
|
|
f72055909b | ||
|
|
b0a8783276 | ||
|
|
7b5ad92e66 | ||
|
|
250067927a | ||
|
|
7c2cbbaca7 | ||
|
|
ac037eabde | ||
|
|
0250b86b3f | ||
|
|
e45acc412b | ||
|
|
854206bc70 | ||
|
|
b192e98a8a | ||
|
|
c4e2c781c6 | ||
|
|
e80b3b8dfd | ||
|
|
2b079ff836 | ||
|
|
37201799b5 |
43
.eslintrc.js
Normal file
43
.eslintrc.js
Normal file
@@ -0,0 +1,43 @@
|
||||
module.exports = {
|
||||
'env': {
|
||||
'browser': true,
|
||||
'es6': true,
|
||||
'commonjs': true,
|
||||
'jquery': true
|
||||
},
|
||||
'extends': 'eslint:recommended',
|
||||
'parserOptions': {
|
||||
'ecmaVersion': 6
|
||||
},
|
||||
'rules': {
|
||||
'indent': [
|
||||
'error',
|
||||
'tab',
|
||||
{
|
||||
'SwitchCase': 1
|
||||
}
|
||||
],
|
||||
'linebreak-style': [
|
||||
'error',
|
||||
'unix'
|
||||
],
|
||||
'quotes': [
|
||||
'error',
|
||||
'single'
|
||||
],
|
||||
'semi': [
|
||||
'error',
|
||||
'always'
|
||||
],
|
||||
'no-console': 'off',
|
||||
'no-unused-vars': [
|
||||
'error', {
|
||||
'vars': 'all',
|
||||
'args': 'after-used',
|
||||
'ignoreRestSiblings': false
|
||||
}
|
||||
],
|
||||
// Requires eslint >= v8.14.0
|
||||
'no-constant-binary-expression': 'error'
|
||||
}
|
||||
};
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -0,0 +1 @@
|
||||
.libs
|
||||
|
||||
@@ -128,7 +128,12 @@ return [
|
||||
'PhanWriteOnlyPublicProperty',
|
||||
'PhanUnreferencedConstant',
|
||||
'PhanWriteOnlyPublicProperty',
|
||||
'PhanReadOnlyPublicProperty'
|
||||
'PhanReadOnlyPublicProperty',
|
||||
// start ignore annotations
|
||||
'PhanUnextractableAnnotationElementName',
|
||||
'PhanUnextractableAnnotationSuffix',
|
||||
// enum problems in comments
|
||||
'PhanCommentObjectInClassConstantType'
|
||||
],
|
||||
|
||||
// Override to hardcode existence and types of (non-builtin) globals in the global scope.
|
||||
|
||||
12
.phive/phars.xml
Normal file
12
.phive/phars.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phive xmlns="https://phar.io/phive">
|
||||
<phar name="phpunit" version="^9.6" installed="9.6.17" location="./tools/phpunit" copy="false"/>
|
||||
<phar name="phpcbf" version="^3.7.2" installed="3.9.0" location="./tools/phpcbf" copy="false"/>
|
||||
<phar name="phpcs" version="^3.7.2" installed="3.9.0" location="./tools/phpcs" copy="false"/>
|
||||
<phar name="phpstan" version="^1.10.37" installed="1.10.59" location="./tools/phpstan" copy="false"/>
|
||||
<phar name="phan" version="^5.4.2" installed="5.4.3" location="./tools/phan" copy="false"/>
|
||||
<phar name="psalm" version="^5.15.0" installed="5.22.2" location="./tools/psalm" copy="false"/>
|
||||
<phar name="phpdox" version="^0.12.0" installed="0.12.0" location="./tools/phpdox" copy="false"/>
|
||||
<phar name="phpdocumentor" version="^3.4.2" installed="3.4.3" location="./tools/phpDocumentor" copy="false"/>
|
||||
<phar name="php-cs-fixer" version="^3.34.1" installed="3.51.0" location="./tools/php-cs-fixer" copy="false"/>
|
||||
</phive>
|
||||
@@ -1,3 +1,5 @@
|
||||
base="/storage/var/www/html/developers/clemens/core_data/php_libraries/trunk/";
|
||||
# must be run in ${base}www/
|
||||
phan --progress-bar -C --analyze-twice
|
||||
# must be run in ${base}
|
||||
cd $base;
|
||||
${base}tools/phan --progress-bar -C --analyze-twice;
|
||||
cd ~;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
base="/storage/var/www/html/developers/clemens/core_data/php_libraries/trunk/";
|
||||
# must be run in ${base}www/
|
||||
phpstan
|
||||
# must be run in ${base}
|
||||
cd $base;
|
||||
${base}tools/phpstan;
|
||||
cd ~;
|
||||
|
||||
@@ -17,7 +17,7 @@ if [ ! -z "${1}" ]; then
|
||||
# "7.3") php_bin="/usr/bin/php7.3 "; ;;
|
||||
# "7.4") php_bin="/usr/bin/php7.4 "; ;;
|
||||
# "8.0") php_bin="/usr/bin/php8.0 "; ;;
|
||||
"8.1") php_bin="/usr/bin/php8.1 "; ;;
|
||||
# "8.1") php_bin="/usr/bin/php8.1 "; ;;
|
||||
"8.2") php_bin="/usr/bin/php8.2 "; ;;
|
||||
*) echo "Not support PHP: ${1}"; exit; ;;
|
||||
esac;
|
||||
@@ -27,13 +27,14 @@ if [ ! -z "${2}" ] && [ -z "${php_bin}" ]; then
|
||||
# "7.3") php_bin="/usr/bin/php7.3 "; ;;
|
||||
# "7.4") php_bin="/usr/bin/php7.4 "; ;;
|
||||
# "8.0") php_bin="/usr/bin/php8.0 "; ;;
|
||||
"8.1") php_bin="/usr/bin/php8.1 "; ;;
|
||||
# "8.1") php_bin="/usr/bin/php8.1 "; ;;
|
||||
"8.2") php_bin="/usr/bin/php8.2 "; ;;
|
||||
*) echo "Not support PHP: ${1}"; exit; ;;
|
||||
esac;
|
||||
fi;
|
||||
|
||||
phpunit_call="${php_bin}${base}www/vendor/bin/phpunit ${opt_testdox} -c ${base}phpunit.xml ${base}4dev/tests/";
|
||||
# Note 4dev/tests/bootstrap.php has to be set as bootstrap file in phpunit.xml
|
||||
phpunit_call="${php_bin}${base}tools/phpunit ${opt_testdox} -c ${base}phpunit.xml ${base}4dev/tests/";
|
||||
|
||||
${phpunit_call};
|
||||
|
||||
|
||||
31
4dev/documentation/DB_Query_Params.md
Normal file
31
4dev/documentation/DB_Query_Params.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# DB Query Params ? and : to $
|
||||
|
||||
dbReturn*
|
||||
dbExec
|
||||
|
||||
keep
|
||||
->query
|
||||
->params
|
||||
for reference
|
||||
|
||||
## : named params
|
||||
|
||||
in order for each named found replace with order number:
|
||||
|
||||
```txt
|
||||
:name, :foo, :bar, :name =>
|
||||
$1, $2, $3, $1
|
||||
```
|
||||
|
||||
```php
|
||||
$query = str_replace(
|
||||
[':name', ':foo', ':bar'],
|
||||
['$1', '$2', '$3'],
|
||||
$query
|
||||
);
|
||||
```
|
||||
|
||||
## ? Params
|
||||
|
||||
Foreach ? set $1 to $n and store that in new params array
|
||||
in QUERY for each ? replace with matching $n
|
||||
@@ -68,13 +68,10 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
|
||||
// logger is always needed
|
||||
// define basic connection set valid and one invalid
|
||||
self::$log = new \CoreLibs\Debug\Logging([
|
||||
self::$log = new \CoreLibs\Logging\Logging([
|
||||
// 'log_folder' => __DIR__ . DIRECTORY_SEPARATOR . 'log',
|
||||
'log_folder' => DIRECTORY_SEPARATOR . 'tmp',
|
||||
'file_id' => 'CoreLibs-ACL-Login-Test',
|
||||
'debug_all' => true,
|
||||
'echo_all' => false,
|
||||
'print_all' => true,
|
||||
'log_file_id' => 'CoreLibs-ACL-Login-Test',
|
||||
]);
|
||||
// test database we need to connect do, if not possible this test is skipped
|
||||
self::$db = new \CoreLibs\DB\IO(
|
||||
@@ -170,8 +167,10 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
// change_password, pw_username, pw_old_password, pw_new_password,
|
||||
// pw_new_password_confirm
|
||||
// 3[session]: override session set
|
||||
// 4[error] : expected error code, 0 for all ok, 3000 for login page view
|
||||
// note that 1000 (no db), 2000 (no session) must be tested too
|
||||
// 4[error] : expected error code, 0 for all ok, 100 for login page view
|
||||
// note that 1000 (no db), 2000 (no session), 3000 (options set error)
|
||||
// must be tested too
|
||||
// <1000 info, >=1000 critical error
|
||||
// 5[return] : expected return array, eg login_error code,
|
||||
// or other info data to match
|
||||
$tests = [
|
||||
@@ -183,7 +182,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 0,
|
||||
'error_string' => 'Success: <b>No error</b>',
|
||||
@@ -201,7 +200,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 0,
|
||||
'error_string' => 'Success: <b>No error</b>',
|
||||
@@ -224,7 +223,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 0,
|
||||
'error_string' => 'Success: <b>No error</b>',
|
||||
@@ -311,7 +310,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
'login_password' => '',
|
||||
],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 102,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -332,7 +331,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
'login_password' => 'abc',
|
||||
],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 102,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -353,7 +352,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
'login_password' => '',
|
||||
],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 102,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -374,7 +373,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
'login_password' => 'abc',
|
||||
],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 1010,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -398,7 +397,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
'login_password' => 'abc',
|
||||
],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
// default password is plain text
|
||||
'login_error' => 1012,
|
||||
@@ -424,7 +423,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
'login_password' => 'admin',
|
||||
],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 106,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -449,7 +448,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
'login_password' => 'admin',
|
||||
],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 104,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -474,7 +473,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
'login_password' => 'admin',
|
||||
],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 105,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -523,7 +522,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
'login_password' => 'admin',
|
||||
],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 107,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -577,7 +576,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
'login_password' => 'admin',
|
||||
],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 107,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -603,7 +602,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
'login_password' => 'admin',
|
||||
],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 107,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -628,7 +627,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
'login_password' => 'admin',
|
||||
],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 108,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -764,7 +763,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
],
|
||||
[],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 1010,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -856,7 +855,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
],
|
||||
[],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 1101,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -912,7 +911,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
],
|
||||
[],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 1102,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -968,7 +967,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
],
|
||||
[],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 1102,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -995,7 +994,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
],
|
||||
[],
|
||||
[],
|
||||
3000,
|
||||
100,
|
||||
[
|
||||
'login_error' => 1102,
|
||||
'error_string' => '<span style="color: red;">Fatal Error:</span> '
|
||||
@@ -1136,7 +1135,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
$login_mock->expects($this->any())
|
||||
->method('loginTerminate')
|
||||
->will(
|
||||
$this->returnCallback(function ($code) {
|
||||
$this->returnCallback(function ($message, $code) {
|
||||
throw new \Exception('', $code);
|
||||
})
|
||||
);
|
||||
@@ -1230,7 +1229,11 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
$login_mock->loginSetMaxLoginErrorCount($mock_settings['max_login_error_count']);
|
||||
// temporary wrong password
|
||||
$_POST['login_password'] = 'wrong';
|
||||
for ($run = 1, $max_run = $login_mock->loginGetMaxLoginErrorCount(); $run <= $max_run; $run++) {
|
||||
for (
|
||||
$run = 1, $max_run = $login_mock->loginGetMaxLoginErrorCount();
|
||||
$run <= $max_run;
|
||||
$run++
|
||||
) {
|
||||
try {
|
||||
$login_mock->loginMainCall();
|
||||
} catch (\Exception $e) {
|
||||
@@ -1478,10 +1481,10 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
// print "AJAX: " . $login_mock->loginGetAjaxFlag() . "\n";
|
||||
// print "AJAX GLOBAL: " . ($GLOBALS['AJAX_PAGE'] ?? '{f}') . "\n";
|
||||
// print "Login error expext: " . ($expected['login_error'] ?? '{0}') . "\n";
|
||||
// if this is 3000, then we do further error checks
|
||||
// if this is 100, then we do further error checks
|
||||
if (
|
||||
$e->getCode() == 3000 ||
|
||||
!empty($_POST['login_exit']) && $_POST['login_exit'] == 3000
|
||||
$e->getCode() == 100 ||
|
||||
!empty($_POST['login_exit']) && $_POST['login_exit'] == 100
|
||||
) {
|
||||
$this->assertEquals(
|
||||
$expected['login_error'],
|
||||
@@ -1819,7 +1822,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
$login_mock->expects($this->any())
|
||||
->method('loginTerminate')
|
||||
->will(
|
||||
$this->returnCallback(function ($code) {
|
||||
$this->returnCallback(function ($message, $code) {
|
||||
throw new \Exception('', $code);
|
||||
})
|
||||
);
|
||||
@@ -1933,7 +1936,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
$login_mock->expects($this->any())
|
||||
->method('loginTerminate')
|
||||
->will(
|
||||
$this->returnCallback(function ($code) {
|
||||
$this->returnCallback(function ($message, $code) {
|
||||
throw new \Exception('', $code);
|
||||
})
|
||||
);
|
||||
@@ -2021,7 +2024,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
$login_mock->expects($this->any())
|
||||
->method('loginTerminate')
|
||||
->will(
|
||||
$this->returnCallback(function ($code) {
|
||||
$this->returnCallback(function ($message, $code) {
|
||||
throw new \Exception('', $code);
|
||||
})
|
||||
);
|
||||
@@ -2117,7 +2120,7 @@ final class CoreLibsACLLoginTest extends TestCase
|
||||
$login_mock->expects($this->any())
|
||||
->method('loginTerminate')
|
||||
->will(
|
||||
$this->returnCallback(function ($code) {
|
||||
$this->returnCallback(function ($message, $code) {
|
||||
throw new \Exception('', $code);
|
||||
})
|
||||
);
|
||||
|
||||
@@ -13,6 +13,11 @@ use PHPUnit\Framework\TestCase;
|
||||
*/
|
||||
final class CoreLibsCheckColorsTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function validateColorProvider(): array
|
||||
{
|
||||
/*
|
||||
@@ -321,7 +326,7 @@ final class CoreLibsCheckColorsTest extends TestCase
|
||||
*/
|
||||
public function testValidateColorException(int $flag): void
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectException(\UnexpectedValueException::class);
|
||||
\CoreLibs\Check\Colors::validateColor('#ffffff', $flag);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,10 +28,10 @@ final class CoreLibsCheckFileTest extends TestCase
|
||||
public function filesList(): array
|
||||
{
|
||||
return [
|
||||
['filename.txt', 'txt', 5],
|
||||
['filename.csv', 'csv', 15],
|
||||
['filename.tsv', 'tsv', 0],
|
||||
['file_does_not_exits', '', -1],
|
||||
['filename.txt', 'txt', 5, 'text/plain'],
|
||||
['filename.csv', 'csv', 15, 'text/csv'],
|
||||
['filename.tsv', 'tsv', 0, 'text/plain'],
|
||||
['file_does_not_exits', '', -1, ''],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -63,6 +63,15 @@ final class CoreLibsCheckFileTest extends TestCase
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function mimeTypeProvider(): array
|
||||
{
|
||||
$list = [];
|
||||
foreach ($this->filesList() as $row) {
|
||||
$list[$row[0] . ' must be mime type ' . $row[3]] = [$row[0], $row[3]];
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if file extension matches
|
||||
*
|
||||
@@ -115,6 +124,51 @@ final class CoreLibsCheckFileTest extends TestCase
|
||||
unlink($this->base_folder . $input);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::getMimeType
|
||||
* @dataProvider mimeTypeProvider
|
||||
* @testdox getMimeType $input must be mime type $expected [$_dataName]
|
||||
*
|
||||
* @param string $input
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testGetMimeType(string $input, string $expected): void
|
||||
{
|
||||
if (!empty($expected)) {
|
||||
$file = $this->base_folder . $input;
|
||||
$fp = fopen($file, 'w');
|
||||
switch ($expected) {
|
||||
case 'text/csv':
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
fwrite($fp, '"This is row","' . $expected . '",' . $i . PHP_EOL);
|
||||
}
|
||||
break;
|
||||
case 'text/tsv':
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
fwrite($fp, "\"This is row\"\t\"" . $expected . "\"\t\"" . $i . PHP_EOL);
|
||||
}
|
||||
break;
|
||||
case 'text/plain':
|
||||
fwrite($fp, 'This is mime type: ' . $expected . PHP_EOL);
|
||||
break;
|
||||
}
|
||||
fclose($fp);
|
||||
} else {
|
||||
$this->expectException(\UnexpectedValueException::class);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Check\File::getMimeType($this->base_folder . $input)
|
||||
);
|
||||
// unlink file
|
||||
if (is_file($this->base_folder . $input)) {
|
||||
unlink($this->base_folder . $input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -31,6 +31,7 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
|
||||
4,
|
||||
'b',
|
||||
'c' => 'test',
|
||||
'single' => 'single',
|
||||
'same' => 'same',
|
||||
'deep' => [
|
||||
'sub' => [
|
||||
@@ -288,6 +289,188 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function arraySearchKeyProvider(): array
|
||||
{
|
||||
/*
|
||||
0: search in array
|
||||
1: search keys
|
||||
2: flat flag
|
||||
3: prefix flag
|
||||
4: expected array
|
||||
*/
|
||||
return [
|
||||
// single
|
||||
'find single, standard' => [
|
||||
0 => self::$array,
|
||||
1 => ['single'],
|
||||
2 => null,
|
||||
3 => null,
|
||||
4 => [
|
||||
0 => [
|
||||
'value' => 'single',
|
||||
'path' => ['single'],
|
||||
],
|
||||
],
|
||||
],
|
||||
'find single, prefix' => [
|
||||
0 => self::$array,
|
||||
1 => ['single'],
|
||||
2 => null,
|
||||
3 => true,
|
||||
4 => [
|
||||
'single' => [
|
||||
0 => [
|
||||
'value' => 'single',
|
||||
'path' => ['single'],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'find single, flat' => [
|
||||
0 => self::$array,
|
||||
1 => ['single'],
|
||||
2 => true,
|
||||
3 => null,
|
||||
4 => [
|
||||
'single',
|
||||
],
|
||||
],
|
||||
'find single, flat, prefix' => [
|
||||
0 => self::$array,
|
||||
1 => ['single'],
|
||||
2 => true,
|
||||
3 => true,
|
||||
4 => [
|
||||
'single' => [
|
||||
'single',
|
||||
],
|
||||
],
|
||||
],
|
||||
// not found
|
||||
'not found, standard' => [
|
||||
0 => self::$array,
|
||||
1 => ['NOT FOUND'],
|
||||
2 => null,
|
||||
3 => null,
|
||||
4 => [],
|
||||
],
|
||||
'not found, standard, prefix' => [
|
||||
0 => self::$array,
|
||||
1 => ['NOT FOUND'],
|
||||
2 => null,
|
||||
3 => true,
|
||||
4 => [
|
||||
'NOT FOUND' => [],
|
||||
],
|
||||
],
|
||||
'not found, flat' => [
|
||||
0 => self::$array,
|
||||
1 => ['NOT FOUND'],
|
||||
2 => true,
|
||||
3 => null,
|
||||
4 => [],
|
||||
],
|
||||
'not found, flat, prefix' => [
|
||||
0 => self::$array,
|
||||
1 => ['NOT FOUND'],
|
||||
2 => true,
|
||||
3 => true,
|
||||
4 => [
|
||||
'NOT FOUND' => [],
|
||||
],
|
||||
],
|
||||
// multi
|
||||
'multiple found, standard' => [
|
||||
0 => self::$array,
|
||||
1 => ['same'],
|
||||
2 => null,
|
||||
3 => null,
|
||||
4 => [
|
||||
[
|
||||
'value' => 'same',
|
||||
'path' => ['a', 'same', ],
|
||||
],
|
||||
[
|
||||
'value' => 'same',
|
||||
'path' => ['same', ],
|
||||
],
|
||||
[
|
||||
'value' => 'same',
|
||||
'path' => ['deep', 'sub', 'same', ],
|
||||
],
|
||||
]
|
||||
],
|
||||
'multiple found, flat' => [
|
||||
0 => self::$array,
|
||||
1 => ['same'],
|
||||
2 => true,
|
||||
3 => null,
|
||||
4 => ['same', 'same', 'same', ],
|
||||
],
|
||||
// search with multiple
|
||||
'search multiple, standard' => [
|
||||
0 => self::$array,
|
||||
1 => ['single', 'nested'],
|
||||
2 => null,
|
||||
3 => null,
|
||||
4 => [
|
||||
[
|
||||
'value' => 'single',
|
||||
'path' => ['single'],
|
||||
],
|
||||
[
|
||||
'value' => 'bar',
|
||||
'path' => ['deep', 'sub', 'nested', ],
|
||||
],
|
||||
],
|
||||
],
|
||||
'search multiple, prefix' => [
|
||||
0 => self::$array,
|
||||
1 => ['single', 'nested'],
|
||||
2 => null,
|
||||
3 => true,
|
||||
4 => [
|
||||
'single' => [
|
||||
[
|
||||
'value' => 'single',
|
||||
'path' => ['single'],
|
||||
],
|
||||
],
|
||||
'nested' => [
|
||||
[
|
||||
'value' => 'bar',
|
||||
'path' => ['deep', 'sub', 'nested', ],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'search multiple, flat' => [
|
||||
0 => self::$array,
|
||||
1 => ['single', 'nested'],
|
||||
2 => true,
|
||||
3 => null,
|
||||
4 => [
|
||||
'single', 'bar',
|
||||
],
|
||||
],
|
||||
'search multiple, flat, prefix' => [
|
||||
0 => self::$array,
|
||||
1 => ['single', 'nested'],
|
||||
2 => true,
|
||||
3 => true,
|
||||
4 => [
|
||||
'single' => ['single', ],
|
||||
'nested' => ['bar', ],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* provides array listing for the merge test
|
||||
*
|
||||
@@ -335,17 +518,20 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
|
||||
return [
|
||||
// error <2 arguments
|
||||
'too view arguments' => [
|
||||
'ArgumentCountError',
|
||||
'arrayMergeRecursive needs two or more array arguments',
|
||||
[1]
|
||||
],
|
||||
// error <2 arrays
|
||||
'only one array' => [
|
||||
'ArgumentCountError',
|
||||
'arrayMergeRecursive needs two or more array arguments',
|
||||
[1],
|
||||
true,
|
||||
],
|
||||
// error element is not array
|
||||
'non array between array' => [
|
||||
'TypeError',
|
||||
'arrayMergeRecursive encountered a non array argument',
|
||||
[1],
|
||||
'string',
|
||||
@@ -691,6 +877,44 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers::arraySearchKey
|
||||
* @dataProvider arraySearchKeyProvider
|
||||
* @testdox arraySearchKey Search array with keys and flat: $flat, prefix: $prefix [$_dataName]
|
||||
*
|
||||
* @param array $input
|
||||
* @param array $needles
|
||||
* @param bool|null $flat
|
||||
* @param bool|null $prefix
|
||||
* @param array $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testArraySearchKey(
|
||||
array $input,
|
||||
array $needles,
|
||||
?bool $flat,
|
||||
?bool $prefix,
|
||||
array $expected
|
||||
): void {
|
||||
if ($flat === null && $prefix === null) {
|
||||
$result = \CoreLibs\Combined\ArrayHandler::arraySearchKey($input, $needles);
|
||||
} elseif ($flat === null) {
|
||||
$result = \CoreLibs\Combined\ArrayHandler::arraySearchKey($input, $needles, prefix: $prefix);
|
||||
} elseif ($prefix === null) {
|
||||
$result = \CoreLibs\Combined\ArrayHandler::arraySearchKey($input, $needles, flat: $flat);
|
||||
} else {
|
||||
$result = \CoreLibs\Combined\ArrayHandler::arraySearchKey($input, $needles, $flat, $prefix);
|
||||
}
|
||||
// print "E: " . print_r($expected, true) . "\n";
|
||||
// print "R: " . print_r($result, true) . "\n";
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$result
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -726,18 +950,20 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
|
||||
*/
|
||||
public function testArrayMergeRecursiveWarningA(): void
|
||||
{
|
||||
set_error_handler(
|
||||
static function (int $errno, string $errstr): never {
|
||||
throw new Exception($errstr, $errno);
|
||||
},
|
||||
E_USER_WARNING
|
||||
);
|
||||
// set_error_handler(
|
||||
// static function (int $errno, string $errstr): never {
|
||||
// throw new Exception($errstr, $errno);
|
||||
// },
|
||||
// E_USER_WARNING
|
||||
// );
|
||||
|
||||
$arrays = func_get_args();
|
||||
// first is expected warning
|
||||
$exception = array_shift($arrays);
|
||||
$warning = array_shift($arrays);
|
||||
|
||||
// phpunit 10.0 compatible
|
||||
$this->expectException($exception);
|
||||
$this->expectExceptionMessage($warning);
|
||||
|
||||
\CoreLibs\Combined\ArrayHandler::arrayMergeRecursive(...$arrays);
|
||||
@@ -872,16 +1098,109 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
|
||||
* @testdox arrayFlatForKey array $input will be $expected [$_dataName]
|
||||
*
|
||||
* @param array $input
|
||||
* @param string $search
|
||||
* @param array $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testArrayFlatForKey(array $input, $search, array $expected): void
|
||||
public function testArrayFlatForKey(array $input, string $search, array $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\ArrayHandler::arrayFlatForKey($input, $search)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerArrayGetNextPrevKey(): array
|
||||
{
|
||||
return [
|
||||
'find, ok' => [
|
||||
'input' => [
|
||||
'a' => 'First',
|
||||
'b' => 'Second',
|
||||
'c' => 'Third',
|
||||
],
|
||||
'b',
|
||||
'a',
|
||||
'c'
|
||||
],
|
||||
'find, first' => [
|
||||
'input' => [
|
||||
'a' => 'First',
|
||||
'b' => 'Second',
|
||||
'c' => 'Third',
|
||||
],
|
||||
'a',
|
||||
null,
|
||||
'b'
|
||||
],
|
||||
'find, last' => [
|
||||
'input' => [
|
||||
'a' => 'First',
|
||||
'b' => 'Second',
|
||||
'c' => 'Third',
|
||||
],
|
||||
'c',
|
||||
'b',
|
||||
null
|
||||
],
|
||||
'find, not found' => [
|
||||
'input' => [
|
||||
'a' => 'First',
|
||||
'b' => 'Second',
|
||||
'c' => 'Third',
|
||||
],
|
||||
'z',
|
||||
null,
|
||||
null
|
||||
],
|
||||
'int, index' => [
|
||||
'input' => [
|
||||
'a',
|
||||
'b',
|
||||
'c'
|
||||
],
|
||||
1,
|
||||
0,
|
||||
2
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::arrayGetPrevKey, ::arrayGetNextKey
|
||||
* @dataProvider providerArrayGetNextPrevKey
|
||||
* @testdox arrayGetNextPrevKey get next/prev key for $search wtih $expected_prev/$expected_next [$_dataName]
|
||||
*
|
||||
* @param array $input
|
||||
* @param int|string $search
|
||||
* @param int|string|null $expected_prev
|
||||
* @param int|string|null $expected_next
|
||||
* @return void
|
||||
*/
|
||||
public function testArrayGetNextPrevKey(
|
||||
array $input,
|
||||
int|string $search,
|
||||
int|string|null $expected_prev,
|
||||
int|string|null $expected_next
|
||||
): void {
|
||||
$this->assertEquals(
|
||||
$expected_prev,
|
||||
\CoreLibs\Combined\ArrayHandler::arrayGetPrevKey($input, $search),
|
||||
'Find prev key in array'
|
||||
);
|
||||
$this->assertEquals(
|
||||
$expected_next,
|
||||
\CoreLibs\Combined\ArrayHandler::arrayGetNextKey($input, $search),
|
||||
'Find next key in array'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -66,6 +66,34 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* date string convert test
|
||||
*
|
||||
* @covers ::dateStringFormat
|
||||
* @dataProvider timestampProvider
|
||||
* @testdox dateStringFormat $input (microtime $flag) will be $expected [$_dataName]
|
||||
*
|
||||
* @param int|float $input
|
||||
* @param bool $flag
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testDateStringFormat(
|
||||
$input,
|
||||
bool $flag_show_micro,
|
||||
bool $flag_micro_as_float,
|
||||
string $expected
|
||||
): void {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::dateStringFormat(
|
||||
$input,
|
||||
$flag_show_micro,
|
||||
$flag_micro_as_float
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* interval for both directions
|
||||
*
|
||||
@@ -74,6 +102,11 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
public function intervalProvider(): array
|
||||
{
|
||||
return [
|
||||
'on hour' => [
|
||||
3600,
|
||||
false,
|
||||
'1h 0m 0s'
|
||||
],
|
||||
'interval no microtime' => [
|
||||
1641515890,
|
||||
false,
|
||||
@@ -82,7 +115,7 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
'interval with microtime' => [
|
||||
1641515890,
|
||||
true,
|
||||
'18999d 0h 38m 10s',
|
||||
'18999d 0h 38m 10s 0ms',
|
||||
],
|
||||
'micro interval no microtime' => [
|
||||
1641515890.123456,
|
||||
@@ -92,7 +125,7 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
'micro interval with microtime' => [
|
||||
1641515890.123456,
|
||||
true,
|
||||
'18999d 0h 38m 10s 1235ms',
|
||||
'18999d 0h 38m 10s 124ms',
|
||||
],
|
||||
'negative interval no microtime' => [
|
||||
-1641515890,
|
||||
@@ -103,27 +136,27 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
'microtime only' => [
|
||||
0.123456,
|
||||
true,
|
||||
'0s 1235ms',
|
||||
'0s 123ms',
|
||||
],
|
||||
'seconds only' => [
|
||||
30.123456,
|
||||
true,
|
||||
'30s 1235ms',
|
||||
'30s 123ms',
|
||||
],
|
||||
'minutes only' => [
|
||||
90.123456,
|
||||
true,
|
||||
'1m 30s 1235ms',
|
||||
'1m 30s 123ms',
|
||||
],
|
||||
'hours only' => [
|
||||
3690.123456,
|
||||
true,
|
||||
'1h 1m 30s 1235ms',
|
||||
'1h 1m 30s 123ms',
|
||||
],
|
||||
'days only' => [
|
||||
90090.123456,
|
||||
true,
|
||||
'1d 1h 1m 30s 1235ms',
|
||||
'1d 1h 1m 30s 123ms',
|
||||
],
|
||||
'already set' => [
|
||||
'1d 1h 1m 30s 1235ms',
|
||||
@@ -143,6 +176,306 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* time seconds convert test
|
||||
*
|
||||
* @covers ::timeStringFormat
|
||||
* @dataProvider intervalProvider
|
||||
* @testdox timeStringFormat $input (microtime $flag) will be $expected [$_dataName]
|
||||
*
|
||||
* @param string|int|float $input
|
||||
* @param bool $flag
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testTimeStringFormat(string|int|float $input, bool $flag, string $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::timeStringFormat($input, $flag)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* interval seconds convert
|
||||
*
|
||||
* @covers ::intervalStringFormat
|
||||
* @dataProvider intervalProvider
|
||||
* @testdox intervalStringFormat $input (microtime $show_micro) will be $expected [$_dataName]
|
||||
*
|
||||
* @param string|int|float $input
|
||||
* @param bool $show_micro
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testIntervalStringFormat(string|int|float $input, bool $show_micro, string $expected): void
|
||||
{
|
||||
// we skip string input, that is not allowed
|
||||
if (is_string($input)) {
|
||||
$this->assertTrue(true, 'Skip strings');
|
||||
return;
|
||||
}
|
||||
// invalid values throw exception in default
|
||||
if ($input == 999999999999999) {
|
||||
$this->expectException(\LengthException::class);
|
||||
}
|
||||
// below is equal to timeStringFormat
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::intervalStringFormat(
|
||||
$input,
|
||||
show_microseconds: $show_micro,
|
||||
show_only_days: true,
|
||||
skip_zero: false,
|
||||
skip_last_zero: false,
|
||||
truncate_nanoseconds: true,
|
||||
truncate_zero_seconds_if_microseconds: false
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function intervalExtendedProvider(): array
|
||||
{
|
||||
return [
|
||||
// A
|
||||
'(60) default value' => [
|
||||
[
|
||||
'seconds' => 60,
|
||||
],
|
||||
'expected' => '1m',
|
||||
'exception' => null
|
||||
],
|
||||
'(60) default value, skip_last_zero:false' => [
|
||||
[
|
||||
'seconds' => 60,
|
||||
'skip_last_zero' => false,
|
||||
],
|
||||
'expected' => '1m 0s 0ms',
|
||||
'exception' => null
|
||||
],
|
||||
// B
|
||||
'(120.1) default value' => [
|
||||
[
|
||||
'seconds' => 120.1,
|
||||
],
|
||||
'expected' => '2m 100ms',
|
||||
'exception' => null
|
||||
],
|
||||
'(120.1) default value, skip_zero:false' => [
|
||||
[
|
||||
'seconds' => 120.1,
|
||||
'skip_zero' => false,
|
||||
],
|
||||
'expected' => '2m 0s 100ms',
|
||||
'exception' => null
|
||||
],
|
||||
'(120.1) default value, skip_last_zero:false' => [
|
||||
[
|
||||
'seconds' => 120.1,
|
||||
'skip_last_zero' => false,
|
||||
],
|
||||
'expected' => '2m 100ms',
|
||||
'exception' => null
|
||||
],
|
||||
// C
|
||||
'(3601) default value' => [
|
||||
[
|
||||
'seconds' => 3601,
|
||||
],
|
||||
'expected' => '1h 1s',
|
||||
'exception' => null
|
||||
],
|
||||
'(3601) default value, skip_zero:false' => [
|
||||
[
|
||||
'seconds' => 3601,
|
||||
'skip_zero' => false,
|
||||
],
|
||||
'expected' => '1h 0m 1s',
|
||||
'exception' => null
|
||||
],
|
||||
'(3601) default value, skip_last_zero:false' => [
|
||||
[
|
||||
'seconds' => 3601,
|
||||
'skip_last_zero' => false,
|
||||
],
|
||||
'expected' => '1h 1s 0ms',
|
||||
'exception' => null
|
||||
],
|
||||
// TODO create unit tests for ALL edge cases
|
||||
// CREATE abort tests, simple, all others are handled in exception tests
|
||||
'exception: \UnexpectedValueException:1' => [
|
||||
[
|
||||
'seconds' => 99999999999999999999999
|
||||
],
|
||||
'expected' => null,
|
||||
'exception' => [
|
||||
'class' => \UnexpectedValueException::class,
|
||||
'code' => 1,
|
||||
],
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* test all options for interval conversion
|
||||
*
|
||||
* @covers ::intervalStringFormat
|
||||
* @dataProvider intervalExtendedProvider
|
||||
* @testdox intervalStringFormat $input will be $expected / $exception [$_dataName]
|
||||
*
|
||||
* @param array<string,null|int|float|bool> $parameter_list
|
||||
* @param string $expected
|
||||
* @param array<string,mixed> $exception
|
||||
* @return void
|
||||
*/
|
||||
public function testExtendedIntervalStringFormat(
|
||||
array $parameter_list,
|
||||
?string $expected,
|
||||
?array $exception
|
||||
): void {
|
||||
if ($expected === null && $exception === null) {
|
||||
$this->assertFalse(true, 'Cannot have expected and exception null in test data');
|
||||
}
|
||||
$parameters = [];
|
||||
foreach (
|
||||
[
|
||||
'seconds' => null,
|
||||
'truncate_after' => '',
|
||||
'natural_seperator' => false,
|
||||
'name_space_seperator' => false,
|
||||
'show_microseconds' => true,
|
||||
'short_time_name' => true,
|
||||
'skip_last_zero' => true,
|
||||
'skip_zero' => true,
|
||||
'show_only_days' => false,
|
||||
'auto_fix_microseconds' => false,
|
||||
'truncate_nanoseconds' => false,
|
||||
'truncate_zero_seconds_if_microseconds' => true,
|
||||
] as $param => $default
|
||||
) {
|
||||
if (empty($parameter_list[$param]) && $default === null) {
|
||||
$this->assertFalse(true, 'Parameter ' . $param . ' is mandatory ');
|
||||
} elseif (!isset($parameter_list[$param]) || $parameter_list[$param] === null) {
|
||||
$parameters[] = $default;
|
||||
} else {
|
||||
$parameters[] = $parameter_list[$param];
|
||||
}
|
||||
}
|
||||
if ($expected !== null) {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
call_user_func_array('CoreLibs\Combined\DateTime::intervalStringFormat', $parameters)
|
||||
);
|
||||
} else {
|
||||
if (empty($exception['class']) || empty($exception['code'])) {
|
||||
$this->assertFalse(true, 'Exception tests need Exception name and Code');
|
||||
}
|
||||
$this->expectException($exception['class']);
|
||||
$this->expectExceptionCode($exception['code']);
|
||||
// if we have a message, must be regex
|
||||
if (!empty($exception['message'])) {
|
||||
$this->expectExceptionMessageMatches($exception['message']);
|
||||
}
|
||||
call_user_func_array('CoreLibs\Combined\DateTime::intervalStringFormat', $parameters);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function exceptionsIntervalProvider(): array
|
||||
{
|
||||
return [
|
||||
'UnexpectedValueException: 1 A' => [
|
||||
'seconds' => 99999999999999999999999,
|
||||
'params' => [],
|
||||
'exception' => \UnexpectedValueException::class,
|
||||
'exception_message' => "/^Seconds value is invalid, too large or more than six decimals: /",
|
||||
'excpetion_code' => 1,
|
||||
],
|
||||
'UnexpectedValueException: 1 B' => [
|
||||
'seconds' => 123.1234567,
|
||||
'params' => [],
|
||||
'exception' => \UnexpectedValueException::class,
|
||||
'exception_message' => "/^Seconds value is invalid, too large or more than six decimals: /",
|
||||
'excpetion_code' => 1,
|
||||
],
|
||||
// exception 2 is very likely covered by exception 1
|
||||
'LengthException: 3' => [
|
||||
'seconds' => 999999999999999999,
|
||||
'params' => [
|
||||
'show_only_days',
|
||||
],
|
||||
'exception' => \LengthException::class,
|
||||
'exception_message' => "/^Input seconds value is too large for days output: /",
|
||||
'excpetion_code' => 3,
|
||||
],
|
||||
'UnexpectedValueException: 4' => [
|
||||
'seconds' => 1234567,
|
||||
'params' => [
|
||||
'truncate_after'
|
||||
],
|
||||
'exception' => \UnexpectedValueException::class,
|
||||
'exception_message' => "/^truncate_after has an invalid value: /",
|
||||
'excpetion_code' => 4,
|
||||
],
|
||||
'UnexpectedValueException: 5' => [
|
||||
'seconds' => 1234567,
|
||||
'params' => [
|
||||
'show_only_days:truncate_after'
|
||||
],
|
||||
'exception' => \UnexpectedValueException::class,
|
||||
'exception_message' =>
|
||||
"/^If show_only_days is turned on, the truncate_after cannot be years or months: /",
|
||||
'excpetion_code' => 5,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test all exceptions
|
||||
*
|
||||
* @covers ::intervalStringFormat
|
||||
* @dataProvider exceptionsIntervalProvider
|
||||
* @testdox intervalStringFormat: test Exceptions
|
||||
*
|
||||
* @param int|float $seconds
|
||||
* @param array<string> $params
|
||||
* @param string $exception
|
||||
* @param string $exception_message
|
||||
* @param int $excpetion_code
|
||||
* @return void
|
||||
*/
|
||||
public function testExceptionsIntervalStringFormat(
|
||||
int|float $seconds,
|
||||
array $params,
|
||||
string $exception,
|
||||
string $exception_message,
|
||||
int $excpetion_code,
|
||||
): void {
|
||||
$this->expectException($exception);
|
||||
$this->expectExceptionMessageMatches($exception_message);
|
||||
$this->expectExceptionCode($excpetion_code);
|
||||
if (empty($params)) {
|
||||
\CoreLibs\Combined\DateTime::intervalStringFormat($seconds);
|
||||
} else {
|
||||
if (in_array('show_only_days', $params)) {
|
||||
\CoreLibs\Combined\DateTime::intervalStringFormat($seconds, show_only_days:true);
|
||||
} elseif (in_array('truncate_after', $params)) {
|
||||
\CoreLibs\Combined\DateTime::intervalStringFormat($seconds, truncate_after: 'v');
|
||||
} elseif (in_array('show_only_days:truncate_after', $params)) {
|
||||
\CoreLibs\Combined\DateTime::intervalStringFormat($seconds, show_only_days:true, truncate_after: 'y');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -203,6 +536,25 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::stringToTime
|
||||
* @dataProvider reverseIntervalProvider
|
||||
* @testdox stringToTime $input will be $expected [$_dataName]
|
||||
*
|
||||
* @param string|int|float $input
|
||||
* @param string|int|float $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testStringToTime($input, $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::stringToTime($input)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -238,6 +590,25 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::checkDate
|
||||
* @dataProvider dateProvider
|
||||
* @testdox checkDate $input will be $expected [$_dataName]
|
||||
*
|
||||
* @param string $input
|
||||
* @param bool $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testCheckDate(string $input, bool $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::checkDate($input)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -297,6 +668,25 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::checkDateTime
|
||||
* @dataProvider dateTimeProvider
|
||||
* @testdox checkDateTime $input will be $expected [$_dataName]
|
||||
*
|
||||
* @param string $input
|
||||
* @param bool $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testCheckDateTime(string $input, bool $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::checkDateTime($input)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -309,45 +699,104 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
'2020-12-12',
|
||||
'2021-12-12',
|
||||
-1,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'dates equal' => [
|
||||
'2020-12-12',
|
||||
'2020-12-12',
|
||||
0,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'second date smaller' => [
|
||||
'2021-12-12',
|
||||
'2020-12-12',
|
||||
1
|
||||
1,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'dates equal with different time' => [
|
||||
'2020-12-12 12:12:12',
|
||||
'2020-12-12 13:13:13',
|
||||
0,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'invalid dates --' => [
|
||||
'--',
|
||||
'--',
|
||||
false
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
1,
|
||||
],
|
||||
'empty dates' => [
|
||||
'',
|
||||
'',
|
||||
false
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
1
|
||||
],
|
||||
'invalid dates' => [
|
||||
'not a date',
|
||||
'not a date either',
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
2
|
||||
],
|
||||
'invalid end date' => [
|
||||
'1990-01-01',
|
||||
'not a date either',
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
3
|
||||
],
|
||||
'out of bound dates' => [
|
||||
'1900-1-1',
|
||||
'9999-12-31',
|
||||
-1
|
||||
-1,
|
||||
null,
|
||||
null,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::compareDate
|
||||
* @dataProvider dateCompareProvider
|
||||
* @testdox compareDate $input_a compared to $input_b will be $expected [$_dataName]
|
||||
*
|
||||
* @param string $input_a
|
||||
* @param string $input_b
|
||||
* @param int|bool $expected
|
||||
* @param string|null $exception
|
||||
* @param int|null $exception_code
|
||||
* @return void
|
||||
*/
|
||||
public function testCompareDate(
|
||||
string $input_a,
|
||||
string $input_b,
|
||||
int|bool $expected,
|
||||
?string $exception,
|
||||
?int $exception_code
|
||||
): void {
|
||||
if ($expected === false) {
|
||||
$this->expectException($exception);
|
||||
$this->expectExceptionCode($exception_code);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::compareDate($input_a, $input_b)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function dateTimeCompareProvider(): array
|
||||
{
|
||||
return [
|
||||
@@ -355,55 +804,120 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
'2020-12-12',
|
||||
'2021-12-12',
|
||||
-1,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'dates equal no timestamp' => [
|
||||
'2020-12-12',
|
||||
'2020-12-12',
|
||||
0,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'second date smaller no timestamp' => [
|
||||
'2021-12-12',
|
||||
'2020-12-12',
|
||||
1
|
||||
1,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'date equal first time smaller' => [
|
||||
'2020-12-12 12:12:12',
|
||||
'2020-12-12 13:13:13',
|
||||
-1,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'date equal time equal' => [
|
||||
'2020-12-12 12:12:12',
|
||||
'2020-12-12 12:12:12',
|
||||
0,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'date equal second time smaller' => [
|
||||
'2020-12-12 13:13:13',
|
||||
'2020-12-12 12:12:12',
|
||||
1,
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'valid date invalid time' => [
|
||||
'2020-12-12 13:99:13',
|
||||
'2020-12-12 12:12:99',
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
2
|
||||
],
|
||||
'valid date invalid end time' => [
|
||||
'2020-12-12 13:12:13',
|
||||
'2020-12-12 12:12:99',
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
3
|
||||
],
|
||||
'invalid datetimes --' => [
|
||||
'--',
|
||||
'--',
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
1
|
||||
],
|
||||
'empty datetimess' => [
|
||||
'',
|
||||
'',
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
1
|
||||
],
|
||||
'invalid datetimes' => [
|
||||
'invalid date times' => [
|
||||
'not a date',
|
||||
'not a date either',
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
2
|
||||
],
|
||||
'invalid end date time' => [
|
||||
'1990-01-01 12:12:12',
|
||||
'not a date either',
|
||||
false,
|
||||
'UnexpectedValueException',
|
||||
3
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::compareDateTime
|
||||
* @dataProvider dateTimeCompareProvider
|
||||
* @testdox compareDateTime $input_a compared to $input_b will be $expected [$_dataName]
|
||||
*
|
||||
* @param string $input_a
|
||||
* @param string $input_b
|
||||
* @param int|bool $expected
|
||||
* @param string|null $exception
|
||||
* @param int|null $exception_code
|
||||
* @return void
|
||||
*/
|
||||
public function testCompareDateTime(
|
||||
string $input_a,
|
||||
string $input_b,
|
||||
int|bool $expected,
|
||||
?string $exception,
|
||||
?int $exception_code
|
||||
): void {
|
||||
if ($expected === false) {
|
||||
$this->expectException($exception);
|
||||
$this->expectExceptionCode($exception_code);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::compareDateTime($input_a, $input_b)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -458,151 +972,6 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* date string convert test
|
||||
*
|
||||
* @covers ::dateStringFormat
|
||||
* @dataProvider timestampProvider
|
||||
* @testdox dateStringFormat $input (microtime $flag) will be $expected [$_dataName]
|
||||
*
|
||||
* @param int|float $input
|
||||
* @param bool $flag
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testDateStringFormat(
|
||||
$input,
|
||||
bool $flag_show_micro,
|
||||
bool $flag_micro_as_float,
|
||||
string $expected
|
||||
): void {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::dateStringFormat(
|
||||
$input,
|
||||
$flag_show_micro,
|
||||
$flag_micro_as_float
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* interval convert test
|
||||
*
|
||||
* @covers ::timeStringFormat
|
||||
* @dataProvider intervalProvider
|
||||
* @testdox timeStringFormat $input (microtime $flag) will be $expected [$_dataName]
|
||||
*
|
||||
* @param int|float $input
|
||||
* @param bool $flag
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testTimeStringFormat($input, bool $flag, string $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::timeStringFormat($input, $flag)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::stringToTime
|
||||
* @dataProvider reverseIntervalProvider
|
||||
* @testdox stringToTime $input will be $expected [$_dataName]
|
||||
*
|
||||
* @param string|int|float $input
|
||||
* @param string|int|float $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testStringToTime($input, $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::stringToTime($input)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::checkDate
|
||||
* @dataProvider dateProvider
|
||||
* @testdox checkDate $input will be $expected [$_dataName]
|
||||
*
|
||||
* @param string $input
|
||||
* @param bool $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testCheckDate(string $input, bool $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::checkDate($input)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::checkDateTime
|
||||
* @dataProvider dateTimeProvider
|
||||
* @testdox checkDateTime $input will be $expected [$_dataName]
|
||||
*
|
||||
* @param string $input
|
||||
* @param bool $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testCheckDateTime(string $input, bool $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::checkDateTime($input)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::compareDate
|
||||
* @dataProvider dateCompareProvider
|
||||
* @testdox compareDate $input_a compared to $input_b will be $expected [$_dataName]
|
||||
*
|
||||
* @param string $input_a
|
||||
* @param string $input_b
|
||||
* @param int|bool $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testCompareDate(string $input_a, string $input_b, $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::compareDate($input_a, $input_b)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::compareDateTime
|
||||
* @dataProvider dateTimeCompareProvider
|
||||
* @testdox compareDateTime $input_a compared to $input_b will be $expected [$_dataName]
|
||||
*
|
||||
* @param string $input_a
|
||||
* @param string $input_b
|
||||
* @param int|bool $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testCompareDateTime(string $input_a, string $input_b, $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::compareDateTime($input_a, $input_b)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -780,6 +1149,70 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||
$output
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function dateRangeHasWeekendProvider(): array
|
||||
{
|
||||
return [
|
||||
'no weekend' => [
|
||||
'2023-07-03',
|
||||
'2023-07-04',
|
||||
false
|
||||
],
|
||||
'start weekend sat' => [
|
||||
'2023-07-01',
|
||||
'2023-07-04',
|
||||
true
|
||||
],
|
||||
'start weekend sun' => [
|
||||
'2023-07-02',
|
||||
'2023-07-04',
|
||||
true
|
||||
],
|
||||
'end weekend sat' => [
|
||||
'2023-07-03',
|
||||
'2023-07-08',
|
||||
true
|
||||
],
|
||||
'end weekend sun' => [
|
||||
'2023-07-03',
|
||||
'2023-07-09',
|
||||
true
|
||||
],
|
||||
'long period > 6 days' => [
|
||||
'2023-07-03',
|
||||
'2023-07-27',
|
||||
true
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::dateRangeHasWeekend
|
||||
* @dataProvider dateRangeHasWeekendProvider
|
||||
* @testdox dateRangeHasWeekend $start_date and $end_date are expected weekend $expected [$_dataName]
|
||||
*
|
||||
* @param string $start_date
|
||||
* @param string $end_date
|
||||
* @param bool $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testDateRangeHasWeekend(
|
||||
string $start_date,
|
||||
string $end_date,
|
||||
bool $expected
|
||||
): void {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Combined\DateTime::dateRangeHasWeekend($start_date, $end_date)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -253,7 +253,8 @@ final class CoreLibsConvertByteTest extends TestCase
|
||||
*/
|
||||
public function testHumanReadableByteFormatException(int $flag): void
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionCode(1);
|
||||
\CoreLibs\Convert\Byte::humanReadableByteFormat(12, $flag);
|
||||
}
|
||||
|
||||
@@ -272,7 +273,8 @@ final class CoreLibsConvertByteTest extends TestCase
|
||||
*/
|
||||
public function testStringByteFormatException(int $flag): void
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionCode(1);
|
||||
\CoreLibs\Convert\Byte::stringByteFormat(12, $flag);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,27 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
3 => false,
|
||||
4 => false
|
||||
],
|
||||
'invalid color red ' => [
|
||||
0 => -12,
|
||||
1 => 12,
|
||||
2 => 12,
|
||||
3 => false,
|
||||
4 => false
|
||||
],
|
||||
'invalid color green ' => [
|
||||
0 => 12,
|
||||
1 => -12,
|
||||
2 => 12,
|
||||
3 => false,
|
||||
4 => false
|
||||
],
|
||||
'invalid color blue ' => [
|
||||
0 => 12,
|
||||
1 => 12,
|
||||
2 => -12,
|
||||
3 => false,
|
||||
4 => false
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -150,10 +171,40 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
'valid' => true,
|
||||
],
|
||||
// invalid values
|
||||
'invalid color' => [
|
||||
'rgb' => [-12, 300, 12],
|
||||
'hsb' => [-12, 300, 12],
|
||||
'hsl' => [-12, 300, 12],
|
||||
'invalid color r/h/h low' => [
|
||||
'rgb' => [-1, 12, 12],
|
||||
'hsb' => [-1, 50, 50],
|
||||
'hsl' => [-1, 50, 50],
|
||||
'valid' => false,
|
||||
],
|
||||
'invalid color r/h/h high' => [
|
||||
'rgb' => [256, 12, 12],
|
||||
'hsb' => [361, 50, 50],
|
||||
'hsl' => [361, 50, 50],
|
||||
'valid' => false,
|
||||
],
|
||||
'invalid color g/s/s low' => [
|
||||
'rgb' => [12, -1, 12],
|
||||
'hsb' => [1, -1, 50],
|
||||
'hsl' => [1, -1, 50],
|
||||
'valid' => false,
|
||||
],
|
||||
'invalid color g/s/s high' => [
|
||||
'rgb' => [12, 256, 12],
|
||||
'hsb' => [1, 101, 50],
|
||||
'hsl' => [1, 101, 50],
|
||||
'valid' => false,
|
||||
],
|
||||
'invalid color b/b/l low' => [
|
||||
'rgb' => [12, 12, -1],
|
||||
'hsb' => [1, 50, -1],
|
||||
'hsl' => [1, 50, -1],
|
||||
'valid' => false,
|
||||
],
|
||||
'invalid color b/b/l high' => [
|
||||
'rgb' => [12, 12, 256],
|
||||
'hsb' => [1, 50, 101],
|
||||
'hsl' => [1, 50, 101],
|
||||
'valid' => false,
|
||||
],
|
||||
];
|
||||
@@ -246,11 +297,22 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
* @param int $input_r
|
||||
* @param int $input_g
|
||||
* @param int $input_b
|
||||
* @param string|bool $expected_hash
|
||||
* @param string|bool $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testRgb2hex(int $input_r, int $input_g, int $input_b, $expected_hash, $expected)
|
||||
{
|
||||
public function testRgb2hex(
|
||||
int $input_r,
|
||||
int $input_g,
|
||||
int $input_b,
|
||||
string|bool $expected_hash,
|
||||
string|bool $expected
|
||||
) {
|
||||
// if expected hash is or expected is false, we need to check for
|
||||
// LengthException
|
||||
if ($expected_hash === false || $expected === false) {
|
||||
$this->expectException(\LengthException::class);
|
||||
}
|
||||
// with #
|
||||
$this->assertEquals(
|
||||
$expected_hash,
|
||||
@@ -292,11 +354,19 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
*/
|
||||
public function testHex2rgb(
|
||||
string $input,
|
||||
$expected,
|
||||
$expected_str,
|
||||
array|bool $expected,
|
||||
string|bool $expected_str,
|
||||
string $separator,
|
||||
$expected_str_sep
|
||||
string|bool $expected_str_sep
|
||||
): void {
|
||||
if ($expected === false || $expected_str === false || $expected_str_sep === false) {
|
||||
$hex_string = preg_replace("/[^0-9A-Fa-f]/", '', $input);
|
||||
if (!is_string($hex_string)) {
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
} else {
|
||||
$this->expectException(\UnexpectedValueException::class);
|
||||
}
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Convert\Colors::hex2rgb($input)
|
||||
@@ -324,8 +394,11 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
* @param array|bool $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testRgb2hsb(int $input_r, int $input_g, int $input_b, $expected): void
|
||||
public function testRgb2hsb(int $input_r, int $input_g, int $input_b, array|bool $expected): void
|
||||
{
|
||||
if ($expected === false) {
|
||||
$this->expectException(\LengthException::class);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Convert\Colors::rgb2hsb($input_r, $input_g, $input_b)
|
||||
@@ -345,8 +418,12 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
* @param array|bool $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testHsb2rgb(float $input_h, float $input_s, float $input_b, $expected): void
|
||||
public function testHsb2rgb(float $input_h, float $input_s, float $input_b, array|bool $expected): void
|
||||
{
|
||||
if ($expected === false) {
|
||||
$this->expectException(\LengthException::class);
|
||||
$expected = [];
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Convert\Colors::hsb2rgb($input_h, $input_s, $input_b)
|
||||
@@ -366,8 +443,11 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
* @param array|bool $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testRgb2hsl(int $input_r, int $input_g, int $input_b, $expected): void
|
||||
public function testRgb2hsl(int $input_r, int $input_g, int $input_b, array|bool $expected): void
|
||||
{
|
||||
if ($expected === false) {
|
||||
$this->expectException(\LengthException::class);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Convert\Colors::rgb2hsl($input_r, $input_g, $input_b)
|
||||
@@ -387,8 +467,11 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
* @param array|bool $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testHsl2rgb($input_h, float $input_s, float $input_l, $expected): void
|
||||
public function testHsl2rgb(int|float $input_h, float $input_s, float $input_l, array|bool $expected): void
|
||||
{
|
||||
if ($expected === false) {
|
||||
$this->expectException(\LengthException::class);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Convert\Colors::hsl2rgb($input_h, $input_s, $input_l)
|
||||
@@ -406,11 +489,11 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
*/
|
||||
public function testHslHsb360hue(): void
|
||||
{
|
||||
$this->assertNotFalse(
|
||||
$this->assertIsArray(
|
||||
\CoreLibs\Convert\Colors::hsl2rgb(360.0, 90.5, 41.2),
|
||||
'HSL to RGB with 360 hue'
|
||||
);
|
||||
$this->assertNotFalse(
|
||||
$this->assertIsArray(
|
||||
\CoreLibs\Convert\Colors::hsb2rgb(360, 95, 78.0),
|
||||
'HSB to RGB with 360 hue'
|
||||
);
|
||||
|
||||
@@ -16,7 +16,7 @@ final class CoreLibsConvertJsonTest extends TestCase
|
||||
/**
|
||||
* test list for json convert tests
|
||||
*
|
||||
* @return array
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function jsonProvider(): array
|
||||
{
|
||||
@@ -54,10 +54,36 @@ final class CoreLibsConvertJsonTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function jsonArrayProvider(): array
|
||||
{
|
||||
return [
|
||||
'valid json' => [
|
||||
[
|
||||
'm' => 2,
|
||||
'f' => 'sub_2'
|
||||
],
|
||||
'{"m":2,"f":"sub_2"}',
|
||||
],
|
||||
'empty json array' => [
|
||||
[],
|
||||
'[]'
|
||||
],
|
||||
'empty json hash' => [
|
||||
['' => ''],
|
||||
'{"":""}'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* json error list
|
||||
*
|
||||
* @return array JSON error list
|
||||
* @return array<mixed> JSON error list
|
||||
*/
|
||||
public function jsonErrorProvider(): array
|
||||
{
|
||||
@@ -127,7 +153,7 @@ final class CoreLibsConvertJsonTest extends TestCase
|
||||
*
|
||||
* @param string|null $input
|
||||
* @param bool $flag
|
||||
* @param array $expected
|
||||
* @param array<mixed> $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testJsonConvertToArray(?string $input, bool $flag, array $expected): void
|
||||
@@ -146,7 +172,8 @@ final class CoreLibsConvertJsonTest extends TestCase
|
||||
* @testdox jsonGetLastError $input will be $expected_i/$expected_s [$_dataName]
|
||||
*
|
||||
* @param string|null $input
|
||||
* @param string $expected
|
||||
* @param int $expected_i
|
||||
* @param string $expected_s
|
||||
* @return void
|
||||
*/
|
||||
public function testJsonGetLastError(?string $input, int $expected_i, string $expected_s): void
|
||||
@@ -161,6 +188,25 @@ final class CoreLibsConvertJsonTest extends TestCase
|
||||
\CoreLibs\Convert\Json::jsonGetLastError(true)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::jsonConvertArrayTo
|
||||
* @dataProvider jsonArrayProvider
|
||||
* @testdox jsonConvertArrayTo $input (Override: $flag) will be $expected [$_dataName]
|
||||
*
|
||||
* @param array<mixed> $input
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testJsonConvertArrayto(array $input, string $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Convert\Json::jsonConvertArrayTo($input)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -43,12 +43,17 @@ final class CoreLibsConvertSetVarTypeNullTest extends TestCase
|
||||
'int, no override' => [
|
||||
1,
|
||||
null,
|
||||
null
|
||||
'1'
|
||||
],
|
||||
'int, override set' => [
|
||||
1,
|
||||
'not int',
|
||||
'not int'
|
||||
'1'
|
||||
],
|
||||
'array, override set' => [
|
||||
[1, 2],
|
||||
null,
|
||||
null
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -201,7 +206,7 @@ final class CoreLibsConvertSetVarTypeNullTest extends TestCase
|
||||
'float' => [
|
||||
1.5,
|
||||
null,
|
||||
null
|
||||
1
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -349,7 +354,7 @@ final class CoreLibsConvertSetVarTypeNullTest extends TestCase
|
||||
'int' => [
|
||||
1,
|
||||
null,
|
||||
null
|
||||
1.0
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
@@ -43,11 +43,16 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase
|
||||
'int, no override' => [
|
||||
1,
|
||||
null,
|
||||
''
|
||||
'1'
|
||||
],
|
||||
'int, override set' => [
|
||||
1,
|
||||
'not int',
|
||||
'1'
|
||||
],
|
||||
'array, override set' => [
|
||||
[1, 2],
|
||||
'not int',
|
||||
'not int'
|
||||
]
|
||||
];
|
||||
@@ -189,7 +194,7 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase
|
||||
'float' => [
|
||||
1.5,
|
||||
null,
|
||||
0
|
||||
1
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -330,7 +335,7 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase
|
||||
'int' => [
|
||||
1,
|
||||
null,
|
||||
0.0
|
||||
1.0
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -341,7 +346,7 @@ final class CoreLibsConvertSetVarTypeTest extends TestCase
|
||||
* @dataProvider varSetTypeFloatProvider
|
||||
* @testdox setFloat $input with override $default will be $expected [$_dataName]
|
||||
*
|
||||
* @param mixed $input
|
||||
* @param mixed $input
|
||||
* @param float|null $default
|
||||
* @param float $expected
|
||||
* @return void
|
||||
|
||||
@@ -256,6 +256,80 @@ final class CoreLibsConvertStringsTest extends TestCase
|
||||
$output
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* provider for testStripMultiplePathSlashes
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function stripMultiplePathSlashesProvider(): array
|
||||
{
|
||||
return [
|
||||
'no slahses' => [
|
||||
'input' => 'string_abc',
|
||||
'expected' => 'string_abc',
|
||||
],
|
||||
'one slash' => [
|
||||
'input' => 'some/foo',
|
||||
'expected' => 'some/foo',
|
||||
],
|
||||
'two slashes' => [
|
||||
'input' => 'some//foo',
|
||||
'expected' => 'some/foo',
|
||||
],
|
||||
'three slashes' => [
|
||||
'input' => 'some///foo',
|
||||
'expected' => 'some/foo',
|
||||
],
|
||||
'slashes in front' => [
|
||||
'input' => '/foo',
|
||||
'expected' => '/foo',
|
||||
],
|
||||
'two slashes in front' => [
|
||||
'input' => '//foo',
|
||||
'expected' => '/foo',
|
||||
],
|
||||
'thee slashes in front' => [
|
||||
'input' => '///foo',
|
||||
'expected' => '/foo',
|
||||
],
|
||||
'slashes in back' => [
|
||||
'input' => 'foo/',
|
||||
'expected' => 'foo/',
|
||||
],
|
||||
'two slashes in back' => [
|
||||
'input' => 'foo//',
|
||||
'expected' => 'foo/',
|
||||
],
|
||||
'thee slashes in back' => [
|
||||
'input' => 'foo///',
|
||||
'expected' => 'foo/',
|
||||
],
|
||||
'multiple slashes' => [
|
||||
'input' => '/foo//bar///string/end_times',
|
||||
'expected' => '/foo/bar/string/end_times',
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* test multiple slashes clean up
|
||||
*
|
||||
* @covers ::stripMultiplePathSlashes
|
||||
* @dataProvider stripMultiplePathSlashesProvider
|
||||
* @testdox stripMultiplePathSlashes $input will be $expected [$_dataName]
|
||||
*
|
||||
* @param string $input
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testStripMultiplePathSlashes(string $input, string $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Convert\Strings::stripMultiplePathSlashes($input)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -22,12 +22,9 @@ final class CoreLibsCreateEmailTest extends TestCase
|
||||
*/
|
||||
public static function setUpBeforeClass(): void
|
||||
{
|
||||
self::$log = new \CoreLibs\Debug\Logging([
|
||||
self::$log = new \CoreLibs\Logging\Logging([
|
||||
'log_folder' => DIRECTORY_SEPARATOR . 'tmp',
|
||||
'file_id' => 'CoreLibs-Create-Email-Test',
|
||||
'debug_all' => true,
|
||||
'echo_all' => false,
|
||||
'print_all' => true,
|
||||
'log_file_id' => 'CoreLibs-Create-Email-Test',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -624,7 +621,7 @@ final class CoreLibsCreateEmailTest extends TestCase
|
||||
// force new set for each run
|
||||
self::$log->setLogUniqueId(true);
|
||||
// set on of unique log id
|
||||
self::$log->setLogPer('run', true);
|
||||
self::$log->setLogFlag(\CoreLibs\Logging\Logger\Flag::per_run);
|
||||
// init logger
|
||||
$status = \CoreLibs\Create\Email::sendEmail(
|
||||
$subject,
|
||||
@@ -646,7 +643,9 @@ final class CoreLibsCreateEmailTest extends TestCase
|
||||
// assert content: must load JSON from log file
|
||||
if ($status == 2) {
|
||||
// open file, get last entry with 'SEND EMAIL JSON' key
|
||||
$file = file_get_contents(self::$log->getLogFileName());
|
||||
$file = file_get_contents(
|
||||
self::$log->getLogFolder() . self::$log->getLogFile()
|
||||
);
|
||||
if ($file !== false) {
|
||||
// extract SEND EMAIL JSON line
|
||||
$found = preg_match_all("/^.* <SEND EMAIL JSON> - (.*)$/m", $file, $matches);
|
||||
|
||||
@@ -30,8 +30,10 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
// setSessionName: true/false,
|
||||
// checkActiveSession: true/false, [1st call, 2nd call]
|
||||
// getSessionId: string or false
|
||||
// 3: exepcted name (session)
|
||||
// 4: expected error string
|
||||
// 3: exepcted name (session)]
|
||||
// 4: Exception thrown on error
|
||||
// 5: exception code, null for none
|
||||
// 6: expected error string
|
||||
return [
|
||||
'session parameter' => [
|
||||
'sessionNameParameter',
|
||||
@@ -44,7 +46,9 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
'getSessionId' => '1234abcd4567'
|
||||
],
|
||||
'sessionNameParameter',
|
||||
''
|
||||
null,
|
||||
null,
|
||||
'',
|
||||
],
|
||||
'session globals' => [
|
||||
'sessionNameGlobals',
|
||||
@@ -57,7 +61,9 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
'getSessionId' => '1234abcd4567'
|
||||
],
|
||||
'sessionNameGlobals',
|
||||
''
|
||||
null,
|
||||
null,
|
||||
'',
|
||||
],
|
||||
'session name default' => [
|
||||
'',
|
||||
@@ -70,7 +76,9 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
'getSessionId' => '1234abcd4567'
|
||||
],
|
||||
'',
|
||||
''
|
||||
null,
|
||||
null,
|
||||
'',
|
||||
],
|
||||
// error checks
|
||||
// 1: we are in cli
|
||||
@@ -85,6 +93,8 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
'getSessionId' => '1234abcd4567'
|
||||
],
|
||||
'',
|
||||
'RuntimeException',
|
||||
1,
|
||||
'[SESSION] No sessions in php cli'
|
||||
],
|
||||
// 2: session disabled
|
||||
@@ -99,6 +109,8 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
'getSessionId' => '1234abcd4567'
|
||||
],
|
||||
'',
|
||||
'RuntimeException',
|
||||
2,
|
||||
'[SESSION] Sessions are disabled'
|
||||
],
|
||||
// 3: invalid session name: string
|
||||
@@ -113,6 +125,8 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
'getSessionId' => '1234abcd4567'
|
||||
],
|
||||
'',
|
||||
'UnexpectedValueException',
|
||||
3,
|
||||
'[SESSION] Invalid session name: 1invalid$session#;'
|
||||
],
|
||||
// 3: invalid session name: only numbers
|
||||
@@ -127,6 +141,8 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
'getSessionId' => '1234abcd4567'
|
||||
],
|
||||
'',
|
||||
'UnexpectedValueException',
|
||||
3,
|
||||
'[SESSION] Invalid session name: 123'
|
||||
],
|
||||
// 3: invalid session name: invalid name short
|
||||
@@ -143,6 +159,8 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
'getSessionId' => '1234abcd4567'
|
||||
],
|
||||
'',
|
||||
'RuntimeException',
|
||||
4,
|
||||
'[SESSION] Failed to activate session'
|
||||
],
|
||||
// 5: get session id return false
|
||||
@@ -157,6 +175,8 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
'getSessionId' => false
|
||||
],
|
||||
'',
|
||||
'UnexpectedValueException',
|
||||
5,
|
||||
'[SESSION] getSessionId did not return a session id'
|
||||
],
|
||||
];
|
||||
@@ -173,6 +193,7 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
* @param string $type
|
||||
* @param array<mixed> $mock_data
|
||||
* @param string $expected
|
||||
* @param string|null $exception
|
||||
* @param string $expected_error
|
||||
* @return void
|
||||
*/
|
||||
@@ -181,6 +202,8 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
string $type,
|
||||
array $mock_data,
|
||||
string $expected,
|
||||
?string $exception,
|
||||
?int $exception_code,
|
||||
string $expected_error
|
||||
): void {
|
||||
// override expected
|
||||
@@ -224,6 +247,11 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
// regex for session id
|
||||
$ression_id_regex = "/^\w+$/";
|
||||
|
||||
if ($exception !== null) {
|
||||
$this->expectException($exception);
|
||||
$this->expectExceptionCode($exception_code);
|
||||
}
|
||||
|
||||
unset($GLOBALS['SET_SESSION_NAME']);
|
||||
$session_id = '';
|
||||
switch ($type) {
|
||||
@@ -253,13 +281,6 @@ final class CoreLibsCreateSessionTest extends TestCase
|
||||
$expected,
|
||||
$session_mock->getSessionName()
|
||||
);
|
||||
} else {
|
||||
// false checks
|
||||
$this->assertEquals(
|
||||
$expected_error,
|
||||
$session_mock->getErrorStr(),
|
||||
'error assert'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,45 +21,83 @@ final class CoreLibsCreateUidsTest extends TestCase
|
||||
public function uniqIdProvider(): array
|
||||
{
|
||||
return [
|
||||
// number length
|
||||
'too short' => [
|
||||
0 => 1,
|
||||
1 => 4,
|
||||
2 => null
|
||||
],
|
||||
'valid length: 10' => [
|
||||
0 => 10,
|
||||
1 => 10,
|
||||
2 => null
|
||||
],
|
||||
'valid length: 9, auto length' => [
|
||||
0 => 9,
|
||||
1 => 8,
|
||||
2 => null
|
||||
],
|
||||
'valid length: 9, force length' => [
|
||||
0 => 9,
|
||||
1 => 9,
|
||||
2 => true,
|
||||
],
|
||||
'very long: 512' => [
|
||||
0 => 512,
|
||||
1 => 512,
|
||||
2 => null
|
||||
],
|
||||
// below is all legacy
|
||||
'md5 hash' => [
|
||||
0 => 'md5',
|
||||
1 => 32,
|
||||
2 => null
|
||||
],
|
||||
'sha256 hash' => [
|
||||
0 => 'sha256',
|
||||
1 => 64
|
||||
1 => 64,
|
||||
2 => null
|
||||
],
|
||||
'ripemd160 hash' => [
|
||||
0 => 'ripemd160',
|
||||
1 => 40
|
||||
1 => 40,
|
||||
2 => null
|
||||
],
|
||||
'adler32 hash' => [
|
||||
0 => 'adler32',
|
||||
1 => 8
|
||||
1 => 8,
|
||||
2 => null
|
||||
],
|
||||
'not in list hash but valid' => [
|
||||
'not in list, set default length' => [
|
||||
0 => 'sha3-512',
|
||||
1 => strlen(hash('sha3-512', 'A'))
|
||||
1 => 64,
|
||||
2 => null
|
||||
],
|
||||
'default hash not set' => [
|
||||
0 => null,
|
||||
1 => 64,
|
||||
2 => null
|
||||
],
|
||||
'invalid name' => [
|
||||
0 => 'iamnotavalidhash',
|
||||
1 => 64,
|
||||
2 => null
|
||||
],
|
||||
'auto: ' . \CoreLibs\Create\Uids::DEFAULT_HASH => [
|
||||
0 => \CoreLibs\Create\Uids::DEFAULT_HASH,
|
||||
1 => strlen(hash(\CoreLibs\Create\Uids::DEFAULT_HASH, 'A'))
|
||||
// auto calls
|
||||
'auto: ' . \CoreLibs\Create\Uids::DEFAULT_UNNIQ_ID_LENGTH => [
|
||||
0 => \CoreLibs\Create\Uids::DEFAULT_UNNIQ_ID_LENGTH,
|
||||
1 => 64,
|
||||
2 => null
|
||||
],
|
||||
'auto: ' . \CoreLibs\Create\Uids::STANDARD_HASH_LONG => [
|
||||
0 => \CoreLibs\Create\Uids::STANDARD_HASH_LONG,
|
||||
1 => strlen(hash(\CoreLibs\Create\Uids::STANDARD_HASH_LONG, 'A'))
|
||||
1 => strlen(hash(\CoreLibs\Create\Uids::STANDARD_HASH_LONG, 'A')),
|
||||
2 => null
|
||||
],
|
||||
'auto: ' . \CoreLibs\Create\Uids::STANDARD_HASH_SHORT => [
|
||||
0 => \CoreLibs\Create\Uids::STANDARD_HASH_SHORT,
|
||||
1 => strlen(hash(\CoreLibs\Create\Uids::STANDARD_HASH_SHORT, 'A'))
|
||||
1 => strlen(hash(\CoreLibs\Create\Uids::STANDARD_HASH_SHORT, 'A')),
|
||||
2 => null
|
||||
],
|
||||
];
|
||||
}
|
||||
@@ -105,25 +143,26 @@ final class CoreLibsCreateUidsTest extends TestCase
|
||||
*
|
||||
* @covers ::uniqId
|
||||
* @dataProvider uniqIdProvider
|
||||
* @testdox uniqId $input will be length $expected [$_dataName]
|
||||
* @testdox uniqId $input will be length $expected (Force $flag) [$_dataName]
|
||||
*
|
||||
* @param string|null $input
|
||||
* @param int|string|null $input
|
||||
* @param string $expected
|
||||
* @param bool|null $flag
|
||||
* @return void
|
||||
*/
|
||||
public function testUniqId(?string $input, int $expected): void
|
||||
public function testUniqId(int|string|null $input, int $expected, ?bool $flag): void
|
||||
{
|
||||
if ($input === null) {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
strlen(\CoreLibs\Create\Uids::uniqId())
|
||||
);
|
||||
$uniq_id_length = strlen(\CoreLibs\Create\Uids::uniqId());
|
||||
} elseif ($flag === null) {
|
||||
$uniq_id_length = strlen(\CoreLibs\Create\Uids::uniqId($input));
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
strlen(\CoreLibs\Create\Uids::uniqId($input))
|
||||
);
|
||||
$uniq_id_length = strlen(\CoreLibs\Create\Uids::uniqId($input, $flag));
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$uniq_id_length
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -10,12 +10,13 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test class for Debug\Logging
|
||||
* @coversDefaultClass \CoreLibs\Debug\Logging
|
||||
* @testdox \CoreLibs\Debug\Logging method tests
|
||||
* @coversDefaultClass \CoreLibs\Debug\LoggingLegacy
|
||||
* @testdox \CoreLibs\Debug\LoggingLegacy method tests
|
||||
*/
|
||||
final class CoreLibsDebugLoggingTest extends TestCase
|
||||
final class CoreLibsDebugLoggingLegacyTest extends TestCase
|
||||
{
|
||||
private const LOG_FOLDER = __DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR;
|
||||
|
||||
/**
|
||||
* test set for options BASIC
|
||||
*
|
||||
@@ -24,7 +25,7 @@ final class CoreLibsDebugLoggingTest extends TestCase
|
||||
* 1: expected
|
||||
* 2: override
|
||||
* override:
|
||||
* - constant for COSNTANTS
|
||||
* - constant for CONSTANTS
|
||||
* - global for _GLOBALS
|
||||
*
|
||||
* @return array
|
||||
@@ -138,7 +139,7 @@ final class CoreLibsDebugLoggingTest extends TestCase
|
||||
// catch this with the message
|
||||
$this->expectExceptionMessage($deprecation_message);
|
||||
}
|
||||
$log = new \CoreLibs\Debug\Logging($options);
|
||||
$log = new \CoreLibs\Debug\LoggingLegacy($options);
|
||||
// reset error handler
|
||||
restore_error_handler();
|
||||
// check that settings match
|
||||
@@ -308,7 +309,7 @@ final class CoreLibsDebugLoggingTest extends TestCase
|
||||
// catch this with the message
|
||||
$this->expectExceptionMessage($deprecation_message);
|
||||
}
|
||||
$log = new \CoreLibs\Debug\Logging($options);
|
||||
$log = new \CoreLibs\Debug\LoggingLegacy($options);
|
||||
// reset error handler
|
||||
restore_error_handler();
|
||||
// check current
|
||||
@@ -385,7 +386,7 @@ final class CoreLibsDebugLoggingTest extends TestCase
|
||||
bool $expected_get
|
||||
): void {
|
||||
// neutral start with default
|
||||
$log = new \CoreLibs\Debug\Logging([
|
||||
$log = new \CoreLibs\Debug\LoggingLegacy([
|
||||
'file_id' => 'testSetGetLogLevelAll',
|
||||
'log_folder' => self::LOG_FOLDER
|
||||
]);
|
||||
@@ -510,7 +511,7 @@ final class CoreLibsDebugLoggingTest extends TestCase
|
||||
$expected_get
|
||||
): void {
|
||||
// neutral start with default
|
||||
$log = new \CoreLibs\Debug\Logging([
|
||||
$log = new \CoreLibs\Debug\LoggingLegacy([
|
||||
'file_id' => 'testSetGetLogLevel',
|
||||
'log_folder' => self::LOG_FOLDER
|
||||
]);
|
||||
@@ -592,7 +593,7 @@ final class CoreLibsDebugLoggingTest extends TestCase
|
||||
bool $expected_get
|
||||
): void {
|
||||
// neutral start with default
|
||||
$log = new \CoreLibs\Debug\Logging([
|
||||
$log = new \CoreLibs\Debug\LoggingLegacy([
|
||||
'file_id' => 'testSetGetLogPer',
|
||||
'log_folder' => self::LOG_FOLDER
|
||||
]);
|
||||
@@ -624,7 +625,7 @@ final class CoreLibsDebugLoggingTest extends TestCase
|
||||
public function testSetGetLogPrintFileDate(bool $input, bool $expected_set, bool $expected_get): void
|
||||
{
|
||||
// neutral start with default
|
||||
$log = new \CoreLibs\Debug\Logging([
|
||||
$log = new \CoreLibs\Debug\LoggingLegacy([
|
||||
'file_id' => 'testSetGetLogPrintFileDate',
|
||||
'log_folder' => self::LOG_FOLDER
|
||||
]);
|
||||
@@ -693,7 +694,7 @@ final class CoreLibsDebugLoggingTest extends TestCase
|
||||
*/
|
||||
public function testPrAr(array $input, string $expected): void
|
||||
{
|
||||
$log = new \CoreLibs\Debug\Logging([
|
||||
$log = new \CoreLibs\Debug\LoggingLegacy([
|
||||
'file_id' => 'testPrAr',
|
||||
'log_folder' => self::LOG_FOLDER
|
||||
]);
|
||||
@@ -757,7 +758,7 @@ final class CoreLibsDebugLoggingTest extends TestCase
|
||||
*/
|
||||
public function testPrBl(bool $input, ?string $true, ?string $false, string $expected): void
|
||||
{
|
||||
$log = new \CoreLibs\Debug\Logging([
|
||||
$log = new \CoreLibs\Debug\LoggingLegacy([
|
||||
'file_id' => 'testPrBl',
|
||||
'log_folder' => self::LOG_FOLDER
|
||||
]);
|
||||
@@ -932,7 +933,7 @@ final class CoreLibsDebugLoggingTest extends TestCase
|
||||
// remove any files named /tmp/error_log_TestDebug*.log
|
||||
array_map('unlink', glob($options['log_folder'] . 'error_msg_' . $options['file_id'] . '*.log'));
|
||||
// init logger
|
||||
$log = new \CoreLibs\Debug\Logging($options);
|
||||
$log = new \CoreLibs\Debug\LoggingLegacy($options);
|
||||
// * debug (A/B)
|
||||
// NULL check for strip/prefix
|
||||
$this->assertEquals(
|
||||
@@ -1046,13 +1047,13 @@ final class CoreLibsDebugLoggingTest extends TestCase
|
||||
public function testLogUniqueId(bool $option, bool $override): void
|
||||
{
|
||||
if ($option === true) {
|
||||
$log = new \CoreLibs\Debug\Logging([
|
||||
$log = new \CoreLibs\Debug\LoggingLegacy([
|
||||
'file_id' => 'testLogUniqueId',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'per_run' => $option
|
||||
]);
|
||||
} else {
|
||||
$log = new \CoreLibs\Debug\Logging([
|
||||
$log = new \CoreLibs\Debug\LoggingLegacy([
|
||||
'file_id' => 'testLogUniqueId',
|
||||
'log_folder' => self::LOG_FOLDER
|
||||
]);
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use CoreLibs\Debug\Support;
|
||||
|
||||
/**
|
||||
* Test class for Debug\Support
|
||||
@@ -40,6 +41,32 @@ final class CoreLibsDebugSupportTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::printTime
|
||||
* @dataProvider printTimeProvider
|
||||
* @testdox printTime test with $microtime and match to regex [$_dataName]
|
||||
*
|
||||
* @param int|null $mircrotime
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testPrintTime(?int $microtime, string $regex): void
|
||||
{
|
||||
if ($microtime === null) {
|
||||
$this->assertMatchesRegularExpression(
|
||||
$regex,
|
||||
Support::printTime()
|
||||
);
|
||||
} else {
|
||||
$this->assertMatchesRegularExpression(
|
||||
$regex,
|
||||
Support::printTime($microtime)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -50,18 +77,55 @@ final class CoreLibsDebugSupportTest extends TestCase
|
||||
return [
|
||||
'empty array' => [
|
||||
0 => [],
|
||||
1 => "<pre>Array\n(\n)\n</pre>"
|
||||
1 => "<pre>Array\n(\n)\n</pre>",
|
||||
2 => "Array\n(\n)\n",
|
||||
],
|
||||
'simple array' => [
|
||||
0 => ['a', 'b'],
|
||||
1 => "<pre>Array\n(\n"
|
||||
. " [0] => a\n"
|
||||
. " [1] => b\n"
|
||||
. ")\n</pre>"
|
||||
. ")\n</pre>",
|
||||
2 => "Array\n(\n"
|
||||
. " [0] => a\n"
|
||||
. " [1] => b\n"
|
||||
. ")\n",
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::printAr
|
||||
* @cover ::printArray
|
||||
* @dataProvider printArrayProvider
|
||||
* @testdox printAr/printArray $input will be $expected [$_dataName]
|
||||
*
|
||||
* @param array $input
|
||||
* @param string $expected
|
||||
* @param string $expected_strip
|
||||
* @return void
|
||||
*/
|
||||
public function testPrintAr(array $input, string $expected, string $expected_strip): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
Support::printAr($input),
|
||||
'assert printAr'
|
||||
);
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
Support::printArray($input),
|
||||
'assert printArray'
|
||||
);
|
||||
$this->assertEquals(
|
||||
$expected_strip,
|
||||
Support::prAr($input),
|
||||
'assert prAr'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -73,27 +137,31 @@ final class CoreLibsDebugSupportTest extends TestCase
|
||||
'true input default' => [
|
||||
0 => true,
|
||||
1 => [],
|
||||
2 => 'true'
|
||||
2 => 'true',
|
||||
3 => 'true',
|
||||
],
|
||||
'false input default' => [
|
||||
0 => false,
|
||||
1 => [],
|
||||
2 => 'false'
|
||||
2 => 'false',
|
||||
3 => 'false'
|
||||
],
|
||||
'false input param name' => [
|
||||
0 => false,
|
||||
1 => [
|
||||
'name' => 'param test'
|
||||
],
|
||||
2 => '<b>param test</b>: false'
|
||||
2 => '<b>param test</b>: false',
|
||||
3 => 'false'
|
||||
],
|
||||
'true input param name, true override' => [
|
||||
0 => true,
|
||||
1 => [
|
||||
'name' => 'param test',
|
||||
'true' => 'ok'
|
||||
'true' => 'ok',
|
||||
],
|
||||
2 => '<b>param test</b>: ok'
|
||||
2 => '<b>param test</b>: ok',
|
||||
3 => 'ok',
|
||||
],
|
||||
'false input param name, true override, false override' => [
|
||||
0 => false,
|
||||
@@ -102,11 +170,77 @@ final class CoreLibsDebugSupportTest extends TestCase
|
||||
'true' => 'ok',
|
||||
'false' => 'not',
|
||||
],
|
||||
2 => '<b>param test</b>: not'
|
||||
2 => '<b>param test</b>: not',
|
||||
3 => 'not'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::printBool
|
||||
* @dataProvider printBoolProvider
|
||||
* @testdox printBool $input will be $expected [$_dataName]
|
||||
*
|
||||
* @param bool $input
|
||||
* @param array $params
|
||||
* @param string $expected
|
||||
* @param string $expected_strip
|
||||
* @return void
|
||||
*/
|
||||
public function testPrintBool(bool $input, array $params, string $expected, string $expected_strip): void
|
||||
{
|
||||
if (
|
||||
isset($params['name']) &&
|
||||
isset($params['true']) &&
|
||||
isset($params['false'])
|
||||
) {
|
||||
$string = Support::printBool(
|
||||
$input,
|
||||
$params['name'],
|
||||
$params['true'],
|
||||
$params['false']
|
||||
);
|
||||
$string_strip = Support::prBl(
|
||||
$input,
|
||||
$params['true'],
|
||||
$params['false']
|
||||
);
|
||||
} elseif (isset($params['name']) && isset($params['true'])) {
|
||||
$string = Support::printBool(
|
||||
$input,
|
||||
$params['name'],
|
||||
$params['true']
|
||||
);
|
||||
$string_strip = Support::prBl(
|
||||
$input,
|
||||
$params['true'],
|
||||
);
|
||||
} elseif (isset($params['name'])) {
|
||||
$string = Support::printBool(
|
||||
$input,
|
||||
$params['name']
|
||||
);
|
||||
$string_strip = Support::prBl(
|
||||
$input
|
||||
);
|
||||
} else {
|
||||
$string = Support::printBool($input);
|
||||
$string_strip = Support::prBl($input);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$string,
|
||||
'assert printBool'
|
||||
);
|
||||
$this->assertEquals(
|
||||
$expected_strip,
|
||||
$string_strip,
|
||||
'assert prBl'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -169,12 +303,10 @@ final class CoreLibsDebugSupportTest extends TestCase
|
||||
'an array, no html' => [
|
||||
['a', 'b'],
|
||||
true,
|
||||
"##HTMLPRE##"
|
||||
. "Array\n(\n"
|
||||
"Array\n(\n"
|
||||
. " [0] => a\n"
|
||||
. " [1] => b\n"
|
||||
. ")\n"
|
||||
. "##/HTMLPRE##",
|
||||
. ")\n",
|
||||
],
|
||||
// resource
|
||||
'a resource' => [
|
||||
@@ -191,6 +323,330 @@ final class CoreLibsDebugSupportTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::printToString
|
||||
* @dataProvider printToStringProvider
|
||||
* @testdox printToString $input with $flag will be $expected [$_dataName]
|
||||
*
|
||||
* @param mixed $input anything
|
||||
* @param boolean|null $flag html flag, only for string and array
|
||||
* @param string $expected always string
|
||||
* @return void
|
||||
*/
|
||||
public function testPrintToString(mixed $input, ?bool $flag, string $expected): void
|
||||
{
|
||||
if ($flag === null) {
|
||||
// if expected starts with / and ends with / then this is a regex compare
|
||||
if (
|
||||
substr($expected, 0, 1) == '/' &&
|
||||
substr($expected, -1, 1) == '/'
|
||||
) {
|
||||
$this->assertMatchesRegularExpression(
|
||||
$expected,
|
||||
Support::printToString($input)
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
Support::printToString($input)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
Support::printToString($input, $flag)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerDumpExportVar(): array
|
||||
{
|
||||
return [
|
||||
'string' => [
|
||||
'input' => 'string',
|
||||
'flag' => null,
|
||||
'expected_dump' => 'string(6) "string"' . "\n",
|
||||
'expected_export' => "<pre>'string'</pre>",
|
||||
],
|
||||
'string, no html' => [
|
||||
'input' => 'string',
|
||||
'flag' => true,
|
||||
'expected_dump' => 'string(6) "string"' . "\n",
|
||||
'expected_export' => "'string'",
|
||||
],
|
||||
// int
|
||||
'int' => [
|
||||
'input' => 6,
|
||||
'flag' => null,
|
||||
'expected_dump' => 'int(6)' . "\n",
|
||||
'expected_export' => "<pre>6</pre>",
|
||||
],
|
||||
// float
|
||||
'float' => [
|
||||
'input' => 1.6,
|
||||
'flag' => null,
|
||||
'expected_dump' => 'float(1.6)' . "\n",
|
||||
'expected_export' => "<pre>1.6</pre>",
|
||||
],
|
||||
// bool
|
||||
'bool' => [
|
||||
'input' => true,
|
||||
'flag' => null,
|
||||
'expected_dump' => 'bool(true)' . "\n",
|
||||
'expected_export' => "<pre>true</pre>",
|
||||
],
|
||||
// array
|
||||
'array' => [
|
||||
'input' => ['string', true],
|
||||
'flag' => null,
|
||||
'expected_dump' => "array(2) {\n"
|
||||
. " [0]=>\n"
|
||||
. " string(6) \"string\"\n"
|
||||
. " [1]=>\n"
|
||||
. " bool(true)\n"
|
||||
. "}\n",
|
||||
'expected_export' => "<pre>array (\n"
|
||||
. " 0 => 'string',\n"
|
||||
. " 1 => true,\n"
|
||||
. ")</pre>",
|
||||
],
|
||||
// more
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::dumpVar
|
||||
* @cover ::exportVar
|
||||
* @dataProvider providerDumpExportVar
|
||||
* @testdox dump/exportVar $input with $flag will be $expected_dump / $expected_export [$_dataName]
|
||||
*
|
||||
* @param mixed $input
|
||||
* @param bool|null $flag
|
||||
* @param string $expected_dump
|
||||
* @param string $expected_export
|
||||
* @return void
|
||||
*/
|
||||
public function testDumpExportVar(mixed $input, ?bool $flag, string $expected_dump, string $expected_export): void
|
||||
{
|
||||
if ($flag === null) {
|
||||
$dump = Support::dumpVar($input);
|
||||
$export = Support::exportVar($input);
|
||||
} else {
|
||||
$dump = Support::dumpVar($input, $flag);
|
||||
$export = Support::exportVar($input, $flag);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected_dump,
|
||||
$dump,
|
||||
'assert dumpVar'
|
||||
);
|
||||
$this->assertEquals(
|
||||
$expected_export,
|
||||
$export,
|
||||
'assert dumpVar'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::getCallerFileLine
|
||||
* @testWith ["vendor/phpunit/phpunit/src/Framework/TestCase.php:6434","phar:///home/clemens/.phive/phars/phpunit-9.6.13.phar/phpunit/Framework/TestCase.php:6434"]
|
||||
* @testdox getCallerFileLine check based on regex .../Framework/TestCase.php:\d+ [$_dataName]
|
||||
*
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCallerFileLine(): void
|
||||
{
|
||||
// regex prefix with path "/../" and then fixed vendor + \d+
|
||||
// or phar start if phiev installed
|
||||
// phar:///home/clemens/.phive/phars/phpunit-9.6.13.phar/phpunit/Framework/TestCase.php
|
||||
$regex = "/^("
|
||||
. "\/.*\/vendor\/phpunit\/phpunit\/src"
|
||||
. "|"
|
||||
. "phar:\/\/\/.*\.phive\/phars\/phpunit-\d+\.\d+\.\d+\.phar\/phpunit"
|
||||
. ")"
|
||||
. "\/Framework\/TestCase.php:\d+$/";
|
||||
$this->assertMatchesRegularExpression(
|
||||
$regex,
|
||||
Support::getCallerFileLine()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::getCallerMethod
|
||||
* @testWith ["testGetCallerMethod"]
|
||||
* @testdox getCallerMethod check if it returns $expected [$_dataName]
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCallerMethod(string $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
Support::getCallerMethod()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::getCallerMethodList
|
||||
* @testWith [["main", "run", "run", "run", "run", "run", "run", "runBare", "runTest", "testGetCallerMethodList"]]
|
||||
* @testdox getCallerMethodList check if it returns $expected [$_dataName]
|
||||
*
|
||||
* @param array $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCallerMethodList(array $expected): void
|
||||
{
|
||||
$compare = Support::getCallerMethodList();
|
||||
// 10: legacy
|
||||
// 11: direct
|
||||
// 12: full call
|
||||
switch (count($compare)) {
|
||||
case 10:
|
||||
// add nothing
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$compare,
|
||||
'assert expected 10'
|
||||
);
|
||||
break;
|
||||
case 11:
|
||||
if ($compare[0] == 'include') {
|
||||
// add include at first
|
||||
array_splice(
|
||||
$expected,
|
||||
0,
|
||||
0,
|
||||
['include']
|
||||
);
|
||||
} else {
|
||||
array_splice(
|
||||
$expected,
|
||||
6,
|
||||
0,
|
||||
['run']
|
||||
);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$compare,
|
||||
'assert expected 11'
|
||||
);
|
||||
break;
|
||||
case 12:
|
||||
// add two "run" before "runBare"
|
||||
array_splice(
|
||||
$expected,
|
||||
7,
|
||||
0,
|
||||
['run']
|
||||
);
|
||||
array_splice(
|
||||
$expected,
|
||||
0,
|
||||
0,
|
||||
['include']
|
||||
);
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$compare,
|
||||
'assert expected 12'
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::getCallStack
|
||||
* @testdox getCallStack check if it returns data [$_dataName]
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCallStack(): void
|
||||
{
|
||||
$call_stack = Support::getCallStack();
|
||||
// print "Get CALL: " . print_r(Support::getCallStack(), true) . "\n";
|
||||
if ($call_stack < 8) {
|
||||
$this->assertFalse(true, 'getCallStack too low: 8');
|
||||
} else {
|
||||
$this->assertTrue(true, 'getCallSteck ok');
|
||||
}
|
||||
// just test top entry
|
||||
$first = array_shift($call_stack);
|
||||
$this->assertStringEndsWith(
|
||||
':tests\CoreLibsDebugSupportTest->testGetCallStack',
|
||||
$first,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* test the lowest one (one above base)
|
||||
*
|
||||
* @cover ::getCallerClass
|
||||
* @testWith ["tests\\CoreLibsDebugSupportTest"]
|
||||
* @testdox getCallerClass check if it returns $expected [$_dataName]
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCallerClass(string $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
Support::getCallerClass()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* test highest return (top level)
|
||||
*
|
||||
* @cover ::getCallerTopLevelClass
|
||||
* @testWith ["PHPUnit\\TextUI\\Command"]
|
||||
* @testdox getCallerTopLevelClass check if it returns $expected [$_dataName]
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCallerTopLevelClass(string $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
Support::getCallerTopLevelClass()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* test highest return (top level)
|
||||
*
|
||||
* @cover ::getCallerClassMethod
|
||||
* @testWith ["tests\\CoreLibsDebugSupportTest->testGetCallerClassMethod"]
|
||||
* @testdox getCallerClassMethod check if it returns $expected [$_dataName]
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCallerClassMethod(string $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
Support::getCallerClassMethod()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -236,205 +692,6 @@ final class CoreLibsDebugSupportTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::printTime
|
||||
* @dataProvider printTimeProvider
|
||||
* @testdox printTime test with $microtime and match to regex [$_dataName]
|
||||
*
|
||||
* @param int|null $mircrotime
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testPrintTime(?int $microtime, string $regex): void
|
||||
{
|
||||
if ($microtime === null) {
|
||||
$this->assertMatchesRegularExpression(
|
||||
$regex,
|
||||
\CoreLibs\Debug\Support::printTime()
|
||||
);
|
||||
} else {
|
||||
$this->assertMatchesRegularExpression(
|
||||
$regex,
|
||||
\CoreLibs\Debug\Support::printTime($microtime)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::printAr
|
||||
* @cover ::printArray
|
||||
* @dataProvider printArrayProvider
|
||||
* @testdox printAr/printArray $input will be $expected [$_dataName]
|
||||
*
|
||||
* @param array $input
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testPrintAr(array $input, string $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Debug\Support::printAr($input),
|
||||
'assert printAr'
|
||||
);
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Debug\Support::printArray($input),
|
||||
'assert printArray'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::printBool
|
||||
* @dataProvider printBoolProvider
|
||||
* @testdox printBool $input will be $expected [$_dataName]
|
||||
*
|
||||
* @param bool $input
|
||||
* @param array $params
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testPrintBool(bool $input, array $params, string $expected): void
|
||||
{
|
||||
if (
|
||||
isset($params['name']) &&
|
||||
isset($params['true']) &&
|
||||
isset($params['false'])
|
||||
) {
|
||||
$string = \CoreLibs\Debug\Support::printBool(
|
||||
$input,
|
||||
$params['name'],
|
||||
$params['true'],
|
||||
$params['false']
|
||||
);
|
||||
} elseif (isset($params['name']) && isset($params['true'])) {
|
||||
$string = \CoreLibs\Debug\Support::printBool(
|
||||
$input,
|
||||
$params['name'],
|
||||
$params['true']
|
||||
);
|
||||
} elseif (isset($params['name'])) {
|
||||
$string = \CoreLibs\Debug\Support::printBool(
|
||||
$input,
|
||||
$params['name']
|
||||
);
|
||||
} else {
|
||||
$string = \CoreLibs\Debug\Support::printBool($input);
|
||||
}
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$string,
|
||||
'assert printBool'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::printToString
|
||||
* @dataProvider printToStringProvider
|
||||
* @testdox printToString $input with $flag will be $expected [$_dataName]
|
||||
*
|
||||
* @param mixed $input anything
|
||||
* @param boolean|null $flag html flag, only for string and array
|
||||
* @param string $expected always string
|
||||
* @return void
|
||||
*/
|
||||
public function testPrintToString(mixed $input, ?bool $flag, string $expected): void
|
||||
{
|
||||
if ($flag === null) {
|
||||
// if expected starts with / and ends with / then this is a regex compare
|
||||
if (
|
||||
substr($expected, 0, 1) == '/' &&
|
||||
substr($expected, -1, 1) == '/'
|
||||
) {
|
||||
$this->assertMatchesRegularExpression(
|
||||
$expected,
|
||||
\CoreLibs\Debug\Support::printToString($input)
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Debug\Support::printToString($input)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Debug\Support::printToString($input, $flag)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::getCallerMethod
|
||||
* @testWith ["testGetCallerMethod"]
|
||||
* @testdox getCallerMethod check if it returns $expected [$_dataName]
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCallerMethod(string $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Debug\Support::getCallerMethod()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::getCallerMethodList
|
||||
* @testWith [["main", "run", "run", "run", "run", "run", "run", "runBare", "runTest", "testGetCallerMethodList"],["include", "main", "run", "run", "run", "run", "run", "run", "run", "runBare", "runTest", "testGetCallerMethodList"]]
|
||||
* @testdox getCallerMethodList check if it returns $expected [$_dataName]
|
||||
*
|
||||
* @param array $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCallerMethodList(array $expected, array $expected_group): void
|
||||
{
|
||||
$compare = \CoreLibs\Debug\Support::getCallerMethodList();
|
||||
// if we direct call we have 10, if we call as folder we get 11
|
||||
if (count($compare) == 10) {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Debug\Support::getCallerMethodList(),
|
||||
'assert expected 10'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
$expected_group,
|
||||
\CoreLibs\Debug\Support::getCallerMethodList(),
|
||||
'assert expected group'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @cover ::getCallerClass
|
||||
* @testWith ["PHPUnit\\TextUI\\Command"]
|
||||
* @testdox getCallerClass check if it returns $expected [$_dataName]
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCallerClass(string $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Debug\Support::getCallerClass()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -453,19 +710,19 @@ final class CoreLibsDebugSupportTest extends TestCase
|
||||
if ($replace === null && $flag === null) {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Debug\Support::debugString($input),
|
||||
Support::debugString($input),
|
||||
'assert all default'
|
||||
);
|
||||
} elseif ($flag === null) {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Debug\Support::debugString($input, $replace),
|
||||
Support::debugString($input, $replace),
|
||||
'assert flag default'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Debug\Support::debugString($input, $replace, $flag),
|
||||
Support::debugString($input, $replace, $flag),
|
||||
'assert all set'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -67,17 +67,17 @@ final class CoreLibsGetSystemTest extends TestCase
|
||||
'original set' => [
|
||||
0 => null,
|
||||
1 => 'NOHOST',
|
||||
2 => 'NOPORT',
|
||||
2 => 0,
|
||||
],
|
||||
'override set no port' => [
|
||||
0 => 'foo.org',
|
||||
1 => 'foo.org',
|
||||
2 => '80'
|
||||
2 => 80
|
||||
],
|
||||
'override set with port' => [
|
||||
0 => 'foo.org:443',
|
||||
1 => 'foo.org',
|
||||
2 => '443'
|
||||
2 => 443
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -99,7 +99,7 @@ final class CoreLibsGetSystemTest extends TestCase
|
||||
1 => 'phpunit',
|
||||
2 => 'phpunit',
|
||||
// NOTE: this can change, so it is a regex check
|
||||
3 => "/^(\/?.*\/?)?vendor\/bin\/phpunit$/",
|
||||
3 => "/^(\/?.*\/?)?(vendor\/bin|tools)\/phpunit$/",
|
||||
],
|
||||
'some path with extension' => [
|
||||
0 => '/some/path/to/file.txt',
|
||||
@@ -138,10 +138,10 @@ final class CoreLibsGetSystemTest extends TestCase
|
||||
*
|
||||
* @param string|null $input
|
||||
* @param string $expected_host
|
||||
* @param string $expected_port
|
||||
* @param int $expected_port
|
||||
* @return void
|
||||
*/
|
||||
public function testGetHostNanme(?string $input, string $expected_host, string $expected_port): void
|
||||
public function testGetHostNanme(?string $input, string $expected_host, int $expected_port): void
|
||||
{
|
||||
// print "HOSTNAME: " . $_SERVER['HTTP_HOST'] . "<br>";
|
||||
// print "SERVER: " . print_r($_SERVER, true) . "\n";
|
||||
|
||||
504
4dev/tests/Logging/CoreLibsLoggingErrorMessagesTest.php
Normal file
504
4dev/tests/Logging/CoreLibsLoggingErrorMessagesTest.php
Normal file
@@ -0,0 +1,504 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use CoreLibs\Logging\Logger\Level;
|
||||
|
||||
/**
|
||||
* Test class for Logging
|
||||
* @coversDefaultClass \CoreLibs\Logging\ErrorMessages
|
||||
* @testdox \CoreLibs\Logging\ErrorMEssages method tests
|
||||
*/
|
||||
final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
{
|
||||
private const LOG_FOLDER = __DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR;
|
||||
|
||||
/**
|
||||
* tear down and remove log data
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function tearDownAfterClass(): void
|
||||
{
|
||||
array_map('unlink', glob(self::LOG_FOLDER . '*.log'));
|
||||
}
|
||||
|
||||
/**
|
||||
* for checking level only
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerErrorMessageLevel(): array
|
||||
{
|
||||
return [
|
||||
'ok' => [
|
||||
'level' => 'ok',
|
||||
'str' => 'OK',
|
||||
'expected' => 'ok',
|
||||
],
|
||||
'info' => [
|
||||
'level' => 'info',
|
||||
'str' => 'INFO',
|
||||
'expected' => 'info',
|
||||
],
|
||||
'notice' => [
|
||||
'level' => 'notice',
|
||||
'str' => 'NOTICE',
|
||||
'expected' => 'notice',
|
||||
],
|
||||
'warn' => [
|
||||
'level' => 'warn',
|
||||
'str' => 'WARN',
|
||||
'expected' => 'warn'
|
||||
],
|
||||
'warning' => [
|
||||
'level' => 'warning',
|
||||
'str' => 'WARN',
|
||||
'expected' => 'warn'
|
||||
],
|
||||
'error' => [
|
||||
'level' => 'error',
|
||||
'str' => 'ERROR',
|
||||
'expected' => 'error'
|
||||
],
|
||||
'abort' => [
|
||||
'level' => 'abort',
|
||||
'str' => 'ABORT',
|
||||
'expected' => 'abort'
|
||||
],
|
||||
'crash' => [
|
||||
'level' => 'crash',
|
||||
'str' => 'CRASH',
|
||||
'expected' => 'crash'
|
||||
],
|
||||
'wrong level' => [
|
||||
'level' => 'wrong',
|
||||
'str' => 'WRONG',
|
||||
'expected' => 'unknown'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @dataProvider providerErrorMessageLevel
|
||||
* @testdox error message level: $level will be $expected [$_dataName]
|
||||
*
|
||||
* @param string $level
|
||||
* @param string $str
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testErrorMessageLevelOk(string $level, string $str, string $expected): void
|
||||
{
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testErrorMessagesLevelOk',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_level' => Level::Error,
|
||||
]);
|
||||
$em = new \CoreLibs\Logging\ErrorMessage($log);
|
||||
$em->setMessage(
|
||||
$level,
|
||||
$str
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
'level' => $expected,
|
||||
'str' => $str,
|
||||
'id' => '',
|
||||
'target' => '',
|
||||
'target_style' => '',
|
||||
'highlight' => [],
|
||||
],
|
||||
$em->getLastErrorMsg()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @testdox Test of all methods for n messages [$_dataName]
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testErrorMessageOk(): void
|
||||
{
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testErrorMessagesOk',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_level' => Level::Error
|
||||
]);
|
||||
$em = new \CoreLibs\Logging\ErrorMessage($log);
|
||||
$em->setErrorMsg(
|
||||
'100',
|
||||
'info',
|
||||
'INFO MESSAGE'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
'id' => '100',
|
||||
'level' => 'info',
|
||||
'str' => 'INFO MESSAGE',
|
||||
'target' => '',
|
||||
'target_style' => '',
|
||||
'highlight' => [],
|
||||
],
|
||||
$em->getLastErrorMsg()
|
||||
);
|
||||
$this->assertEquals(
|
||||
['100'],
|
||||
$em->getErrorIds()
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
[
|
||||
'id' => '100',
|
||||
'level' => 'info',
|
||||
'str' => 'INFO MESSAGE',
|
||||
'target' => '',
|
||||
'target_style' => '',
|
||||
'highlight' => [],
|
||||
]
|
||||
],
|
||||
$em->getErrorMsg()
|
||||
);
|
||||
|
||||
$em->setErrorMsg(
|
||||
'200',
|
||||
'error',
|
||||
'ERROR MESSAGE'
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
'id' => '200',
|
||||
'level' => 'error',
|
||||
'str' => 'ERROR MESSAGE',
|
||||
'target' => '',
|
||||
'target_style' => '',
|
||||
'highlight' => [],
|
||||
],
|
||||
$em->getLastErrorMsg()
|
||||
);
|
||||
$this->assertEquals(
|
||||
['100', '200'],
|
||||
$em->getErrorIds()
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
[
|
||||
'id' => '100',
|
||||
'level' => 'info',
|
||||
'str' => 'INFO MESSAGE',
|
||||
'target' => '',
|
||||
'target_style' => '',
|
||||
'highlight' => [],
|
||||
],
|
||||
[
|
||||
'id' => '200',
|
||||
'level' => 'error',
|
||||
'str' => 'ERROR MESSAGE',
|
||||
'target' => '',
|
||||
'target_style' => '',
|
||||
'highlight' => [],
|
||||
]
|
||||
],
|
||||
$em->getErrorMsg()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerErrorMessageLog(): array
|
||||
{
|
||||
return [
|
||||
'error, not logged' => [
|
||||
'id' => '200',
|
||||
'level' => 'error',
|
||||
'str' => 'ERROR MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'expected' => '<ERROR> ERROR MESSAGE',
|
||||
],
|
||||
'error, logged' => [
|
||||
'id' => '200',
|
||||
'level' => 'error',
|
||||
'str' => 'ERROR MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => true,
|
||||
'expected' => '<ERROR> ERROR MESSAGE',
|
||||
],
|
||||
'error, logged, message' => [
|
||||
'id' => '200',
|
||||
'level' => 'error',
|
||||
'str' => 'ERROR MESSAGE',
|
||||
'message' => 'OTHER ERROR MESSAGE',
|
||||
'log_error' => true,
|
||||
'expected' => '<ERROR> OTHER ERROR MESSAGE',
|
||||
],
|
||||
'notice' => [
|
||||
'id' => '100',
|
||||
'level' => 'notice',
|
||||
'str' => 'NOTICE MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'expected' => '<NOTICE> NOTICE MESSAGE',
|
||||
],
|
||||
'notice, message' => [
|
||||
'id' => '100',
|
||||
'level' => 'notice',
|
||||
'str' => 'NOTICE MESSAGE',
|
||||
'message' => 'OTHER NOTICE MESSAGE',
|
||||
'log_error' => null,
|
||||
'expected' => '<NOTICE> OTHER NOTICE MESSAGE',
|
||||
],
|
||||
'crash' => [
|
||||
'id' => '300',
|
||||
'level' => 'crash',
|
||||
'str' => 'CRASH MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'expected' => '<ALERT> CRASH MESSAGE',
|
||||
],
|
||||
'crash, message' => [
|
||||
'id' => '300',
|
||||
'level' => 'crash',
|
||||
'str' => 'CRASH MESSAGE',
|
||||
'message' => 'OTHER CRASH MESSAGE',
|
||||
'log_error' => null,
|
||||
'expected' => '<ALERT> OTHER CRASH MESSAGE',
|
||||
],
|
||||
'abort' => [
|
||||
'id' => '200',
|
||||
'level' => 'abort',
|
||||
'str' => 'ABORT MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'expected' => '<CRITICAL> ABORT MESSAGE',
|
||||
],
|
||||
'abort, message' => [
|
||||
'id' => '200',
|
||||
'level' => 'abort',
|
||||
'str' => 'ABORT MESSAGE',
|
||||
'message' => 'OTHER ABORT MESSAGE',
|
||||
'log_error' => null,
|
||||
'expected' => '<CRITICAL> OTHER ABORT MESSAGE',
|
||||
],
|
||||
'unknown' => [
|
||||
'id' => '400',
|
||||
'level' => 'wrong level',
|
||||
'str' => 'WRONG LEVEL MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'expected' => '<EMERGENCY> WRONG LEVEL MESSAGE',
|
||||
],
|
||||
'unknown, message' => [
|
||||
'id' => '400',
|
||||
'level' => 'wrong level',
|
||||
'str' => 'WRONG LEVEL MESSAGE',
|
||||
'message' => 'OTHER WRONG LEVEL MESSAGE',
|
||||
'log_error' => null,
|
||||
'expected' => '<EMERGENCY> OTHER WRONG LEVEL MESSAGE',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @dataProvider providerErrorMessageLog
|
||||
* @testdox Test Log writing with log level Error [$_dataName]
|
||||
*
|
||||
* @param string $id
|
||||
* @param string $level
|
||||
* @param string $str
|
||||
* @param string|null $message
|
||||
* @param bool|null $log_error
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testErrorMessageLogErrorLevel(
|
||||
string $id,
|
||||
string $level,
|
||||
string $str,
|
||||
?string $message,
|
||||
?bool $log_error,
|
||||
string $expected
|
||||
): void {
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testErrorMessagesLogError',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_level' => Level::Notice,
|
||||
'log_per_run' => true
|
||||
]);
|
||||
$em = new \CoreLibs\Logging\ErrorMessage($log);
|
||||
$em->setErrorMsg(
|
||||
$id,
|
||||
$level,
|
||||
$str,
|
||||
message: $message,
|
||||
log_error: $log_error
|
||||
);
|
||||
$file_content = '';
|
||||
if (is_file($log->getLogFolder() . $log->getLogFile())) {
|
||||
$file_content = file_get_contents(
|
||||
$log->getLogFolder() . $log->getLogFile()
|
||||
) ?: '';
|
||||
}
|
||||
// if error, if null or false, it will not be logged
|
||||
if ($level == 'error' && ($log_error === null || $log_error === false)) {
|
||||
$this->assertStringNotContainsString(
|
||||
$expected,
|
||||
$file_content
|
||||
);
|
||||
} else {
|
||||
$this->assertStringContainsString(
|
||||
$expected,
|
||||
$file_content
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @dataProvider providerErrorMessageLog
|
||||
* @testdox Test Log writing with log Level Debug [$_dataName]
|
||||
*
|
||||
* @param string $id
|
||||
* @param string $level
|
||||
* @param string $str
|
||||
* @param string|null $message
|
||||
* @param bool|null $log_error
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testErrorMessageLogErrorDebug(
|
||||
string $id,
|
||||
string $level,
|
||||
string $str,
|
||||
?string $message,
|
||||
?bool $log_error,
|
||||
string $expected
|
||||
): void {
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testErrorMessagesLogDebug',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_level' => Level::Debug,
|
||||
'log_per_run' => true
|
||||
]);
|
||||
$em = new \CoreLibs\Logging\ErrorMessage($log);
|
||||
$em->setErrorMsg(
|
||||
$id,
|
||||
$level,
|
||||
$str,
|
||||
message: $message,
|
||||
log_error: $log_error
|
||||
);
|
||||
$file_content = '';
|
||||
if (is_file($log->getLogFolder() . $log->getLogFile())) {
|
||||
$file_content = file_get_contents(
|
||||
$log->getLogFolder() . $log->getLogFile()
|
||||
) ?: '';
|
||||
}
|
||||
// if error, and log is debug level, only explicit false are not logged
|
||||
if ($level == 'error' && $log_error === false) {
|
||||
$this->assertStringNotContainsString(
|
||||
$expected,
|
||||
$file_content
|
||||
);
|
||||
} else {
|
||||
$this->assertStringContainsString(
|
||||
$expected,
|
||||
$file_content
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @testdox Test jump target set and reporting
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testJumpTarget(): void
|
||||
{
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testErrorMessagesLogDebug',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_level' => Level::Debug,
|
||||
'log_per_run' => true
|
||||
]);
|
||||
$em = new \CoreLibs\Logging\ErrorMessage($log);
|
||||
$em->setJumpTarget(
|
||||
'target-f',
|
||||
'Target text'
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
['target' => 'target-f', 'info' => 'Target text', 'level' => 'error']
|
||||
],
|
||||
$em->getJumpTarget()
|
||||
);
|
||||
// set same target, keep as before
|
||||
$em->setJumpTarget(
|
||||
'target-f',
|
||||
'Other text'
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
['target' => 'target-f', 'info' => 'Target text', 'level' => 'error']
|
||||
],
|
||||
$em->getJumpTarget()
|
||||
);
|
||||
// add new now two messages
|
||||
$em->setJumpTarget(
|
||||
'target-s',
|
||||
'More text'
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
['target' => 'target-f', 'info' => 'Target text', 'level' => 'error'],
|
||||
['target' => 'target-s', 'info' => 'More text', 'level' => 'error'],
|
||||
],
|
||||
$em->getJumpTarget()
|
||||
);
|
||||
// add empty info
|
||||
$em->setJumpTarget(
|
||||
'target-e',
|
||||
''
|
||||
);
|
||||
$this->assertEquals(
|
||||
[
|
||||
['target' => 'target-f', 'info' => 'Target text', 'level' => 'error'],
|
||||
['target' => 'target-s', 'info' => 'More text', 'level' => 'error'],
|
||||
['target' => 'target-e', 'info' => 'Jump to: target-e', 'level' => 'error'],
|
||||
],
|
||||
$em->getJumpTarget()
|
||||
);
|
||||
// add through message
|
||||
$em->setErrorMsg('E-101', 'abort', 'Abort message', jump_target:[
|
||||
'target' => 'abort-target',
|
||||
'info' => 'Abort error'
|
||||
]);
|
||||
$this->assertEquals(
|
||||
[
|
||||
['target' => 'target-f', 'info' => 'Target text', 'level' => 'error'],
|
||||
['target' => 'target-s', 'info' => 'More text', 'level' => 'error'],
|
||||
['target' => 'target-e', 'info' => 'Jump to: target-e', 'level' => 'error'],
|
||||
['target' => 'abort-target', 'info' => 'Abort error', 'level' => 'abort'],
|
||||
],
|
||||
$em->getJumpTarget()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
847
4dev/tests/Logging/CoreLibsLoggingLoggingTest.php
Normal file
847
4dev/tests/Logging/CoreLibsLoggingLoggingTest.php
Normal file
@@ -0,0 +1,847 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use CoreLibs\Logging\Logger\Level;
|
||||
use CoreLibs\Logging\Logger\Flag;
|
||||
|
||||
// TODO: setLogPer test log file written matches pattern
|
||||
|
||||
/**
|
||||
* Test class for Logging
|
||||
* @coversDefaultClass \CoreLibs\Logging\Logging
|
||||
* @testdox \CoreLibs\Logging\Logging method tests
|
||||
*/
|
||||
final class CoreLibsLoggingLoggingTest extends TestCase
|
||||
{
|
||||
private const LOG_FOLDER = __DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR;
|
||||
private const REGEX_BASE = "\[[\d\-\s\.:]+\]\s{1}" // date
|
||||
. "\[[\w\.]+(:\d+)?\]\s{1}" // host:port
|
||||
. "\[(phar:\/\/)?[\w\-\.\/]+:\d+\]\s{1}" // folder/file [note phar:// is for phpunit]
|
||||
. "\[\w+\]\s{1}" // run id
|
||||
. "{[\w\\\\]+((::|->)\w+)?}\s{1}"; // class
|
||||
|
||||
public static function tearDownAfterClass(): void
|
||||
{
|
||||
array_map('unlink', glob(self::LOG_FOLDER . '*.log'));
|
||||
}
|
||||
|
||||
/**
|
||||
* test set for options BASIC
|
||||
*
|
||||
* 0: options
|
||||
* - null for NOT set
|
||||
* 1: expected
|
||||
* 2: override
|
||||
* override:
|
||||
* - constant for COSNTANTS
|
||||
* - global for _GLOBALS
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function optionsProvider(): array
|
||||
{
|
||||
return [
|
||||
'log folder set' => [
|
||||
'options' => [
|
||||
'log_folder' => DIRECTORY_SEPARATOR . 'tmp',
|
||||
'log_file_id' => 'testClassInit',
|
||||
],
|
||||
'expected' => [
|
||||
'log_folder' => DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR,
|
||||
'log_level' => Level::Debug,
|
||||
'log_file_id' => 'testClassInit',
|
||||
],
|
||||
'override' => [],
|
||||
],
|
||||
// -> deprecation warning, log_folder must be set
|
||||
'no log folder set' => [
|
||||
'options' => [
|
||||
'log_file_id' => 'testClassInit'
|
||||
],
|
||||
'expected' => [
|
||||
'log_folder' => getcwd() . DIRECTORY_SEPARATOR,
|
||||
'log_level' => Level::Debug,
|
||||
'log_file_id' => 'testClassInit',
|
||||
],
|
||||
'override' => []
|
||||
],
|
||||
// -> upcoming deprecated
|
||||
'file_id set but not log_file_id' => [
|
||||
'options' => [
|
||||
'log_folder' => DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR,
|
||||
'file_id' => 'testClassInit',
|
||||
],
|
||||
'expected' => [
|
||||
'log_folder' => DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR,
|
||||
'log_level' => Level::Debug,
|
||||
'log_file_id' => 'testClassInit',
|
||||
],
|
||||
'override' => [],
|
||||
],
|
||||
// both file_id and log_file_id set -> WARNING
|
||||
'file_id and log_file_id set' => [
|
||||
'options' => [
|
||||
'log_folder' => DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR,
|
||||
'file_id' => 'testClassInit',
|
||||
'log_file_id' => 'testClassInit',
|
||||
],
|
||||
'expected' => [
|
||||
'log_folder' => DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR,
|
||||
'log_level' => Level::Debug,
|
||||
'log_file_id' => 'testClassInit',
|
||||
],
|
||||
'override' => [],
|
||||
],
|
||||
// no log file id set -> error,
|
||||
'nothing set' => [
|
||||
'options' => [],
|
||||
'expected' => [
|
||||
'log_folder' => getcwd() . DIRECTORY_SEPARATOR,
|
||||
'log_level' => Level::Debug,
|
||||
'log_file_id' => 'NOHOST-0_PHPUnit-TextUI-Command',
|
||||
],
|
||||
'override' => []
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* init logging class
|
||||
*
|
||||
* @dataProvider optionsProvider
|
||||
* @testdox init test [$_dataName]
|
||||
*
|
||||
* @param array $options
|
||||
* @param array $expected
|
||||
* @param array $override
|
||||
* @return void
|
||||
*/
|
||||
public function testClassInit(array $options, array $expected, array $override): void
|
||||
{
|
||||
if (!empty($override['constant'])) {
|
||||
foreach ($override['constant'] as $var => $value) {
|
||||
if (!defined($var)) {
|
||||
define($var, $value);
|
||||
}
|
||||
}
|
||||
// for deprecated no log_folder set
|
||||
// if base is defined and it does have AAASetupData set
|
||||
// change the log_folder "Debug" to "AAASetupData"
|
||||
if (
|
||||
defined('BASE') &&
|
||||
strpos(BASE, DIRECTORY_SEPARATOR . 'AAASetupData') !== false
|
||||
) {
|
||||
$expected['log_folder'] = str_replace(
|
||||
DIRECTORY_SEPARATOR . 'Debug',
|
||||
DIRECTORY_SEPARATOR . 'AAASetupData',
|
||||
$expected['log_folder']
|
||||
);
|
||||
}
|
||||
}
|
||||
// if not log folder and constant set -> expect E_USER_DEPRECATION
|
||||
if (!empty($override['constant']) && empty($options['log_folder'])) {
|
||||
// the deprecation message
|
||||
$deprecation_message = 'options: log_folder must be set. '
|
||||
. 'Setting via BASE and LOG constants is deprecated';
|
||||
// convert E_USER_DEPRECATED to a exception
|
||||
set_error_handler(
|
||||
static function (int $errno, string $errstr): never {
|
||||
throw new \Exception($errstr, $errno);
|
||||
},
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
// catch this with the message
|
||||
$this->expectExceptionMessage($deprecation_message);
|
||||
}
|
||||
// alert for log file id with globals
|
||||
if (!empty($override['constant']) && empty($options['log_file_id'])) {
|
||||
//
|
||||
}
|
||||
// alert for log file id and file id set
|
||||
if (
|
||||
!empty($options['log_file_id']) &&
|
||||
!empty($options['file_id'])
|
||||
) {
|
||||
set_error_handler(
|
||||
static function (int $errno, string $errstr): never {
|
||||
throw new \InvalidArgumentException($errstr, $errno);
|
||||
},
|
||||
E_USER_WARNING
|
||||
);
|
||||
$error_message = 'options: "file_id" is deprecated use: "log_file_id".';
|
||||
$this->expectExceptionMessage($error_message);
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
set_error_handler(
|
||||
static function (int $errno, string $errstr): never {
|
||||
throw new \Exception($errstr, $errno);
|
||||
},
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
$this->expectException(\Exception::class);
|
||||
// $error_message = 'options: both log_file_id and log_id are set at the same time, will use log_file_id';
|
||||
// $this->expectExceptionMessage($error_message);
|
||||
}
|
||||
// empty log folder
|
||||
if (empty($override['constant']) && empty($options['log_folder'])) {
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessageMatches("/^Missing mandatory option: \"/");
|
||||
} elseif (empty($options['log_file_id']) && !empty($options['file_id'])) {
|
||||
// the deprecation message
|
||||
$deprecation_message = 'options: "file_id" is deprecated use: "log_file_id".';
|
||||
// convert E_USER_DEPRECATED to a exception
|
||||
set_error_handler(
|
||||
static function (int $errno, string $errstr): never {
|
||||
throw new \Exception($errstr, $errno);
|
||||
},
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
// catch this with the message
|
||||
$this->expectExceptionMessage($deprecation_message);
|
||||
}
|
||||
$log = new \CoreLibs\Logging\Logging($options);
|
||||
// reset error handler
|
||||
restore_error_handler();
|
||||
// check that settings match
|
||||
$this->assertEquals(
|
||||
$expected['log_folder'],
|
||||
$log->getLogFolder(),
|
||||
'log folder not matching'
|
||||
);
|
||||
$this->assertEquals(
|
||||
$expected['log_file_id'],
|
||||
$log->getLogFileId(),
|
||||
'log file id not matching'
|
||||
);
|
||||
}
|
||||
|
||||
// test all setters/getters
|
||||
|
||||
// setLoggingLevel
|
||||
// getLoggingLevel
|
||||
// loggingLevelIsDebug
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::setLoggingLevel
|
||||
* @covers ::getLoggingLevel
|
||||
* @covers ::loggingLevelIsDebug
|
||||
* @testdox setLoggingLevel set/get checks
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetLoggingLevel(): void
|
||||
{
|
||||
// valid that is not Debug
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testSetLoggingLevel',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_level' => Level::Info
|
||||
]);
|
||||
$this->assertEquals(
|
||||
Level::Info,
|
||||
$log->getLoggingLevel()
|
||||
);
|
||||
$this->assertFalse(
|
||||
$log->loggingLevelIsDebug()
|
||||
);
|
||||
// not set, should be debug]
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testSetLoggingLevel',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
]);
|
||||
$this->assertEquals(
|
||||
Level::Debug,
|
||||
$log->getLoggingLevel()
|
||||
);
|
||||
$this->assertTrue(
|
||||
$log->loggingLevelIsDebug()
|
||||
);
|
||||
// invalid, should be debug, will throw excpetion too
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Option: "log_level" is not of instance \CoreLibs\Logging\Logger\Level');
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testSetLoggingLevel',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_level' => 'I'
|
||||
]);
|
||||
$this->assertEquals(
|
||||
Level::Debug,
|
||||
$log->getLoggingLevel()
|
||||
);
|
||||
$this->assertTrue(
|
||||
$log->loggingLevelIsDebug()
|
||||
);
|
||||
// set valid, then change
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testSetLoggingLevel',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_level' => Level::Info
|
||||
]);
|
||||
$this->assertEquals(
|
||||
Level::Info,
|
||||
$log->getLoggingLevel()
|
||||
);
|
||||
$log->setLoggingLevel(Level::Notice);
|
||||
$this->assertEquals(
|
||||
Level::Notice,
|
||||
$log->getLoggingLevel()
|
||||
);
|
||||
// illegal logging level
|
||||
$this->expectException(\Psr\Log\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessageMatches("/^Level \"NotGood\" is not defined, use one of: /");
|
||||
$log->setLoggingLevel('NotGood');
|
||||
}
|
||||
|
||||
// setLogFileId
|
||||
// getLogFileId
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::setLogFileId
|
||||
* @covers ::getLogFileId
|
||||
* @testdox setLogFileId set/get checks
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testLogFileId(): void
|
||||
{
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testLogFileId',
|
||||
'log_folder' => self::LOG_FOLDER
|
||||
]);
|
||||
// bad, keep same
|
||||
$log->setLogFileId('$$##$%#$%&');
|
||||
$this->assertEquals(
|
||||
'testLogFileId',
|
||||
$log->getLogFileId()
|
||||
);
|
||||
// good, change
|
||||
$log->setLogFileId('validID');
|
||||
$this->assertEquals(
|
||||
'validID',
|
||||
$log->getLogFileId()
|
||||
);
|
||||
// invalid on init
|
||||
$this->expectException(\Psr\Log\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('LogFileId: no log file id set');
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => '$$$"#"#$"#$',
|
||||
'log_folder' => self::LOG_FOLDER
|
||||
]);
|
||||
}
|
||||
|
||||
// setLogUniqueId
|
||||
// getLogUniqueId
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function logUniqueIdProvider(): array
|
||||
{
|
||||
return [
|
||||
'option set' => [
|
||||
'option' => true,
|
||||
'override' => false,
|
||||
],
|
||||
'direct set' => [
|
||||
'option' => false,
|
||||
'override' => false,
|
||||
],
|
||||
'override set' => [
|
||||
'option' => false,
|
||||
'override' => true,
|
||||
],
|
||||
'option and override set' => [
|
||||
'option' => false,
|
||||
'override' => true,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::setLogUniqueId
|
||||
* @covers ::getLogUniqueId
|
||||
* @dataProvider logUniqueIdProvider
|
||||
* @testdox per run log id set test: option: $option, override: $override [$_dataName]
|
||||
*
|
||||
* @param bool $option
|
||||
* @param bool $override
|
||||
* @return void
|
||||
*/
|
||||
public function testLogUniqueId(bool $option, bool $override): void
|
||||
{
|
||||
if ($option === true) {
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testLogUniqueId',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_per_run' => $option
|
||||
]);
|
||||
} else {
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testLogUniqueId',
|
||||
'log_folder' => self::LOG_FOLDER
|
||||
]);
|
||||
$log->setLogUniqueId();
|
||||
}
|
||||
$per_run_id = $log->getLogUniqueId();
|
||||
$this->assertMatchesRegularExpression(
|
||||
"/^\d{4}-\d{2}-\d{2}_\d{6}_U_[a-z0-9]{8}$/",
|
||||
$per_run_id,
|
||||
'assert per log run id 1st'
|
||||
);
|
||||
if ($override === true) {
|
||||
$log->setLogUniqueId(true);
|
||||
$per_run_id_2nd = $log->getLogUniqueId();
|
||||
$this->assertMatchesRegularExpression(
|
||||
"/^\d{4}-\d{2}-\d{2}_\d{6}_U_[a-z0-9]{8}$/",
|
||||
$per_run_id_2nd,
|
||||
'assert per log run id 2nd'
|
||||
);
|
||||
$this->assertNotEquals(
|
||||
$per_run_id,
|
||||
$per_run_id_2nd,
|
||||
'1st and 2nd don\'t match'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// setLogDate
|
||||
// getLogDate
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::setLogDate
|
||||
* @covers ::getLogDate
|
||||
* @testdox setLogDate set/get checks
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetLogDate(): void
|
||||
{
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testLogFileId',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_per_date' => true,
|
||||
]);
|
||||
$this->assertEquals(
|
||||
date('Y-m-d'),
|
||||
$log->getLogDate()
|
||||
);
|
||||
}
|
||||
|
||||
// setLogFlag
|
||||
// getLogFlag
|
||||
// unsetLogFlag
|
||||
// getLogFlags
|
||||
// Logger\Flag
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers Logger\Flag
|
||||
* @testdox Logger\Flag enum test
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testLoggerFlag(): void
|
||||
{
|
||||
// logger flags to check that they exist
|
||||
$flags = [
|
||||
'all_off' => 0,
|
||||
'per_run' => 1,
|
||||
'per_date' => 2,
|
||||
'per_group' => 4,
|
||||
'per_page' => 8,
|
||||
'per_class' => 16,
|
||||
'per_level' => 32,
|
||||
];
|
||||
// from int -> return value
|
||||
foreach ($flags as $name => $value) {
|
||||
$this->assertEquals(
|
||||
Flag::fromName($name),
|
||||
Flag::fromValue($value)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::setLogFlag
|
||||
* @covers ::getLogFlag
|
||||
* @covers ::unsetLogFlag
|
||||
* @covers ::getLogFlags
|
||||
* @testdox setLogDate set/get checks
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetLogFlag(): void
|
||||
{
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testSetLogFlag',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
]);
|
||||
// set valid log flag
|
||||
$log->setLogFlag(Flag::per_run);
|
||||
$this->assertTrue(
|
||||
$log->getLogFlag(Flag::per_run)
|
||||
);
|
||||
// flags seum
|
||||
$this->assertEquals(
|
||||
Flag::per_run->value,
|
||||
$log->getLogFlags(),
|
||||
);
|
||||
// unset valid log flag
|
||||
$log->unsetLogFlag(Flag::per_run);
|
||||
$this->assertFalse(
|
||||
$log->getLogFlag(Flag::per_run)
|
||||
);
|
||||
// illegal Flags cannot be set, they will throw eerros onrun
|
||||
|
||||
// test multi set and sum is equals set
|
||||
$log->setLogFlag(Flag::per_date);
|
||||
$log->setLogFlag(Flag::per_group);
|
||||
$this->assertEquals(
|
||||
Flag::per_date->value + Flag::per_group->value,
|
||||
$log->getLogFlags()
|
||||
);
|
||||
}
|
||||
|
||||
// setLogFolder
|
||||
// getLogFolder
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::setLogFolder
|
||||
* @covers ::getLogFolder
|
||||
* @testdox setLogFolder set/get checks, init check
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetLogFolder(): void
|
||||
{
|
||||
// set to good folder
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testSetLogFolder',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
]);
|
||||
$this->assertEquals(
|
||||
self::LOG_FOLDER,
|
||||
$log->getLogFolder()
|
||||
);
|
||||
// set to a good other folder
|
||||
$log->setLogFolder(DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR);
|
||||
$this->assertEquals(
|
||||
DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR,
|
||||
$log->getLogFolder()
|
||||
);
|
||||
// good other folder with missing trailing slash
|
||||
$log->setLogFolder(DIRECTORY_SEPARATOR . 'tmp');
|
||||
$this->assertEquals(
|
||||
DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR,
|
||||
$log->getLogFolder()
|
||||
);
|
||||
// a bad folder -> last good folder
|
||||
$log->setLogFolder('I-am-not existing');
|
||||
$this->assertEquals(
|
||||
DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR,
|
||||
$log->getLogFolder()
|
||||
);
|
||||
// init with a bad folder
|
||||
$this->expectException(\Psr\Log\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Folder: "I-am-bad" is not writeable for logging');
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testSetLogFolderInvalid',
|
||||
'log_folder' => 'I-am-bad',
|
||||
]);
|
||||
}
|
||||
|
||||
// getLogFile (no set, only correct after log run)
|
||||
|
||||
// setLogMaxFileSize
|
||||
// getLogMaxFileSize
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::setLogMaxFileSize
|
||||
* @covers ::getLogMaxFileSize
|
||||
* @testdox setLogMaxFileSize set/get checks, init check
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetLogMaxFileSize(): void
|
||||
{
|
||||
// init set to 0
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testSetLogMaxFileSize',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
]);
|
||||
$this->assertEquals(
|
||||
0,
|
||||
$log->getLogMaxFileSize()
|
||||
);
|
||||
// set to new, valid size
|
||||
$file_size = 200 * 1024;
|
||||
$valid = $log->setLogMaxFileSize($file_size);
|
||||
$this->assertTrue($valid);
|
||||
$this->assertEquals(
|
||||
$file_size,
|
||||
$log->getLogMaxFileSize()
|
||||
);
|
||||
// invalid size, < 0, will be last and return false
|
||||
$valid = $log->setLogMaxFileSize(-1);
|
||||
$this->assertFalse($valid);
|
||||
$this->assertEquals(
|
||||
$file_size,
|
||||
$log->getLogMaxFileSize()
|
||||
);
|
||||
// too small (< MIN_LOG_MAX_FILESIZE)
|
||||
$valid = $log->setLogMaxFileSize($log::MIN_LOG_MAX_FILESIZE - 1);
|
||||
$this->assertFalse($valid);
|
||||
$this->assertEquals(
|
||||
$file_size,
|
||||
$log->getLogMaxFileSize()
|
||||
);
|
||||
}
|
||||
|
||||
// getOption (option params)
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::getOption
|
||||
* @testdox getOption checks
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetOption(): void
|
||||
{
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testGetOption',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
]);
|
||||
$this->assertEquals(
|
||||
self::LOG_FOLDER,
|
||||
$log->getOption('log_folder')
|
||||
);
|
||||
// not found
|
||||
$this->assertNull(
|
||||
$log->getOption('I_do not exist')
|
||||
);
|
||||
}
|
||||
|
||||
// test all logger functions
|
||||
// debug (special)
|
||||
// info
|
||||
// notice
|
||||
// warning
|
||||
// error
|
||||
// critical
|
||||
// alert
|
||||
// emergency
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::debug
|
||||
* @testdox debug checks
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDebug(): void
|
||||
{
|
||||
// init logger
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testDebug',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
]);
|
||||
// clean all data in folder first
|
||||
array_map('unlink', glob($log->getLogFolder() . $log->getLogFileId() . '*.log'));
|
||||
|
||||
$group_id = 'G';
|
||||
$message = 'D';
|
||||
$log_status = $log->debug($group_id, $message);
|
||||
$this->assertTrue($log_status, 'debug write successful');
|
||||
$file_content = file_get_contents(
|
||||
$log->getLogFolder() . $log->getLogFile()
|
||||
) ?: '';
|
||||
$log_level = $log->getLoggingLevel()->getName();
|
||||
// [2023-05-30 15:51:39.36128800] [NOHOST:0]
|
||||
// [www/vendor/bin/phpunit] [7b9d0747] {PHPUnit\TextUI\Command}
|
||||
// <DEBUG:G> D
|
||||
$this->assertMatchesRegularExpression(
|
||||
"/" . self::REGEX_BASE
|
||||
. "<$log_level:$group_id>\s{1}" // log level / group id
|
||||
. "$message" // message
|
||||
. "/",
|
||||
$file_content
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerLoggingLevelWrite(): array
|
||||
{
|
||||
return [
|
||||
'info' => [
|
||||
'message' => 'I',
|
||||
'file_id' => Level::Info->name,
|
||||
'level' => Level::Info
|
||||
],
|
||||
'notice' => [
|
||||
'message' => 'N',
|
||||
'file_id' => Level::Notice->name,
|
||||
'level' => Level::Notice
|
||||
],
|
||||
'warning' => [
|
||||
'message' => 'W',
|
||||
'file_id' => Level::Warning->name,
|
||||
'level' => Level::Warning
|
||||
],
|
||||
'error' => [
|
||||
'message' => 'E',
|
||||
'file_id' => Level::Error->name,
|
||||
'level' => Level::Error
|
||||
],
|
||||
'crticial' => [
|
||||
'message' => 'C',
|
||||
'file_id' => Level::Critical->name,
|
||||
'level' => Level::Critical
|
||||
],
|
||||
'alert' => [
|
||||
'message' => 'A',
|
||||
'file_id' => Level::Alert->name,
|
||||
'level' => Level::Alert
|
||||
],
|
||||
'emergency' => [
|
||||
'message' => 'Em',
|
||||
'file_id' => Level::Emergency->name,
|
||||
'level' => Level::Emergency
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::info
|
||||
* @covers ::notice
|
||||
* @covers ::warning
|
||||
* @covers ::error
|
||||
* @covers ::critical
|
||||
* @covers ::alert
|
||||
* @covers ::emergency
|
||||
* @dataProvider providerLoggingLevelWrite
|
||||
* @testdox logging level write checks for $level [$_dataName]
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testLoggingLevelWrite(string $message, string $file_id, Level $level): void
|
||||
{
|
||||
// init logger
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'test' . $file_id,
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_level' => $level,
|
||||
]);
|
||||
// clean all data in folder first
|
||||
array_map('unlink', glob($log->getLogFolder() . $log->getLogFileId() . '*.log'));
|
||||
|
||||
switch ($level->value) {
|
||||
case 200:
|
||||
$log_status = $log->info($message);
|
||||
break;
|
||||
case 250:
|
||||
$log_status = $log->notice($message);
|
||||
break;
|
||||
case 300:
|
||||
$log_status = $log->warning($message);
|
||||
break;
|
||||
case 400:
|
||||
$log_status = $log->error($message);
|
||||
break;
|
||||
case 500:
|
||||
$log_status = $log->critical($message);
|
||||
break;
|
||||
case 550:
|
||||
$log_status = $log->alert($message);
|
||||
break;
|
||||
case 600:
|
||||
$log_status = $log->emergency($message);
|
||||
break;
|
||||
}
|
||||
$this->assertTrue($log_status, 'log write successful');
|
||||
$file_content = file_get_contents(
|
||||
$log->getLogFolder() . $log->getLogFile()
|
||||
) ?: '';
|
||||
$log_level = $log->getLoggingLevel()->getName();
|
||||
$this->assertMatchesRegularExpression(
|
||||
"/" . self::REGEX_BASE
|
||||
. "<$log_level>\s{1}" // log level / group id
|
||||
. "$message" // message
|
||||
. "/",
|
||||
$file_content,
|
||||
'log message regex'
|
||||
);
|
||||
}
|
||||
|
||||
// check log level that writer writes in correct level
|
||||
// also that non debug ignores prefix/group
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::log
|
||||
* @testdox log() general call test
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testLoggingLog(): void
|
||||
{
|
||||
// init logger
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
'log_file_id' => 'testLoggingLog',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
'log_per_level' => true,
|
||||
]);
|
||||
$log_ok = $log->log(Level::Debug, 'DEBUG', group_id: 'GROUP_ID', prefix: 'PREFIX:');
|
||||
$this->assertTrue($log_ok, 'assert ::log (debug) OK');
|
||||
$this->assertEquals(
|
||||
$log->getLogFile(),
|
||||
$log->getLogFileId() . '_DEBUG.log'
|
||||
);
|
||||
$log_ok = $log->log(Level::Info, 'INFO', group_id: 'GROUP_ID', prefix: 'PREFIX:');
|
||||
$this->assertTrue($log_ok, 'assert ::log (info) OK');
|
||||
$this->assertEquals(
|
||||
$log->getLogFile(),
|
||||
$log->getLogFileId() . '_INFO.log'
|
||||
);
|
||||
}
|
||||
|
||||
// must test flow:
|
||||
// init normal
|
||||
// log -> check file name
|
||||
// set per date
|
||||
// log -> check file name
|
||||
// and same for per_run
|
||||
|
||||
// deprecated calls check?
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -1,2 +1,3 @@
|
||||
*
|
||||
*log
|
||||
*LOG
|
||||
!.gitignore
|
||||
@@ -16,17 +16,89 @@ final class CoreLibsOutputImageTest extends TestCase
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @testdox Output\Image Class tests
|
||||
* @covers ::createThumbnail
|
||||
* @testdox createThumbnail checks
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testOutputImage()
|
||||
public function testCreateThumbnail(): void
|
||||
{
|
||||
// $this->assertTrue(true, 'Output Image Tests not implemented');
|
||||
$this->markTestIncomplete(
|
||||
'Output\Image Tests have not yet been implemented'
|
||||
// CONVERT does not exist
|
||||
$this->expectException(\RuntimeException::class);
|
||||
\CoreLibs\Output\Image::createThumbnail('do_not_exist.png', 200, 200);
|
||||
// set convert
|
||||
$paths = [
|
||||
'/bin',
|
||||
'/usr/bin',
|
||||
'/usr/local/bin',
|
||||
];
|
||||
// find convert
|
||||
foreach ($paths as $path) {
|
||||
if (
|
||||
file_exists($path . DIRECTORY_SEPARATOR . 'convert') &&
|
||||
is_file($path . DIRECTORY_SEPARATOR . 'convert')
|
||||
) {
|
||||
// image magick convert location
|
||||
define('CONVERT', $path . DIRECTORY_SEPARATOR . 'convert');
|
||||
break;
|
||||
}
|
||||
}
|
||||
unset($paths);
|
||||
// cannot set dummy file
|
||||
$this->expectException(\Exception::class);
|
||||
\CoreLibs\Output\Image::createThumbnail('do_not_exist.png', 200, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::createThumbnailSimple
|
||||
* @testdox createThumbnailSimple checks
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateThumbnailSimple(): void
|
||||
{
|
||||
// file does not exist
|
||||
$this->expectException(\UnexpectedValueException::class);
|
||||
\CoreLibs\Output\Image::createThumbnailSimple(
|
||||
'do_not_exist.png',
|
||||
200,
|
||||
200,
|
||||
cache_folder: '/tmp/',
|
||||
web_folder: '/tmp/'
|
||||
);
|
||||
// $this->markTestSkipped('No implementation for Output\Image at the moment');
|
||||
// cache folder is not dir
|
||||
$this->expectException(\UnexpectedValueException::class);
|
||||
\CoreLibs\Output\Image::createThumbnailSimple(
|
||||
'do_not_exist.png',
|
||||
200,
|
||||
200,
|
||||
cache_folder: '/foo/bar/',
|
||||
web_folder: '/tmp/'
|
||||
);
|
||||
// target cache folder is not writeable
|
||||
|
||||
// RuntimeException: imagecreatetruecolor failed
|
||||
// RuntimeException: imagecolorallocatealpha failed
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::correctImageOrientation
|
||||
* @testdox correctImageOrientation checks
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCorrectImageOrientation(): void
|
||||
{
|
||||
// test file does not exist
|
||||
$this->expectException(\UnexpectedValueException::class);
|
||||
\CoreLibs\Output\Image::correctImageOrientation('do_not_exist.png');
|
||||
// test folder not writeable
|
||||
// test exit_read_data not present (how)?
|
||||
// test image rotate
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,11 +7,11 @@ namespace tests;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test class for Check\Password
|
||||
* @coversDefaultClass \CoreLibs\Check\Password
|
||||
* @testdox \CoreLibs\Check\Password method tests
|
||||
* Test class for Security\Password
|
||||
* @coversDefaultClass \CoreLibs\Security\Password
|
||||
* @testdox \CoreLibs\Security\Password method tests
|
||||
*/
|
||||
final class CoreLibsCheckPasswordTest extends TestCase
|
||||
final class CoreLibsSecurityPasswordTest extends TestCase
|
||||
{
|
||||
public function passwordProvider(): array
|
||||
{
|
||||
@@ -46,7 +46,7 @@ final class CoreLibsCheckPasswordTest extends TestCase
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Check\Password::passwordVerify($input, \CoreLibs\Check\Password::passwordSet($input_hash))
|
||||
\CoreLibs\Security\Password::passwordVerify($input, \CoreLibs\Security\Password::passwordSet($input_hash))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ final class CoreLibsCheckPasswordTest extends TestCase
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Check\Password::passwordRehashCheck($input)
|
||||
\CoreLibs\Security\Password::passwordRehashCheck($input)
|
||||
);
|
||||
}
|
||||
}
|
||||
172
4dev/tests/Security/CoreLibsSecuritySymmetricEncryptionTest.php
Normal file
172
4dev/tests/Security/CoreLibsSecuritySymmetricEncryptionTest.php
Normal file
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use CoreLibs\Security\CreateKey;
|
||||
use CoreLibs\Security\SymmetricEncryption;
|
||||
|
||||
/**
|
||||
* Test class for Security\SymmetricEncryption and Security\CreateKey
|
||||
* @coversDefaultClass \CoreLibs\Security\SymmetricEncryption
|
||||
* @testdox \CoreLibs\Security\SymmetricEncryption method tests
|
||||
*/
|
||||
final class CoreLibsSecuritySymmetricEncryptionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerEncryptDecryptSuccess(): array
|
||||
{
|
||||
return [
|
||||
'valid string' => [
|
||||
'input' => 'I am a secret',
|
||||
'expected' => 'I am a secret',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* test encrypt/decrypt produce correct output
|
||||
*
|
||||
* @covers ::generateRandomKey
|
||||
* @covers ::encrypt
|
||||
* @covers ::decrypt
|
||||
* @dataProvider providerEncryptDecryptSuccess
|
||||
* @testdox encrypt/decrypt $input must be $expected [$_dataName]
|
||||
*
|
||||
* @param string $input
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testEncryptDecryptSuccess(string $input, string $expected): void
|
||||
{
|
||||
$key = CreateKey::generateRandomKey();
|
||||
$encrypted = SymmetricEncryption::encrypt($input, $key);
|
||||
$decrypted = SymmetricEncryption::decrypt($encrypted, $key);
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$decrypted
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerEncryptFailed(): array
|
||||
{
|
||||
return [
|
||||
'wrong decryption key' => [
|
||||
'input' => 'I am a secret',
|
||||
'excpetion_message' => 'Invalid Key'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test decryption with wrong key
|
||||
*
|
||||
* @covers ::generateRandomKey
|
||||
* @covers ::encrypt
|
||||
* @covers ::decrypt
|
||||
* @dataProvider providerEncryptFailed
|
||||
* @testdox decrypt with wrong key $input throws $exception_message [$_dataName]
|
||||
*
|
||||
* @param string $input
|
||||
* @param string $exception_message
|
||||
* @return void
|
||||
*/
|
||||
public function testEncryptFailed(string $input, string $exception_message): void
|
||||
{
|
||||
$key = CreateKey::generateRandomKey();
|
||||
$encrypted = SymmetricEncryption::encrypt($input, $key);
|
||||
$wrong_key = CreateKey::generateRandomKey();
|
||||
$this->expectExceptionMessage($exception_message);
|
||||
SymmetricEncryption::decrypt($encrypted, $wrong_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerWrongKey(): array
|
||||
{
|
||||
return [
|
||||
'not hex key' => [
|
||||
'key' => 'not_a_hex_key',
|
||||
'exception_message' => 'Invalid hex key'
|
||||
],
|
||||
'too short hex key' => [
|
||||
'key' => '1cabd5cba9e042f12522f4ff2de5c31d233b',
|
||||
'excpetion_message' => 'Key is not the correct size (must be '
|
||||
. 'SODIUM_CRYPTO_SECRETBOX_KEYBYTES bytes long).'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* test invalid key provided to decrypt or encrypt
|
||||
*
|
||||
* @covers ::encrypt
|
||||
* @covers ::decrypt
|
||||
* @dataProvider providerWrongKey
|
||||
* @testdox wrong key $key throws $exception_message [$_dataName]
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $exception_message
|
||||
* @return void
|
||||
*/
|
||||
public function testWrongKey(string $key, string $exception_message): void
|
||||
{
|
||||
$this->expectExceptionMessage($exception_message);
|
||||
SymmetricEncryption::encrypt('test', $key);
|
||||
// we must encrypt valid thing first so we can fail with the wrong kjey
|
||||
$enc_key = CreateKey::generateRandomKey();
|
||||
$encrypted = SymmetricEncryption::encrypt('test', $enc_key);
|
||||
$this->expectExceptionMessage($exception_message);
|
||||
SymmetricEncryption::decrypt($encrypted, $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerWrongCiphertext(): array
|
||||
{
|
||||
return [
|
||||
'too short ciphertext' => [
|
||||
'input' => 'short',
|
||||
'exception_message' => 'Invalid ciphertext (too short)'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::decrypt
|
||||
* @dataProvider providerWrongCiphertext
|
||||
* @testdox too short ciphertext $input throws $exception_message [$_dataName]
|
||||
*
|
||||
* @param string $input
|
||||
* @param string $exception_message
|
||||
* @return void
|
||||
*/
|
||||
public function testWrongCiphertext(string $input, string $exception_message): void
|
||||
{
|
||||
$key = CreateKey::generateRandomKey();
|
||||
$this->expectExceptionMessage($exception_message);
|
||||
SymmetricEncryption::decrypt($input, $key);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
34
4dev/tests/Template/CoreLibsTemplateHtmlBuilderBlockTest.php
Normal file
34
4dev/tests/Template/CoreLibsTemplateHtmlBuilderBlockTest.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use CoreLibs\Template\HtmlBuilder\Block;
|
||||
|
||||
/**
|
||||
* Test class for Template\HtmlBuilder\Block
|
||||
* @coversDefaultClass \CoreLibs\Template\HtmlBuilder\Block
|
||||
* @testdox \CoreLibs\Template\HtmlBuilder\Block method tests
|
||||
*/
|
||||
final class CoreLibsTemplateHtmlBuilderBlockTest extends TestCase
|
||||
{
|
||||
public function testCreateBlock(): void
|
||||
{
|
||||
$el = Block::cel('div', 'id', 'content', ['css'], ['onclick' => 'foo();']);
|
||||
$this->assertEquals(
|
||||
'<div id="id" class="css" onclick="foo();">content</div>',
|
||||
Block::buildHtml($el)
|
||||
);
|
||||
}
|
||||
|
||||
// ael
|
||||
// aelx|addSub
|
||||
// resetSub
|
||||
// acssel/rcssel/scssel
|
||||
// buildHtml
|
||||
// buildHtmlFromList|printHtmlFromArray
|
||||
}
|
||||
|
||||
// __END__
|
||||
546
4dev/tests/Template/CoreLibsTemplateHtmlBuilderElementTest.php
Normal file
546
4dev/tests/Template/CoreLibsTemplateHtmlBuilderElementTest.php
Normal file
@@ -0,0 +1,546 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use CoreLibs\Template\HtmlBuilder\Element;
|
||||
use CoreLibs\Template\HtmlBuilder\General\Error;
|
||||
use CoreLibs\Template\HtmlBuilder\General\HtmlBuilderExcpetion;
|
||||
|
||||
/**
|
||||
* Test class for Template\HtmlBuilder\Element
|
||||
* @coversDefaultClass \CoreLibs\Template\HtmlBuilder\Element
|
||||
* @testdox \CoreLibs\Template\HtmlBuilder\Element method tests
|
||||
*/
|
||||
final class CoreLibsTemplateHtmlBuilderElementTest extends TestCase
|
||||
{
|
||||
public function providerCreateElements(): array
|
||||
{
|
||||
return [
|
||||
'simple div' => [
|
||||
'tag' => 'div',
|
||||
'id' => 'id',
|
||||
'content' => 'content',
|
||||
'css' => ['css'],
|
||||
'options' => ['onclick' => 'foo();'],
|
||||
'expected' => '<div id="id" class="css" onclick="foo();">content</div>'
|
||||
],
|
||||
'simple input' => [
|
||||
'tag' => 'input',
|
||||
'id' => 'id',
|
||||
'content' => null,
|
||||
'css' => ['css'],
|
||||
'options' => ['name' => 'name', 'onclick' => 'foo();'],
|
||||
'expected' => '<input id="id" name="name" class="css" onclick="foo();">'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::Element
|
||||
* @covers ::buildHtml
|
||||
* @covers ::getTag
|
||||
* @covers ::getId
|
||||
* @covers ::getContent
|
||||
* @covers ::getOptions
|
||||
* @covers ::getCss
|
||||
* @dataProvider providerCreateElements
|
||||
* @testdox create single new Element test [$_dataName]
|
||||
*
|
||||
* @param string $tag
|
||||
* @param string|null $id
|
||||
* @param string|null $content
|
||||
* @param array|null $css
|
||||
* @param array|null $options
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateElement(
|
||||
string $tag,
|
||||
?string $id,
|
||||
?string $content,
|
||||
?array $css,
|
||||
?array $options,
|
||||
string $expected
|
||||
): void {
|
||||
$el = new Element($tag, $id ?? '', $content ?? '', $css ?? [], $options ?? []);
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$el->buildHtml(),
|
||||
'element creation failed'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$tag,
|
||||
$el->getTag(),
|
||||
'get tag failed'
|
||||
);
|
||||
|
||||
if ($id !== null) {
|
||||
$this->assertEquals(
|
||||
$id,
|
||||
$el->getId(),
|
||||
'get id failed'
|
||||
);
|
||||
}
|
||||
if ($content !== null) {
|
||||
$this->assertEquals(
|
||||
$content,
|
||||
$el->getContent(),
|
||||
'get content failed'
|
||||
);
|
||||
}
|
||||
if ($css !== null) {
|
||||
$this->assertEquals(
|
||||
$css,
|
||||
$el->getCss(),
|
||||
'get css failed'
|
||||
);
|
||||
}
|
||||
if ($options !== null) {
|
||||
$this->assertEquals(
|
||||
$options,
|
||||
$el->getOptions(),
|
||||
'get options failed'
|
||||
);
|
||||
}
|
||||
if (!empty($options['name'])) {
|
||||
$this->assertEquals(
|
||||
$options['name'],
|
||||
$el->getName(),
|
||||
'get name failed'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* css add/remove
|
||||
*
|
||||
* @cover ::getCss
|
||||
* @cover ::addCss
|
||||
* @cover ::removeCss
|
||||
* @testdox test handling of adding and removing css classes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCssHandling(): void
|
||||
{
|
||||
$el = new Element('div', 'css-test', 'CSS content');
|
||||
$this->assertEqualsCanonicalizing(
|
||||
[],
|
||||
$el->getCss(),
|
||||
'check empty css'
|
||||
);
|
||||
$el->addCss('foo');
|
||||
$this->assertEqualsCanonicalizing(
|
||||
['foo'],
|
||||
$el->getCss(),
|
||||
'check added one css'
|
||||
);
|
||||
$el->removeCss('foo');
|
||||
$this->assertEqualsCanonicalizing(
|
||||
[],
|
||||
$el->getCss(),
|
||||
'check remove added css'
|
||||
);
|
||||
// add serveral
|
||||
// remove some of them
|
||||
$el->addCss('a', 'b', 'c');
|
||||
$this->assertEqualsCanonicalizing(
|
||||
['a', 'b', 'c'],
|
||||
$el->getCss(),
|
||||
'check added some css'
|
||||
);
|
||||
$el->removeCss('a', 'c');
|
||||
// $this->assertArray
|
||||
$this->assertEqualsCanonicalizing(
|
||||
['b'],
|
||||
$el->getCss(),
|
||||
'check remove some css'
|
||||
);
|
||||
// chained add and remove
|
||||
$el->addCss('a', 'b', 'c', 'd')->removeCss('b', 'd');
|
||||
$this->assertEqualsCanonicalizing(
|
||||
['a', 'c'],
|
||||
$el->getCss(),
|
||||
'check chain add remove some css'
|
||||
);
|
||||
$el->resetCss();
|
||||
$this->assertEqualsCanonicalizing(
|
||||
[],
|
||||
$el->getCss(),
|
||||
'check reset css'
|
||||
);
|
||||
// remove something that does not eixst
|
||||
$el->addCss('exists');
|
||||
$el->removeCss('not');
|
||||
$this->assertEqualsCanonicalizing(
|
||||
['exists'],
|
||||
$el->getCss(),
|
||||
'check remove not exitsing'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* nested test
|
||||
*
|
||||
* @testdox nested test and loop assign detection
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBuildNested(): void
|
||||
{
|
||||
Error::resetMessages();
|
||||
$el = new Element('div', 'build-test');
|
||||
$el_sub = new Element('div', 'sub-1');
|
||||
$el->addSub($el_sub);
|
||||
$this->assertEquals(
|
||||
'<div id="build-test"><div id="sub-1"></div></div>',
|
||||
$el->buildHtml(),
|
||||
'nested build failed'
|
||||
);
|
||||
// this would create a loop, throws error
|
||||
$this->expectException(HtmlBuilderExcpetion::class);
|
||||
$this->expectExceptionMessage("Cannot assign Element to itself, this would create an infinite loop");
|
||||
$el_sub->addSub($el_sub);
|
||||
$this->assertEquals(
|
||||
'<div id="sub-1"></div>',
|
||||
$el_sub->buildHtml(),
|
||||
'loop detection failed'
|
||||
);
|
||||
$this->assertTrue(
|
||||
Error::hasError(),
|
||||
'failed to throw error post loop detection'
|
||||
);
|
||||
$this->assertEquals(
|
||||
[[
|
||||
'level' => 'Error',
|
||||
'id' => '100',
|
||||
'message' => 'Cannot assign Element to itself, this would create an infinite loop',
|
||||
'context' => ['tag' => 'div', 'id' => 'sub-1']
|
||||
]],
|
||||
Error::getMessages(),
|
||||
'check error is 100 failed'
|
||||
);
|
||||
// get sub
|
||||
$this->assertEquals(
|
||||
[$el_sub],
|
||||
$el->getSub(),
|
||||
'get sub failed'
|
||||
);
|
||||
// reset sub
|
||||
$el->resetSub();
|
||||
$this->assertEquals(
|
||||
[],
|
||||
$el->getSub(),
|
||||
'reset sub failed'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @testdox updated nested connection
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNestedChangeContent(): void
|
||||
{
|
||||
$el = new Element('div', 'build-test');
|
||||
$el_s_1 = new Element('div', 'sub-1');
|
||||
$el_s_2 = new Element('div', 'sub-2');
|
||||
$el_s_3 = new Element('div', 'sub-3');
|
||||
$el_s_4 = new Element('div', 'sub-4');
|
||||
|
||||
$el->addSub($el_s_1, $el_s_2);
|
||||
// only sub -1, -2
|
||||
$this->assertEquals(
|
||||
'<div id="build-test"><div id="sub-1"></div><div id="sub-2"></div></div>',
|
||||
$el->buildHtml(),
|
||||
'check basic nested'
|
||||
);
|
||||
|
||||
// now add -3, -4 to both -1 and -2
|
||||
$el_s_1->addSub($el_s_3, $el_s_4);
|
||||
$el_s_2->addSub($el_s_3, $el_s_4);
|
||||
$this->assertEquals(
|
||||
'<div id="build-test"><div id="sub-1"><div id="sub-3"></div><div id="sub-4">'
|
||||
. '</div></div><div id="sub-2"><div id="sub-3"></div><div id="sub-4"></div>'
|
||||
. '</div></div>',
|
||||
$el->buildHtml(),
|
||||
'check nested added'
|
||||
);
|
||||
|
||||
// now add some css to el_s_3, will update in both sets
|
||||
$el_s_3->addCss('red');
|
||||
$this->assertEquals(
|
||||
'<div id="build-test"><div id="sub-1"><div id="sub-3" class="red"></div><div id="sub-4">'
|
||||
. '</div></div><div id="sub-2"><div id="sub-3" class="red"></div><div id="sub-4"></div>'
|
||||
. '</div></div>',
|
||||
$el->buildHtml(),
|
||||
'check nested u@dated'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @testdox test change tag/id/content
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testChangeElementData(): void
|
||||
{
|
||||
$el = new Element('div', 'id', 'Content');
|
||||
// content change
|
||||
$this->assertEquals(
|
||||
'Content',
|
||||
$el->getContent(),
|
||||
'set content'
|
||||
);
|
||||
$el->setContent('New Content');
|
||||
$this->assertEquals(
|
||||
'New Content',
|
||||
$el->getContent(),
|
||||
'changed content'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'div',
|
||||
$el->getTag(),
|
||||
'set tag'
|
||||
);
|
||||
$el->setTag('span');
|
||||
$this->assertEquals(
|
||||
'span',
|
||||
$el->getTag(),
|
||||
'changed tag'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'id',
|
||||
$el->getId(),
|
||||
'set id'
|
||||
);
|
||||
$el->setId('id-2');
|
||||
$this->assertEquals(
|
||||
'id-2',
|
||||
$el->getId(),
|
||||
'changed id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @testdox test change options
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testChangeOptions(): void
|
||||
{
|
||||
$el = new Element('button', 'id', 'Action', ['css'], ['value' => '3']);
|
||||
$this->assertEquals(
|
||||
['value' => '3'],
|
||||
$el->getOptions(),
|
||||
'set option'
|
||||
);
|
||||
$el->setOptions([
|
||||
'value' => '2'
|
||||
]);
|
||||
$this->assertEquals(
|
||||
['value' => '2'],
|
||||
$el->getOptions(),
|
||||
'changed option'
|
||||
);
|
||||
// add a new one
|
||||
$el->setOptions([
|
||||
'Foo' => 'bar',
|
||||
'Moo' => 'cow'
|
||||
]);
|
||||
$this->assertEquals(
|
||||
[
|
||||
'value' => '2',
|
||||
'Foo' => 'bar',
|
||||
'Moo' => 'cow'
|
||||
],
|
||||
$el->getOptions(),
|
||||
'changed option'
|
||||
);
|
||||
}
|
||||
|
||||
// build output
|
||||
// build from array list
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @testdox build element tree from object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBuildHtmlObject(): void
|
||||
{
|
||||
// build a simple block
|
||||
// div -> div -> button
|
||||
// -> div -> span
|
||||
// -> div -> input
|
||||
$el = new Element('div', 'master', '', ['master']);
|
||||
$el->addSub(
|
||||
Element::addElement(
|
||||
new Element('div', 'div-button', '', ['dv-bt']),
|
||||
new Element('button', 'button-id', 'Click me', ['bt-red'], [
|
||||
'OnClick' => 'action();',
|
||||
'value' => 'click',
|
||||
'type' => 'button'
|
||||
]),
|
||||
),
|
||||
Element::addElement(
|
||||
new Element('div', 'div-span', '', ['dv-sp']),
|
||||
new Element('span', 'span-id', 'Big important message<br>other', ['red']),
|
||||
),
|
||||
Element::addElement(
|
||||
new Element('div', 'div-input', '', ['dv-in']),
|
||||
new Element('input', 'input-id', '', ['in-blue'], [
|
||||
'OnClick' => 'otherAction();',
|
||||
'value' => 'Touch',
|
||||
'type' => 'button'
|
||||
]),
|
||||
),
|
||||
);
|
||||
$this->assertEquals(
|
||||
'<div id="master" class="master">'
|
||||
. '<div id="div-button" class="dv-bt">'
|
||||
. '<button id="button-id" name="button-id" class="bt-red" OnClick="action();" '
|
||||
. 'value="click" type="button">Click me</button>'
|
||||
. '</div>'
|
||||
. '<div id="div-span" class="dv-sp">'
|
||||
. '<span id="span-id" class="red">Big important message<br>other</span>'
|
||||
. '</div>'
|
||||
. '<div id="div-input" class="dv-in">'
|
||||
. '<input id="input-id" name="input-id" '
|
||||
. 'class="in-blue" OnClick="otherAction();" value="Touch" type="button">'
|
||||
. '</div>'
|
||||
. '</div>',
|
||||
$el->buildHtml()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @testdox build elements from array list
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testbuildHtmlArray(): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
'<div id="id-1">A</div>'
|
||||
. '<div id="id-2">B</div>'
|
||||
. '<div id="id-3">C</div>',
|
||||
Element::buildHtmlFromList([
|
||||
new Element('div', 'id-1', 'A'),
|
||||
new Element('div', 'id-2', 'B'),
|
||||
new Element('div', 'id-3', 'C'),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @testdox check for invalid tag detection, possible invalid id, possible invalid css
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testInvalidElement(): void
|
||||
{
|
||||
Error::resetMessages();
|
||||
$this->expectException(HtmlBuilderExcpetion::class);
|
||||
$this->expectExceptionMessage("Could not create Element");
|
||||
$el = new Element('');
|
||||
$this->assertTrue(
|
||||
Error::hasError(),
|
||||
'failed to set error invalid tag detection'
|
||||
);
|
||||
$this->assertEquals(
|
||||
[[
|
||||
'level' => 'Error',
|
||||
'id' => '201',
|
||||
'message' => 'invalid or empty tag',
|
||||
'context' => ['tag' => '']
|
||||
]],
|
||||
Error::getMessages(),
|
||||
'check error message failed'
|
||||
);
|
||||
|
||||
// if we set invalid tag
|
||||
$el = new Element('div');
|
||||
$this->expectException(HtmlBuilderExcpetion::class);
|
||||
$this->expectExceptionMessageMatches("/^Invalid or empty tag: /");
|
||||
$this->expectExceptionMessage("Invalid or empty tag: 123123");
|
||||
$el->setTag('123123');
|
||||
$this->assertTrue(
|
||||
Error::hasError(),
|
||||
'failed to set error invalid tag detection'
|
||||
);
|
||||
$this->assertEquals(
|
||||
[[
|
||||
'level' => 'Error',
|
||||
'id' => '201',
|
||||
'message' => 'invalid or empty tag',
|
||||
'context' => ['tag' => '']
|
||||
]],
|
||||
Error::getMessages(),
|
||||
'check error message failed'
|
||||
);
|
||||
|
||||
|
||||
// invalid id (warning)
|
||||
Error::resetMessages();
|
||||
$el = new Element('div', '-$a15');
|
||||
$this->assertTrue(
|
||||
Error::hasWarning(),
|
||||
'failed to set warning invalid id detection'
|
||||
);
|
||||
$this->assertEquals(
|
||||
[[
|
||||
'level' => 'Warning',
|
||||
'id' => '202',
|
||||
'message' => 'possible invalid id',
|
||||
'context' => ['id' => '-$a15', 'tag' => 'div']
|
||||
]],
|
||||
Error::getMessages(),
|
||||
'check error message failed'
|
||||
);
|
||||
|
||||
// invalid name
|
||||
Error::resetMessages();
|
||||
$el = new Element('div', 'valid', '', [], ['name' => '-$asdf&']);
|
||||
$this->assertTrue(
|
||||
Error::hasWarning(),
|
||||
'failed to set warning invalid name detection'
|
||||
);
|
||||
$this->assertEquals(
|
||||
[[
|
||||
'level' => 'Warning',
|
||||
'id' => '203',
|
||||
'message' => 'possible invalid name',
|
||||
'context' => ['name' => '-$asdf&', 'id' => 'valid', 'tag' => 'div']
|
||||
]],
|
||||
Error::getMessages(),
|
||||
'check error message failed'
|
||||
);
|
||||
}
|
||||
|
||||
// static add element
|
||||
// print object/array
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use CoreLibs\Template\HtmlBuilder\StringReplace;
|
||||
|
||||
/**
|
||||
* Test class for Template\HtmlBuilder\StringReplace
|
||||
* @coversDefaultClass \CoreLibs\Template\HtmlBuilder\StringReplace
|
||||
* @testdox \CoreLibs\Template\HtmlBuilder\StringReplace method tests
|
||||
*/
|
||||
final class CoreLibsTemplateHtmlBuilderStringReplaceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::replaceData
|
||||
* @testdox test basic replaceData
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReplaceData(): void
|
||||
{
|
||||
$html_block = <<<HTML
|
||||
<div id="{ID}" class="{CSS}">
|
||||
{CONTENT}
|
||||
</div>
|
||||
HTML;
|
||||
|
||||
$this->assertEquals(
|
||||
<<<HTML
|
||||
<div id="block-id" class="blue,red">
|
||||
Some content here<br>with bla bla inside
|
||||
</div>
|
||||
HTML,
|
||||
StringReplace::replaceData(
|
||||
$html_block,
|
||||
[
|
||||
'ID' => 'block-id',
|
||||
'CSS' => join(',', ['blue', 'red']),
|
||||
'{CONTENT}' => 'Some content here<br>with bla bla inside',
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @testdox replaceData error
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
/* public function testReplaceDataErrors(): void
|
||||
{
|
||||
$this->expectException(HtmlBuilderExcpetion::class);
|
||||
$this->expectExceptionMessage("Replace and content array count differ");
|
||||
StringReplace::replaceData('<span>{FOO}</span>', ['{FOO}', '{BAR}'], ['foo']);
|
||||
} */
|
||||
}
|
||||
|
||||
// __END__
|
||||
15
4dev/tests/bootstrap.php
Normal file
15
4dev/tests/bootstrap.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
$set = 0;
|
||||
foreach (['/../../www', '/../www', '/../..', '/..', '/../../src', '/../src'] as $src) {
|
||||
if (is_file(dirname(__DIR__) . $src . '/vendor/autoload.php')) {
|
||||
require dirname(__DIR__) . $src . '/vendor/autoload.php';
|
||||
$set = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$set) {
|
||||
die("Cannot find /vendor/autoload.php in reference to: " . dirname(__DIR__));
|
||||
}
|
||||
|
||||
// __END__
|
||||
43
README.Exceptions.md
Normal file
43
README.Exceptions.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# Exception rules
|
||||
|
||||
What exceptions to use for what
|
||||
|
||||
NOTE: There will be custom Excpetions creaed and so some rules will change
|
||||
|
||||
NOTE: For catching: always catch \Exception at the end to avoid missing some changed exceptions
|
||||
|
||||
NOTE: Changed exceptions will have marked as critical API change
|
||||
|
||||
## \Exception
|
||||
|
||||
if there is nothing else matching, use this one
|
||||
|
||||
## \InvalidArgumentException
|
||||
|
||||
if argument to a function is not expected type
|
||||
|
||||
## \UnexpectedValueException
|
||||
|
||||
If the value is not matching to what we expect
|
||||
|
||||
## \LengthException
|
||||
|
||||
Given value is out of range
|
||||
|
||||
## \RuntimeException
|
||||
|
||||
Missing php modules or external programs
|
||||
|
||||
## \OutOfRangeException
|
||||
|
||||
Not in range of given expression (array or other)
|
||||
|
||||
## Below are ERRORs
|
||||
|
||||
### \ArgumentCountError [ERROR]
|
||||
|
||||
If we have dynamic argument methods and we are missing a certain arguemnt count
|
||||
|
||||
### \TypeError
|
||||
|
||||
Invalid type
|
||||
22
README.md
22
README.md
@@ -92,3 +92,25 @@ Loads classes internal (not passed in, not extend)
|
||||
* \CoreLibs\Admin\EditBase loads \CoreLibs\Template\SmartyExtend, \CoreLibs\Output\Form\Generate
|
||||
* \CoreLibs\Output\From\Generate loads \CoreLibs\Debug\Logging, \CoreLibs\Language\L10n if not passed on
|
||||
* \CoreLibs\Output\From\Generate loads \CoreLibs\Output\From\TableArrays
|
||||
|
||||
## PHP unit testing and Intelephense
|
||||
|
||||
Intelephense can not directly read phar files so we do the following
|
||||
|
||||
In the workspace root we have `.libs/`, be in the workspace folder not the `.libs/` folder
|
||||
|
||||
`php -r "(new Phar('/path/to/.phive/phars/phpunit-9.6.13.phar'))->extractTo('.libs/phpunit/');"`
|
||||
|
||||
andd add in vscode Intelephense > Enviroment: Include Paths (intelephense.environment.includePaths)
|
||||
|
||||
```json
|
||||
"intelephense.environment.includePaths": [
|
||||
"/.libs/phpunit/"
|
||||
]
|
||||
```
|
||||
|
||||
Add `.libs` to the master .gitingore
|
||||
|
||||
### Update phpunit
|
||||
|
||||
On a version update the old phpunit folder in .libs has to be removed and the new version extracted again
|
||||
|
||||
@@ -3,17 +3,7 @@
|
||||
"version": "dev-master",
|
||||
"description": "CoreLibs: Development package",
|
||||
"type": "library",
|
||||
"require-dev": {
|
||||
"phpstan/phpstan": "^1.10",
|
||||
"phan/phan": "^5.4",
|
||||
"phpstan/extension-installer": "^1.2",
|
||||
"vimeo/psalm": "^5.7",
|
||||
"phpstan/phpstan-deprecation-rules": "^1.1"
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"phpstan/extension-installer": true
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.1"
|
||||
|
||||
2563
composer.lock
generated
2563
composer.lock
generated
File diff suppressed because it is too large
Load Diff
15
jsconfig.json
Normal file
15
jsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Node",
|
||||
"target": "ES2020",
|
||||
"jsx": "react",
|
||||
"allowImportingTsExtensions": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"**/node_modules/*"
|
||||
]
|
||||
}
|
||||
18
phpcs.xml
Normal file
18
phpcs.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset name="MyStandard">
|
||||
<description>PSR12 override rules (strict, standard). Switch spaces indent to tab.</description>
|
||||
<arg name="tab-width" value="4"/>
|
||||
<rule ref="PSR1"/>
|
||||
<rule ref="PSR12">
|
||||
<!-- turn off white space check for tab -->
|
||||
<exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
|
||||
</rule>
|
||||
<!-- no space indent, must be tab, 4 is tab iwdth -->
|
||||
<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
|
||||
<rule ref="Generic.WhiteSpace.ScopeIndent">
|
||||
<properties>
|
||||
<property name="indent" value="4"/>
|
||||
<property name="tabIndent" value="true"/>
|
||||
</properties>
|
||||
</rule>
|
||||
</ruleset>
|
||||
@@ -1,47 +0,0 @@
|
||||
parameters:
|
||||
ignoreErrors:
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$connection of function pg_escape_bytea expects PgSql\\\\Connection\\|string, object\\|resource given\\.$#"
|
||||
count: 1
|
||||
path: www/lib/CoreLibs/DB/SQL/PgSQL.php
|
||||
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$connection of function pg_escape_identifier expects PgSql\\\\Connection\\|string, object\\|resource given\\.$#"
|
||||
count: 1
|
||||
path: www/lib/CoreLibs/DB/SQL/PgSQL.php
|
||||
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$connection of function pg_escape_literal expects PgSql\\\\Connection\\|string, object\\|resource given\\.$#"
|
||||
count: 1
|
||||
path: www/lib/CoreLibs/DB/SQL/PgSQL.php
|
||||
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$connection of function pg_escape_string expects PgSql\\\\Connection\\|string, object\\|resource given\\.$#"
|
||||
count: 1
|
||||
path: www/lib/CoreLibs/DB/SQL/PgSQL.php
|
||||
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$connection of function pg_execute expects PgSql\\\\Connection\\|string, object\\|resource given\\.$#"
|
||||
count: 1
|
||||
path: www/lib/CoreLibs/DB/SQL/PgSQL.php
|
||||
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$connection of function pg_parameter_status expects PgSql\\\\Connection\\|string, object\\|resource given\\.$#"
|
||||
count: 1
|
||||
path: www/lib/CoreLibs/DB/SQL/PgSQL.php
|
||||
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$connection of function pg_prepare expects PgSql\\\\Connection\\|string, object\\|resource given\\.$#"
|
||||
count: 1
|
||||
path: www/lib/CoreLibs/DB/SQL/PgSQL.php
|
||||
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$connection of function pg_query expects PgSql\\\\Connection\\|string, object\\|resource given\\.$#"
|
||||
count: 1
|
||||
path: www/lib/CoreLibs/DB/SQL/PgSQL.php
|
||||
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$connection of function pg_query_params expects PgSql\\\\Connection\\|string, object\\|resource given\\.$#"
|
||||
count: 1
|
||||
path: www/lib/CoreLibs/DB/SQL/PgSQL.php
|
||||
|
||||
@@ -39,9 +39,9 @@ parameters:
|
||||
- www/vendor
|
||||
# ignore errores with
|
||||
ignoreErrors:
|
||||
- # in the class_test tree we allow deprecated calls
|
||||
message: "#^Call to deprecated method #"
|
||||
path: %currentWorkingDirectory%/www/admin/class_test.*.php
|
||||
# - # in the class_test tree we allow deprecated calls
|
||||
# message: "#^Call to deprecated method #"
|
||||
# path: %currentWorkingDirectory%/www/admin/class_test.*.php
|
||||
# - '#Expression in empty\(\) is always falsy.#'
|
||||
# -
|
||||
# message: '#Reflection error: [a-zA-Z0-9\\_]+ not found.#'
|
||||
@@ -53,3 +53,6 @@ parameters:
|
||||
# paths:
|
||||
# - ...
|
||||
# - ...
|
||||
#-
|
||||
# message: "#^Call to deprecated method #"
|
||||
# path: www/admin/class_test*.php
|
||||
|
||||
@@ -3,5 +3,6 @@
|
||||
colors="true"
|
||||
verbose="true"
|
||||
convertDeprecationsToExceptions="true"
|
||||
bootstrap="4dev/tests/bootstrap.php"
|
||||
>
|
||||
</phpunit>
|
||||
|
||||
1
tools/phan
Symbolic link
1
tools/phan
Symbolic link
@@ -0,0 +1 @@
|
||||
/home/clemens/.phive/phars/phan-5.4.3.phar
|
||||
1
tools/php-cs-fixer
Symbolic link
1
tools/php-cs-fixer
Symbolic link
@@ -0,0 +1 @@
|
||||
/home/clemens/.phive/phars/php-cs-fixer-3.51.0.phar
|
||||
1
tools/phpDocumentor
Symbolic link
1
tools/phpDocumentor
Symbolic link
@@ -0,0 +1 @@
|
||||
/home/clemens/.phive/phars/phpdocumentor-3.4.3.phar
|
||||
1
tools/phpcbf
Symbolic link
1
tools/phpcbf
Symbolic link
@@ -0,0 +1 @@
|
||||
/home/clemens/.phive/phars/phpcbf-3.9.0.phar
|
||||
1
tools/phpcs
Symbolic link
1
tools/phpcs
Symbolic link
@@ -0,0 +1 @@
|
||||
/home/clemens/.phive/phars/phpcs-3.9.0.phar
|
||||
1
tools/phpdox
Symbolic link
1
tools/phpdox
Symbolic link
@@ -0,0 +1 @@
|
||||
/home/clemens/.phive/phars/phpdox-0.12.0.phar
|
||||
1
tools/phpstan
Symbolic link
1
tools/phpstan
Symbolic link
@@ -0,0 +1 @@
|
||||
/home/clemens/.phive/phars/phpstan-1.10.59.phar
|
||||
1
tools/phpunit
Symbolic link
1
tools/phpunit
Symbolic link
@@ -0,0 +1 @@
|
||||
/home/clemens/.phive/phars/phpunit-9.6.17.phar
|
||||
1
tools/psalm
Symbolic link
1
tools/psalm
Symbolic link
@@ -0,0 +1 @@
|
||||
/home/clemens/.phive/phars/psalm-5.22.2.phar
|
||||
23
vendor/amphp/amp/LICENSE
vendored
23
vendor/amphp/amp/LICENSE
vendored
@@ -1,23 +0,0 @@
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2019 amphp
|
||||
Copyright (c) 2016 PHP Asynchronous Interoperability Group
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
74
vendor/amphp/amp/composer.json
vendored
74
vendor/amphp/amp/composer.json
vendored
@@ -1,74 +0,0 @@
|
||||
{
|
||||
"name": "amphp/amp",
|
||||
"homepage": "https://amphp.org/amp",
|
||||
"description": "A non-blocking concurrency framework for PHP applications.",
|
||||
"keywords": [
|
||||
"async",
|
||||
"asynchronous",
|
||||
"concurrency",
|
||||
"promise",
|
||||
"awaitable",
|
||||
"future",
|
||||
"non-blocking",
|
||||
"event",
|
||||
"event-loop"
|
||||
],
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Daniel Lowrey",
|
||||
"email": "rdlowrey@php.net"
|
||||
},
|
||||
{
|
||||
"name": "Aaron Piotrowski",
|
||||
"email": "aaron@trowski.com"
|
||||
},
|
||||
{
|
||||
"name": "Bob Weinand",
|
||||
"email": "bobwei9@hotmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Niklas Keller",
|
||||
"email": "me@kelunik.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"ext-json": "*",
|
||||
"amphp/phpunit-util": "^1",
|
||||
"amphp/php-cs-fixer-config": "dev-master",
|
||||
"react/promise": "^2",
|
||||
"phpunit/phpunit": "^7 | ^8 | ^9",
|
||||
"psalm/phar": "^3.11@dev",
|
||||
"jetbrains/phpstorm-stubs": "^2019.3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Amp\\": "lib"
|
||||
},
|
||||
"files": [
|
||||
"lib/functions.php",
|
||||
"lib/Internal/functions.php"
|
||||
]
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Amp\\Test\\": "test"
|
||||
}
|
||||
},
|
||||
"support": {
|
||||
"issues": "https://github.com/amphp/amp/issues",
|
||||
"irc": "irc://irc.freenode.org/amphp"
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.x-dev"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"test": "@php -dzend.assertions=1 -dassert.exception=1 ./vendor/bin/phpunit",
|
||||
"code-style": "@php ./vendor/bin/php-cs-fixer fix"
|
||||
}
|
||||
}
|
||||
80
vendor/amphp/amp/lib/CallableMaker.php
vendored
80
vendor/amphp/amp/lib/CallableMaker.php
vendored
@@ -1,80 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
if (\PHP_VERSION_ID < 70100) {
|
||||
/** @psalm-suppress DuplicateClass */
|
||||
trait CallableMaker
|
||||
{
|
||||
/** @var \ReflectionClass */
|
||||
private static $__reflectionClass;
|
||||
|
||||
/** @var \ReflectionMethod[] */
|
||||
private static $__reflectionMethods = [];
|
||||
|
||||
/**
|
||||
* Creates a callable from a protected or private instance method that may be invoked by callers requiring a
|
||||
* publicly invokable callback.
|
||||
*
|
||||
* @param string $method Instance method name.
|
||||
*
|
||||
* @return callable
|
||||
*
|
||||
* @psalm-suppress MixedInferredReturnType
|
||||
*/
|
||||
private function callableFromInstanceMethod(string $method): callable
|
||||
{
|
||||
if (!isset(self::$__reflectionMethods[$method])) {
|
||||
if (self::$__reflectionClass === null) {
|
||||
self::$__reflectionClass = new \ReflectionClass(self::class);
|
||||
}
|
||||
self::$__reflectionMethods[$method] = self::$__reflectionClass->getMethod($method);
|
||||
}
|
||||
|
||||
return self::$__reflectionMethods[$method]->getClosure($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a callable from a protected or private static method that may be invoked by methods requiring a
|
||||
* publicly invokable callback.
|
||||
*
|
||||
* @param string $method Static method name.
|
||||
*
|
||||
* @return callable
|
||||
*
|
||||
* @psalm-suppress MixedInferredReturnType
|
||||
*/
|
||||
private static function callableFromStaticMethod(string $method): callable
|
||||
{
|
||||
if (!isset(self::$__reflectionMethods[$method])) {
|
||||
if (self::$__reflectionClass === null) {
|
||||
self::$__reflectionClass = new \ReflectionClass(self::class);
|
||||
}
|
||||
self::$__reflectionMethods[$method] = self::$__reflectionClass->getMethod($method);
|
||||
}
|
||||
|
||||
return self::$__reflectionMethods[$method]->getClosure();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/** @psalm-suppress DuplicateClass */
|
||||
trait CallableMaker
|
||||
{
|
||||
/**
|
||||
* @deprecated Use \Closure::fromCallable() instead of this method in PHP 7.1.
|
||||
*/
|
||||
private function callableFromInstanceMethod(string $method): callable
|
||||
{
|
||||
return \Closure::fromCallable([$this, $method]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use \Closure::fromCallable() instead of this method in PHP 7.1.
|
||||
*/
|
||||
private static function callableFromStaticMethod(string $method): callable
|
||||
{
|
||||
return \Closure::fromCallable([self::class, $method]);
|
||||
}
|
||||
}
|
||||
} // @codeCoverageIgnoreEnd
|
||||
49
vendor/amphp/amp/lib/CancellationToken.php
vendored
49
vendor/amphp/amp/lib/CancellationToken.php
vendored
@@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* Cancellation tokens are simple objects that allow registering handlers to subscribe to cancellation requests.
|
||||
*/
|
||||
interface CancellationToken
|
||||
{
|
||||
/**
|
||||
* Subscribes a new handler to be invoked on a cancellation request.
|
||||
*
|
||||
* This handler might be invoked immediately in case the token has already been cancelled. Returned generators will
|
||||
* automatically be run as coroutines. Any unhandled exceptions will be throw into the event loop.
|
||||
*
|
||||
* @param callable(CancelledException) $callback Callback to be invoked on a cancellation request. Will receive a
|
||||
* `CancelledException` as first argument that may be used to fail the operation's promise.
|
||||
*
|
||||
* @return string Identifier that can be used to cancel the subscription.
|
||||
*/
|
||||
public function subscribe(callable $callback): string;
|
||||
|
||||
/**
|
||||
* Unsubscribes a previously registered handler.
|
||||
*
|
||||
* The handler will no longer be called as long as this method isn't invoked from a subscribed callback.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function unsubscribe(string $id);
|
||||
|
||||
/**
|
||||
* Returns whether cancellation has been requested yet.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isRequested(): bool;
|
||||
|
||||
/**
|
||||
* Throws the `CancelledException` if cancellation has been requested, otherwise does nothing.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws CancelledException
|
||||
*/
|
||||
public function throwIfRequested();
|
||||
}
|
||||
163
vendor/amphp/amp/lib/CancellationTokenSource.php
vendored
163
vendor/amphp/amp/lib/CancellationTokenSource.php
vendored
@@ -1,163 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
use function Amp\Promise\rethrow;
|
||||
|
||||
/**
|
||||
* A cancellation token source provides a mechanism to cancel operations.
|
||||
*
|
||||
* Cancellation of operation works by creating a cancellation token source and passing the corresponding token when
|
||||
* starting the operation. To cancel the operation, invoke `CancellationTokenSource::cancel()`.
|
||||
*
|
||||
* Any operation can decide what to do on a cancellation request, it has "don't care" semantics. An operation SHOULD be
|
||||
* aborted, but MAY continue. Example: A DNS client might continue to receive and cache the response, as the query has
|
||||
* been sent anyway. An HTTP client would usually close a connection, but might not do so in case a response is close to
|
||||
* be fully received to reuse the connection.
|
||||
*
|
||||
* **Example**
|
||||
*
|
||||
* ```php
|
||||
* $tokenSource = new CancellationTokenSource;
|
||||
* $token = $tokenSource->getToken();
|
||||
*
|
||||
* $response = yield $httpClient->request("https://example.com/stream", $token);
|
||||
* $responseBody = $response->getBody();
|
||||
*
|
||||
* while (($chunk = yield $response->read()) !== null) {
|
||||
* // consume $chunk
|
||||
*
|
||||
* if ($noLongerInterested) {
|
||||
* $cancellationTokenSource->cancel();
|
||||
* break;
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @see CancellationToken
|
||||
* @see CancelledException
|
||||
*/
|
||||
final class CancellationTokenSource
|
||||
{
|
||||
/** @var CancellationToken */
|
||||
private $token;
|
||||
|
||||
/** @var callable|null */
|
||||
private $onCancel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$onCancel = null;
|
||||
|
||||
$this->token = new class($onCancel) implements CancellationToken {
|
||||
/** @var string */
|
||||
private $nextId = "a";
|
||||
|
||||
/** @var callable[] */
|
||||
private $callbacks = [];
|
||||
|
||||
/** @var \Throwable|null */
|
||||
private $exception;
|
||||
|
||||
/**
|
||||
* @param mixed $onCancel
|
||||
* @param-out callable $onCancel
|
||||
*/
|
||||
public function __construct(&$onCancel)
|
||||
{
|
||||
/** @psalm-suppress MissingClosureReturnType We still support PHP 7.0 */
|
||||
$onCancel = function (\Throwable $exception) {
|
||||
$this->exception = $exception;
|
||||
|
||||
$callbacks = $this->callbacks;
|
||||
$this->callbacks = [];
|
||||
|
||||
foreach ($callbacks as $callback) {
|
||||
$this->invokeCallback($callback);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable $callback
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function invokeCallback(callable $callback)
|
||||
{
|
||||
// No type declaration to prevent exception outside the try!
|
||||
try {
|
||||
/** @var mixed $result */
|
||||
$result = $callback($this->exception);
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
/** @psalm-var \Generator<mixed, Promise|ReactPromise|(Promise|ReactPromise)[], mixed, mixed> $result */
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use ($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public function subscribe(callable $callback): string
|
||||
{
|
||||
$id = $this->nextId++;
|
||||
|
||||
if ($this->exception) {
|
||||
$this->invokeCallback($callback);
|
||||
} else {
|
||||
$this->callbacks[$id] = $callback;
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function unsubscribe(string $id)
|
||||
{
|
||||
unset($this->callbacks[$id]);
|
||||
}
|
||||
|
||||
public function isRequested(): bool
|
||||
{
|
||||
return isset($this->exception);
|
||||
}
|
||||
|
||||
public function throwIfRequested()
|
||||
{
|
||||
if (isset($this->exception)) {
|
||||
throw $this->exception;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
$this->onCancel = $onCancel;
|
||||
}
|
||||
|
||||
public function getToken(): CancellationToken
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Throwable|null $previous Exception to be used as the previous exception to CancelledException.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function cancel(\Throwable $previous = null)
|
||||
{
|
||||
if ($this->onCancel === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$onCancel = $this->onCancel;
|
||||
$this->onCancel = null;
|
||||
$onCancel(new CancelledException($previous));
|
||||
}
|
||||
}
|
||||
17
vendor/amphp/amp/lib/CancelledException.php
vendored
17
vendor/amphp/amp/lib/CancelledException.php
vendored
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* Will be thrown in case an operation is cancelled.
|
||||
*
|
||||
* @see CancellationToken
|
||||
* @see CancellationTokenSource
|
||||
*/
|
||||
class CancelledException extends \Exception
|
||||
{
|
||||
public function __construct(\Throwable $previous = null)
|
||||
{
|
||||
parent::__construct("The operation was cancelled", 0, $previous);
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
final class CombinedCancellationToken implements CancellationToken
|
||||
{
|
||||
/** @var array{0: CancellationToken, 1: string}[] */
|
||||
private $tokens = [];
|
||||
|
||||
/** @var string */
|
||||
private $nextId = "a";
|
||||
|
||||
/** @var callable[] */
|
||||
private $callbacks = [];
|
||||
|
||||
/** @var CancelledException|null */
|
||||
private $exception;
|
||||
|
||||
public function __construct(CancellationToken ...$tokens)
|
||||
{
|
||||
$thatException = &$this->exception;
|
||||
$thatCallbacks = &$this->callbacks;
|
||||
|
||||
foreach ($tokens as $token) {
|
||||
$id = $token->subscribe(static function (CancelledException $exception) use (&$thatException, &$thatCallbacks) {
|
||||
$thatException = $exception;
|
||||
|
||||
$callbacks = $thatCallbacks;
|
||||
$thatCallbacks = [];
|
||||
|
||||
foreach ($callbacks as $callback) {
|
||||
asyncCall($callback, $thatException);
|
||||
}
|
||||
});
|
||||
|
||||
$this->tokens[] = [$token, $id];
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
foreach ($this->tokens as list($token, $id)) {
|
||||
/** @var CancellationToken $token */
|
||||
$token->unsubscribe($id);
|
||||
}
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function subscribe(callable $callback): string
|
||||
{
|
||||
$id = $this->nextId++;
|
||||
|
||||
if ($this->exception) {
|
||||
asyncCall($callback, $this->exception);
|
||||
} else {
|
||||
$this->callbacks[$id] = $callback;
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function unsubscribe(string $id)
|
||||
{
|
||||
unset($this->callbacks[$id]);
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function isRequested(): bool
|
||||
{
|
||||
foreach ($this->tokens as list($token)) {
|
||||
if ($token->isRequested()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function throwIfRequested()
|
||||
{
|
||||
foreach ($this->tokens as list($token)) {
|
||||
$token->throwIfRequested();
|
||||
}
|
||||
}
|
||||
}
|
||||
160
vendor/amphp/amp/lib/Coroutine.php
vendored
160
vendor/amphp/amp/lib/Coroutine.php
vendored
@@ -1,160 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
|
||||
/**
|
||||
* Creates a promise from a generator function yielding promises.
|
||||
*
|
||||
* When a promise is yielded, execution of the generator is interrupted until the promise is resolved. A success
|
||||
* value is sent into the generator, while a failure reason is thrown into the generator. Using a coroutine,
|
||||
* asynchronous code can be written without callbacks and be structured like synchronous code.
|
||||
*
|
||||
* @template-covariant TReturn
|
||||
* @template-implements Promise<TReturn>
|
||||
*/
|
||||
final class Coroutine implements Promise
|
||||
{
|
||||
use Internal\Placeholder;
|
||||
|
||||
/**
|
||||
* Attempts to transform the non-promise yielded from the generator into a promise, otherwise returns an instance
|
||||
* `Amp\Failure` failed with an instance of `Amp\InvalidYieldError`.
|
||||
*
|
||||
* @param mixed $yielded Non-promise yielded from generator.
|
||||
* @param \Generator $generator No type for performance, we already know the type.
|
||||
*
|
||||
* @return Promise
|
||||
*/
|
||||
private static function transform($yielded, $generator): Promise
|
||||
{
|
||||
$exception = null; // initialize here, see https://github.com/vimeo/psalm/issues/2951
|
||||
|
||||
try {
|
||||
if (\is_array($yielded)) {
|
||||
return Promise\all($yielded);
|
||||
}
|
||||
|
||||
if ($yielded instanceof ReactPromise) {
|
||||
return Promise\adapt($yielded);
|
||||
}
|
||||
|
||||
// No match, continue to returning Failure below.
|
||||
} catch (\Throwable $exception) {
|
||||
// Conversion to promise failed, fall-through to returning Failure below.
|
||||
}
|
||||
|
||||
return new Failure(new InvalidYieldError(
|
||||
$generator,
|
||||
\sprintf(
|
||||
"Unexpected yield; Expected an instance of %s or %s or an array of such instances",
|
||||
Promise::class,
|
||||
ReactPromise::class
|
||||
),
|
||||
$exception
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Generator $generator
|
||||
* @psalm-param \Generator<mixed,Promise|ReactPromise|array<array-key,
|
||||
* Promise|ReactPromise>,mixed,Promise<TReturn>|ReactPromise|TReturn> $generator
|
||||
*/
|
||||
public function __construct(\Generator $generator)
|
||||
{
|
||||
try {
|
||||
$yielded = $generator->current();
|
||||
|
||||
if (!$yielded instanceof Promise) {
|
||||
if (!$generator->valid()) {
|
||||
$this->resolve($generator->getReturn());
|
||||
return;
|
||||
}
|
||||
|
||||
$yielded = self::transform($yielded, $generator);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->fail($exception);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Throwable|null $e Exception to be thrown into the generator.
|
||||
* @param mixed $v Value to be sent into the generator.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @psalm-suppress MissingClosureParamType
|
||||
* @psalm-suppress MissingClosureReturnType
|
||||
*/
|
||||
$onResolve = function (\Throwable $e = null, $v) use ($generator, &$onResolve) {
|
||||
/** @var bool $immediate Used to control iterative coroutine continuation. */
|
||||
static $immediate = true;
|
||||
|
||||
/** @var \Throwable|null $exception Promise failure reason when executing next coroutine step, null at all other times. */
|
||||
static $exception;
|
||||
|
||||
/** @var mixed $value Promise success value when executing next coroutine step, null at all other times. */
|
||||
static $value;
|
||||
|
||||
$exception = $e;
|
||||
/** @psalm-suppress MixedAssignment */
|
||||
$value = $v;
|
||||
|
||||
if (!$immediate) {
|
||||
$immediate = true;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
try {
|
||||
do {
|
||||
if ($exception) {
|
||||
// Throw exception at current execution point.
|
||||
$yielded = $generator->throw($exception);
|
||||
} else {
|
||||
// Send the new value and execute to next yield statement.
|
||||
$yielded = $generator->send($value);
|
||||
}
|
||||
|
||||
if (!$yielded instanceof Promise) {
|
||||
if (!$generator->valid()) {
|
||||
$this->resolve($generator->getReturn());
|
||||
$onResolve = null;
|
||||
return;
|
||||
}
|
||||
|
||||
$yielded = self::transform($yielded, $generator);
|
||||
}
|
||||
|
||||
$immediate = false;
|
||||
$yielded->onResolve($onResolve);
|
||||
} while ($immediate);
|
||||
|
||||
$immediate = true;
|
||||
} catch (\Throwable $exception) {
|
||||
$this->fail($exception);
|
||||
$onResolve = null;
|
||||
} finally {
|
||||
$exception = null;
|
||||
$value = null;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
Loop::defer(static function () use ($e) {
|
||||
throw $e;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
$yielded->onResolve($onResolve);
|
||||
|
||||
unset($generator, $yielded, $onResolve);
|
||||
} catch (\Throwable $e) {
|
||||
Loop::defer(static function () use ($e) {
|
||||
throw $e;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
76
vendor/amphp/amp/lib/Deferred.php
vendored
76
vendor/amphp/amp/lib/Deferred.php
vendored
@@ -1,76 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* Deferred is a container for a promise that is resolved using the resolve() and fail() methods of this object.
|
||||
* The contained promise may be accessed using the promise() method. This object should not be part of a public
|
||||
* API, but used internally to create and resolve a promise.
|
||||
*
|
||||
* @template TValue
|
||||
*/
|
||||
final class Deferred
|
||||
{
|
||||
/** @var Promise<TValue> Has public resolve and fail methods. */
|
||||
private $resolver;
|
||||
|
||||
/** @var Promise<TValue> Hides placeholder methods */
|
||||
private $promise;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->resolver = new class implements Promise {
|
||||
use Internal\Placeholder {
|
||||
resolve as public;
|
||||
fail as public;
|
||||
isResolved as public;
|
||||
}
|
||||
};
|
||||
|
||||
$this->promise = new Internal\PrivatePromise($this->resolver);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Promise<TValue>
|
||||
*/
|
||||
public function promise(): Promise
|
||||
{
|
||||
return $this->promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fulfill the promise with the given value.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @psalm-param TValue|Promise<TValue> $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function resolve($value = null)
|
||||
{
|
||||
/** @psalm-suppress UndefinedInterfaceMethod */
|
||||
$this->resolver->resolve($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fails the promise the the given reason.
|
||||
*
|
||||
* @param \Throwable $reason
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function fail(\Throwable $reason)
|
||||
{
|
||||
/** @psalm-suppress UndefinedInterfaceMethod */
|
||||
$this->resolver->fail($reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool True if the promise has been resolved.
|
||||
*/
|
||||
public function isResolved(): bool
|
||||
{
|
||||
return $this->resolver->isResolved();
|
||||
}
|
||||
}
|
||||
58
vendor/amphp/amp/lib/Delayed.php
vendored
58
vendor/amphp/amp/lib/Delayed.php
vendored
@@ -1,58 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* Creates a promise that resolves itself with a given value after a number of milliseconds.
|
||||
*
|
||||
* @template-covariant TReturn
|
||||
* @template-implements Promise<TReturn>
|
||||
*/
|
||||
final class Delayed implements Promise
|
||||
{
|
||||
use Internal\Placeholder;
|
||||
|
||||
/** @var string|null Event loop watcher identifier. */
|
||||
private $watcher;
|
||||
|
||||
/**
|
||||
* @param int $time Milliseconds before succeeding the promise.
|
||||
* @param TReturn $value Succeed the promise with this value.
|
||||
*/
|
||||
public function __construct(int $time, $value = null)
|
||||
{
|
||||
$this->watcher = Loop::delay($time, function () use ($value) {
|
||||
$this->watcher = null;
|
||||
$this->resolve($value);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* References the internal watcher in the event loop, keeping the loop running while this promise is pending.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function reference(): self
|
||||
{
|
||||
if ($this->watcher !== null) {
|
||||
Loop::reference($this->watcher);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unreferences the internal watcher in the event loop, allowing the loop to stop while this promise is pending if
|
||||
* no other events are pending in the loop.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function unreference(): self
|
||||
{
|
||||
if ($this->watcher !== null) {
|
||||
Loop::unreference($this->watcher);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
84
vendor/amphp/amp/lib/Emitter.php
vendored
84
vendor/amphp/amp/lib/Emitter.php
vendored
@@ -1,84 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* Emitter is a container for an iterator that can emit values using the emit() method and completed using the
|
||||
* complete() and fail() methods of this object. The contained iterator may be accessed using the iterate()
|
||||
* method. This object should not be part of a public API, but used internally to create and emit values to an
|
||||
* iterator.
|
||||
*
|
||||
* @template TValue
|
||||
*/
|
||||
final class Emitter
|
||||
{
|
||||
/** @var Iterator<TValue> Has public emit, complete, and fail methods. */
|
||||
private $emitter;
|
||||
|
||||
/** @var Iterator<TValue> Hides producer methods. */
|
||||
private $iterator;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->emitter = new class implements Iterator {
|
||||
use Internal\Producer {
|
||||
emit as public;
|
||||
complete as public;
|
||||
fail as public;
|
||||
}
|
||||
};
|
||||
|
||||
$this->iterator = new Internal\PrivateIterator($this->emitter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator
|
||||
* @psalm-return Iterator<TValue>
|
||||
*/
|
||||
public function iterate(): Iterator
|
||||
{
|
||||
return $this->iterator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits a value to the iterator.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @psalm-param TValue $value
|
||||
*
|
||||
* @return Promise
|
||||
* @psalm-return Promise<null>
|
||||
* @psalm-suppress MixedInferredReturnType
|
||||
* @psalm-suppress MixedReturnStatement
|
||||
*/
|
||||
public function emit($value): Promise
|
||||
{
|
||||
/** @psalm-suppress UndefinedInterfaceMethod */
|
||||
return $this->emitter->emit($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Completes the iterator.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function complete()
|
||||
{
|
||||
/** @psalm-suppress UndefinedInterfaceMethod */
|
||||
$this->emitter->complete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fails the iterator with the given reason.
|
||||
*
|
||||
* @param \Throwable $reason
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function fail(\Throwable $reason)
|
||||
{
|
||||
/** @psalm-suppress UndefinedInterfaceMethod */
|
||||
$this->emitter->fail($reason);
|
||||
}
|
||||
}
|
||||
52
vendor/amphp/amp/lib/Failure.php
vendored
52
vendor/amphp/amp/lib/Failure.php
vendored
@@ -1,52 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
|
||||
/**
|
||||
* Creates a failed promise using the given exception.
|
||||
*
|
||||
* @template-covariant TValue
|
||||
* @template-implements Promise<TValue>
|
||||
*/
|
||||
final class Failure implements Promise
|
||||
{
|
||||
/** @var \Throwable $exception */
|
||||
private $exception;
|
||||
|
||||
/**
|
||||
* @param \Throwable $exception Rejection reason.
|
||||
*/
|
||||
public function __construct(\Throwable $exception)
|
||||
{
|
||||
$this->exception = $exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function onResolve(callable $onResolved)
|
||||
{
|
||||
try {
|
||||
/** @var mixed $result */
|
||||
$result = $onResolved($this->exception, null);
|
||||
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
Promise\rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use ($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
187
vendor/amphp/amp/lib/Internal/Placeholder.php
vendored
187
vendor/amphp/amp/lib/Internal/Placeholder.php
vendored
@@ -1,187 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Internal;
|
||||
|
||||
use Amp\Coroutine;
|
||||
use Amp\Failure;
|
||||
use Amp\Loop;
|
||||
use Amp\Promise;
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
|
||||
/**
|
||||
* Trait used by Promise implementations. Do not use this trait in your code, instead compose your class from one of
|
||||
* the available classes implementing \Amp\Promise.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
trait Placeholder
|
||||
{
|
||||
/** @var bool */
|
||||
private $resolved = false;
|
||||
|
||||
/** @var mixed */
|
||||
private $result;
|
||||
|
||||
/** @var ResolutionQueue|null|callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
|
||||
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
|
||||
* mixed>|null)|callable(\Throwable|null, mixed): void */
|
||||
private $onResolved;
|
||||
|
||||
/** @var null|array */
|
||||
private $resolutionTrace;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function onResolve(callable $onResolved)
|
||||
{
|
||||
if ($this->resolved) {
|
||||
if ($this->result instanceof Promise) {
|
||||
$this->result->onResolve($onResolved);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
/** @var mixed $result */
|
||||
$result = $onResolved(null, $this->result);
|
||||
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
Promise\rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use ($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (null === $this->onResolved) {
|
||||
$this->onResolved = $onResolved;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$this->onResolved instanceof ResolutionQueue) {
|
||||
/** @psalm-suppress InternalClass */
|
||||
$this->onResolved = new ResolutionQueue($this->onResolved);
|
||||
}
|
||||
|
||||
/** @psalm-suppress InternalMethod */
|
||||
$this->onResolved->push($onResolved);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
try {
|
||||
$this->result = null;
|
||||
} catch (\Throwable $e) {
|
||||
Loop::defer(static function () use ($e) {
|
||||
throw $e;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \Error Thrown if the promise has already been resolved.
|
||||
*/
|
||||
private function resolve($value = null)
|
||||
{
|
||||
if ($this->resolved) {
|
||||
$message = "Promise has already been resolved";
|
||||
|
||||
if (isset($this->resolutionTrace)) {
|
||||
$trace = formatStacktrace($this->resolutionTrace);
|
||||
$message .= ". Previous resolution trace:\n\n{$trace}\n\n";
|
||||
} else {
|
||||
// @codeCoverageIgnoreStart
|
||||
$message .= ", define environment variable AMP_DEBUG or const AMP_DEBUG = true and enable assertions "
|
||||
. "for a stacktrace of the previous resolution.";
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
throw new \Error($message);
|
||||
}
|
||||
|
||||
\assert((function () {
|
||||
$env = \getenv("AMP_DEBUG") ?: "0";
|
||||
if (($env !== "0" && $env !== "false") || (\defined("AMP_DEBUG") && \AMP_DEBUG)) {
|
||||
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
\array_shift($trace); // remove current closure
|
||||
$this->resolutionTrace = $trace;
|
||||
}
|
||||
|
||||
return true;
|
||||
})());
|
||||
|
||||
if ($value instanceof ReactPromise) {
|
||||
$value = Promise\adapt($value);
|
||||
}
|
||||
|
||||
$this->resolved = true;
|
||||
$this->result = $value;
|
||||
|
||||
if ($this->onResolved === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$onResolved = $this->onResolved;
|
||||
$this->onResolved = null;
|
||||
|
||||
if ($this->result instanceof Promise) {
|
||||
$this->result->onResolve($onResolved);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
/** @var mixed $result */
|
||||
$result = $onResolved(null, $this->result);
|
||||
$onResolved = null; // allow garbage collection of $onResolved, to catch any exceptions from destructors
|
||||
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
Promise\rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use ($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Throwable $reason Failure reason.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function fail(\Throwable $reason)
|
||||
{
|
||||
$this->resolve(new Failure($reason));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool True if the placeholder has been resolved.
|
||||
*/
|
||||
private function isResolved(): bool
|
||||
{
|
||||
return $this->resolved;
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Internal;
|
||||
|
||||
use Amp\Iterator;
|
||||
use Amp\Promise;
|
||||
|
||||
/**
|
||||
* Wraps an Iterator instance that has public methods to emit, complete, and fail into an object that only allows
|
||||
* access to the public API methods.
|
||||
*
|
||||
* @template-covariant TValue
|
||||
* @template-implements Iterator<TValue>
|
||||
*/
|
||||
final class PrivateIterator implements Iterator
|
||||
{
|
||||
/** @var Iterator<TValue> */
|
||||
private $iterator;
|
||||
|
||||
/**
|
||||
* @param Iterator $iterator
|
||||
*
|
||||
* @psalm-param Iterator<TValue> $iterator
|
||||
*/
|
||||
public function __construct(Iterator $iterator)
|
||||
{
|
||||
$this->iterator = $iterator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Promise<bool>
|
||||
*/
|
||||
public function advance(): Promise
|
||||
{
|
||||
return $this->iterator->advance();
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-return TValue
|
||||
*/
|
||||
public function getCurrent()
|
||||
{
|
||||
return $this->iterator->getCurrent();
|
||||
}
|
||||
}
|
||||
25
vendor/amphp/amp/lib/Internal/PrivatePromise.php
vendored
25
vendor/amphp/amp/lib/Internal/PrivatePromise.php
vendored
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Internal;
|
||||
|
||||
use Amp\Promise;
|
||||
|
||||
/**
|
||||
* Wraps a Promise instance that has public methods to resolve and fail the promise into an object that only allows
|
||||
* access to the public API methods.
|
||||
*/
|
||||
final class PrivatePromise implements Promise
|
||||
{
|
||||
/** @var Promise */
|
||||
private $promise;
|
||||
|
||||
public function __construct(Promise $promise)
|
||||
{
|
||||
$this->promise = $promise;
|
||||
}
|
||||
|
||||
public function onResolve(callable $onResolved)
|
||||
{
|
||||
$this->promise->onResolve($onResolved);
|
||||
}
|
||||
}
|
||||
212
vendor/amphp/amp/lib/Internal/Producer.php
vendored
212
vendor/amphp/amp/lib/Internal/Producer.php
vendored
@@ -1,212 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Internal;
|
||||
|
||||
use Amp\Deferred;
|
||||
use Amp\Failure;
|
||||
use Amp\Promise;
|
||||
use Amp\Success;
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
|
||||
/**
|
||||
* Trait used by Iterator implementations. Do not use this trait in your code, instead compose your class from one of
|
||||
* the available classes implementing \Amp\Iterator.
|
||||
* Note that it is the responsibility of the user of this trait to ensure that listeners have a chance to listen first
|
||||
* before emitting values.
|
||||
*
|
||||
* @internal
|
||||
* @template-covariant TValue
|
||||
*/
|
||||
trait Producer
|
||||
{
|
||||
/** @var Promise|null */
|
||||
private $complete;
|
||||
|
||||
/** @var mixed[] */
|
||||
private $values = [];
|
||||
|
||||
/** @var Deferred[] */
|
||||
private $backPressure = [];
|
||||
|
||||
/** @var int */
|
||||
private $consumePosition = -1;
|
||||
|
||||
/** @var int */
|
||||
private $emitPosition = -1;
|
||||
|
||||
/** @var Deferred|null */
|
||||
private $waiting;
|
||||
|
||||
/** @var null|array */
|
||||
private $resolutionTrace;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return Promise<bool>
|
||||
*/
|
||||
public function advance(): Promise
|
||||
{
|
||||
if ($this->waiting !== null) {
|
||||
throw new \Error("The prior promise returned must resolve before invoking this method again");
|
||||
}
|
||||
|
||||
unset($this->values[$this->consumePosition]);
|
||||
|
||||
$position = ++$this->consumePosition;
|
||||
|
||||
if (\array_key_exists($position, $this->values)) {
|
||||
\assert(isset($this->backPressure[$position]));
|
||||
$deferred = $this->backPressure[$position];
|
||||
unset($this->backPressure[$position]);
|
||||
$deferred->resolve();
|
||||
|
||||
return new Success(true);
|
||||
}
|
||||
|
||||
if ($this->complete) {
|
||||
return $this->complete;
|
||||
}
|
||||
|
||||
$this->waiting = new Deferred;
|
||||
|
||||
return $this->waiting->promise();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return TValue
|
||||
*/
|
||||
public function getCurrent()
|
||||
{
|
||||
if (empty($this->values) && $this->complete) {
|
||||
throw new \Error("The iterator has completed");
|
||||
}
|
||||
|
||||
if (!\array_key_exists($this->consumePosition, $this->values)) {
|
||||
throw new \Error("Promise returned from advance() must resolve before calling this method");
|
||||
}
|
||||
|
||||
return $this->values[$this->consumePosition];
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits a value from the iterator. The returned promise is resolved once the emitted value has been consumed.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Promise
|
||||
* @psalm-return Promise<null>
|
||||
*
|
||||
* @throws \Error If the iterator has completed.
|
||||
*/
|
||||
private function emit($value): Promise
|
||||
{
|
||||
if ($this->complete) {
|
||||
throw new \Error("Iterators cannot emit values after calling complete");
|
||||
}
|
||||
|
||||
if ($value instanceof ReactPromise) {
|
||||
$value = Promise\adapt($value);
|
||||
}
|
||||
|
||||
if ($value instanceof Promise) {
|
||||
$deferred = new Deferred;
|
||||
$value->onResolve(function ($e, $v) use ($deferred) {
|
||||
if ($this->complete) {
|
||||
$deferred->fail(
|
||||
new \Error("The iterator was completed before the promise result could be emitted")
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($e) {
|
||||
$this->fail($e);
|
||||
$deferred->fail($e);
|
||||
return;
|
||||
}
|
||||
|
||||
$deferred->resolve($this->emit($v));
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
$position = ++$this->emitPosition;
|
||||
|
||||
$this->values[$position] = $value;
|
||||
|
||||
if ($this->waiting !== null) {
|
||||
$waiting = $this->waiting;
|
||||
$this->waiting = null;
|
||||
$waiting->resolve(true);
|
||||
return new Success; // Consumer was already waiting for a new value, so back-pressure is unnecessary.
|
||||
}
|
||||
|
||||
$this->backPressure[$position] = $pressure = new Deferred;
|
||||
|
||||
return $pressure->promise();
|
||||
}
|
||||
|
||||
/**
|
||||
* Completes the iterator.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \Error If the iterator has already been completed.
|
||||
*/
|
||||
private function complete()
|
||||
{
|
||||
if ($this->complete) {
|
||||
$message = "Iterator has already been completed";
|
||||
|
||||
if (isset($this->resolutionTrace)) {
|
||||
$trace = formatStacktrace($this->resolutionTrace);
|
||||
$message .= ". Previous completion trace:\n\n{$trace}\n\n";
|
||||
} else {
|
||||
// @codeCoverageIgnoreStart
|
||||
$message .= ", define environment variable AMP_DEBUG or const AMP_DEBUG = true and enable assertions "
|
||||
. "for a stacktrace of the previous resolution.";
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
throw new \Error($message);
|
||||
}
|
||||
|
||||
\assert((function () {
|
||||
$env = \getenv("AMP_DEBUG") ?: "0";
|
||||
if (($env !== "0" && $env !== "false") || (\defined("AMP_DEBUG") && \AMP_DEBUG)) {
|
||||
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
\array_shift($trace); // remove current closure
|
||||
$this->resolutionTrace = $trace;
|
||||
}
|
||||
|
||||
return true;
|
||||
})());
|
||||
|
||||
$this->complete = new Success(false);
|
||||
|
||||
if ($this->waiting !== null) {
|
||||
$waiting = $this->waiting;
|
||||
$this->waiting = null;
|
||||
$waiting->resolve($this->complete);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Throwable $exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function fail(\Throwable $exception)
|
||||
{
|
||||
$this->complete = new Failure($exception);
|
||||
|
||||
if ($this->waiting !== null) {
|
||||
$waiting = $this->waiting;
|
||||
$this->waiting = null;
|
||||
$waiting->resolve($this->complete);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Internal;
|
||||
|
||||
use Amp\Coroutine;
|
||||
use Amp\Loop;
|
||||
use Amp\Promise;
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
|
||||
/**
|
||||
* Stores a set of functions to be invoked when a promise is resolved.
|
||||
*
|
||||
* @internal
|
||||
* @psalm-internal Amp\Internal
|
||||
*/
|
||||
class ResolutionQueue
|
||||
{
|
||||
/** @var array<array-key, callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
|
||||
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
|
||||
* mixed>|null) | callable(\Throwable|null, mixed): void> */
|
||||
private $queue = [];
|
||||
|
||||
/**
|
||||
* @param callable|null $callback Initial callback to add to queue.
|
||||
*
|
||||
* @psalm-param null|callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
|
||||
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
|
||||
* mixed>|null) | callable(\Throwable|null, mixed): void $callback
|
||||
*/
|
||||
public function __construct(callable $callback = null)
|
||||
{
|
||||
if ($callback !== null) {
|
||||
$this->push($callback);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unrolls instances of self to avoid blowing up the call stack on resolution.
|
||||
*
|
||||
* @param callable $callback
|
||||
*
|
||||
* @psalm-param callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
|
||||
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
|
||||
* mixed>|null) | callable(\Throwable|null, mixed): void $callback
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function push(callable $callback)
|
||||
{
|
||||
if ($callback instanceof self) {
|
||||
$this->queue = \array_merge($this->queue, $callback->queue);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->queue[] = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls each callback in the queue, passing the provided values to the function.
|
||||
*
|
||||
* @param \Throwable|null $exception
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke($exception, $value)
|
||||
{
|
||||
foreach ($this->queue as $callback) {
|
||||
try {
|
||||
$result = $callback($exception, $value);
|
||||
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
Promise\rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use ($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
117
vendor/amphp/amp/lib/Internal/functions.php
vendored
117
vendor/amphp/amp/lib/Internal/functions.php
vendored
@@ -1,117 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Internal;
|
||||
|
||||
/**
|
||||
* Formats a stacktrace obtained via `debug_backtrace()`.
|
||||
*
|
||||
* @param array<array{file?: string, line: int, type?: string, class: string, function: string}> $trace Output of
|
||||
* `debug_backtrace()`.
|
||||
*
|
||||
* @return string Formatted stacktrace.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @internal
|
||||
*/
|
||||
function formatStacktrace(array $trace): string
|
||||
{
|
||||
return \implode("\n", \array_map(static function ($e, $i) {
|
||||
$line = "#{$i} ";
|
||||
|
||||
if (isset($e["file"])) {
|
||||
$line .= "{$e['file']}:{$e['line']} ";
|
||||
}
|
||||
|
||||
if (isset($e["type"])) {
|
||||
$line .= $e["class"] . $e["type"];
|
||||
}
|
||||
|
||||
return $line . $e["function"] . "()";
|
||||
}, $trace, \array_keys($trace)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a `TypeError` with a standardized error message.
|
||||
*
|
||||
* @param string[] $expected Expected types.
|
||||
* @param mixed $given Given value.
|
||||
*
|
||||
* @return \TypeError
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
function createTypeError(array $expected, $given): \TypeError
|
||||
{
|
||||
$givenType = \is_object($given) ? \sprintf("instance of %s", \get_class($given)) : \gettype($given);
|
||||
|
||||
if (\count($expected) === 1) {
|
||||
$expectedType = "Expected the following type: " . \array_pop($expected);
|
||||
} else {
|
||||
$expectedType = "Expected one of the following types: " . \implode(", ", $expected);
|
||||
}
|
||||
|
||||
return new \TypeError("{$expectedType}; {$givenType} given");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current time relative to an arbitrary point in time.
|
||||
*
|
||||
* @return int Time in milliseconds.
|
||||
*/
|
||||
function getCurrentTime(): int
|
||||
{
|
||||
/** @var int|null $startTime */
|
||||
static $startTime;
|
||||
/** @var int|null $nextWarning */
|
||||
static $nextWarning;
|
||||
|
||||
if (\PHP_INT_SIZE === 4) {
|
||||
// @codeCoverageIgnoreStart
|
||||
if ($startTime === null) {
|
||||
$startTime = \PHP_VERSION_ID >= 70300 ? \hrtime(false)[0] : \time();
|
||||
$nextWarning = \PHP_INT_MAX - 86400 * 7;
|
||||
}
|
||||
|
||||
if (\PHP_VERSION_ID >= 70300) {
|
||||
list($seconds, $nanoseconds) = \hrtime(false);
|
||||
$seconds -= $startTime;
|
||||
|
||||
if ($seconds >= $nextWarning) {
|
||||
$timeToOverflow = (\PHP_INT_MAX - $seconds * 1000) / 1000;
|
||||
\trigger_error(
|
||||
"getCurrentTime() will overflow in $timeToOverflow seconds, please restart the process before that. " .
|
||||
"You're using a 32 bit version of PHP, so time will overflow about every 24 days. Regular restarts are required.",
|
||||
\E_USER_WARNING
|
||||
);
|
||||
|
||||
/** @psalm-suppress PossiblyNullOperand */
|
||||
$nextWarning += 600; // every 10 minutes
|
||||
}
|
||||
|
||||
return (int) ($seconds * 1000 + $nanoseconds / 1000000);
|
||||
}
|
||||
|
||||
$seconds = \microtime(true) - $startTime;
|
||||
if ($seconds >= $nextWarning) {
|
||||
$timeToOverflow = (\PHP_INT_MAX - $seconds * 1000) / 1000;
|
||||
\trigger_error(
|
||||
"getCurrentTime() will overflow in $timeToOverflow seconds, please restart the process before that. " .
|
||||
"You're using a 32 bit version of PHP, so time will overflow about every 24 days. Regular restarts are required.",
|
||||
\E_USER_WARNING
|
||||
);
|
||||
|
||||
/** @psalm-suppress PossiblyNullOperand */
|
||||
$nextWarning += 600; // every 10 minutes
|
||||
}
|
||||
|
||||
return (int) ($seconds * 1000);
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
if (\PHP_VERSION_ID >= 70300) {
|
||||
list($seconds, $nanoseconds) = \hrtime(false);
|
||||
return (int) ($seconds * 1000 + $nanoseconds / 1000000);
|
||||
}
|
||||
|
||||
return (int) (\microtime(true) * 1000);
|
||||
}
|
||||
39
vendor/amphp/amp/lib/InvalidYieldError.php
vendored
39
vendor/amphp/amp/lib/InvalidYieldError.php
vendored
@@ -1,39 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
class InvalidYieldError extends \Error
|
||||
{
|
||||
/**
|
||||
* @param \Generator $generator
|
||||
* @param string $prefix
|
||||
* @param \Throwable|null $previous
|
||||
*/
|
||||
public function __construct(\Generator $generator, string $prefix, \Throwable $previous = null)
|
||||
{
|
||||
$yielded = $generator->current();
|
||||
$prefix .= \sprintf(
|
||||
"; %s yielded at key %s",
|
||||
\is_object($yielded) ? \get_class($yielded) : \gettype($yielded),
|
||||
\var_export($generator->key(), true)
|
||||
);
|
||||
|
||||
if (!$generator->valid()) {
|
||||
parent::__construct($prefix, 0, $previous);
|
||||
return;
|
||||
}
|
||||
|
||||
$reflGen = new \ReflectionGenerator($generator);
|
||||
$exeGen = $reflGen->getExecutingGenerator();
|
||||
if ($isSubgenerator = ($exeGen !== $generator)) {
|
||||
$reflGen = new \ReflectionGenerator($exeGen);
|
||||
}
|
||||
|
||||
parent::__construct(\sprintf(
|
||||
"%s on line %s in %s",
|
||||
$prefix,
|
||||
$reflGen->getExecutingLine(),
|
||||
$reflGen->getExecutingFile()
|
||||
), 0, $previous);
|
||||
}
|
||||
}
|
||||
34
vendor/amphp/amp/lib/Iterator.php
vendored
34
vendor/amphp/amp/lib/Iterator.php
vendored
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* Defines an asynchronous iterator over a set of values that is designed to be used within a coroutine.
|
||||
*
|
||||
* @template-covariant TValue
|
||||
*/
|
||||
interface Iterator
|
||||
{
|
||||
/**
|
||||
* Succeeds with true if an emitted value is available by calling getCurrent() or false if the iterator has
|
||||
* resolved. If the iterator fails, the returned promise will fail with the same exception.
|
||||
*
|
||||
* @return Promise
|
||||
* @psalm-return Promise<bool>
|
||||
*
|
||||
* @throws \Error If the prior promise returned from this method has not resolved.
|
||||
* @throws \Throwable The exception used to fail the iterator.
|
||||
*/
|
||||
public function advance(): Promise;
|
||||
|
||||
/**
|
||||
* Gets the last emitted value or throws an exception if the iterator has completed.
|
||||
*
|
||||
* @return mixed Value emitted from the iterator.
|
||||
* @psalm-return TValue
|
||||
*
|
||||
* @throws \Error If the iterator has resolved or advance() was not called before calling this method.
|
||||
* @throws \Throwable The exception used to fail the iterator.
|
||||
*/
|
||||
public function getCurrent();
|
||||
}
|
||||
44
vendor/amphp/amp/lib/LazyPromise.php
vendored
44
vendor/amphp/amp/lib/LazyPromise.php
vendored
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* Creates a promise that calls $promisor only when the result of the promise is requested (i.e. onResolve() is called
|
||||
* on the promise). $promisor can return a promise or any value. If $promisor throws an exception, the promise fails
|
||||
* with that exception. If $promisor returns a Generator, it will be run as a coroutine.
|
||||
*/
|
||||
final class LazyPromise implements Promise
|
||||
{
|
||||
/** @var callable|null */
|
||||
private $promisor;
|
||||
|
||||
/** @var Promise|null */
|
||||
private $promise;
|
||||
|
||||
/**
|
||||
* @param callable $promisor Function which starts an async operation, returning a Promise (or any value).
|
||||
* Generators will be run as a coroutine.
|
||||
*/
|
||||
public function __construct(callable $promisor)
|
||||
{
|
||||
$this->promisor = $promisor;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function onResolve(callable $onResolved)
|
||||
{
|
||||
if ($this->promise === null) {
|
||||
\assert($this->promisor !== null);
|
||||
|
||||
$provider = $this->promisor;
|
||||
$this->promisor = null;
|
||||
$this->promise = call($provider);
|
||||
}
|
||||
|
||||
\assert($this->promise !== null);
|
||||
|
||||
$this->promise->onResolve($onResolved);
|
||||
}
|
||||
}
|
||||
456
vendor/amphp/amp/lib/Loop.php
vendored
456
vendor/amphp/amp/lib/Loop.php
vendored
@@ -1,456 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
use Amp\Loop\Driver;
|
||||
use Amp\Loop\DriverFactory;
|
||||
use Amp\Loop\InvalidWatcherError;
|
||||
use Amp\Loop\UnsupportedFeatureException;
|
||||
use Amp\Loop\Watcher;
|
||||
|
||||
/**
|
||||
* Accessor to allow global access to the event loop.
|
||||
*
|
||||
* @see \Amp\Loop\Driver
|
||||
*/
|
||||
final class Loop
|
||||
{
|
||||
/**
|
||||
* @var Driver
|
||||
*/
|
||||
private static $driver;
|
||||
|
||||
/**
|
||||
* Disable construction as this is a static class.
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
// intentionally left blank
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the driver to be used for `Loop::run()`.
|
||||
*
|
||||
* @param Driver $driver
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function set(Driver $driver)
|
||||
{
|
||||
try {
|
||||
self::$driver = new class extends Driver {
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function activate(array $watchers)
|
||||
{
|
||||
throw new \Error("Can't activate watcher during garbage collection.");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function dispatch(bool $blocking)
|
||||
{
|
||||
throw new \Error("Can't dispatch during garbage collection.");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function deactivate(Watcher $watcher)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public function getHandle()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
\gc_collect_cycles();
|
||||
} finally {
|
||||
self::$driver = $driver;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the event loop and optionally execute a callback within the scope of it.
|
||||
*
|
||||
* The loop MUST continue to run until it is either stopped explicitly, no referenced watchers exist anymore, or an
|
||||
* exception is thrown that cannot be handled. Exceptions that cannot be handled are exceptions thrown from an
|
||||
* error handler or exceptions that would be passed to an error handler but none exists to handle them.
|
||||
*
|
||||
* @param callable|null $callback The callback to execute.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function run(callable $callback = null)
|
||||
{
|
||||
if ($callback) {
|
||||
self::$driver->defer($callback);
|
||||
}
|
||||
|
||||
self::$driver->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the event loop.
|
||||
*
|
||||
* When an event loop is stopped, it continues with its current tick and exits the loop afterwards. Multiple calls
|
||||
* to stop MUST be ignored and MUST NOT raise an exception.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function stop()
|
||||
{
|
||||
self::$driver->stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defer the execution of a callback.
|
||||
*
|
||||
* The deferred callable MUST be executed before any other type of watcher in a tick. Order of enabling MUST be
|
||||
* preserved when executing the callbacks.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param callable(string $watcherId, mixed $data) $callback The callback to defer. The `$watcherId` will be
|
||||
* invalidated before the callback call.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public static function defer(callable $callback, $data = null): string
|
||||
{
|
||||
return self::$driver->defer($callback, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delay the execution of a callback.
|
||||
*
|
||||
* The delay is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be determined by which
|
||||
* timers expire first, but timers with the same expiration time MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param int $delay The amount of time, in milliseconds, to delay the execution for.
|
||||
* @param callable(string $watcherId, mixed $data) $callback The callback to delay. The `$watcherId` will be
|
||||
* invalidated before the callback call.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public static function delay(int $delay, callable $callback, $data = null): string
|
||||
{
|
||||
return self::$driver->delay($delay, $callback, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repeatedly execute a callback.
|
||||
*
|
||||
* The interval between executions is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be
|
||||
* determined by which timers expire first, but timers with the same expiration time MAY be executed in any order.
|
||||
* The first execution is scheduled after the first interval period.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param int $interval The time interval, in milliseconds, to wait between executions.
|
||||
* @param callable(string $watcherId, mixed $data) $callback The callback to repeat.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public static function repeat(int $interval, callable $callback, $data = null): string
|
||||
{
|
||||
return self::$driver->repeat($interval, $callback, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a callback when a stream resource becomes readable or is closed for reading.
|
||||
*
|
||||
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
|
||||
* watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid
|
||||
* resources, but are not required to, due to the high performance impact. Watchers on closed resources are
|
||||
* therefore undefined behavior.
|
||||
*
|
||||
* Multiple watchers on the same stream MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param resource $stream The stream to monitor.
|
||||
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public static function onReadable($stream, callable $callback, $data = null): string
|
||||
{
|
||||
return self::$driver->onReadable($stream, $callback, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a callback when a stream resource becomes writable or is closed for writing.
|
||||
*
|
||||
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
|
||||
* watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid
|
||||
* resources, but are not required to, due to the high performance impact. Watchers on closed resources are
|
||||
* therefore undefined behavior.
|
||||
*
|
||||
* Multiple watchers on the same stream MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param resource $stream The stream to monitor.
|
||||
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public static function onWritable($stream, callable $callback, $data = null): string
|
||||
{
|
||||
return self::$driver->onWritable($stream, $callback, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a callback when a signal is received.
|
||||
*
|
||||
* Warning: Installing the same signal on different instances of this interface is deemed undefined behavior.
|
||||
* Implementations MAY try to detect this, if possible, but are not required to. This is due to technical
|
||||
* limitations of the signals being registered globally per process.
|
||||
*
|
||||
* Multiple watchers on the same signal MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param int $signo The signal number to monitor.
|
||||
* @param callable(string $watcherId, int $signo, mixed $data) $callback The callback to execute.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*
|
||||
* @throws UnsupportedFeatureException If signal handling is not supported.
|
||||
*/
|
||||
public static function onSignal(int $signo, callable $callback, $data = null): string
|
||||
{
|
||||
return self::$driver->onSignal($signo, $callback, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable a watcher to be active starting in the next tick.
|
||||
*
|
||||
* Watchers MUST immediately be marked as enabled, but only be activated (i.e. callbacks can be called) right before
|
||||
* the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws InvalidWatcherError If the watcher identifier is invalid.
|
||||
*/
|
||||
public static function enable(string $watcherId)
|
||||
{
|
||||
self::$driver->enable($watcherId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable a watcher immediately.
|
||||
*
|
||||
* A watcher MUST be disabled immediately, e.g. if a defer watcher disables a later defer watcher, the second defer
|
||||
* watcher isn't executed in this tick.
|
||||
*
|
||||
* Disabling a watcher MUST NOT invalidate the watcher. Calling this function MUST NOT fail, even if passed an
|
||||
* invalid watcher.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function disable(string $watcherId)
|
||||
{
|
||||
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
|
||||
// Prior to PHP 7.2, self::$driver may be unset during destruct.
|
||||
// See https://github.com/amphp/amp/issues/212.
|
||||
return;
|
||||
}
|
||||
|
||||
self::$driver->disable($watcherId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel a watcher.
|
||||
*
|
||||
* This will detatch the event loop from all resources that are associated to the watcher. After this operation the
|
||||
* watcher is permanently invalid. Calling this function MUST NOT fail, even if passed an invalid watcher.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function cancel(string $watcherId)
|
||||
{
|
||||
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
|
||||
// Prior to PHP 7.2, self::$driver may be unset during destruct.
|
||||
// See https://github.com/amphp/amp/issues/212.
|
||||
return;
|
||||
}
|
||||
|
||||
self::$driver->cancel($watcherId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reference a watcher.
|
||||
*
|
||||
* This will keep the event loop alive whilst the watcher is still being monitored. Watchers have this state by
|
||||
* default.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws InvalidWatcherError If the watcher identifier is invalid.
|
||||
*/
|
||||
public static function reference(string $watcherId)
|
||||
{
|
||||
self::$driver->reference($watcherId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unreference a watcher.
|
||||
*
|
||||
* The event loop should exit the run method when only unreferenced watchers are still being monitored. Watchers
|
||||
* are all referenced by default.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function unreference(string $watcherId)
|
||||
{
|
||||
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
|
||||
// Prior to PHP 7.2, self::$driver may be unset during destruct.
|
||||
// See https://github.com/amphp/amp/issues/212.
|
||||
return;
|
||||
}
|
||||
|
||||
self::$driver->unreference($watcherId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current loop time in millisecond increments. Note this value does not necessarily correlate to
|
||||
* wall-clock time, rather the value returned is meant to be used in relative comparisons to prior values returned
|
||||
* by this method (intervals, expiration calculations, etc.) and is only updated once per loop tick.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function now(): int
|
||||
{
|
||||
return self::$driver->now();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores information in the loop bound registry.
|
||||
*
|
||||
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages
|
||||
* MUST use their namespace as prefix for keys. They may do so by using `SomeClass::class` as key.
|
||||
*
|
||||
* If packages want to expose loop bound state to consumers other than the package, they SHOULD provide a dedicated
|
||||
* interface for that purpose instead of sharing the storage key.
|
||||
*
|
||||
* @param string $key The namespaced storage key.
|
||||
* @param mixed $value The value to be stored.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function setState(string $key, $value)
|
||||
{
|
||||
self::$driver->setState($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets information stored bound to the loop.
|
||||
*
|
||||
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages
|
||||
* MUST use their namespace as prefix for keys. They may do so by using `SomeClass::class` as key.
|
||||
*
|
||||
* If packages want to expose loop bound state to consumers other than the package, they SHOULD provide a dedicated
|
||||
* interface for that purpose instead of sharing the storage key.
|
||||
*
|
||||
* @param string $key The namespaced storage key.
|
||||
*
|
||||
* @return mixed The previously stored value or `null` if it doesn't exist.
|
||||
*/
|
||||
public static function getState(string $key)
|
||||
{
|
||||
return self::$driver->getState($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a callback to be executed when an error occurs.
|
||||
*
|
||||
* The callback receives the error as the first and only parameter. The return value of the callback gets ignored.
|
||||
* If it can't handle the error, it MUST throw the error. Errors thrown by the callback or during its invocation
|
||||
* MUST be thrown into the `run` loop and stop the driver.
|
||||
*
|
||||
* Subsequent calls to this method will overwrite the previous handler.
|
||||
*
|
||||
* @param callable(\Throwable $error)|null $callback The callback to execute. `null` will clear the
|
||||
* current handler.
|
||||
*
|
||||
* @return callable(\Throwable $error)|null The previous handler, `null` if there was none.
|
||||
*/
|
||||
public static function setErrorHandler(callable $callback = null)
|
||||
{
|
||||
return self::$driver->setErrorHandler($callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an associative array of information about the event loop driver.
|
||||
*
|
||||
* The returned array MUST contain the following data describing the driver's currently registered watchers:
|
||||
*
|
||||
* [
|
||||
* "defer" => ["enabled" => int, "disabled" => int],
|
||||
* "delay" => ["enabled" => int, "disabled" => int],
|
||||
* "repeat" => ["enabled" => int, "disabled" => int],
|
||||
* "on_readable" => ["enabled" => int, "disabled" => int],
|
||||
* "on_writable" => ["enabled" => int, "disabled" => int],
|
||||
* "on_signal" => ["enabled" => int, "disabled" => int],
|
||||
* "enabled_watchers" => ["referenced" => int, "unreferenced" => int],
|
||||
* "running" => bool
|
||||
* ];
|
||||
*
|
||||
* Implementations MAY optionally add more information in the array but at minimum the above `key => value` format
|
||||
* MUST always be provided.
|
||||
*
|
||||
* @return array Statistics about the loop in the described format.
|
||||
*/
|
||||
public static function getInfo(): array
|
||||
{
|
||||
return self::$driver->getInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the event loop driver that is in scope.
|
||||
*
|
||||
* @return Driver
|
||||
*/
|
||||
public static function get(): Driver
|
||||
{
|
||||
return self::$driver;
|
||||
}
|
||||
}
|
||||
|
||||
// Default factory, don't move this to a file loaded by the composer "files" autoload mechanism, otherwise custom
|
||||
// implementations might have issues setting a default loop, because it's overridden by us then.
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
Loop::set((new DriverFactory)->create());
|
||||
// @codeCoverageIgnoreEnd
|
||||
742
vendor/amphp/amp/lib/Loop/Driver.php
vendored
742
vendor/amphp/amp/lib/Loop/Driver.php
vendored
@@ -1,742 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Loop;
|
||||
|
||||
use Amp\Coroutine;
|
||||
use Amp\Promise;
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
use function Amp\Promise\rethrow;
|
||||
|
||||
/**
|
||||
* Event loop driver which implements all basic operations to allow interoperability.
|
||||
*
|
||||
* Watchers (enabled or new watchers) MUST immediately be marked as enabled, but only be activated (i.e. callbacks can
|
||||
* be called) right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* All registered callbacks MUST NOT be called from a file with strict types enabled (`declare(strict_types=1)`).
|
||||
*/
|
||||
abstract class Driver
|
||||
{
|
||||
// Don't use 1e3 / 1e6, they result in a float instead of int
|
||||
const MILLISEC_PER_SEC = 1000;
|
||||
const MICROSEC_PER_SEC = 1000000;
|
||||
|
||||
/** @var string */
|
||||
private $nextId = "a";
|
||||
|
||||
/** @var Watcher[] */
|
||||
private $watchers = [];
|
||||
|
||||
/** @var Watcher[] */
|
||||
private $enableQueue = [];
|
||||
|
||||
/** @var Watcher[] */
|
||||
private $deferQueue = [];
|
||||
|
||||
/** @var Watcher[] */
|
||||
private $nextTickQueue = [];
|
||||
|
||||
/** @var callable(\Throwable):void|null */
|
||||
private $errorHandler;
|
||||
|
||||
/** @var bool */
|
||||
private $running = false;
|
||||
|
||||
/** @var array */
|
||||
private $registry = [];
|
||||
|
||||
/**
|
||||
* Run the event loop.
|
||||
*
|
||||
* One iteration of the loop is called one "tick". A tick covers the following steps:
|
||||
*
|
||||
* 1. Activate watchers created / enabled in the last tick / before `run()`.
|
||||
* 2. Execute all enabled defer watchers.
|
||||
* 3. Execute all due timer, pending signal and actionable stream callbacks, each only once per tick.
|
||||
*
|
||||
* The loop MUST continue to run until it is either stopped explicitly, no referenced watchers exist anymore, or an
|
||||
* exception is thrown that cannot be handled. Exceptions that cannot be handled are exceptions thrown from an
|
||||
* error handler or exceptions that would be passed to an error handler but none exists to handle them.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$this->running = true;
|
||||
|
||||
try {
|
||||
while ($this->running) {
|
||||
if ($this->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
$this->tick();
|
||||
}
|
||||
} finally {
|
||||
$this->stop();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool True if no enabled and referenced watchers remain in the loop.
|
||||
*/
|
||||
private function isEmpty(): bool
|
||||
{
|
||||
foreach ($this->watchers as $watcher) {
|
||||
if ($watcher->enabled && $watcher->referenced) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a single tick of the event loop.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function tick()
|
||||
{
|
||||
if (empty($this->deferQueue)) {
|
||||
$this->deferQueue = $this->nextTickQueue;
|
||||
} else {
|
||||
$this->deferQueue = \array_merge($this->deferQueue, $this->nextTickQueue);
|
||||
}
|
||||
$this->nextTickQueue = [];
|
||||
|
||||
$this->activate($this->enableQueue);
|
||||
$this->enableQueue = [];
|
||||
|
||||
foreach ($this->deferQueue as $watcher) {
|
||||
if (!isset($this->deferQueue[$watcher->id])) {
|
||||
continue; // Watcher disabled by another defer watcher.
|
||||
}
|
||||
|
||||
unset($this->watchers[$watcher->id], $this->deferQueue[$watcher->id]);
|
||||
|
||||
try {
|
||||
/** @var mixed $result */
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
}
|
||||
|
||||
/** @psalm-suppress RedundantCondition */
|
||||
$this->dispatch(empty($this->nextTickQueue) && empty($this->enableQueue) && $this->running && !$this->isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Activates (enables) all the given watchers.
|
||||
*
|
||||
* @param Watcher[] $watchers
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function activate(array $watchers);
|
||||
|
||||
/**
|
||||
* Dispatches any pending read/write, timer, and signal events.
|
||||
*
|
||||
* @param bool $blocking
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function dispatch(bool $blocking);
|
||||
|
||||
/**
|
||||
* Stop the event loop.
|
||||
*
|
||||
* When an event loop is stopped, it continues with its current tick and exits the loop afterwards. Multiple calls
|
||||
* to stop MUST be ignored and MUST NOT raise an exception.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function stop()
|
||||
{
|
||||
$this->running = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defer the execution of a callback.
|
||||
*
|
||||
* The deferred callable MUST be executed before any other type of watcher in a tick. Order of enabling MUST be
|
||||
* preserved when executing the callbacks.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param callable (string $watcherId, mixed $data) $callback The callback to defer. The `$watcherId` will be
|
||||
* invalidated before the callback call.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public function defer(callable $callback, $data = null): string
|
||||
{
|
||||
/** @psalm-var Watcher<null> $watcher */
|
||||
$watcher = new Watcher;
|
||||
$watcher->type = Watcher::DEFER;
|
||||
$watcher->id = $this->nextId++;
|
||||
$watcher->callback = $callback;
|
||||
$watcher->data = $data;
|
||||
|
||||
$this->watchers[$watcher->id] = $watcher;
|
||||
$this->nextTickQueue[$watcher->id] = $watcher;
|
||||
|
||||
return $watcher->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delay the execution of a callback.
|
||||
*
|
||||
* The delay is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be determined by which
|
||||
* timers expire first, but timers with the same expiration time MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param int $delay The amount of time, in milliseconds, to delay the execution for.
|
||||
* @param callable (string $watcherId, mixed $data) $callback The callback to delay. The `$watcherId` will be
|
||||
* invalidated before the callback call.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public function delay(int $delay, callable $callback, $data = null): string
|
||||
{
|
||||
if ($delay < 0) {
|
||||
throw new \Error("Delay must be greater than or equal to zero");
|
||||
}
|
||||
|
||||
/** @psalm-var Watcher<int> $watcher */
|
||||
$watcher = new Watcher;
|
||||
$watcher->type = Watcher::DELAY;
|
||||
$watcher->id = $this->nextId++;
|
||||
$watcher->callback = $callback;
|
||||
$watcher->value = $delay;
|
||||
$watcher->expiration = $this->now() + $delay;
|
||||
$watcher->data = $data;
|
||||
|
||||
$this->watchers[$watcher->id] = $watcher;
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
|
||||
return $watcher->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Repeatedly execute a callback.
|
||||
*
|
||||
* The interval between executions is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be
|
||||
* determined by which timers expire first, but timers with the same expiration time MAY be executed in any order.
|
||||
* The first execution is scheduled after the first interval period.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param int $interval The time interval, in milliseconds, to wait between executions.
|
||||
* @param callable (string $watcherId, mixed $data) $callback The callback to repeat.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public function repeat(int $interval, callable $callback, $data = null): string
|
||||
{
|
||||
if ($interval < 0) {
|
||||
throw new \Error("Interval must be greater than or equal to zero");
|
||||
}
|
||||
|
||||
/** @psalm-var Watcher<int> $watcher */
|
||||
$watcher = new Watcher;
|
||||
$watcher->type = Watcher::REPEAT;
|
||||
$watcher->id = $this->nextId++;
|
||||
$watcher->callback = $callback;
|
||||
$watcher->value = $interval;
|
||||
$watcher->expiration = $this->now() + $interval;
|
||||
$watcher->data = $data;
|
||||
|
||||
$this->watchers[$watcher->id] = $watcher;
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
|
||||
return $watcher->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a callback when a stream resource becomes readable or is closed for reading.
|
||||
*
|
||||
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
|
||||
* watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid
|
||||
* resources, but are not required to, due to the high performance impact. Watchers on closed resources are
|
||||
* therefore undefined behavior.
|
||||
*
|
||||
* Multiple watchers on the same stream MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param resource $stream The stream to monitor.
|
||||
* @param callable (string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public function onReadable($stream, callable $callback, $data = null): string
|
||||
{
|
||||
/** @psalm-var Watcher<resource> $watcher */
|
||||
$watcher = new Watcher;
|
||||
$watcher->type = Watcher::READABLE;
|
||||
$watcher->id = $this->nextId++;
|
||||
$watcher->callback = $callback;
|
||||
$watcher->value = $stream;
|
||||
$watcher->data = $data;
|
||||
|
||||
$this->watchers[$watcher->id] = $watcher;
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
|
||||
return $watcher->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a callback when a stream resource becomes writable or is closed for writing.
|
||||
*
|
||||
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
|
||||
* watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid
|
||||
* resources, but are not required to, due to the high performance impact. Watchers on closed resources are
|
||||
* therefore undefined behavior.
|
||||
*
|
||||
* Multiple watchers on the same stream MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param resource $stream The stream to monitor.
|
||||
* @param callable (string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public function onWritable($stream, callable $callback, $data = null): string
|
||||
{
|
||||
/** @psalm-var Watcher<resource> $watcher */
|
||||
$watcher = new Watcher;
|
||||
$watcher->type = Watcher::WRITABLE;
|
||||
$watcher->id = $this->nextId++;
|
||||
$watcher->callback = $callback;
|
||||
$watcher->value = $stream;
|
||||
$watcher->data = $data;
|
||||
|
||||
$this->watchers[$watcher->id] = $watcher;
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
|
||||
return $watcher->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a callback when a signal is received.
|
||||
*
|
||||
* Warning: Installing the same signal on different instances of this interface is deemed undefined behavior.
|
||||
* Implementations MAY try to detect this, if possible, but are not required to. This is due to technical
|
||||
* limitations of the signals being registered globally per process.
|
||||
*
|
||||
* Multiple watchers on the same signal MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param int $signo The signal number to monitor.
|
||||
* @param callable (string $watcherId, int $signo, mixed $data) $callback The callback to execute.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*
|
||||
* @throws UnsupportedFeatureException If signal handling is not supported.
|
||||
*/
|
||||
public function onSignal(int $signo, callable $callback, $data = null): string
|
||||
{
|
||||
/** @psalm-var Watcher<int> $watcher */
|
||||
$watcher = new Watcher;
|
||||
$watcher->type = Watcher::SIGNAL;
|
||||
$watcher->id = $this->nextId++;
|
||||
$watcher->callback = $callback;
|
||||
$watcher->value = $signo;
|
||||
$watcher->data = $data;
|
||||
|
||||
$this->watchers[$watcher->id] = $watcher;
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
|
||||
return $watcher->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable a watcher to be active starting in the next tick.
|
||||
*
|
||||
* Watchers MUST immediately be marked as enabled, but only be activated (i.e. callbacks can be called) right before
|
||||
* the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws InvalidWatcherError If the watcher identifier is invalid.
|
||||
*/
|
||||
public function enable(string $watcherId)
|
||||
{
|
||||
if (!isset($this->watchers[$watcherId])) {
|
||||
throw new InvalidWatcherError($watcherId, "Cannot enable an invalid watcher identifier: '{$watcherId}'");
|
||||
}
|
||||
|
||||
$watcher = $this->watchers[$watcherId];
|
||||
|
||||
if ($watcher->enabled) {
|
||||
return; // Watcher already enabled.
|
||||
}
|
||||
|
||||
$watcher->enabled = true;
|
||||
|
||||
switch ($watcher->type) {
|
||||
case Watcher::DEFER:
|
||||
$this->nextTickQueue[$watcher->id] = $watcher;
|
||||
break;
|
||||
|
||||
case Watcher::REPEAT:
|
||||
case Watcher::DELAY:
|
||||
\assert(\is_int($watcher->value));
|
||||
|
||||
$watcher->expiration = $this->now() + $watcher->value;
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel a watcher.
|
||||
*
|
||||
* This will detach the event loop from all resources that are associated to the watcher. After this operation the
|
||||
* watcher is permanently invalid. Calling this function MUST NOT fail, even if passed an invalid watcher.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function cancel(string $watcherId)
|
||||
{
|
||||
$this->disable($watcherId);
|
||||
unset($this->watchers[$watcherId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable a watcher immediately.
|
||||
*
|
||||
* A watcher MUST be disabled immediately, e.g. if a defer watcher disables a later defer watcher, the second defer
|
||||
* watcher isn't executed in this tick.
|
||||
*
|
||||
* Disabling a watcher MUST NOT invalidate the watcher. Calling this function MUST NOT fail, even if passed an
|
||||
* invalid watcher.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function disable(string $watcherId)
|
||||
{
|
||||
if (!isset($this->watchers[$watcherId])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$watcher = $this->watchers[$watcherId];
|
||||
|
||||
if (!$watcher->enabled) {
|
||||
return; // Watcher already disabled.
|
||||
}
|
||||
|
||||
$watcher->enabled = false;
|
||||
$id = $watcher->id;
|
||||
|
||||
switch ($watcher->type) {
|
||||
case Watcher::DEFER:
|
||||
if (isset($this->nextTickQueue[$id])) {
|
||||
// Watcher was only queued to be enabled.
|
||||
unset($this->nextTickQueue[$id]);
|
||||
} else {
|
||||
unset($this->deferQueue[$id]);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
if (isset($this->enableQueue[$id])) {
|
||||
// Watcher was only queued to be enabled.
|
||||
unset($this->enableQueue[$id]);
|
||||
} else {
|
||||
$this->deactivate($watcher);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivates (disables) the given watcher.
|
||||
*
|
||||
* @param Watcher $watcher
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function deactivate(Watcher $watcher);
|
||||
|
||||
/**
|
||||
* Reference a watcher.
|
||||
*
|
||||
* This will keep the event loop alive whilst the watcher is still being monitored. Watchers have this state by
|
||||
* default.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws InvalidWatcherError If the watcher identifier is invalid.
|
||||
*/
|
||||
public function reference(string $watcherId)
|
||||
{
|
||||
if (!isset($this->watchers[$watcherId])) {
|
||||
throw new InvalidWatcherError($watcherId, "Cannot reference an invalid watcher identifier: '{$watcherId}'");
|
||||
}
|
||||
|
||||
$this->watchers[$watcherId]->referenced = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unreference a watcher.
|
||||
*
|
||||
* The event loop should exit the run method when only unreferenced watchers are still being monitored. Watchers
|
||||
* are all referenced by default.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function unreference(string $watcherId)
|
||||
{
|
||||
if (!isset($this->watchers[$watcherId])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->watchers[$watcherId]->referenced = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores information in the loop bound registry.
|
||||
*
|
||||
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages
|
||||
* MUST use their namespace as prefix for keys. They may do so by using `SomeClass::class` as key.
|
||||
*
|
||||
* If packages want to expose loop bound state to consumers other than the package, they SHOULD provide a dedicated
|
||||
* interface for that purpose instead of sharing the storage key.
|
||||
*
|
||||
* @param string $key The namespaced storage key.
|
||||
* @param mixed $value The value to be stored.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
final public function setState(string $key, $value)
|
||||
{
|
||||
if ($value === null) {
|
||||
unset($this->registry[$key]);
|
||||
} else {
|
||||
$this->registry[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets information stored bound to the loop.
|
||||
*
|
||||
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages
|
||||
* MUST use their namespace as prefix for keys. They may do so by using `SomeClass::class` as key.
|
||||
*
|
||||
* If packages want to expose loop bound state to consumers other than the package, they SHOULD provide a dedicated
|
||||
* interface for that purpose instead of sharing the storage key.
|
||||
*
|
||||
* @param string $key The namespaced storage key.
|
||||
*
|
||||
* @return mixed The previously stored value or `null` if it doesn't exist.
|
||||
*/
|
||||
final public function getState(string $key)
|
||||
{
|
||||
return isset($this->registry[$key]) ? $this->registry[$key] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a callback to be executed when an error occurs.
|
||||
*
|
||||
* The callback receives the error as the first and only parameter. The return value of the callback gets ignored.
|
||||
* If it can't handle the error, it MUST throw the error. Errors thrown by the callback or during its invocation
|
||||
* MUST be thrown into the `run` loop and stop the driver.
|
||||
*
|
||||
* Subsequent calls to this method will overwrite the previous handler.
|
||||
*
|
||||
* @param callable(\Throwable $error):void|null $callback The callback to execute. `null` will clear the
|
||||
* current handler.
|
||||
*
|
||||
* @return callable(\Throwable $error):void|null The previous handler, `null` if there was none.
|
||||
*/
|
||||
public function setErrorHandler(callable $callback = null)
|
||||
{
|
||||
$previous = $this->errorHandler;
|
||||
$this->errorHandler = $callback;
|
||||
return $previous;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the error handler with the given exception.
|
||||
*
|
||||
* @param \Throwable $exception The exception thrown from a watcher callback.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Throwable If no error handler has been set.
|
||||
*/
|
||||
protected function error(\Throwable $exception)
|
||||
{
|
||||
if ($this->errorHandler === null) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
($this->errorHandler)($exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current loop time in millisecond increments. Note this value does not necessarily correlate to
|
||||
* wall-clock time, rather the value returned is meant to be used in relative comparisons to prior values returned
|
||||
* by this method (intervals, expiration calculations, etc.) and is only updated once per loop tick.
|
||||
*
|
||||
* Extending classes should override this function to return a value cached once per loop tick.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function now(): int
|
||||
{
|
||||
return (int) (\microtime(true) * self::MILLISEC_PER_SEC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the underlying loop handle.
|
||||
*
|
||||
* Example: the `uv_loop` resource for `libuv` or the `EvLoop` object for `libev` or `null` for a native driver.
|
||||
*
|
||||
* Note: This function is *not* exposed in the `Loop` class. Users shall access it directly on the respective loop
|
||||
* instance.
|
||||
*
|
||||
* @return null|object|resource The loop handle the event loop operates on. `null` if there is none.
|
||||
*/
|
||||
abstract public function getHandle();
|
||||
|
||||
/**
|
||||
* Returns the same array of data as getInfo().
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __debugInfo()
|
||||
{
|
||||
// @codeCoverageIgnoreStart
|
||||
return $this->getInfo();
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an associative array of information about the event loop driver.
|
||||
*
|
||||
* The returned array MUST contain the following data describing the driver's currently registered watchers:
|
||||
*
|
||||
* [
|
||||
* "defer" => ["enabled" => int, "disabled" => int],
|
||||
* "delay" => ["enabled" => int, "disabled" => int],
|
||||
* "repeat" => ["enabled" => int, "disabled" => int],
|
||||
* "on_readable" => ["enabled" => int, "disabled" => int],
|
||||
* "on_writable" => ["enabled" => int, "disabled" => int],
|
||||
* "on_signal" => ["enabled" => int, "disabled" => int],
|
||||
* "enabled_watchers" => ["referenced" => int, "unreferenced" => int],
|
||||
* "running" => bool
|
||||
* ];
|
||||
*
|
||||
* Implementations MAY optionally add more information in the array but at minimum the above `key => value` format
|
||||
* MUST always be provided.
|
||||
*
|
||||
* @return array Statistics about the loop in the described format.
|
||||
*/
|
||||
public function getInfo(): array
|
||||
{
|
||||
$watchers = [
|
||||
"referenced" => 0,
|
||||
"unreferenced" => 0,
|
||||
];
|
||||
|
||||
$defer = $delay = $repeat = $onReadable = $onWritable = $onSignal = [
|
||||
"enabled" => 0,
|
||||
"disabled" => 0,
|
||||
];
|
||||
|
||||
foreach ($this->watchers as $watcher) {
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
$array = &$onReadable;
|
||||
break;
|
||||
case Watcher::WRITABLE:
|
||||
$array = &$onWritable;
|
||||
break;
|
||||
case Watcher::SIGNAL:
|
||||
$array = &$onSignal;
|
||||
break;
|
||||
case Watcher::DEFER:
|
||||
$array = &$defer;
|
||||
break;
|
||||
case Watcher::DELAY:
|
||||
$array = &$delay;
|
||||
break;
|
||||
case Watcher::REPEAT:
|
||||
$array = &$repeat;
|
||||
break;
|
||||
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
if ($watcher->enabled) {
|
||||
++$array["enabled"];
|
||||
|
||||
if ($watcher->referenced) {
|
||||
++$watchers["referenced"];
|
||||
} else {
|
||||
++$watchers["unreferenced"];
|
||||
}
|
||||
} else {
|
||||
++$array["disabled"];
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
"enabled_watchers" => $watchers,
|
||||
"defer" => $defer,
|
||||
"delay" => $delay,
|
||||
"repeat" => $repeat,
|
||||
"on_readable" => $onReadable,
|
||||
"on_writable" => $onWritable,
|
||||
"on_signal" => $onSignal,
|
||||
"running" => (bool) $this->running,
|
||||
];
|
||||
}
|
||||
}
|
||||
73
vendor/amphp/amp/lib/Loop/DriverFactory.php
vendored
73
vendor/amphp/amp/lib/Loop/DriverFactory.php
vendored
@@ -1,73 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Loop;
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
class DriverFactory
|
||||
{
|
||||
/**
|
||||
* Creates a new loop instance and chooses the best available driver.
|
||||
*
|
||||
* @return Driver
|
||||
*
|
||||
* @throws \Error If an invalid class has been specified via AMP_LOOP_DRIVER
|
||||
*/
|
||||
public function create(): Driver
|
||||
{
|
||||
$driver = (function () {
|
||||
if ($driver = $this->createDriverFromEnv()) {
|
||||
return $driver;
|
||||
}
|
||||
|
||||
if (UvDriver::isSupported()) {
|
||||
return new UvDriver;
|
||||
}
|
||||
|
||||
if (EvDriver::isSupported()) {
|
||||
return new EvDriver;
|
||||
}
|
||||
|
||||
if (EventDriver::isSupported()) {
|
||||
return new EventDriver;
|
||||
}
|
||||
|
||||
return new NativeDriver;
|
||||
})();
|
||||
|
||||
if (\getenv("AMP_DEBUG_TRACE_WATCHERS")) {
|
||||
return new TracingDriver($driver);
|
||||
}
|
||||
|
||||
return $driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Driver|null
|
||||
*/
|
||||
private function createDriverFromEnv()
|
||||
{
|
||||
$driver = \getenv("AMP_LOOP_DRIVER");
|
||||
|
||||
if (!$driver) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!\class_exists($driver)) {
|
||||
throw new \Error(\sprintf(
|
||||
"Driver '%s' does not exist.",
|
||||
$driver
|
||||
));
|
||||
}
|
||||
|
||||
if (!\is_subclass_of($driver, Driver::class)) {
|
||||
throw new \Error(\sprintf(
|
||||
"Driver '%s' is not a subclass of '%s'.",
|
||||
$driver,
|
||||
Driver::class
|
||||
));
|
||||
}
|
||||
|
||||
return new $driver;
|
||||
}
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
317
vendor/amphp/amp/lib/Loop/EvDriver.php
vendored
317
vendor/amphp/amp/lib/Loop/EvDriver.php
vendored
@@ -1,317 +0,0 @@
|
||||
<?php /** @noinspection PhpComposerExtensionStubsInspection */
|
||||
|
||||
namespace Amp\Loop;
|
||||
|
||||
use Amp\Coroutine;
|
||||
use Amp\Promise;
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
use function Amp\Internal\getCurrentTime;
|
||||
use function Amp\Promise\rethrow;
|
||||
|
||||
class EvDriver extends Driver
|
||||
{
|
||||
/** @var \EvSignal[]|null */
|
||||
private static $activeSignals;
|
||||
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return \extension_loaded("ev");
|
||||
}
|
||||
|
||||
/** @var \EvLoop */
|
||||
private $handle;
|
||||
/** @var \EvWatcher[] */
|
||||
private $events = [];
|
||||
/** @var callable */
|
||||
private $ioCallback;
|
||||
/** @var callable */
|
||||
private $timerCallback;
|
||||
/** @var callable */
|
||||
private $signalCallback;
|
||||
/** @var \EvSignal[] */
|
||||
private $signals = [];
|
||||
/** @var int Internal timestamp for now. */
|
||||
private $now;
|
||||
/** @var int Loop time offset */
|
||||
private $nowOffset;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->handle = new \EvLoop;
|
||||
$this->nowOffset = getCurrentTime();
|
||||
$this->now = \random_int(0, $this->nowOffset);
|
||||
$this->nowOffset -= $this->now;
|
||||
|
||||
if (self::$activeSignals === null) {
|
||||
self::$activeSignals = &$this->signals;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \EvIO $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->ioCallback = function (\EvIO $event) {
|
||||
/** @var Watcher $watcher */
|
||||
$watcher = $event->data;
|
||||
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param \EvTimer $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->timerCallback = function (\EvTimer $event) {
|
||||
/** @var Watcher $watcher */
|
||||
$watcher = $event->data;
|
||||
|
||||
if ($watcher->type & Watcher::DELAY) {
|
||||
$this->cancel($watcher->id);
|
||||
} elseif ($watcher->value === 0) {
|
||||
// Disable and re-enable so it's not executed repeatedly in the same tick
|
||||
// See https://github.com/amphp/amp/issues/131
|
||||
$this->disable($watcher->id);
|
||||
$this->enable($watcher->id);
|
||||
}
|
||||
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param \EvSignal $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->signalCallback = function (\EvSignal $event) {
|
||||
/** @var Watcher $watcher */
|
||||
$watcher = $event->data;
|
||||
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function cancel(string $watcherId)
|
||||
{
|
||||
parent::cancel($watcherId);
|
||||
unset($this->events[$watcherId]);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
foreach ($this->events as $event) {
|
||||
/** @psalm-suppress all */
|
||||
if ($event !== null) { // Events may have been nulled in extension depending on destruct order.
|
||||
$event->stop();
|
||||
}
|
||||
}
|
||||
|
||||
// We need to clear all references to events manually, see
|
||||
// https://bitbucket.org/osmanov/pecl-ev/issues/31/segfault-in-ev_timer_stop
|
||||
$this->events = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$active = self::$activeSignals;
|
||||
|
||||
\assert($active !== null);
|
||||
|
||||
foreach ($active as $event) {
|
||||
$event->stop();
|
||||
}
|
||||
|
||||
self::$activeSignals = &$this->signals;
|
||||
|
||||
foreach ($this->signals as $event) {
|
||||
$event->start();
|
||||
}
|
||||
|
||||
try {
|
||||
parent::run();
|
||||
} finally {
|
||||
foreach ($this->signals as $event) {
|
||||
$event->stop();
|
||||
}
|
||||
|
||||
self::$activeSignals = &$active;
|
||||
|
||||
foreach ($active as $event) {
|
||||
$event->start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function stop()
|
||||
{
|
||||
$this->handle->stop();
|
||||
parent::stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function now(): int
|
||||
{
|
||||
$this->now = getCurrentTime() - $this->nowOffset;
|
||||
|
||||
return $this->now;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHandle(): \EvLoop
|
||||
{
|
||||
return $this->handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function dispatch(bool $blocking)
|
||||
{
|
||||
$this->handle->run($blocking ? \Ev::RUN_ONCE : \Ev::RUN_ONCE | \Ev::RUN_NOWAIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function activate(array $watchers)
|
||||
{
|
||||
$this->handle->nowUpdate();
|
||||
$now = $this->now();
|
||||
|
||||
foreach ($watchers as $watcher) {
|
||||
if (!isset($this->events[$id = $watcher->id])) {
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
|
||||
$this->events[$id] = $this->handle->io($watcher->value, \Ev::READ, $this->ioCallback, $watcher);
|
||||
break;
|
||||
|
||||
case Watcher::WRITABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
|
||||
$this->events[$id] = $this->handle->io(
|
||||
$watcher->value,
|
||||
\Ev::WRITE,
|
||||
$this->ioCallback,
|
||||
$watcher
|
||||
);
|
||||
break;
|
||||
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
\assert(\is_int($watcher->value));
|
||||
|
||||
$interval = $watcher->value / self::MILLISEC_PER_SEC;
|
||||
$this->events[$id] = $this->handle->timer(
|
||||
\max(0, ($watcher->expiration - $now) / self::MILLISEC_PER_SEC),
|
||||
($watcher->type & Watcher::REPEAT) ? $interval : 0,
|
||||
$this->timerCallback,
|
||||
$watcher
|
||||
);
|
||||
break;
|
||||
|
||||
case Watcher::SIGNAL:
|
||||
\assert(\is_int($watcher->value));
|
||||
|
||||
$this->events[$id] = $this->handle->signal($watcher->value, $this->signalCallback, $watcher);
|
||||
break;
|
||||
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
} else {
|
||||
$this->events[$id]->start();
|
||||
}
|
||||
|
||||
if ($watcher->type === Watcher::SIGNAL) {
|
||||
/** @psalm-suppress PropertyTypeCoercion */
|
||||
$this->signals[$id] = $this->events[$id];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function deactivate(Watcher $watcher)
|
||||
{
|
||||
if (isset($this->events[$id = $watcher->id])) {
|
||||
$this->events[$id]->stop();
|
||||
|
||||
if ($watcher->type === Watcher::SIGNAL) {
|
||||
unset($this->signals[$id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
367
vendor/amphp/amp/lib/Loop/EventDriver.php
vendored
367
vendor/amphp/amp/lib/Loop/EventDriver.php
vendored
@@ -1,367 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Loop;
|
||||
|
||||
use Amp\Coroutine;
|
||||
use Amp\Promise;
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
use function Amp\Internal\getCurrentTime;
|
||||
use function Amp\Promise\rethrow;
|
||||
|
||||
class EventDriver extends Driver
|
||||
{
|
||||
/** @var \Event[]|null */
|
||||
private static $activeSignals;
|
||||
|
||||
/** @var \EventBase */
|
||||
private $handle;
|
||||
|
||||
/** @var \Event[] */
|
||||
private $events = [];
|
||||
|
||||
/** @var callable */
|
||||
private $ioCallback;
|
||||
|
||||
/** @var callable */
|
||||
private $timerCallback;
|
||||
|
||||
/** @var callable */
|
||||
private $signalCallback;
|
||||
|
||||
/** @var \Event[] */
|
||||
private $signals = [];
|
||||
|
||||
/** @var int Internal timestamp for now. */
|
||||
private $now;
|
||||
|
||||
/** @var int Loop time offset */
|
||||
private $nowOffset;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$config = new \EventConfig();
|
||||
if (\DIRECTORY_SEPARATOR !== '\\') {
|
||||
$config->requireFeatures(\EventConfig::FEATURE_FDS);
|
||||
}
|
||||
|
||||
$this->handle = new \EventBase($config);
|
||||
$this->nowOffset = getCurrentTime();
|
||||
$this->now = \random_int(0, $this->nowOffset);
|
||||
$this->nowOffset -= $this->now;
|
||||
|
||||
if (self::$activeSignals === null) {
|
||||
self::$activeSignals = &$this->signals;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $resource
|
||||
* @param $what
|
||||
* @param Watcher $watcher
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->ioCallback = function ($resource, $what, Watcher $watcher) {
|
||||
\assert(\is_resource($watcher->value));
|
||||
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param $resource
|
||||
* @param $what
|
||||
* @param Watcher $watcher
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->timerCallback = function ($resource, $what, Watcher $watcher) {
|
||||
\assert(\is_int($watcher->value));
|
||||
|
||||
if ($watcher->type & Watcher::DELAY) {
|
||||
$this->cancel($watcher->id);
|
||||
} else {
|
||||
$this->events[$watcher->id]->add($watcher->value / self::MILLISEC_PER_SEC);
|
||||
}
|
||||
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param $signum
|
||||
* @param $what
|
||||
* @param Watcher $watcher
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->signalCallback = function ($signum, $what, Watcher $watcher) {
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function cancel(string $watcherId)
|
||||
{
|
||||
parent::cancel($watcherId);
|
||||
|
||||
if (isset($this->events[$watcherId])) {
|
||||
$this->events[$watcherId]->free();
|
||||
unset($this->events[$watcherId]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return \extension_loaded("event");
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
// Unset here, otherwise $event->del() in the loop may fail with a warning, because __destruct order isn't defined.
|
||||
// Related https://github.com/amphp/amp/issues/159.
|
||||
$events = $this->events;
|
||||
$this->events = [];
|
||||
|
||||
foreach ($events as $event) {
|
||||
if ($event !== null) { // Events may have been nulled in extension depending on destruct order.
|
||||
$event->free();
|
||||
}
|
||||
}
|
||||
|
||||
// Manually free the loop handle to fully release loop resources.
|
||||
// See https://github.com/amphp/amp/issues/177.
|
||||
if ($this->handle !== null) {
|
||||
$this->handle->free();
|
||||
$this->handle = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$active = self::$activeSignals;
|
||||
|
||||
\assert($active !== null);
|
||||
|
||||
foreach ($active as $event) {
|
||||
$event->del();
|
||||
}
|
||||
|
||||
self::$activeSignals = &$this->signals;
|
||||
|
||||
foreach ($this->signals as $event) {
|
||||
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
|
||||
$event->add();
|
||||
}
|
||||
|
||||
try {
|
||||
parent::run();
|
||||
} finally {
|
||||
foreach ($this->signals as $event) {
|
||||
$event->del();
|
||||
}
|
||||
|
||||
self::$activeSignals = &$active;
|
||||
|
||||
foreach ($active as $event) {
|
||||
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
|
||||
$event->add();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function stop()
|
||||
{
|
||||
$this->handle->stop();
|
||||
parent::stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function now(): int
|
||||
{
|
||||
$this->now = getCurrentTime() - $this->nowOffset;
|
||||
|
||||
return $this->now;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHandle(): \EventBase
|
||||
{
|
||||
return $this->handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function dispatch(bool $blocking)
|
||||
{
|
||||
$this->handle->loop($blocking ? \EventBase::LOOP_ONCE : \EventBase::LOOP_ONCE | \EventBase::LOOP_NONBLOCK);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function activate(array $watchers)
|
||||
{
|
||||
$now = $this->now();
|
||||
|
||||
foreach ($watchers as $watcher) {
|
||||
if (!isset($this->events[$id = $watcher->id])) {
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
|
||||
$this->events[$id] = new \Event(
|
||||
$this->handle,
|
||||
$watcher->value,
|
||||
\Event::READ | \Event::PERSIST,
|
||||
$this->ioCallback,
|
||||
$watcher
|
||||
);
|
||||
break;
|
||||
|
||||
case Watcher::WRITABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
|
||||
$this->events[$id] = new \Event(
|
||||
$this->handle,
|
||||
$watcher->value,
|
||||
\Event::WRITE | \Event::PERSIST,
|
||||
$this->ioCallback,
|
||||
$watcher
|
||||
);
|
||||
break;
|
||||
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
\assert(\is_int($watcher->value));
|
||||
|
||||
$this->events[$id] = new \Event(
|
||||
$this->handle,
|
||||
-1,
|
||||
\Event::TIMEOUT,
|
||||
$this->timerCallback,
|
||||
$watcher
|
||||
);
|
||||
break;
|
||||
|
||||
case Watcher::SIGNAL:
|
||||
\assert(\is_int($watcher->value));
|
||||
|
||||
$this->events[$id] = new \Event(
|
||||
$this->handle,
|
||||
$watcher->value,
|
||||
\Event::SIGNAL | \Event::PERSIST,
|
||||
$this->signalCallback,
|
||||
$watcher
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
}
|
||||
|
||||
switch ($watcher->type) {
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
\assert(\is_int($watcher->value));
|
||||
|
||||
$interval = \max(0, $watcher->expiration - $now);
|
||||
$this->events[$id]->add($interval > 0 ? $interval / self::MILLISEC_PER_SEC : 0);
|
||||
break;
|
||||
|
||||
case Watcher::SIGNAL:
|
||||
$this->signals[$id] = $this->events[$id];
|
||||
// no break
|
||||
|
||||
default:
|
||||
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
|
||||
$this->events[$id]->add();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function deactivate(Watcher $watcher)
|
||||
{
|
||||
if (isset($this->events[$id = $watcher->id])) {
|
||||
$this->events[$id]->del();
|
||||
|
||||
if ($watcher->type === Watcher::SIGNAL) {
|
||||
unset($this->signals[$id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
169
vendor/amphp/amp/lib/Loop/Internal/TimerQueue.php
vendored
169
vendor/amphp/amp/lib/Loop/Internal/TimerQueue.php
vendored
@@ -1,169 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Loop\Internal;
|
||||
|
||||
use Amp\Loop\Watcher;
|
||||
|
||||
/**
|
||||
* Uses a binary tree stored in an array to implement a heap.
|
||||
*/
|
||||
final class TimerQueue
|
||||
{
|
||||
/** @var Watcher[] */
|
||||
private $data = [];
|
||||
|
||||
/** @var int[] */
|
||||
private $pointers = [];
|
||||
|
||||
/**
|
||||
* @param int $node Rebuild the data array from the given node upward.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function heapifyUp(int $node)
|
||||
{
|
||||
$entry = $this->data[$node];
|
||||
while ($node !== 0 && $entry->expiration < $this->data[$parent = ($node - 1) >> 1]->expiration) {
|
||||
$this->swap($node, $parent);
|
||||
$node = $parent;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $node Rebuild the data array from the given node downward.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function heapifyDown(int $node)
|
||||
{
|
||||
$length = \count($this->data);
|
||||
while (($child = ($node << 1) + 1) < $length) {
|
||||
if ($this->data[$child]->expiration < $this->data[$node]->expiration
|
||||
&& ($child + 1 >= $length || $this->data[$child]->expiration < $this->data[$child + 1]->expiration)
|
||||
) {
|
||||
// Left child is less than parent and right child.
|
||||
$swap = $child;
|
||||
} elseif ($child + 1 < $length && $this->data[$child + 1]->expiration < $this->data[$node]->expiration) {
|
||||
// Right child is less than parent and left child.
|
||||
$swap = $child + 1;
|
||||
} else { // Left and right child are greater than parent.
|
||||
break;
|
||||
}
|
||||
|
||||
$this->swap($node, $swap);
|
||||
$node = $swap;
|
||||
}
|
||||
}
|
||||
|
||||
private function swap(int $left, int $right)
|
||||
{
|
||||
$temp = $this->data[$left];
|
||||
|
||||
$this->data[$left] = $this->data[$right];
|
||||
$this->pointers[$this->data[$right]->id] = $left;
|
||||
|
||||
$this->data[$right] = $temp;
|
||||
$this->pointers[$temp->id] = $right;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the watcher into the queue. Time complexity: O(log(n)).
|
||||
*
|
||||
* @param Watcher $watcher
|
||||
*
|
||||
* @psalm-param Watcher<int> $watcher
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function insert(Watcher $watcher)
|
||||
{
|
||||
\assert($watcher->expiration !== null);
|
||||
\assert(!isset($this->pointers[$watcher->id]));
|
||||
|
||||
$node = \count($this->data);
|
||||
$this->data[$node] = $watcher;
|
||||
$this->pointers[$watcher->id] = $node;
|
||||
|
||||
$this->heapifyUp($node);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given watcher from the queue. Time complexity: O(log(n)).
|
||||
*
|
||||
* @param Watcher $watcher
|
||||
*
|
||||
* @psalm-param Watcher<int> $watcher
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function remove(Watcher $watcher)
|
||||
{
|
||||
$id = $watcher->id;
|
||||
|
||||
if (!isset($this->pointers[$id])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->removeAndRebuild($this->pointers[$id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes and returns the Watcher on top of the heap if it has expired, otherwise null is returned.
|
||||
* Time complexity: O(log(n)).
|
||||
*
|
||||
* @param int $now Current loop time.
|
||||
*
|
||||
* @return Watcher|null Expired watcher at the top of the heap or null if the watcher has not expired.
|
||||
*
|
||||
* @psalm-return Watcher<int>|null
|
||||
*/
|
||||
public function extract(int $now)
|
||||
{
|
||||
if (empty($this->data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$watcher = $this->data[0];
|
||||
|
||||
if ($watcher->expiration > $now) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->removeAndRebuild(0);
|
||||
|
||||
return $watcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the expiration time value at the top of the heap. Time complexity: O(1).
|
||||
*
|
||||
* @return int|null Expiration time of the watcher at the top of the heap or null if the heap is empty.
|
||||
*/
|
||||
public function peek()
|
||||
{
|
||||
return isset($this->data[0]) ? $this->data[0]->expiration : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $node Remove the given node and then rebuild the data array.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function removeAndRebuild(int $node)
|
||||
{
|
||||
$length = \count($this->data) - 1;
|
||||
$id = $this->data[$node]->id;
|
||||
$left = $this->data[$node] = $this->data[$length];
|
||||
$this->pointers[$left->id] = $node;
|
||||
unset($this->data[$length], $this->pointers[$id]);
|
||||
|
||||
if ($node < $length) { // don't need to do anything if we removed the last element
|
||||
$parent = ($node - 1) >> 1;
|
||||
if ($parent >= 0 && $this->data[$node]->expiration < $this->data[$parent]->expiration) {
|
||||
$this->heapifyUp($node);
|
||||
} else {
|
||||
$this->heapifyDown($node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Loop;
|
||||
|
||||
/**
|
||||
* MUST be thrown if any operation (except disable() and cancel()) is attempted with an invalid watcher identifier.
|
||||
*
|
||||
* An invalid watcher identifier is any identifier that is not yet emitted by the driver or cancelled by the user.
|
||||
*/
|
||||
class InvalidWatcherError extends \Error
|
||||
{
|
||||
/** @var string */
|
||||
private $watcherId;
|
||||
|
||||
/**
|
||||
* @param string $watcherId The watcher identifier.
|
||||
* @param string $message The exception message.
|
||||
*/
|
||||
public function __construct(string $watcherId, string $message)
|
||||
{
|
||||
$this->watcherId = $watcherId;
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string The watcher identifier.
|
||||
*/
|
||||
public function getWatcherId()
|
||||
{
|
||||
return $this->watcherId;
|
||||
}
|
||||
}
|
||||
445
vendor/amphp/amp/lib/Loop/NativeDriver.php
vendored
445
vendor/amphp/amp/lib/Loop/NativeDriver.php
vendored
@@ -1,445 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Loop;
|
||||
|
||||
use Amp\CallableMaker;
|
||||
use Amp\Coroutine;
|
||||
use Amp\Promise;
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
use function Amp\Internal\getCurrentTime;
|
||||
use function Amp\Promise\rethrow;
|
||||
|
||||
class NativeDriver extends Driver
|
||||
{
|
||||
use CallableMaker;
|
||||
|
||||
/** @var resource[] */
|
||||
private $readStreams = [];
|
||||
|
||||
/** @var Watcher[][] */
|
||||
private $readWatchers = [];
|
||||
|
||||
/** @var resource[] */
|
||||
private $writeStreams = [];
|
||||
|
||||
/** @var Watcher[][] */
|
||||
private $writeWatchers = [];
|
||||
|
||||
/** @var Internal\TimerQueue */
|
||||
private $timerQueue;
|
||||
|
||||
/** @var Watcher[][] */
|
||||
private $signalWatchers = [];
|
||||
|
||||
/** @var int Internal timestamp for now. */
|
||||
private $now;
|
||||
|
||||
/** @var int Loop time offset */
|
||||
private $nowOffset;
|
||||
|
||||
/** @var bool */
|
||||
private $signalHandling;
|
||||
|
||||
/** @var callable */
|
||||
private $streamSelectErrorHandler;
|
||||
|
||||
/** @var bool */
|
||||
private $streamSelectIgnoreResult = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->timerQueue = new Internal\TimerQueue;
|
||||
$this->signalHandling = \extension_loaded("pcntl");
|
||||
$this->nowOffset = getCurrentTime();
|
||||
$this->now = \random_int(0, $this->nowOffset);
|
||||
$this->nowOffset -= $this->now;
|
||||
$this->streamSelectErrorHandler = function ($errno, $message) {
|
||||
// Casing changed in PHP 8 from 'unable' to 'Unable'
|
||||
if (\stripos($message, "stream_select(): unable to select [4]: ") === 0) { // EINTR
|
||||
$this->streamSelectIgnoreResult = true;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (\strpos($message, 'FD_SETSIZE') !== false) {
|
||||
$message = \str_replace(["\r\n", "\n", "\r"], " ", $message);
|
||||
$pattern = '(stream_select\(\): You MUST recompile PHP with a larger value of FD_SETSIZE. It is set to (\d+), but you have descriptors numbered at least as high as (\d+)\.)';
|
||||
|
||||
if (\preg_match($pattern, $message, $match)) {
|
||||
$helpLink = 'https://amphp.org/amp/event-loop/#implementations';
|
||||
|
||||
$message = 'You have reached the limits of stream_select(). It has a FD_SETSIZE of ' . $match[1]
|
||||
. ', but you have file descriptors numbered at least as high as ' . $match[2] . '. '
|
||||
. "You can install one of the extensions listed on {$helpLink} to support a higher number of "
|
||||
. "concurrent file descriptors. If a large number of open file descriptors is unexpected, you "
|
||||
. "might be leaking file descriptors that aren't closed correctly.";
|
||||
}
|
||||
}
|
||||
|
||||
throw new \Exception($message, $errno);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws \Amp\Loop\UnsupportedFeatureException If the pcntl extension is not available.
|
||||
*/
|
||||
public function onSignal(int $signo, callable $callback, $data = null): string
|
||||
{
|
||||
if (!$this->signalHandling) {
|
||||
throw new UnsupportedFeatureException("Signal handling requires the pcntl extension");
|
||||
}
|
||||
|
||||
return parent::onSignal($signo, $callback, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function now(): int
|
||||
{
|
||||
$this->now = getCurrentTime() - $this->nowOffset;
|
||||
|
||||
return $this->now;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHandle()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $blocking
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \Throwable
|
||||
*/
|
||||
protected function dispatch(bool $blocking)
|
||||
{
|
||||
$this->selectStreams(
|
||||
$this->readStreams,
|
||||
$this->writeStreams,
|
||||
$blocking ? $this->getTimeout() : 0
|
||||
);
|
||||
|
||||
$now = $this->now();
|
||||
|
||||
while ($watcher = $this->timerQueue->extract($now)) {
|
||||
if ($watcher->type & Watcher::REPEAT) {
|
||||
$watcher->enabled = false; // Trick base class into adding to enable queue when calling enable()
|
||||
$this->enable($watcher->id);
|
||||
} else {
|
||||
$this->cancel($watcher->id);
|
||||
}
|
||||
|
||||
try {
|
||||
// Execute the timer.
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->signalHandling) {
|
||||
\pcntl_signal_dispatch();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function activate(array $watchers)
|
||||
{
|
||||
foreach ($watchers as $watcher) {
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
|
||||
$streamId = (int) $watcher->value;
|
||||
$this->readWatchers[$streamId][$watcher->id] = $watcher;
|
||||
$this->readStreams[$streamId] = $watcher->value;
|
||||
break;
|
||||
|
||||
case Watcher::WRITABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
|
||||
$streamId = (int) $watcher->value;
|
||||
$this->writeWatchers[$streamId][$watcher->id] = $watcher;
|
||||
$this->writeStreams[$streamId] = $watcher->value;
|
||||
break;
|
||||
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
\assert(\is_int($watcher->value));
|
||||
$this->timerQueue->insert($watcher);
|
||||
break;
|
||||
|
||||
case Watcher::SIGNAL:
|
||||
\assert(\is_int($watcher->value));
|
||||
|
||||
if (!isset($this->signalWatchers[$watcher->value])) {
|
||||
if (!@\pcntl_signal($watcher->value, $this->callableFromInstanceMethod('handleSignal'))) {
|
||||
$message = "Failed to register signal handler";
|
||||
if ($error = \error_get_last()) {
|
||||
$message .= \sprintf("; Errno: %d; %s", $error["type"], $error["message"]);
|
||||
}
|
||||
throw new \Error($message);
|
||||
}
|
||||
}
|
||||
|
||||
$this->signalWatchers[$watcher->value][$watcher->id] = $watcher;
|
||||
break;
|
||||
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function deactivate(Watcher $watcher)
|
||||
{
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
$streamId = (int) $watcher->value;
|
||||
unset($this->readWatchers[$streamId][$watcher->id]);
|
||||
if (empty($this->readWatchers[$streamId])) {
|
||||
unset($this->readWatchers[$streamId], $this->readStreams[$streamId]);
|
||||
}
|
||||
break;
|
||||
|
||||
case Watcher::WRITABLE:
|
||||
$streamId = (int) $watcher->value;
|
||||
unset($this->writeWatchers[$streamId][$watcher->id]);
|
||||
if (empty($this->writeWatchers[$streamId])) {
|
||||
unset($this->writeWatchers[$streamId], $this->writeStreams[$streamId]);
|
||||
}
|
||||
break;
|
||||
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
$this->timerQueue->remove($watcher);
|
||||
break;
|
||||
|
||||
case Watcher::SIGNAL:
|
||||
\assert(\is_int($watcher->value));
|
||||
|
||||
if (isset($this->signalWatchers[$watcher->value])) {
|
||||
unset($this->signalWatchers[$watcher->value][$watcher->id]);
|
||||
|
||||
if (empty($this->signalWatchers[$watcher->value])) {
|
||||
unset($this->signalWatchers[$watcher->value]);
|
||||
@\pcntl_signal($watcher->value, \SIG_DFL);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource[] $read
|
||||
* @param resource[] $write
|
||||
* @param int $timeout
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function selectStreams(array $read, array $write, int $timeout)
|
||||
{
|
||||
$timeout /= self::MILLISEC_PER_SEC;
|
||||
|
||||
if (!empty($read) || !empty($write)) { // Use stream_select() if there are any streams in the loop.
|
||||
if ($timeout >= 0) {
|
||||
$seconds = (int) $timeout;
|
||||
$microseconds = (int) (($timeout - $seconds) * self::MICROSEC_PER_SEC);
|
||||
} else {
|
||||
$seconds = null;
|
||||
$microseconds = null;
|
||||
}
|
||||
|
||||
// Failed connection attempts are indicated via except on Windows
|
||||
// @link https://github.com/reactphp/event-loop/blob/8bd064ce23c26c4decf186c2a5a818c9a8209eb0/src/StreamSelectLoop.php#L279-L287
|
||||
// @link https://docs.microsoft.com/de-de/windows/win32/api/winsock2/nf-winsock2-select
|
||||
$except = null;
|
||||
if (\DIRECTORY_SEPARATOR === '\\') {
|
||||
$except = $write;
|
||||
}
|
||||
|
||||
\set_error_handler($this->streamSelectErrorHandler);
|
||||
|
||||
try {
|
||||
$result = \stream_select($read, $write, $except, $seconds, $microseconds);
|
||||
} finally {
|
||||
\restore_error_handler();
|
||||
}
|
||||
|
||||
if ($this->streamSelectIgnoreResult || $result === 0) {
|
||||
$this->streamSelectIgnoreResult = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$result) {
|
||||
$this->error(new \Exception('Unknown error during stream_select'));
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($read as $stream) {
|
||||
$streamId = (int) $stream;
|
||||
if (!isset($this->readWatchers[$streamId])) {
|
||||
continue; // All read watchers disabled.
|
||||
}
|
||||
|
||||
foreach ($this->readWatchers[$streamId] as $watcher) {
|
||||
if (!isset($this->readWatchers[$streamId][$watcher->id])) {
|
||||
continue; // Watcher disabled by another IO watcher.
|
||||
}
|
||||
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $stream, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
\assert(\is_array($write)); // See https://github.com/vimeo/psalm/issues/3036
|
||||
|
||||
if ($except) {
|
||||
foreach ($except as $key => $socket) {
|
||||
$write[$key] = $socket;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($write as $stream) {
|
||||
$streamId = (int) $stream;
|
||||
if (!isset($this->writeWatchers[$streamId])) {
|
||||
continue; // All write watchers disabled.
|
||||
}
|
||||
|
||||
foreach ($this->writeWatchers[$streamId] as $watcher) {
|
||||
if (!isset($this->writeWatchers[$streamId][$watcher->id])) {
|
||||
continue; // Watcher disabled by another IO watcher.
|
||||
}
|
||||
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $stream, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($timeout < 0) { // Only signal watchers are enabled, so sleep indefinitely.
|
||||
\usleep(\PHP_INT_MAX);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($timeout > 0) { // Sleep until next timer expires.
|
||||
\usleep((int) ($timeout * self::MICROSEC_PER_SEC));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int Milliseconds until next timer expires or -1 if there are no pending times.
|
||||
*/
|
||||
private function getTimeout(): int
|
||||
{
|
||||
$expiration = $this->timerQueue->peek();
|
||||
|
||||
if ($expiration === null) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
$expiration -= getCurrentTime() - $this->nowOffset;
|
||||
|
||||
return $expiration > 0 ? $expiration : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $signo
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function handleSignal(int $signo)
|
||||
{
|
||||
foreach ($this->signalWatchers[$signo] as $watcher) {
|
||||
if (!isset($this->signalWatchers[$signo][$watcher->id])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $signo, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
251
vendor/amphp/amp/lib/Loop/TracingDriver.php
vendored
251
vendor/amphp/amp/lib/Loop/TracingDriver.php
vendored
@@ -1,251 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Loop;
|
||||
|
||||
use function Amp\Internal\formatStacktrace;
|
||||
|
||||
final class TracingDriver extends Driver
|
||||
{
|
||||
/** @var Driver */
|
||||
private $driver;
|
||||
/** @var true[] */
|
||||
private $enabledWatchers = [];
|
||||
/** @var true[] */
|
||||
private $unreferencedWatchers = [];
|
||||
/** @var string[] */
|
||||
private $creationTraces = [];
|
||||
/** @var string[] */
|
||||
private $cancelTraces = [];
|
||||
|
||||
public function __construct(Driver $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->driver->run();
|
||||
}
|
||||
|
||||
public function stop()
|
||||
{
|
||||
$this->driver->stop();
|
||||
}
|
||||
|
||||
public function defer(callable $callback, $data = null): string
|
||||
{
|
||||
$id = $this->driver->defer(function (...$args) use ($callback) {
|
||||
$this->cancel($args[0]);
|
||||
return $callback(...$args);
|
||||
}, $data);
|
||||
|
||||
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
$this->enabledWatchers[$id] = true;
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function delay(int $delay, callable $callback, $data = null): string
|
||||
{
|
||||
$id = $this->driver->delay($delay, function (...$args) use ($callback) {
|
||||
$this->cancel($args[0]);
|
||||
return $callback(...$args);
|
||||
}, $data);
|
||||
|
||||
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
$this->enabledWatchers[$id] = true;
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function repeat(int $interval, callable $callback, $data = null): string
|
||||
{
|
||||
$id = $this->driver->repeat($interval, $callback, $data);
|
||||
|
||||
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
$this->enabledWatchers[$id] = true;
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function onReadable($stream, callable $callback, $data = null): string
|
||||
{
|
||||
$id = $this->driver->onReadable($stream, $callback, $data);
|
||||
|
||||
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
$this->enabledWatchers[$id] = true;
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function onWritable($stream, callable $callback, $data = null): string
|
||||
{
|
||||
$id = $this->driver->onWritable($stream, $callback, $data);
|
||||
|
||||
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
$this->enabledWatchers[$id] = true;
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function onSignal(int $signo, callable $callback, $data = null): string
|
||||
{
|
||||
$id = $this->driver->onSignal($signo, $callback, $data);
|
||||
|
||||
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
$this->enabledWatchers[$id] = true;
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function enable(string $watcherId)
|
||||
{
|
||||
try {
|
||||
$this->driver->enable($watcherId);
|
||||
$this->enabledWatchers[$watcherId] = true;
|
||||
} catch (InvalidWatcherError $e) {
|
||||
throw new InvalidWatcherError(
|
||||
$watcherId,
|
||||
$e->getMessage() . "\r\n\r\n" . $this->getTraces($watcherId)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function cancel(string $watcherId)
|
||||
{
|
||||
$this->driver->cancel($watcherId);
|
||||
|
||||
if (!isset($this->cancelTraces[$watcherId])) {
|
||||
$this->cancelTraces[$watcherId] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
}
|
||||
|
||||
unset($this->enabledWatchers[$watcherId], $this->unreferencedWatchers[$watcherId]);
|
||||
}
|
||||
|
||||
public function disable(string $watcherId)
|
||||
{
|
||||
$this->driver->disable($watcherId);
|
||||
unset($this->enabledWatchers[$watcherId]);
|
||||
}
|
||||
|
||||
public function reference(string $watcherId)
|
||||
{
|
||||
try {
|
||||
$this->driver->reference($watcherId);
|
||||
unset($this->unreferencedWatchers[$watcherId]);
|
||||
} catch (InvalidWatcherError $e) {
|
||||
throw new InvalidWatcherError(
|
||||
$watcherId,
|
||||
$e->getMessage() . "\r\n\r\n" . $this->getTraces($watcherId)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function unreference(string $watcherId)
|
||||
{
|
||||
$this->driver->unreference($watcherId);
|
||||
$this->unreferencedWatchers[$watcherId] = true;
|
||||
}
|
||||
|
||||
public function setErrorHandler(callable $callback = null)
|
||||
{
|
||||
return $this->driver->setErrorHandler($callback);
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function getHandle()
|
||||
{
|
||||
$this->driver->getHandle();
|
||||
}
|
||||
|
||||
public function dump(): string
|
||||
{
|
||||
$dump = "Enabled, referenced watchers keeping the loop running: ";
|
||||
|
||||
foreach ($this->enabledWatchers as $watcher => $_) {
|
||||
if (isset($this->unreferencedWatchers[$watcher])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$dump .= "Watcher ID: " . $watcher . "\r\n";
|
||||
$dump .= $this->getCreationTrace($watcher);
|
||||
$dump .= "\r\n\r\n";
|
||||
}
|
||||
|
||||
return \rtrim($dump);
|
||||
}
|
||||
|
||||
public function getInfo(): array
|
||||
{
|
||||
return $this->driver->getInfo();
|
||||
}
|
||||
|
||||
public function __debugInfo()
|
||||
{
|
||||
return $this->driver->__debugInfo();
|
||||
}
|
||||
|
||||
public function now(): int
|
||||
{
|
||||
return $this->driver->now();
|
||||
}
|
||||
|
||||
protected function error(\Throwable $exception)
|
||||
{
|
||||
$this->driver->error($exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function activate(array $watchers)
|
||||
{
|
||||
// nothing to do in a decorator
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function dispatch(bool $blocking)
|
||||
{
|
||||
// nothing to do in a decorator
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function deactivate(Watcher $watcher)
|
||||
{
|
||||
// nothing to do in a decorator
|
||||
}
|
||||
|
||||
private function getTraces(string $watcherId): string
|
||||
{
|
||||
return "Creation Trace:\r\n" . $this->getCreationTrace($watcherId) . "\r\n\r\n" .
|
||||
"Cancellation Trace:\r\n" . $this->getCancelTrace($watcherId);
|
||||
}
|
||||
|
||||
private function getCreationTrace(string $watcher): string
|
||||
{
|
||||
if (!isset($this->creationTraces[$watcher])) {
|
||||
return 'No creation trace, yet.';
|
||||
}
|
||||
|
||||
return $this->creationTraces[$watcher];
|
||||
}
|
||||
|
||||
private function getCancelTrace(string $watcher): string
|
||||
{
|
||||
if (!isset($this->cancelTraces[$watcher])) {
|
||||
return 'No cancellation trace, yet.';
|
||||
}
|
||||
|
||||
return $this->cancelTraces[$watcher];
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Loop;
|
||||
|
||||
/**
|
||||
* MUST be thrown if a feature is not supported by the system.
|
||||
*
|
||||
* This might happen if ext-pcntl is missing and the loop driver doesn't support another way to dispatch signals.
|
||||
*/
|
||||
class UnsupportedFeatureException extends \Exception
|
||||
{
|
||||
}
|
||||
350
vendor/amphp/amp/lib/Loop/UvDriver.php
vendored
350
vendor/amphp/amp/lib/Loop/UvDriver.php
vendored
@@ -1,350 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Loop;
|
||||
|
||||
use Amp\Coroutine;
|
||||
use Amp\Promise;
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
use function Amp\Promise\rethrow;
|
||||
|
||||
class UvDriver extends Driver
|
||||
{
|
||||
/** @var resource A uv_loop resource created with uv_loop_new() */
|
||||
private $handle;
|
||||
|
||||
/** @var resource[] */
|
||||
private $events = [];
|
||||
|
||||
/** @var Watcher[][] */
|
||||
private $watchers = [];
|
||||
|
||||
/** @var resource[] */
|
||||
private $streams = [];
|
||||
|
||||
/** @var callable */
|
||||
private $ioCallback;
|
||||
|
||||
/** @var callable */
|
||||
private $timerCallback;
|
||||
|
||||
/** @var callable */
|
||||
private $signalCallback;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->handle = \uv_loop_new();
|
||||
|
||||
/**
|
||||
* @param $event
|
||||
* @param $status
|
||||
* @param $events
|
||||
* @param $resource
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->ioCallback = function ($event, $status, $events, $resource) {
|
||||
$watchers = $this->watchers[(int) $event];
|
||||
|
||||
switch ($status) {
|
||||
case 0: // OK
|
||||
break;
|
||||
|
||||
default: // Invoke the callback on errors, as this matches behavior with other loop back-ends.
|
||||
// Re-enable watcher as libuv disables the watcher on non-zero status.
|
||||
$flags = 0;
|
||||
foreach ($watchers as $watcher) {
|
||||
$flags |= $watcher->enabled ? $watcher->type : 0;
|
||||
}
|
||||
\uv_poll_start($event, $flags, $this->ioCallback);
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($watchers as $watcher) {
|
||||
// $events is OR'ed with 4 to trigger watcher if no events are indicated (0) or on UV_DISCONNECT (4).
|
||||
// http://docs.libuv.org/en/v1.x/poll.html
|
||||
if (!($watcher->enabled && ($watcher->type & $events || ($events | 4) === 4))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $resource, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->timerCallback = function ($event) {
|
||||
$watcher = $this->watchers[(int) $event][0];
|
||||
|
||||
if ($watcher->type & Watcher::DELAY) {
|
||||
unset($this->events[$watcher->id], $this->watchers[(int) $event]); // Avoid call to uv_is_active().
|
||||
$this->cancel($watcher->id); // Remove reference to watcher in parent.
|
||||
} elseif ($watcher->value === 0) {
|
||||
// Disable and re-enable so it's not executed repeatedly in the same tick
|
||||
// See https://github.com/amphp/amp/issues/131
|
||||
$this->disable($watcher->id);
|
||||
$this->enable($watcher->id);
|
||||
}
|
||||
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param $event
|
||||
* @param $signo
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->signalCallback = function ($event, $signo) {
|
||||
$watcher = $this->watchers[(int) $event][0];
|
||||
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $signo, $watcher->data);
|
||||
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function cancel(string $watcherId)
|
||||
{
|
||||
parent::cancel($watcherId);
|
||||
|
||||
if (!isset($this->events[$watcherId])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$event = $this->events[$watcherId];
|
||||
$eventId = (int) $event;
|
||||
|
||||
if (isset($this->watchers[$eventId][0])) { // All except IO watchers.
|
||||
unset($this->watchers[$eventId]);
|
||||
} elseif (isset($this->watchers[$eventId][$watcherId])) {
|
||||
$watcher = $this->watchers[$eventId][$watcherId];
|
||||
unset($this->watchers[$eventId][$watcherId]);
|
||||
|
||||
if (empty($this->watchers[$eventId])) {
|
||||
unset($this->watchers[$eventId], $this->streams[(int) $watcher->value]);
|
||||
}
|
||||
}
|
||||
|
||||
unset($this->events[$watcherId]);
|
||||
}
|
||||
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return \extension_loaded("uv");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function now(): int
|
||||
{
|
||||
\uv_update_time($this->handle);
|
||||
|
||||
/** @psalm-suppress TooManyArguments */
|
||||
return \uv_now($this->handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHandle()
|
||||
{
|
||||
return $this->handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function dispatch(bool $blocking)
|
||||
{
|
||||
/** @psalm-suppress TooManyArguments */
|
||||
\uv_run($this->handle, $blocking ? \UV::RUN_ONCE : \UV::RUN_NOWAIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function activate(array $watchers)
|
||||
{
|
||||
$now = $this->now();
|
||||
|
||||
foreach ($watchers as $watcher) {
|
||||
$id = $watcher->id;
|
||||
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
case Watcher::WRITABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
|
||||
$streamId = (int) $watcher->value;
|
||||
|
||||
if (isset($this->streams[$streamId])) {
|
||||
$event = $this->streams[$streamId];
|
||||
} elseif (isset($this->events[$id])) {
|
||||
$event = $this->streams[$streamId] = $this->events[$id];
|
||||
} else {
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
$event = $this->streams[$streamId] = \uv_poll_init_socket($this->handle, $watcher->value);
|
||||
}
|
||||
|
||||
$eventId = (int) $event;
|
||||
$this->events[$id] = $event;
|
||||
$this->watchers[$eventId][$id] = $watcher;
|
||||
|
||||
$flags = 0;
|
||||
foreach ($this->watchers[$eventId] as $w) {
|
||||
$flags |= $w->enabled ? $w->type : 0;
|
||||
}
|
||||
\uv_poll_start($event, $flags, $this->ioCallback);
|
||||
break;
|
||||
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
\assert(\is_int($watcher->value));
|
||||
|
||||
if (isset($this->events[$id])) {
|
||||
$event = $this->events[$id];
|
||||
} else {
|
||||
$event = $this->events[$id] = \uv_timer_init($this->handle);
|
||||
}
|
||||
|
||||
$this->watchers[(int) $event] = [$watcher];
|
||||
|
||||
\uv_timer_start(
|
||||
$event,
|
||||
\max(0, $watcher->expiration - $now),
|
||||
($watcher->type & Watcher::REPEAT) ? $watcher->value : 0,
|
||||
$this->timerCallback
|
||||
);
|
||||
break;
|
||||
|
||||
case Watcher::SIGNAL:
|
||||
\assert(\is_int($watcher->value));
|
||||
|
||||
if (isset($this->events[$id])) {
|
||||
$event = $this->events[$id];
|
||||
} else {
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
$event = $this->events[$id] = \uv_signal_init($this->handle);
|
||||
}
|
||||
|
||||
$this->watchers[(int) $event] = [$watcher];
|
||||
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
\uv_signal_start($event, $this->signalCallback, $watcher->value);
|
||||
break;
|
||||
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function deactivate(Watcher $watcher)
|
||||
{
|
||||
$id = $watcher->id;
|
||||
|
||||
if (!isset($this->events[$id])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$event = $this->events[$id];
|
||||
|
||||
if (!\uv_is_active($event)) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
case Watcher::WRITABLE:
|
||||
$flags = 0;
|
||||
foreach ($this->watchers[(int) $event] as $w) {
|
||||
$flags |= $w->enabled ? $w->type : 0;
|
||||
}
|
||||
|
||||
if ($flags) {
|
||||
\uv_poll_start($event, $flags, $this->ioCallback);
|
||||
} else {
|
||||
\uv_poll_stop($event);
|
||||
}
|
||||
break;
|
||||
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
\uv_timer_stop($event);
|
||||
break;
|
||||
|
||||
case Watcher::SIGNAL:
|
||||
\uv_signal_stop($event);
|
||||
break;
|
||||
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
}
|
||||
}
|
||||
57
vendor/amphp/amp/lib/Loop/Watcher.php
vendored
57
vendor/amphp/amp/lib/Loop/Watcher.php
vendored
@@ -1,57 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Loop;
|
||||
|
||||
use Amp\Struct;
|
||||
|
||||
/**
|
||||
* @template TValue as (int|resource|null)
|
||||
*
|
||||
* @psalm-suppress MissingConstructor
|
||||
*/
|
||||
class Watcher
|
||||
{
|
||||
use Struct;
|
||||
|
||||
const IO = 0b00000011;
|
||||
const READABLE = 0b00000001;
|
||||
const WRITABLE = 0b00000010;
|
||||
const DEFER = 0b00000100;
|
||||
const TIMER = 0b00011000;
|
||||
const DELAY = 0b00001000;
|
||||
const REPEAT = 0b00010000;
|
||||
const SIGNAL = 0b00100000;
|
||||
|
||||
/** @var int */
|
||||
public $type;
|
||||
|
||||
/** @var bool */
|
||||
public $enabled = true;
|
||||
|
||||
/** @var bool */
|
||||
public $referenced = true;
|
||||
|
||||
/** @var string */
|
||||
public $id;
|
||||
|
||||
/** @var callable */
|
||||
public $callback;
|
||||
|
||||
/**
|
||||
* Data provided to the watcher callback.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* Watcher-dependent value storage. Stream for IO watchers, signal number for signal watchers, interval for timers.
|
||||
*
|
||||
* @var resource|int|null
|
||||
* @psalm-var TValue
|
||||
*/
|
||||
public $value;
|
||||
|
||||
/** @var int|null */
|
||||
public $expiration;
|
||||
}
|
||||
29
vendor/amphp/amp/lib/MultiReasonException.php
vendored
29
vendor/amphp/amp/lib/MultiReasonException.php
vendored
@@ -1,29 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
class MultiReasonException extends \Exception
|
||||
{
|
||||
/** @var \Throwable[] */
|
||||
private $reasons;
|
||||
|
||||
/**
|
||||
* @param \Throwable[] $reasons Array of exceptions rejecting the promise.
|
||||
* @param string|null $message
|
||||
*/
|
||||
public function __construct(array $reasons, string $message = null)
|
||||
{
|
||||
parent::__construct($message ?: "Multiple errors encountered; use "
|
||||
. self::class . "::getReasons() to retrieve the array of exceptions thrown");
|
||||
|
||||
$this->reasons = $reasons;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Throwable[]
|
||||
*/
|
||||
public function getReasons(): array
|
||||
{
|
||||
return $this->reasons;
|
||||
}
|
||||
}
|
||||
53
vendor/amphp/amp/lib/NullCancellationToken.php
vendored
53
vendor/amphp/amp/lib/NullCancellationToken.php
vendored
@@ -1,53 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* A NullCancellationToken can be used to avoid conditionals to check whether a token has been provided.
|
||||
*
|
||||
* Instead of writing
|
||||
*
|
||||
* ```php
|
||||
* if ($token) {
|
||||
* $token->throwIfRequested();
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* potentially multiple times, it allows writing
|
||||
*
|
||||
* ```php
|
||||
* $token = $token ?? new NullCancellationToken;
|
||||
*
|
||||
* // ...
|
||||
*
|
||||
* $token->throwIfRequested();
|
||||
* ```
|
||||
*
|
||||
* instead.
|
||||
*/
|
||||
final class NullCancellationToken implements CancellationToken
|
||||
{
|
||||
/** @inheritdoc */
|
||||
public function subscribe(callable $callback): string
|
||||
{
|
||||
return "null-token";
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function unsubscribe(string $id)
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function isRequested(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function throwIfRequested()
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
}
|
||||
43
vendor/amphp/amp/lib/Producer.php
vendored
43
vendor/amphp/amp/lib/Producer.php
vendored
@@ -1,43 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* @template-covariant TValue
|
||||
* @template-implements Iterator<TValue>
|
||||
*/
|
||||
final class Producer implements Iterator
|
||||
{
|
||||
/**
|
||||
* @use Internal\Producer<TValue>
|
||||
*/
|
||||
use CallableMaker, Internal\Producer;
|
||||
|
||||
/**
|
||||
* @param callable(callable(TValue):Promise):\Generator $producer
|
||||
*
|
||||
* @throws \Error Thrown if the callable does not return a Generator.
|
||||
*/
|
||||
public function __construct(callable $producer)
|
||||
{
|
||||
$result = $producer($this->callableFromInstanceMethod("emit"));
|
||||
|
||||
if (!$result instanceof \Generator) {
|
||||
throw new \Error("The callable did not return a Generator");
|
||||
}
|
||||
|
||||
$coroutine = new Coroutine($result);
|
||||
$coroutine->onResolve(function ($exception) {
|
||||
if ($this->complete) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($exception) {
|
||||
$this->fail($exception);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->complete();
|
||||
});
|
||||
}
|
||||
}
|
||||
37
vendor/amphp/amp/lib/Promise.php
vendored
37
vendor/amphp/amp/lib/Promise.php
vendored
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* Representation of the future value of an asynchronous operation.
|
||||
*
|
||||
* @template-covariant TValue
|
||||
* @psalm-yield TValue
|
||||
*/
|
||||
interface Promise
|
||||
{
|
||||
/**
|
||||
* Registers a callback to be invoked when the promise is resolved.
|
||||
*
|
||||
* If this method is called multiple times, additional handlers will be registered instead of replacing any already
|
||||
* existing handlers.
|
||||
*
|
||||
* If the promise is already resolved, the callback MUST be executed immediately.
|
||||
*
|
||||
* Exceptions MUST NOT be thrown from this method. Any exceptions thrown from invoked callbacks MUST be
|
||||
* forwarded to the event-loop error handler.
|
||||
*
|
||||
* Note: You shouldn't implement this interface yourself. Instead, provide a method that returns a promise for the
|
||||
* operation you're implementing. Objects other than pure placeholders implementing it are a very bad idea.
|
||||
*
|
||||
* @param callable $onResolved The first argument shall be `null` on success, while the second shall be `null` on
|
||||
* failure.
|
||||
*
|
||||
* @psalm-param callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
|
||||
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
|
||||
* mixed>|null) | callable(\Throwable|null, mixed): void $onResolved
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function onResolve(callable $onResolved);
|
||||
}
|
||||
78
vendor/amphp/amp/lib/Struct.php
vendored
78
vendor/amphp/amp/lib/Struct.php
vendored
@@ -1,78 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* A "safe" struct trait for public property aggregators.
|
||||
*
|
||||
* This trait is intended to make using public properties a little safer by throwing when
|
||||
* nonexistent property names are read or written.
|
||||
*/
|
||||
trait Struct
|
||||
{
|
||||
/**
|
||||
* The minimum percentage [0-100] at which to recommend a similar property
|
||||
* name when generating error messages.
|
||||
*/
|
||||
private $__propertySuggestThreshold = 70;
|
||||
|
||||
/**
|
||||
* @param string $property
|
||||
*
|
||||
* @psalm-return no-return
|
||||
*/
|
||||
public function __get(string $property)
|
||||
{
|
||||
throw new \Error(
|
||||
$this->generateStructPropertyError($property)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $property
|
||||
* @param mixed $value
|
||||
*
|
||||
* @psalm-return no-return
|
||||
*/
|
||||
public function __set(string $property, $value)
|
||||
{
|
||||
throw new \Error(
|
||||
$this->generateStructPropertyError($property)
|
||||
);
|
||||
}
|
||||
|
||||
private function generateStructPropertyError(string $property): string
|
||||
{
|
||||
$suggestion = $this->suggestPropertyName($property);
|
||||
$suggestStr = ($suggestion == "") ? "" : " ... did you mean \"{$suggestion}?\"";
|
||||
|
||||
return \sprintf(
|
||||
"%s property \"%s\" does not exist%s",
|
||||
\str_replace("\0", "@", \get_class($this)), // Handle anonymous class names.
|
||||
$property,
|
||||
$suggestStr
|
||||
);
|
||||
}
|
||||
|
||||
private function suggestPropertyName(string $badProperty): string
|
||||
{
|
||||
$badProperty = \strtolower($badProperty);
|
||||
$bestMatch = "";
|
||||
$bestMatchPercentage = 0;
|
||||
|
||||
/** @psalm-suppress RawObjectIteration */
|
||||
foreach ($this as $property => $value) {
|
||||
// Never suggest properties that begin with an underscore
|
||||
if ($property[0] === "_") {
|
||||
continue;
|
||||
}
|
||||
\similar_text($badProperty, \strtolower($property), $byRefPercentage);
|
||||
if ($byRefPercentage > $bestMatchPercentage) {
|
||||
$bestMatchPercentage = $byRefPercentage;
|
||||
$bestMatch = $property;
|
||||
}
|
||||
}
|
||||
|
||||
return ($bestMatchPercentage >= $this->__propertySuggestThreshold) ? $bestMatch : "";
|
||||
}
|
||||
}
|
||||
60
vendor/amphp/amp/lib/Success.php
vendored
60
vendor/amphp/amp/lib/Success.php
vendored
@@ -1,60 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
|
||||
/**
|
||||
* Creates a successful promise using the given value (which can be any value except an object implementing
|
||||
* `Amp\Promise` or `React\Promise\PromiseInterface`).
|
||||
*
|
||||
* @template-covariant TValue
|
||||
* @template-implements Promise<TValue>
|
||||
*/
|
||||
final class Success implements Promise
|
||||
{
|
||||
/** @var mixed */
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* @param mixed $value Anything other than a Promise object.
|
||||
*
|
||||
* @psalm-param TValue $value
|
||||
*
|
||||
* @throws \Error If a promise is given as the value.
|
||||
*/
|
||||
public function __construct($value = null)
|
||||
{
|
||||
if ($value instanceof Promise || $value instanceof ReactPromise) {
|
||||
throw new \Error("Cannot use a promise as success value");
|
||||
}
|
||||
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function onResolve(callable $onResolved)
|
||||
{
|
||||
try {
|
||||
$result = $onResolved(null, $this->value);
|
||||
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
Promise\rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use ($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
use function Amp\Internal\formatStacktrace;
|
||||
|
||||
/**
|
||||
* A TimeoutCancellationToken automatically requests cancellation after the timeout has elapsed.
|
||||
*/
|
||||
final class TimeoutCancellationToken implements CancellationToken
|
||||
{
|
||||
/** @var string */
|
||||
private $watcher;
|
||||
|
||||
/** @var CancellationToken */
|
||||
private $token;
|
||||
|
||||
/**
|
||||
* @param int $timeout Milliseconds until cancellation is requested.
|
||||
* @param string $message Message for TimeoutException. Default is "Operation timed out".
|
||||
*/
|
||||
public function __construct(int $timeout, string $message = "Operation timed out")
|
||||
{
|
||||
$source = new CancellationTokenSource;
|
||||
$this->token = $source->getToken();
|
||||
|
||||
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
$this->watcher = Loop::delay($timeout, static function () use ($source, $message, $trace) {
|
||||
$trace = formatStacktrace($trace);
|
||||
$source->cancel(new TimeoutException("$message\r\nTimeoutCancellationToken was created here:\r\n$trace"));
|
||||
});
|
||||
|
||||
Loop::unreference($this->watcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the delay watcher.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
Loop::cancel($this->watcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function subscribe(callable $callback): string
|
||||
{
|
||||
return $this->token->subscribe($callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function unsubscribe(string $id)
|
||||
{
|
||||
$this->token->unsubscribe($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isRequested(): bool
|
||||
{
|
||||
return $this->token->isRequested();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function throwIfRequested()
|
||||
{
|
||||
$this->token->throwIfRequested();
|
||||
}
|
||||
}
|
||||
19
vendor/amphp/amp/lib/TimeoutException.php
vendored
19
vendor/amphp/amp/lib/TimeoutException.php
vendored
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* Thrown if a promise doesn't resolve within a specified timeout.
|
||||
*
|
||||
* @see \Amp\Promise\timeout()
|
||||
*/
|
||||
class TimeoutException extends \Exception
|
||||
{
|
||||
/**
|
||||
* @param string $message Exception message.
|
||||
*/
|
||||
public function __construct(string $message = "Operation timed out")
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
825
vendor/amphp/amp/lib/functions.php
vendored
825
vendor/amphp/amp/lib/functions.php
vendored
@@ -1,825 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp
|
||||
{
|
||||
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
|
||||
/**
|
||||
* Returns a new function that wraps $callback in a promise/coroutine-aware function that automatically runs
|
||||
* Generators as coroutines. The returned function always returns a promise when invoked. Errors have to be handled
|
||||
* by the callback caller or they will go unnoticed.
|
||||
*
|
||||
* Use this function to create a coroutine-aware callable for a promise-aware callback caller.
|
||||
*
|
||||
* @template TReturn
|
||||
* @template TPromise
|
||||
* @template TGeneratorReturn
|
||||
* @template TGeneratorPromise
|
||||
*
|
||||
* @template TGenerator as TGeneratorReturn|Promise<TGeneratorPromise>
|
||||
* @template T as TReturn|Promise<TPromise>|\Generator<mixed, mixed, mixed, TGenerator>
|
||||
*
|
||||
* @formatter:off
|
||||
*
|
||||
* @param callable(...mixed): T $callback
|
||||
*
|
||||
* @return callable
|
||||
* @psalm-return (T is Promise ? (callable(mixed...): Promise<TPromise>) : (T is \Generator ? (TGenerator is Promise ? (callable(mixed...): Promise<TGeneratorPromise>) : (callable(mixed...): Promise<TGeneratorReturn>)) : (callable(mixed...): Promise<TReturn>)))
|
||||
*
|
||||
* @formatter:on
|
||||
*
|
||||
* @see asyncCoroutine()
|
||||
*
|
||||
* @psalm-suppress InvalidReturnType
|
||||
*/
|
||||
function coroutine(callable $callback): callable
|
||||
{
|
||||
/** @psalm-suppress InvalidReturnStatement */
|
||||
return static function (...$args) use ($callback): Promise {
|
||||
return call($callback, ...$args);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new function that wraps $callback in a promise/coroutine-aware function that automatically runs
|
||||
* Generators as coroutines. The returned function always returns void when invoked. Errors are forwarded to the
|
||||
* loop's error handler using `Amp\Promise\rethrow()`.
|
||||
*
|
||||
* Use this function to create a coroutine-aware callable for a non-promise-aware callback caller.
|
||||
*
|
||||
* @param callable(...mixed): mixed $callback
|
||||
*
|
||||
* @return callable
|
||||
* @psalm-return callable(mixed...): void
|
||||
*
|
||||
* @see coroutine()
|
||||
*/
|
||||
function asyncCoroutine(callable $callback): callable
|
||||
{
|
||||
return static function (...$args) use ($callback) {
|
||||
Promise\rethrow(call($callback, ...$args));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the given function, always returning a promise. If the function returns a Generator, it will be run as a
|
||||
* coroutine. If the function throws, a failed promise will be returned.
|
||||
*
|
||||
* @template TReturn
|
||||
* @template TPromise
|
||||
* @template TGeneratorReturn
|
||||
* @template TGeneratorPromise
|
||||
*
|
||||
* @template TGenerator as TGeneratorReturn|Promise<TGeneratorPromise>
|
||||
* @template T as TReturn|Promise<TPromise>|\Generator<mixed, mixed, mixed, TGenerator>
|
||||
*
|
||||
* @formatter:off
|
||||
*
|
||||
* @param callable(...mixed): T $callback
|
||||
* @param mixed ...$args Arguments to pass to the function.
|
||||
*
|
||||
* @return Promise
|
||||
* @psalm-return (T is Promise ? Promise<TPromise> : (T is \Generator ? (TGenerator is Promise ? Promise<TGeneratorPromise> : Promise<TGeneratorReturn>) : Promise<TReturn>))
|
||||
*
|
||||
* @formatter:on
|
||||
*/
|
||||
function call(callable $callback, ...$args): Promise
|
||||
{
|
||||
try {
|
||||
$result = $callback(...$args);
|
||||
} catch (\Throwable $exception) {
|
||||
return new Failure($exception);
|
||||
}
|
||||
|
||||
if ($result instanceof \Generator) {
|
||||
return new Coroutine($result);
|
||||
}
|
||||
|
||||
if ($result instanceof Promise) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ($result instanceof ReactPromise) {
|
||||
return Promise\adapt($result);
|
||||
}
|
||||
|
||||
return new Success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the given function. If the function returns a Generator, it will be run as a coroutine. If the function
|
||||
* throws or returns a failing promise, the failure is forwarded to the loop error handler.
|
||||
*
|
||||
* @param callable(...mixed): mixed $callback
|
||||
* @param mixed ...$args Arguments to pass to the function.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function asyncCall(callable $callback, ...$args)
|
||||
{
|
||||
Promise\rethrow(call($callback, ...$args));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sleeps for the specified number of milliseconds.
|
||||
*
|
||||
* @param int $milliseconds
|
||||
*
|
||||
* @return Delayed
|
||||
*/
|
||||
function delay(int $milliseconds): Delayed
|
||||
{
|
||||
return new Delayed($milliseconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current time relative to an arbitrary point in time.
|
||||
*
|
||||
* @return int Time in milliseconds.
|
||||
*/
|
||||
function getCurrentTime(): int
|
||||
{
|
||||
return Internal\getCurrentTime();
|
||||
}
|
||||
}
|
||||
|
||||
namespace Amp\Promise
|
||||
{
|
||||
|
||||
use Amp\Deferred;
|
||||
use Amp\Loop;
|
||||
use Amp\MultiReasonException;
|
||||
use Amp\Promise;
|
||||
use Amp\Success;
|
||||
use Amp\TimeoutException;
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
use function Amp\call;
|
||||
use function Amp\Internal\createTypeError;
|
||||
|
||||
/**
|
||||
* Registers a callback that will forward the failure reason to the event loop's error handler if the promise fails.
|
||||
*
|
||||
* Use this function if you neither return the promise nor handle a possible error yourself to prevent errors from
|
||||
* going entirely unnoticed.
|
||||
*
|
||||
* @param Promise|ReactPromise $promise Promise to register the handler on.
|
||||
*
|
||||
* @return void
|
||||
* @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface.
|
||||
*
|
||||
*/
|
||||
function rethrow($promise)
|
||||
{
|
||||
if (!$promise instanceof Promise) {
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} else {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
}
|
||||
|
||||
$promise->onResolve(static function ($exception) {
|
||||
if ($exception) {
|
||||
throw $exception;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the event loop until the promise is resolved. Should not be called within a running event loop.
|
||||
*
|
||||
* Use this function only in synchronous contexts to wait for an asynchronous operation. Use coroutines and yield to
|
||||
* await promise resolution in a fully asynchronous application instead.
|
||||
*
|
||||
* @template TPromise
|
||||
* @template T as Promise<TPromise>|ReactPromise
|
||||
*
|
||||
* @param Promise|ReactPromise $promise Promise to wait for.
|
||||
*
|
||||
* @return mixed Promise success value.
|
||||
*
|
||||
* @psalm-param T $promise
|
||||
* @psalm-return (T is Promise ? TPromise : mixed)
|
||||
*
|
||||
* @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface.
|
||||
* @throws \Error If the event loop stopped without the $promise being resolved.
|
||||
* @throws \Throwable Promise failure reason.
|
||||
*/
|
||||
function wait($promise)
|
||||
{
|
||||
if (!$promise instanceof Promise) {
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} else {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
}
|
||||
|
||||
$resolved = false;
|
||||
|
||||
try {
|
||||
Loop::run(function () use (&$resolved, &$value, &$exception, $promise) {
|
||||
$promise->onResolve(function ($e, $v) use (&$resolved, &$value, &$exception) {
|
||||
Loop::stop();
|
||||
$resolved = true;
|
||||
$exception = $e;
|
||||
$value = $v;
|
||||
});
|
||||
});
|
||||
} catch (\Throwable $throwable) {
|
||||
throw new \Error("Loop exceptionally stopped without resolving the promise", 0, $throwable);
|
||||
}
|
||||
|
||||
if (!$resolved) {
|
||||
throw new \Error("Loop stopped without resolving the promise");
|
||||
}
|
||||
|
||||
if ($exception) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an artificial timeout for any `Promise`.
|
||||
*
|
||||
* If the timeout expires before the promise is resolved, the returned promise fails with an instance of
|
||||
* `Amp\TimeoutException`.
|
||||
*
|
||||
* @template TReturn
|
||||
*
|
||||
* @param Promise<TReturn>|ReactPromise $promise Promise to which the timeout is applied.
|
||||
* @param int $timeout Timeout in milliseconds.
|
||||
*
|
||||
* @return Promise<TReturn>
|
||||
*
|
||||
* @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface.
|
||||
*/
|
||||
function timeout($promise, int $timeout): Promise
|
||||
{
|
||||
if (!$promise instanceof Promise) {
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} else {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
}
|
||||
|
||||
$deferred = new Deferred;
|
||||
|
||||
$watcher = Loop::delay($timeout, static function () use (&$deferred) {
|
||||
$temp = $deferred; // prevent double resolve
|
||||
$deferred = null;
|
||||
$temp->fail(new TimeoutException);
|
||||
});
|
||||
Loop::unreference($watcher);
|
||||
|
||||
$promise->onResolve(function () use (&$deferred, $promise, $watcher) {
|
||||
if ($deferred !== null) {
|
||||
Loop::cancel($watcher);
|
||||
$deferred->resolve($promise);
|
||||
}
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an artificial timeout for any `Promise`.
|
||||
*
|
||||
* If the promise is resolved before the timeout expires, the result is returned
|
||||
*
|
||||
* If the timeout expires before the promise is resolved, a default value is returned
|
||||
*
|
||||
* @template TReturn
|
||||
*
|
||||
* @param Promise<TReturn>|ReactPromise $promise Promise to which the timeout is applied.
|
||||
* @param int $timeout Timeout in milliseconds.
|
||||
* @param TReturn $default
|
||||
*
|
||||
* @return Promise<TReturn>
|
||||
*
|
||||
* @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface.
|
||||
*/
|
||||
function timeoutWithDefault($promise, int $timeout, $default = null): Promise
|
||||
{
|
||||
$promise = timeout($promise, $timeout);
|
||||
|
||||
return call(static function () use ($promise, $default) {
|
||||
try {
|
||||
return yield $promise;
|
||||
} catch (TimeoutException $exception) {
|
||||
return $default;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapts any object with a done(callable $onFulfilled, callable $onRejected) or then(callable $onFulfilled,
|
||||
* callable $onRejected) method to a promise usable by components depending on placeholders implementing
|
||||
* \AsyncInterop\Promise.
|
||||
*
|
||||
* @param object $promise Object with a done() or then() method.
|
||||
*
|
||||
* @return Promise Promise resolved by the $thenable object.
|
||||
*
|
||||
* @throws \Error If the provided object does not have a then() method.
|
||||
*/
|
||||
function adapt($promise): Promise
|
||||
{
|
||||
if (!\is_object($promise)) {
|
||||
throw new \Error("Object must be provided");
|
||||
}
|
||||
|
||||
$deferred = new Deferred;
|
||||
|
||||
if (\method_exists($promise, 'done')) {
|
||||
$promise->done([$deferred, 'resolve'], [$deferred, 'fail']);
|
||||
} elseif (\method_exists($promise, 'then')) {
|
||||
$promise->then([$deferred, 'resolve'], [$deferred, 'fail']);
|
||||
} else {
|
||||
throw new \Error("Object must have a 'then' or 'done' method");
|
||||
}
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a promise that is resolved when all promises are resolved. The returned promise will not fail.
|
||||
* Returned promise succeeds with a two-item array delineating successful and failed promise results,
|
||||
* with keys identical and corresponding to the original given array.
|
||||
*
|
||||
* This function is the same as some() with the notable exception that it will never fail even
|
||||
* if all promises in the array resolve unsuccessfully.
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @param Promise<TValue>[]|ReactPromise[] $promises
|
||||
*
|
||||
* @return Promise<array{0: \Throwable[], 1: TValue[]}>
|
||||
*
|
||||
* @throws \Error If a non-Promise is in the array.
|
||||
*/
|
||||
function any(array $promises): Promise
|
||||
{
|
||||
return some($promises, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a promise that succeeds when all promises succeed, and fails if any promise fails. Returned
|
||||
* promise succeeds with an array of values used to succeed each contained promise, with keys corresponding to
|
||||
* the array of promises.
|
||||
*
|
||||
* @param Promise[]|ReactPromise[] $promises Array of only promises.
|
||||
*
|
||||
* @return Promise
|
||||
*
|
||||
* @throws \Error If a non-Promise is in the array.
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @psalm-param array<array-key, Promise<TValue>|ReactPromise> $promises
|
||||
* @psalm-assert array<array-key, Promise<TValue>|ReactPromise> $promises $promises
|
||||
* @psalm-return Promise<array<array-key, TValue>>
|
||||
*/
|
||||
function all(array $promises): Promise
|
||||
{
|
||||
if (empty($promises)) {
|
||||
return new Success([]);
|
||||
}
|
||||
|
||||
$deferred = new Deferred;
|
||||
$result = $deferred->promise();
|
||||
|
||||
$pending = \count($promises);
|
||||
$values = [];
|
||||
|
||||
foreach ($promises as $key => $promise) {
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} elseif (!$promise instanceof Promise) {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
|
||||
$values[$key] = null; // add entry to array to preserve order
|
||||
$promise->onResolve(function ($exception, $value) use (&$deferred, &$values, &$pending, $key) {
|
||||
if ($pending === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($exception) {
|
||||
$pending = 0;
|
||||
$deferred->fail($exception);
|
||||
$deferred = null;
|
||||
return;
|
||||
}
|
||||
|
||||
$values[$key] = $value;
|
||||
if (0 === --$pending) {
|
||||
$deferred->resolve($values);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a promise that succeeds when the first promise succeeds, and fails only if all promises fail.
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @param Promise<TValue>[]|ReactPromise[] $promises Array of only promises.
|
||||
*
|
||||
* @return Promise<TValue>
|
||||
*
|
||||
* @throws \Error If the array is empty or a non-Promise is in the array.
|
||||
*/
|
||||
function first(array $promises): Promise
|
||||
{
|
||||
if (empty($promises)) {
|
||||
throw new \Error("No promises provided");
|
||||
}
|
||||
|
||||
$deferred = new Deferred;
|
||||
$result = $deferred->promise();
|
||||
|
||||
$pending = \count($promises);
|
||||
$exceptions = [];
|
||||
|
||||
foreach ($promises as $key => $promise) {
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} elseif (!$promise instanceof Promise) {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
|
||||
$exceptions[$key] = null; // add entry to array to preserve order
|
||||
$promise->onResolve(function ($error, $value) use (&$deferred, &$exceptions, &$pending, $key) {
|
||||
if ($pending === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$error) {
|
||||
$pending = 0;
|
||||
$deferred->resolve($value);
|
||||
$deferred = null;
|
||||
return;
|
||||
}
|
||||
|
||||
$exceptions[$key] = $error;
|
||||
if (0 === --$pending) {
|
||||
$deferred->fail(new MultiReasonException($exceptions));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves with a two-item array delineating successful and failed Promise results.
|
||||
*
|
||||
* The returned promise will only fail if the given number of required promises fail.
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @param Promise<TValue>[]|ReactPromise[] $promises Array of only promises.
|
||||
* @param int $required Number of promises that must succeed for the
|
||||
* returned promise to succeed.
|
||||
*
|
||||
* @return Promise<array{0: \Throwable[], 1: TValue[]}>
|
||||
*
|
||||
* @throws \Error If a non-Promise is in the array.
|
||||
*/
|
||||
function some(array $promises, int $required = 1): Promise
|
||||
{
|
||||
if ($required < 0) {
|
||||
throw new \Error("Number of promises required must be non-negative");
|
||||
}
|
||||
|
||||
$pending = \count($promises);
|
||||
|
||||
if ($required > $pending) {
|
||||
throw new \Error("Too few promises provided");
|
||||
}
|
||||
|
||||
if (empty($promises)) {
|
||||
return new Success([[], []]);
|
||||
}
|
||||
|
||||
$deferred = new Deferred;
|
||||
$result = $deferred->promise();
|
||||
$values = [];
|
||||
$exceptions = [];
|
||||
|
||||
foreach ($promises as $key => $promise) {
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} elseif (!$promise instanceof Promise) {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
|
||||
$values[$key] = $exceptions[$key] = null; // add entry to arrays to preserve order
|
||||
$promise->onResolve(static function ($exception, $value) use (
|
||||
&$values,
|
||||
&$exceptions,
|
||||
&$pending,
|
||||
$key,
|
||||
$required,
|
||||
$deferred
|
||||
) {
|
||||
if ($exception) {
|
||||
$exceptions[$key] = $exception;
|
||||
unset($values[$key]);
|
||||
} else {
|
||||
$values[$key] = $value;
|
||||
unset($exceptions[$key]);
|
||||
}
|
||||
|
||||
if (0 === --$pending) {
|
||||
if (\count($values) < $required) {
|
||||
$deferred->fail(new MultiReasonException($exceptions));
|
||||
} else {
|
||||
$deferred->resolve([$exceptions, $values]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a promise into another promise, altering the exception or result.
|
||||
*
|
||||
* @param Promise|ReactPromise $promise
|
||||
* @param callable $callback
|
||||
*
|
||||
* @return Promise
|
||||
*/
|
||||
function wrap($promise, callable $callback): Promise
|
||||
{
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} elseif (!$promise instanceof Promise) {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
|
||||
$deferred = new Deferred();
|
||||
|
||||
$promise->onResolve(static function (\Throwable $exception = null, $result) use ($deferred, $callback) {
|
||||
try {
|
||||
$result = $callback($exception, $result);
|
||||
} catch (\Throwable $exception) {
|
||||
$deferred->fail($exception);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$deferred->resolve($result);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
}
|
||||
|
||||
namespace Amp\Iterator
|
||||
{
|
||||
|
||||
use Amp\Delayed;
|
||||
use Amp\Emitter;
|
||||
use Amp\Iterator;
|
||||
use Amp\Producer;
|
||||
use Amp\Promise;
|
||||
use function Amp\call;
|
||||
use function Amp\coroutine;
|
||||
use function Amp\Internal\createTypeError;
|
||||
|
||||
/**
|
||||
* Creates an iterator from the given iterable, emitting the each value. The iterable may contain promises. If any
|
||||
* promise fails, the iterator will fail with the same reason.
|
||||
*
|
||||
* @param array|\Traversable $iterable Elements to emit.
|
||||
* @param int $delay Delay between element emissions in milliseconds.
|
||||
*
|
||||
* @return Iterator
|
||||
*
|
||||
* @throws \TypeError If the argument is not an array or instance of \Traversable.
|
||||
*/
|
||||
function fromIterable(/* iterable */
|
||||
$iterable,
|
||||
int $delay = 0
|
||||
): Iterator {
|
||||
if (!$iterable instanceof \Traversable && !\is_array($iterable)) {
|
||||
throw createTypeError(["array", "Traversable"], $iterable);
|
||||
}
|
||||
|
||||
if ($delay) {
|
||||
return new Producer(static function (callable $emit) use ($iterable, $delay) {
|
||||
foreach ($iterable as $value) {
|
||||
yield new Delayed($delay);
|
||||
yield $emit($value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return new Producer(static function (callable $emit) use ($iterable) {
|
||||
foreach ($iterable as $value) {
|
||||
yield $emit($value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @template TValue
|
||||
* @template TReturn
|
||||
*
|
||||
* @param Iterator<TValue> $iterator
|
||||
* @param callable (TValue $value): TReturn $onEmit
|
||||
*
|
||||
* @return Iterator<TReturn>
|
||||
*/
|
||||
function map(Iterator $iterator, callable $onEmit): Iterator
|
||||
{
|
||||
return new Producer(static function (callable $emit) use ($iterator, $onEmit) {
|
||||
while (yield $iterator->advance()) {
|
||||
yield $emit($onEmit($iterator->getCurrent()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @template TValue
|
||||
*
|
||||
* @param Iterator<TValue> $iterator
|
||||
* @param callable(TValue $value):bool $filter
|
||||
*
|
||||
* @return Iterator<TValue>
|
||||
*/
|
||||
function filter(Iterator $iterator, callable $filter): Iterator
|
||||
{
|
||||
return new Producer(static function (callable $emit) use ($iterator, $filter) {
|
||||
while (yield $iterator->advance()) {
|
||||
if ($filter($iterator->getCurrent())) {
|
||||
yield $emit($iterator->getCurrent());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an iterator that emits values emitted from any iterator in the array of iterators.
|
||||
*
|
||||
* @param Iterator[] $iterators
|
||||
*
|
||||
* @return Iterator
|
||||
*/
|
||||
function merge(array $iterators): Iterator
|
||||
{
|
||||
$emitter = new Emitter;
|
||||
$result = $emitter->iterate();
|
||||
|
||||
$coroutine = coroutine(static function (Iterator $iterator) use (&$emitter) {
|
||||
while ((yield $iterator->advance()) && $emitter !== null) {
|
||||
yield $emitter->emit($iterator->getCurrent());
|
||||
}
|
||||
});
|
||||
|
||||
$coroutines = [];
|
||||
foreach ($iterators as $iterator) {
|
||||
if (!$iterator instanceof Iterator) {
|
||||
throw createTypeError([Iterator::class], $iterator);
|
||||
}
|
||||
|
||||
$coroutines[] = $coroutine($iterator);
|
||||
}
|
||||
|
||||
Promise\all($coroutines)->onResolve(static function ($exception) use (&$emitter) {
|
||||
if ($exception) {
|
||||
$emitter->fail($exception);
|
||||
$emitter = null;
|
||||
} else {
|
||||
$emitter->complete();
|
||||
}
|
||||
});
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenates the given iterators into a single iterator, emitting values from a single iterator at a time. The
|
||||
* prior iterator must complete before values are emitted from any subsequent iterators. Iterators are concatenated
|
||||
* in the order given (iteration order of the array).
|
||||
*
|
||||
* @param Iterator[] $iterators
|
||||
*
|
||||
* @return Iterator
|
||||
*/
|
||||
function concat(array $iterators): Iterator
|
||||
{
|
||||
foreach ($iterators as $iterator) {
|
||||
if (!$iterator instanceof Iterator) {
|
||||
throw createTypeError([Iterator::class], $iterator);
|
||||
}
|
||||
}
|
||||
|
||||
$emitter = new Emitter;
|
||||
$previous = [];
|
||||
$promise = Promise\all($previous);
|
||||
|
||||
$coroutine = coroutine(static function (Iterator $iterator, callable $emit) {
|
||||
while (yield $iterator->advance()) {
|
||||
yield $emit($iterator->getCurrent());
|
||||
}
|
||||
});
|
||||
|
||||
foreach ($iterators as $iterator) {
|
||||
$emit = coroutine(static function ($value) use ($emitter, $promise) {
|
||||
static $pending = true, $failed = false;
|
||||
|
||||
if ($failed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($pending) {
|
||||
try {
|
||||
yield $promise;
|
||||
$pending = false;
|
||||
} catch (\Throwable $exception) {
|
||||
$failed = true;
|
||||
return; // Prior iterator failed.
|
||||
}
|
||||
}
|
||||
|
||||
yield $emitter->emit($value);
|
||||
});
|
||||
$previous[] = $coroutine($iterator, $emit);
|
||||
$promise = Promise\all($previous);
|
||||
}
|
||||
|
||||
$promise->onResolve(static function ($exception) use ($emitter) {
|
||||
if ($exception) {
|
||||
$emitter->fail($exception);
|
||||
return;
|
||||
}
|
||||
|
||||
$emitter->complete();
|
||||
});
|
||||
|
||||
return $emitter->iterate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Discards all remaining items and returns the number of discarded items.
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @param Iterator $iterator
|
||||
*
|
||||
* @return Promise
|
||||
*
|
||||
* @psalm-param Iterator<TValue> $iterator
|
||||
* @psalm-return Promise<int>
|
||||
*/
|
||||
function discard(Iterator $iterator): Promise
|
||||
{
|
||||
return call(static function () use ($iterator): \Generator {
|
||||
$count = 0;
|
||||
|
||||
while (yield $iterator->advance()) {
|
||||
$count++;
|
||||
}
|
||||
|
||||
return $count;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all items from an iterator into an array.
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @param Iterator $iterator
|
||||
*
|
||||
* @psalm-param Iterator<TValue> $iterator
|
||||
*
|
||||
* @return Promise
|
||||
* @psalm-return Promise<array<array-key, TValue>>
|
||||
*/
|
||||
function toArray(Iterator $iterator): Promise
|
||||
{
|
||||
return call(static function () use ($iterator) {
|
||||
/** @psalm-var list $array */
|
||||
$array = [];
|
||||
|
||||
while (yield $iterator->advance()) {
|
||||
$array[] = $iterator->getCurrent();
|
||||
}
|
||||
|
||||
return $array;
|
||||
});
|
||||
}
|
||||
}
|
||||
135
vendor/amphp/byte-stream/.github/workflows/ci.yml
vendored
135
vendor/amphp/byte-stream/.github/workflows/ci.yml
vendored
@@ -1,135 +0,0 @@
|
||||
name: Continuous Integration
|
||||
|
||||
on:
|
||||
push: null
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
unit_tests:
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- operating-system: 'ubuntu-latest'
|
||||
php-version: '7.1'
|
||||
|
||||
- operating-system: 'ubuntu-latest'
|
||||
php-version: '7.2'
|
||||
|
||||
- operating-system: 'ubuntu-latest'
|
||||
php-version: '7.3'
|
||||
|
||||
- operating-system: 'ubuntu-latest'
|
||||
php-version: '7.4'
|
||||
|
||||
- operating-system: 'ubuntu-latest'
|
||||
php-version: '8.0'
|
||||
composer-flags: '--ignore-platform-req=php'
|
||||
|
||||
- operating-system: 'windows-latest'
|
||||
php-version: '8.0'
|
||||
composer-flags: '--ignore-platform-req=php'
|
||||
|
||||
- operating-system: 'macos-latest'
|
||||
php-version: '8.0'
|
||||
composer-flags: '--ignore-platform-req=php'
|
||||
|
||||
name: PHP ${{ matrix.php-version }} on ${{ matrix.operating-system }}
|
||||
|
||||
runs-on: ${{ matrix.operating-system }}
|
||||
|
||||
steps:
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: ${{ matrix.php-version }}
|
||||
|
||||
- name: Use LF line ends
|
||||
run: |
|
||||
git config --global core.autocrlf false
|
||||
git config --global core.eol lf
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Get Composer cache directory
|
||||
id: composer-cache
|
||||
run: echo "::set-output name=dir::$(composer config cache-dir)"
|
||||
|
||||
- name: Cache dependencies
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ${{ steps.composer-cache.outputs.dir }}
|
||||
key: composer-${{ runner.os }}-${{ matrix.php-version }}-${{ hashFiles('**/composer.*') }}-${{ matrix.composer-flags }}
|
||||
restore-keys: |
|
||||
composer-${{ runner.os }}-${{ matrix.php-version }}-${{ hashFiles('**/composer.*') }}-
|
||||
composer-${{ runner.os }}-${{ matrix.php-version }}-
|
||||
|
||||
- name: Install dependencies
|
||||
uses: nick-invision/retry@v2
|
||||
with:
|
||||
timeout_minutes: 5
|
||||
max_attempts: 5
|
||||
retry_wait_seconds: 30
|
||||
command: |
|
||||
php_version=$(php -v)
|
||||
composer update --optimize-autoloader --no-interaction --no-progress ${{ matrix.composer-flags }}
|
||||
composer info -D
|
||||
|
||||
- name: Run unit tests
|
||||
run: vendor/bin/phpunit --verbose
|
||||
|
||||
coding_standards:
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- operating-system: 'ubuntu-latest'
|
||||
php-version: '8.0'
|
||||
composer-flags: '--ignore-platform-req=php'
|
||||
|
||||
name: Coding standards
|
||||
runs-on: ${{ matrix.operating-system }}
|
||||
|
||||
steps:
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: ${{ matrix.php-version }}
|
||||
|
||||
- name: Use LF line ends
|
||||
run: |
|
||||
git config --global core.autocrlf false
|
||||
git config --global core.eol lf
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Get Composer cache directory
|
||||
id: composer-cache
|
||||
run: echo "::set-output name=dir::$(composer config cache-dir)"
|
||||
|
||||
- name: Cache dependencies
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ${{ steps.composer-cache.outputs.dir }}
|
||||
key: composer-${{ runner.os }}-${{ matrix.php-version }}-${{ hashFiles('**/composer.*') }}-${{ matrix.composer-flags }}
|
||||
restore-keys: |
|
||||
composer-${{ runner.os }}-${{ matrix.php-version }}-${{ hashFiles('**/composer.*') }}-
|
||||
composer-${{ runner.os }}-${{ matrix.php-version }}-
|
||||
|
||||
- name: Install dependencies
|
||||
uses: nick-invision/retry@v2
|
||||
with:
|
||||
timeout_minutes: 5
|
||||
max_attempts: 5
|
||||
retry_wait_seconds: 30
|
||||
command: |
|
||||
php_version=$(php -v)
|
||||
composer update --optimize-autoloader --no-interaction --no-progress ${{ matrix.composer-flags }}
|
||||
composer info -D
|
||||
|
||||
- name: Run style fixer
|
||||
env:
|
||||
PHP_CS_FIXER_IGNORE_ENV: 1
|
||||
run: vendor/bin/php-cs-fixer --diff --dry-run -v fix
|
||||
22
vendor/amphp/byte-stream/LICENSE
vendored
22
vendor/amphp/byte-stream/LICENSE
vendored
@@ -1,22 +0,0 @@
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016-2021 amphp
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
58
vendor/amphp/byte-stream/composer.json
vendored
58
vendor/amphp/byte-stream/composer.json
vendored
@@ -1,58 +0,0 @@
|
||||
{
|
||||
"name": "amphp/byte-stream",
|
||||
"homepage": "http://amphp.org/byte-stream",
|
||||
"description": "A stream abstraction to make working with non-blocking I/O simple.",
|
||||
"support": {
|
||||
"issues": "https://github.com/amphp/byte-stream/issues",
|
||||
"irc": "irc://irc.freenode.org/amphp"
|
||||
},
|
||||
"keywords": [
|
||||
"stream",
|
||||
"async",
|
||||
"non-blocking",
|
||||
"amp",
|
||||
"amphp",
|
||||
"io"
|
||||
],
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Aaron Piotrowski",
|
||||
"email": "aaron@trowski.com"
|
||||
},
|
||||
{
|
||||
"name": "Niklas Keller",
|
||||
"email": "me@kelunik.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.1",
|
||||
"amphp/amp": "^2"
|
||||
},
|
||||
"require-dev": {
|
||||
"amphp/phpunit-util": "^1.4",
|
||||
"phpunit/phpunit": "^6 || ^7 || ^8",
|
||||
"friendsofphp/php-cs-fixer": "^2.3",
|
||||
"amphp/php-cs-fixer-config": "dev-master",
|
||||
"psalm/phar": "^3.11.4",
|
||||
"jetbrains/phpstorm-stubs": "^2019.3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Amp\\ByteStream\\": "lib"
|
||||
},
|
||||
"files": [
|
||||
"lib/functions.php"
|
||||
]
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Amp\\ByteStream\\Test\\": "test"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.x-dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\ByteStream\Base64;
|
||||
|
||||
use Amp\ByteStream\InputStream;
|
||||
use Amp\ByteStream\StreamException;
|
||||
use Amp\Promise;
|
||||
use function Amp\call;
|
||||
|
||||
final class Base64DecodingInputStream implements InputStream
|
||||
{
|
||||
/** @var InputStream|null */
|
||||
private $source;
|
||||
|
||||
/** @var string|null */
|
||||
private $buffer = '';
|
||||
|
||||
public function __construct(InputStream $source)
|
||||
{
|
||||
$this->source = $source;
|
||||
}
|
||||
|
||||
public function read(): Promise
|
||||
{
|
||||
return call(function () {
|
||||
if ($this->source === null) {
|
||||
throw new StreamException('Failed to read stream chunk due to invalid base64 data');
|
||||
}
|
||||
|
||||
$chunk = yield $this->source->read();
|
||||
if ($chunk === null) {
|
||||
if ($this->buffer === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$chunk = \base64_decode($this->buffer, true);
|
||||
if ($chunk === false) {
|
||||
$this->source = null;
|
||||
$this->buffer = null;
|
||||
|
||||
throw new StreamException('Failed to read stream chunk due to invalid base64 data');
|
||||
}
|
||||
|
||||
$this->buffer = null;
|
||||
|
||||
return $chunk;
|
||||
}
|
||||
|
||||
$this->buffer .= $chunk;
|
||||
|
||||
$length = \strlen($this->buffer);
|
||||
$chunk = \base64_decode(\substr($this->buffer, 0, $length - $length % 4), true);
|
||||
if ($chunk === false) {
|
||||
$this->source = null;
|
||||
$this->buffer = null;
|
||||
|
||||
throw new StreamException('Failed to read stream chunk due to invalid base64 data');
|
||||
}
|
||||
|
||||
$this->buffer = \substr($this->buffer, $length - $length % 4);
|
||||
|
||||
return $chunk;
|
||||
});
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user