|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FondBot\Application; |
|
6
|
|
|
|
|
7
|
|
|
use Throwable; |
|
8
|
|
|
use Whoops\Run; |
|
9
|
|
|
use Monolog\Logger; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
use Monolog\Handler\HandlerInterface; |
|
12
|
|
|
use Whoops\Handler\PrettyPageHandler; |
|
13
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
|
14
|
|
|
use League\Container\ServiceProvider\BootableServiceProviderInterface; |
|
15
|
|
|
|
|
16
|
|
|
abstract class LogServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface |
|
17
|
|
|
{ |
|
18
|
|
|
protected $provides = [ |
|
19
|
|
|
LoggerInterface::class, |
|
20
|
|
|
'application_log', |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Define handlers. |
|
25
|
|
|
* |
|
26
|
|
|
* @return HandlerInterface[] |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract public function handlers(): array; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Method will be invoked on registration of a service provider implementing |
|
32
|
|
|
* this interface. Provides ability for eager loading of Service Providers. |
|
33
|
|
|
* |
|
34
|
|
|
* @return void |
|
35
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
|
38
|
|
|
* @throws \Exception |
|
39
|
|
|
* @throws \InvalidArgumentException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function boot(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$this->container->share(LoggerInterface::class, function () { |
|
44
|
|
|
$logger = new Logger('FondBot'); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($this->handlers() as $handler) { |
|
47
|
|
|
$logger->pushHandler($handler); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $logger; |
|
51
|
|
|
}); |
|
52
|
|
|
|
|
53
|
|
|
if (PHP_SAPI !== 'cli') { |
|
54
|
|
|
$whoops = new Run; |
|
55
|
|
|
$whoops->pushHandler(new PrettyPageHandler); |
|
|
|
|
|
|
56
|
|
|
$whoops->pushHandler(function (Throwable $exception) { |
|
57
|
|
|
/** @var LoggerInterface $logger */ |
|
58
|
|
|
$logger = $this->container->get(LoggerInterface::class); |
|
59
|
|
|
|
|
60
|
|
|
$logger->error($exception); |
|
61
|
|
|
}); |
|
62
|
|
|
$whoops->register(); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Use the register method to register items with the container via the |
|
68
|
|
|
* protected $this->container property or the `getContainer` method |
|
69
|
|
|
* from the ContainerAwareTrait. |
|
70
|
|
|
* |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
|
|
public function register(): void |
|
74
|
|
|
{ |
|
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: