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
|
|
|
// merge config |
30
|
|
|
$this->mergeConfigFrom(__DIR__ . '/../../publish/config/sql_logger.php', 'sql_logger'); |
31
|
|
|
|
32
|
|
|
// register files to be published |
33
|
|
|
$this->publishes($this->getPublished()); |
34
|
|
|
|
35
|
|
|
// get settings |
36
|
|
|
$logStatus = $this->config->logQueries(); |
37
|
|
|
$slowLogStatus = $this->config->logSlowQueries(); |
38
|
|
|
$slowLogTime = $this->config->slowLogTime(); |
39
|
|
|
$override = $this->config->overrideFile(); |
40
|
|
|
$directory = $this->config->logDirectory(); |
41
|
|
|
$convertToSeconds = $this->config->useSeconds(); |
42
|
|
|
$separateConsoleLog = $this->config->separateConsoleLogs(); |
43
|
|
|
|
44
|
|
|
// if any of logging type is enabled we will listen database to get all |
45
|
|
|
// executed queries |
46
|
|
|
if ($logStatus || $slowLogStatus) { |
47
|
|
|
// create logger class |
48
|
|
|
$logger = new SqlLogger($this->app, $logStatus, $slowLogStatus, $slowLogTime, $override, |
49
|
|
|
$directory, $convertToSeconds, $separateConsoleLog); |
50
|
|
|
|
51
|
|
|
// listen to database queries |
52
|
|
|
$this->app['db']->listen(function ( |
53
|
|
|
$query, |
54
|
|
|
$bindings = null, |
55
|
|
|
$time = null |
56
|
|
|
) use ($logger) { |
57
|
|
|
$logger->log($query, $bindings, $time); |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get files to be published. |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
protected function getPublished() |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
realpath(__DIR__ . |
71
|
|
|
'/../../publish/config/sql_logger.php') => (function_exists('config_path') ? |
72
|
|
|
config_path('sql_logger.php') : |
73
|
|
|
base_path('config/sql_logger.php')), |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|