|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mnabialek\LaravelSqlLogger\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use Mnabialek\LaravelSqlLogger\Config; |
|
6
|
|
|
use Mnabialek\LaravelSqlLogger\SqlLogger; |
|
7
|
|
|
|
|
8
|
|
|
class ServiceProvider extends \Illuminate\Support\ServiceProvider |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Config |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $config; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* {@inheritdoc} |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct($app) |
|
19
|
|
|
{ |
|
20
|
|
|
parent::__construct($app); |
|
21
|
|
|
$this->config = $this->app->make(Config::class); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function register() |
|
28
|
|
|
{ |
|
29
|
|
|
// make sure events will be fired in Lumen |
|
30
|
|
|
$this->app->make('events'); |
|
31
|
|
|
|
|
32
|
|
|
// merge config |
|
33
|
|
|
$this->mergeConfigFrom($this->configFileLocation(), 'sql_logger'); |
|
34
|
|
|
|
|
35
|
|
|
// register files to be published |
|
36
|
|
|
$this->publishes($this->getPublished()); |
|
37
|
|
|
|
|
38
|
|
|
// if no logging is enabled, we can stop here, nothing more should be done |
|
39
|
|
|
if (! $this->shouldLogAnything()) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// create logger class |
|
44
|
|
|
$logger = $this->app->make(SqlLogger::class); |
|
45
|
|
|
|
|
46
|
|
|
// listen to database queries |
|
47
|
|
|
$this->app['db']->listen($this->getListenClosure($logger)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get files to be published. |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getPublished() |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
$this->configFileLocation() => (function_exists('config_path') ? |
|
59
|
|
|
config_path('sql_logger.php') : |
|
60
|
|
|
base_path('config/sql_logger.php')), |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Verify whether anything should be logged. |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function shouldLogAnything() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->config->logAllQueries() || $this->config->logSlowQueries(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get config file location. |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool|string |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function configFileLocation() |
|
80
|
|
|
{ |
|
81
|
|
|
return realpath(__DIR__ . '/../../publish/config/sql_logger.php'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get closure that will be used for listening executed database queries. |
|
86
|
|
|
* |
|
87
|
|
|
* @param SqlLogger $logger |
|
88
|
|
|
* |
|
89
|
|
|
* @return \Closure |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getListenClosure(SqlLogger $logger) |
|
92
|
|
|
{ |
|
93
|
|
|
return function ($query, $bindings = null, $time = null) use ($logger) { |
|
94
|
|
|
$logger->log($query, $bindings, $time); |
|
95
|
|
|
}; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|