1 | <?php |
||
23 | class StreamHandler extends AbstractProcessingHandler |
||
24 | { |
||
25 | /** |
||
26 | * @param resource|string $stream |
||
27 | * @param int $level The minimum logging level at which this handler will be triggered |
||
28 | * @param bool $bubble Whether the messages that are handled can bubble up the stack or not |
||
29 | * @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write) |
||
30 | * @param bool $useLocking Try to lock log file before doing any writes |
||
31 | * |
||
32 | * @throws \Exception If a missing directory is not buildable |
||
33 | * @throws \InvalidArgumentException If stream is not a resource or string |
||
34 | */ |
||
35 | public function __construct( |
||
53 | /** |
||
54 | * Closes the handler. |
||
55 | * |
||
56 | * This will be called automatically when the object is destroyed |
||
57 | */ |
||
58 | public function close() |
||
65 | /** |
||
66 | * Return the currently active stream if it is open. |
||
67 | * |
||
68 | * @return resource|null |
||
69 | */ |
||
70 | public function getStream() |
||
74 | /** |
||
75 | * Return the stream URL if it was configured with a URL and not an active resource. |
||
76 | * |
||
77 | * @return string|null |
||
78 | */ |
||
79 | public function getUrl() |
||
83 | /** |
||
84 | * Checks whether the given record will be handled by this handler. |
||
85 | * |
||
86 | * This is mostly done for performance reasons, to avoid calling processors for nothing. |
||
87 | * |
||
88 | * Handlers should still check the record levels within handle(), returning false in isHandling() |
||
89 | * is no guarantee that handle() will not be called, and isHandling() might not be called |
||
90 | * for a given record. |
||
91 | * |
||
92 | * @param array $record Partial log record containing only a level key |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | public function isHandling(array $record): bool |
||
103 | /** |
||
104 | * Turn on or off preserving(logging) of messages with this handler. |
||
105 | * |
||
106 | * Allows class to stay registered for messages but be enabled or disabled during runtime. |
||
107 | * |
||
108 | * @param bool $value |
||
109 | * |
||
110 | * @return self Fluent interface. |
||
111 | */ |
||
112 | public function setPreserve(bool $value = true): self |
||
117 | /** |
||
118 | * Writes the record down to the log of the implementing handler. |
||
119 | * |
||
120 | * @param array $record |
||
121 | * |
||
122 | * @throws \LogicException |
||
123 | * @throws \UnexpectedValueException |
||
124 | */ |
||
125 | protected function write(array $record) |
||
155 | /** |
||
156 | * @var int|null $filePermission |
||
157 | */ |
||
158 | protected $filePermission; |
||
159 | /** |
||
160 | * @var resource $stream |
||
161 | */ |
||
162 | protected $stream; |
||
163 | /** |
||
164 | * @var string $url |
||
165 | */ |
||
166 | protected $url; |
||
167 | /** |
||
168 | * @var bool $useLocking |
||
169 | */ |
||
170 | protected $useLocking; |
||
171 | /** |
||
172 | * Tries to create the required directory once. |
||
173 | * |
||
174 | * @throws \UnexpectedValueException |
||
175 | */ |
||
176 | private function createDir() |
||
195 | /** |
||
196 | * @param int $code |
||
197 | * @param string $msg |
||
198 | */ |
||
199 | private function customErrorHandler(int $code, string $msg) |
||
203 | /** |
||
204 | * @param string $stream |
||
205 | * |
||
206 | * @return string|null |
||
207 | */ |
||
208 | private function getDirFromStream(string $stream) |
||
218 | /** |
||
219 | * @var bool $dirCreated |
||
220 | */ |
||
221 | private $dirCreated; |
||
222 | /** |
||
223 | * @var string $errorMessage |
||
224 | */ |
||
225 | private $errorMessage; |
||
226 | /** |
||
227 | * @var bool $preserve |
||
228 | */ |
||
229 | private $preserve = true; |
||
230 | } |
||
231 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: