Completed
Push — 1.0 ( 794bad...d76faf )
by Vladimir
08:37
created

LogServiceProvider::handlers()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
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
    ];
21
22
    /**
23
     * Define handlers.
24
     *
25
     * @return HandlerInterface[]
26
     */
27
    abstract public function handlers(): array;
28
29
    /**
30
     * Method will be invoked on registration of a service provider implementing
31
     * this interface. Provides ability for eager loading of Service Providers.
32
     *
33
     * @return void
34
     *
35
     * @throws \Psr\Container\NotFoundExceptionInterface
36
     * @throws \Psr\Container\ContainerExceptionInterface
37
     * @throws \Exception
38
     * @throws \InvalidArgumentException
39
     */
40
    public function boot(): void
41
    {
42 1
        $this->container->share(LoggerInterface::class, function () {
43 1
            $logger = new Logger('FondBot');
44
45 1
            foreach ($this->handlers() as $handler) {
46 1
                $logger->pushHandler($handler);
47
            }
48
49 1
            return $logger;
50 1
        });
51
52 1
        $this->container->share(Logger::class, $this->container->get(LoggerInterface::class));
53
54 1
        if (PHP_SAPI !== 'cli') {
55
            $whoops = new Run;
56
            $whoops->pushHandler(new PrettyPageHandler);
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\PrettyPageHandler() is of type object<Whoops\Handler\PrettyPageHandler>, but the function expects a callable.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
            $whoops->pushHandler(function (Throwable $exception) {
58
                /** @var LoggerInterface $logger */
59
                $logger = $this->container->get(LoggerInterface::class);
60
61
                $logger->error($exception);
62
            });
63
            $whoops->register();
64
        }
65 1
    }
66
67
    /**
68
     * Use the register method to register items with the container via the
69
     * protected $this->container property or the `getContainer` method
70
     * from the ContainerAwareTrait.
71
     *
72
     * @return void
73
     */
74 1
    public function register(): void
75
    {
76 1
    }
77
}
78