1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of PHPinnacle/Ensign. |
4
|
|
|
* |
5
|
|
|
* (c) PHPinnacle Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types = 1); |
12
|
|
|
|
13
|
|
|
use Amp\Coroutine; |
14
|
|
|
use Amp\Failure; |
|
|
|
|
15
|
|
|
use Amp\LazyPromise; |
16
|
|
|
use Amp\Loop; |
17
|
|
|
use Amp\Promise; |
18
|
|
|
use Amp\Success; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param callable $callable |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
|
function loop(callable $callable): void |
26
|
|
|
{ |
27
|
11 |
|
Loop::run($callable); |
28
|
6 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param mixed $value |
32
|
|
|
* |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
function is_promise($value): bool |
36
|
|
|
{ |
37
|
9 |
|
if (\is_array($value) && \count($value) === \count(\array_filter($value, '\is_promise'))) { |
38
|
1 |
|
return true; |
39
|
|
|
} |
40
|
|
|
|
41
|
9 |
|
return $value instanceof Promise; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param mixed $value |
46
|
|
|
* |
47
|
|
|
* @return Success |
48
|
|
|
*/ |
49
|
|
|
function success($value = null): Success |
50
|
|
|
{ |
51
|
8 |
|
return new Success($value); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Throwable $error |
56
|
|
|
* |
57
|
|
|
* @return Failure |
58
|
|
|
*/ |
59
|
|
|
function failure(\Throwable $error): Failure |
60
|
|
|
{ |
61
|
1 |
|
return new Failure($error); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Generator $generator |
66
|
|
|
* |
67
|
|
|
* @return Coroutine |
68
|
|
|
*/ |
69
|
|
|
function coroutine(\Generator $generator): Coroutine |
70
|
|
|
{ |
71
|
8 |
|
return new Coroutine($generator); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param mixed ...$arguments |
76
|
|
|
* |
77
|
|
|
* @return Promise |
78
|
|
|
*/ |
79
|
|
|
function promise(...$arguments): Promise |
80
|
|
|
{ |
81
|
9 |
|
if (empty($arguments)) { |
82
|
|
|
return \success(); |
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
if (\is_promise($arguments)) { |
86
|
1 |
|
return Promise\all($arguments); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
9 |
|
if (\is_callable($arguments[0])) { |
90
|
2 |
|
$first = \array_shift($arguments); |
91
|
|
|
|
92
|
2 |
|
return Amp\call($first, ...$arguments); |
93
|
|
|
} |
94
|
|
|
|
95
|
7 |
|
return isset($arguments[1]) ? \success($arguments) : \success($arguments[0]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param mixed ...$arguments |
100
|
|
|
* |
101
|
|
|
* @return Promise |
102
|
|
|
*/ |
103
|
|
|
function future(...$arguments): Promise |
104
|
|
|
{ |
105
|
|
|
return new LazyPromise(function () use ($arguments) { |
106
|
2 |
|
return \promise(...$arguments); |
107
|
2 |
|
}); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Promise $promise |
112
|
|
|
* @param int $time |
113
|
|
|
* |
114
|
|
|
* @return Promise |
115
|
|
|
*/ |
116
|
|
|
function timeout(Promise $promise, int $time): Promise |
117
|
|
|
{ |
118
|
|
|
return Promise\timeout($promise, $time); |
119
|
|
|
} |
120
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: