mnabialek /
laravel-sql-logger
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Mnabialek\LaravelSqlLogger; |
||
| 4 | |||
| 5 | use Carbon\Carbon; |
||
| 6 | use Illuminate\Container\Container; |
||
| 7 | use Mnabialek\LaravelSqlLogger\Objects\Concerns\ReplacesBindings; |
||
| 8 | use Mnabialek\LaravelSqlLogger\Objects\SqlQuery; |
||
| 9 | |||
| 10 | class Formatter |
||
| 11 | { |
||
| 12 | use ReplacesBindings; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var Container |
||
| 16 | */ |
||
| 17 | private $app; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var Config |
||
| 21 | */ |
||
| 22 | private $config; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Formatter constructor. |
||
| 26 | * |
||
| 27 | * @param Container $app |
||
| 28 | * @param Config $config |
||
| 29 | */ |
||
| 30 | public function __construct(Container $app, Config $config) |
||
| 31 | { |
||
| 32 | $this->app = $app; |
||
| 33 | $this->config = $config; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Get formatted line. |
||
| 38 | * |
||
| 39 | * @param SqlQuery $query |
||
| 40 | * |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | public function getLine(SqlQuery $query) |
||
| 44 | { |
||
| 45 | $replace = [ |
||
| 46 | '[origin]' => $this->originLine(), |
||
| 47 | '[query_nr]' => $query->number(), |
||
| 48 | '[datetime]' => Carbon::now()->toDateTimeString(), |
||
| 49 | '[query_time]' => $this->time($query->time()), |
||
| 50 | '[query]' => $this->queryLine($query), |
||
| 51 | '[separator]' => $this->separatorLine(), |
||
| 52 | '\n' => PHP_EOL, |
||
| 53 | ]; |
||
| 54 | |||
| 55 | return str_replace(array_keys($replace), array_values($replace), $this->config->entryFormat()); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Format time. |
||
| 60 | * |
||
| 61 | * @param float $time |
||
| 62 | * |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | protected function time($time) |
||
| 66 | { |
||
| 67 | return $this->config->useSeconds() ? ($time / 1000.0) . 's' : $time . 'ms'; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get origin line. |
||
| 72 | * |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | protected function originLine() |
||
| 76 | { |
||
| 77 | return 'Origin ' . ($this->app->runningInConsole() |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 78 | ? '(console): ' . $this->getArtisanLine() |
||
| 79 | : '(request): ' . $this->getRequestLine()); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get query line. |
||
| 84 | * |
||
| 85 | * @param SqlQuery $query |
||
| 86 | * |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | protected function queryLine(SqlQuery $query) |
||
| 90 | { |
||
| 91 | return $this->format($query->get()) . ';'; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get Artisan line. |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | protected function getArtisanLine() |
||
| 100 | { |
||
| 101 | $command = $this->app['request']->server('argv', []); |
||
| 102 | |||
| 103 | if (is_array($command)) { |
||
| 104 | $command = implode(' ', $command); |
||
| 105 | } |
||
| 106 | |||
| 107 | return $command; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get request line. |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | protected function getRequestLine() |
||
| 116 | { |
||
| 117 | return $this->app['request']->method() . ' ' . $this->app['request']->fullUrl(); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get separator line. |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | protected function separatorLine() |
||
| 126 | { |
||
| 127 | return '/*' . str_repeat('=', 50) . '*/'; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Format given query. |
||
| 132 | * |
||
| 133 | * @param string $query |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | protected function format($query) |
||
| 138 | { |
||
| 139 | return $this->removeNewLines($query); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Remove new lines from SQL to keep it in single line if possible. |
||
| 144 | * |
||
| 145 | * @param string $sql |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | protected function removeNewLines($sql) |
||
| 150 | { |
||
| 151 | if (! $this->config->newLinesToSpaces()) { |
||
| 152 | return $sql; |
||
| 153 | } |
||
| 154 | |||
| 155 | return preg_replace($this->wrapRegex($this->notInsideQuotes('\v', false)), ' ', $sql); |
||
| 156 | } |
||
| 157 | } |
||
| 158 |