|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FondBot\Application; |
|
6
|
|
|
|
|
7
|
|
|
use Whoops\Run; |
|
8
|
|
|
use Zend\Diactoros\Response; |
|
9
|
|
|
use League\Route\RouteCollection; |
|
10
|
|
|
use Whoops\Handler\PrettyPageHandler; |
|
11
|
|
|
use Zend\Diactoros\Response\SapiEmitter; |
|
12
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
|
13
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
|
14
|
|
|
|
|
15
|
|
|
class RouteServiceProvider extends AbstractServiceProvider |
|
16
|
|
|
{ |
|
17
|
|
|
protected $provides = [ |
|
18
|
|
|
'request', |
|
19
|
|
|
'response', |
|
20
|
|
|
'emitter', |
|
21
|
|
|
'router', |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
private $prefix; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(string $prefix) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->prefix = $prefix; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Use the register method to register items with the container via the |
|
33
|
|
|
* protected $this->container property or the `getContainer` method |
|
34
|
|
|
* from the ContainerAwareTrait. |
|
35
|
|
|
* |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
|
|
public function register(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$whoops = new Run; |
|
41
|
|
|
$whoops->pushHandler(new PrettyPageHandler); |
|
|
|
|
|
|
42
|
|
|
$whoops->register(); |
|
43
|
|
|
|
|
44
|
|
|
$this->getContainer()->share('request', function () { |
|
45
|
|
|
return ServerRequestFactory::fromGlobals( |
|
46
|
|
|
$_SERVER, |
|
47
|
|
|
$_GET, |
|
48
|
|
|
$_POST, |
|
49
|
|
|
$_COOKIE, |
|
50
|
|
|
$_FILES |
|
51
|
|
|
); |
|
52
|
|
|
}); |
|
53
|
|
|
$this->getContainer()->share('response', Response::class); |
|
54
|
|
|
$this->getContainer()->share('emitter', SapiEmitter::class); |
|
55
|
|
|
|
|
56
|
|
|
$this->getContainer()->share('router', function () { |
|
57
|
|
|
$router = new RouteCollection($this->getContainer()); |
|
58
|
|
|
|
|
59
|
|
|
$controller = new Controller($this->getContainer()->get(Kernel::class)); |
|
60
|
|
|
|
|
61
|
|
|
$router->map('GET', $this->buildPath('/'), [$controller, 'index']); |
|
62
|
|
|
$router->map('GET', $this->buildPath('/channels/{name}'), [$controller, 'webhook']); |
|
63
|
|
|
|
|
64
|
|
|
return $router; |
|
65
|
|
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function buildPath(string $path): string |
|
69
|
|
|
{ |
|
70
|
|
|
if ($this->prefix !== null) { |
|
71
|
|
|
return $this->prefix.'/'.$path; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $path; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: