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