1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Caridea |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2017-2018 LibreWorks contributors |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Caridea\Dispatch; |
22
|
|
|
|
23
|
|
|
use Psr\Http\Message\ResponseInterface; |
24
|
|
|
use Caridea\Dispatch\Middleware\Prioritized; |
25
|
|
|
use Caridea\Dispatch\Middleware\PriorityDelegate; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Like `Runner`, but runs middleware in order of priority in addition to |
29
|
|
|
* insertion order. |
30
|
|
|
* |
31
|
|
|
* @copyright 2017-2018 LibreWorks contributors |
32
|
|
|
* @license Apache-2.0 |
33
|
|
|
*/ |
34
|
|
|
class PriorityRunner extends Runner |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
*/ |
39
|
1 |
|
public function __construct(iterable $middleware, ?ResponseInterface $response = null) |
40
|
|
|
{ |
41
|
1 |
|
parent::__construct($middleware, $response); |
42
|
1 |
|
$this->middleware = array_map(function ($mw) { |
43
|
1 |
|
return $mw instanceof Prioritized ? |
44
|
1 |
|
$mw : new PriorityDelegate($mw, PHP_INT_MIN); |
45
|
1 |
|
}, $this->middleware); |
46
|
|
|
// Higher priority should go first; $this->middleware is reversed |
47
|
1 |
|
usort($this->middleware, function ($a, $b) { |
48
|
1 |
|
return $a->getPriority() <=> $b->getPriority(); |
49
|
1 |
|
}); |
50
|
1 |
|
} |
51
|
|
|
} |
52
|
|
|
|