Skip to content

Commit 1f3eb2b

Browse files
authored
Merge pull request #15 from aivis/master
update ModelEventListener, fix 5.4 issue with payload
2 parents 46c45d6 + bba21bc commit 1f3eb2b

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/Understand/UnderstandLaravel5/ModelEventListener.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public function __construct(Logger $logger, array $additionalFields)
3737
*
3838
* @param string $eventName
3939
* @param \Illuminate\Database\Eloquent\Model $model
40+
* @param string $modelLevelEventName
4041
*/
41-
public function logModelEvent($eventName, Model $model)
42+
public function logModelEvent($eventName, Model $model, $modelLevelEventName = null)
4243
{
4344
$log = [
45+
'message' => is_null($modelLevelEventName) ? null : (string)$modelLevelEventName,
4446
'id' => (int) $model->id,
4547
'model_event' => $eventName,
4648
'model_name' => get_class($model),

src/Understand/UnderstandLaravel5/UnderstandLaravel5ServiceProvider.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Illuminate\Foundation\AliasLoader;
55
use Illuminate\Support\Str;
66
use Illuminate\Foundation\Application;
7+
use Illuminate\Database\Eloquent\Model as EloquentModel;
78

89
class UnderstandLaravel5ServiceProvider extends ServiceProvider
910
{
@@ -213,7 +214,18 @@ protected function registerModelEventListenerProvider()
213214
return new ModelEventListener($logger, $additional);
214215
});
215216
}
216-
217+
218+
/**
219+
* Detect Laravel version
220+
*
221+
* @param array $versions
222+
* @return type
223+
*/
224+
protected function detectLaravelVersion(array $versions)
225+
{
226+
return Str::startsWith(Application::VERSION, $versions);
227+
}
228+
217229
/**
218230
* Listen Laravel logs
219231
*
@@ -222,7 +234,7 @@ protected function registerModelEventListenerProvider()
222234
protected function listenLaravelEvents()
223235
{
224236
// only Laravel versions below L5.4 supports `illuminate.log`
225-
if (Str::startsWith(Application::VERSION, ['5.0', '5.1', '5.2', '5.3']))
237+
if ($this->detectLaravelVersion(['5.0', '5.1', '5.2', '5.3']))
226238
{
227239
$this->app['events']->listen('illuminate.log', function($level, $message, $context)
228240
{
@@ -303,10 +315,25 @@ protected function listenEloquentEvents()
303315

304316
foreach ($events as $listenerName => $eventName)
305317
{
306-
$this->app['events']->listen($listenerName, function($model) use($modelLogger, $eventName)
318+
if ($this->detectLaravelVersion(['5.0', '5.1', '5.2', '5.3']))
307319
{
308-
$modelLogger->logModelEvent($eventName, $model);
309-
});
320+
$this->app['events']->listen($listenerName, function($model) use($modelLogger, $eventName)
321+
{
322+
$modelLevelEventName = 'eloquent.' . $eventName . ': ' . get_class($model);
323+
324+
$modelLogger->logModelEvent($eventName, $model, $modelLevelEventName);
325+
});
326+
}
327+
else
328+
{
329+
$this->app['events']->listen($listenerName, function($modelLevelEventName, $eventPayload) use($modelLogger, $eventName)
330+
{
331+
if (isset($eventPayload[0]) && $eventPayload[0] instanceof EloquentModel)
332+
{
333+
$modelLogger->logModelEvent($eventName, $eventPayload[0], $modelLevelEventName);
334+
}
335+
});
336+
}
310337
}
311338
}
312339

0 commit comments

Comments
 (0)