1 | <?php |
||
2 | |||
3 | namespace SilverStripe\FullTextSearch\Utils\Logging; |
||
4 | |||
5 | use Monolog\Formatter\FormatterInterface; |
||
6 | use Monolog\Formatter\LineFormatter; |
||
7 | use Monolog\Handler\HandlerInterface; |
||
8 | use Monolog\Handler\StreamHandler; |
||
9 | use Monolog\Logger; |
||
10 | use SilverStripe\Control\Director; |
||
11 | use SilverStripe\Core\Injector\Injector; |
||
12 | use SilverStripe\FullTextSearch\Utils\Logging\QueuedJobLogHandler; |
||
13 | |||
14 | /** |
||
15 | * Provides logging based on monolog |
||
16 | */ |
||
17 | class MonologFactory implements SearchLogFactory |
||
18 | { |
||
19 | public function getOutputLogger($name, $verbose) |
||
20 | { |
||
21 | $logger = $this->getLoggerFor($name); |
||
22 | $formatter = $this->getFormatter(); |
||
23 | |||
24 | // Notice handling |
||
25 | if ($verbose) { |
||
26 | $messageHandler = $this->getStreamHandler($formatter, 'php://stdout', Logger::INFO); |
||
27 | $logger->pushHandler($messageHandler); |
||
28 | } |
||
29 | |||
30 | // Error handling. buble is false so that errors aren't logged twice |
||
31 | $errorHandler = $this->getStreamHandler($formatter, 'php://stderr', Logger::ERROR, false); |
||
32 | $logger->pushHandler($errorHandler); |
||
33 | return $logger; |
||
0 ignored issues
–
show
|
|||
34 | } |
||
35 | |||
36 | public function getQueuedJobLogger($job) |
||
37 | { |
||
38 | $logger = $this->getLoggerFor(get_class($job)); |
||
39 | $handler = $this->getJobHandler($job); |
||
40 | $logger->pushHandler($handler); |
||
41 | return $logger; |
||
0 ignored issues
–
show
The expression
return $logger returns the type Monolog\Logger which is incompatible with the return type mandated by SilverStripe\FullTextSea...y::getQueuedJobLogger() of Psr\Log .
In the issue above, the returned value is violating the contract defined by the mentioned interface. Let's take a look at an example: interface HasName {
/** @return string */
public function getName();
}
class Name {
public $name;
}
class User implements HasName {
/** @return string|Name */
public function getName() {
return new Name('foo'); // This is a violation of the ``HasName`` interface
// which only allows a string value to be returned.
}
}
Loading history...
|
|||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Generate a handler for the given stream |
||
46 | * |
||
47 | * @param FormatterInterface $formatter |
||
48 | * @param string $stream Name of preferred stream |
||
49 | * @param int $level |
||
50 | * @param bool $bubble |
||
51 | * @return HandlerInterface |
||
52 | */ |
||
53 | protected function getStreamHandler(FormatterInterface $formatter, $stream, $level = Logger::DEBUG, $bubble = true) |
||
54 | { |
||
55 | // Unless cli, force output to php://output |
||
56 | $stream = Director::is_cli() ? $stream : 'php://output'; |
||
57 | $handler = Injector::inst()->createWithArgs( |
||
58 | StreamHandler::class, |
||
59 | array($stream, $level, $bubble) |
||
60 | ); |
||
61 | $handler->setFormatter($formatter); |
||
62 | return $handler; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Gets a formatter for standard output |
||
67 | * |
||
68 | * @return FormatterInterface |
||
69 | */ |
||
70 | protected function getFormatter() |
||
71 | { |
||
72 | // Get formatter |
||
73 | $format = LineFormatter::SIMPLE_FORMAT; |
||
74 | if (!Director::is_cli()) { |
||
75 | $format = "<p>$format</p>"; |
||
76 | } |
||
77 | return Injector::inst()->createWithArgs( |
||
78 | LineFormatter::class, |
||
79 | array($format) |
||
80 | ); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Get a logger for a named class |
||
85 | * |
||
86 | * @param string $name |
||
87 | * @return Logger |
||
88 | */ |
||
89 | protected function getLoggerFor($name) |
||
90 | { |
||
91 | return Injector::inst()->createWithArgs( |
||
92 | Logger::class, |
||
93 | array(strtolower($name)) |
||
94 | ); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Generate handler for a job object |
||
99 | * |
||
100 | * @param QueuedJob $job |
||
101 | * @return HandlerInterface |
||
102 | */ |
||
103 | protected function getJobHandler($job) |
||
104 | { |
||
105 | return Injector::inst()->createWithArgs( |
||
106 | QueuedJobLogHandler::class, |
||
107 | array($job, Logger::INFO) |
||
108 | ); |
||
109 | } |
||
110 | } |
||
111 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: