Skip to content

Commit 3378d08

Browse files
committed
:octocat: fixed logger
1 parent 52213fb commit 3378d08

10 files changed

+97
-98
lines changed

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "chillerlan/php-database",
3-
"description": "An extensible database wrapper and query builder for PHP 7+",
3+
"description": "An extensible database wrapper and query builder for PHP 7.2+",
44
"homepage": "https://github.com/chillerlan/php-database",
55
"license": "MIT",
66
"type": "library",
7+
"minimum-stability": "stable",
78
"keywords": [
89
"database", "query", "builder", "querybuilder", "driver", "mysql",
910
"postgres", "mariadb", "firebird", "sqlite", "mssql"
@@ -19,17 +20,16 @@
1920
"issues": "https://github.com/chillerlan/php-database/issues",
2021
"source": "https://github.com/chillerlan/php-database"
2122
},
22-
"minimum-stability": "stable",
2323
"require": {
24-
"php": ">=7.2.0",
24+
"php": "^7.2",
25+
"chillerlan/php-traits": "^1.1",
2526
"psr/simple-cache": "^1.0",
26-
"psr/log": "^1.0",
27-
"chillerlan/php-log": "^1.0",
28-
"chillerlan/php-traits": "^1.1"
27+
"psr/log": "^1.0"
2928
},
3029
"require-dev": {
3130
"chillerlan/php-cache": "^1.0",
32-
"phpunit/phpunit": "^6.5"
31+
"chillerlan/php-log": "^1.0",
32+
"phpunit/phpunit": "^7.1"
3333
},
3434
"autoload": {
3535
"psr-4": {

src/DatabaseAbstract.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
use chillerlan\Database\{
1616
Drivers\DriverInterface, Query\QueryBuilder
1717
};
18-
use chillerlan\{
19-
Logger\LogTrait, Traits\ClassLoader, Traits\ContainerInterface
18+
use chillerlan\Traits\{
19+
ClassLoader, ContainerInterface
2020
};
21-
use Psr\{
22-
Log\LoggerAwareInterface, Log\LoggerInterface, SimpleCache\CacheInterface
21+
use Psr\Log\{
22+
LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger
2323
};
24+
use Psr\SimpleCache\CacheInterface;
2425

2526
abstract class DatabaseAbstract implements LoggerAwareInterface{
26-
use ClassLoader, LogTrait;
27+
use ClassLoader, LoggerAwareTrait;
2728

2829
/**
2930
* @var \chillerlan\Database\DatabaseOptions
@@ -55,27 +56,23 @@ abstract class DatabaseAbstract implements LoggerAwareInterface{
5556
public function __construct(ContainerInterface $options, CacheInterface $cache = null, LoggerInterface $logger = null){
5657
$this->options = $options;
5758
$this->cache = $cache;
58-
$this->driver = $this->loadClass($this->options->driver, DriverInterface::class, $this->options, $this->cache, $this->log);
59-
$this->query = new QueryBuilder($this->driver, $this->log);
6059

61-
if($logger instanceof LoggerInterface){
62-
$this->setLogger($logger);
63-
}
60+
// set a default logger
61+
$this->logger = $logger ?? new NullLogger;
6462

63+
$this->driver = $this->loadClass($this->options->driver, DriverInterface::class, $this->options, $this->cache, $this->logger);
64+
$this->query = new QueryBuilder($this->driver, $this->logger);
6565
}
6666

6767
/**
6868
* @param \Psr\Log\LoggerInterface $logger
6969
*
70-
* @return \chillerlan\Database\Database
70+
* @return void
7171
*/
72-
public function setLogger(LoggerInterface $logger):DatabaseAbstract{
73-
$this->log = $logger;
72+
public function setLogger(LoggerInterface $logger):void{
73+
$this->logger = $logger;
7474
$this->driver->setLogger($logger);
7575
$this->query->setLogger($logger);
76-
77-
return $this;
7876
}
7977

80-
8178
}

src/Drivers/DriverAbstract.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
use chillerlan\Database\{
1616
Dialects\Dialect, Result
1717
};
18-
use chillerlan\Logger\LogTrait;
1918
use chillerlan\Traits\ContainerInterface;
20-
use Psr\{
21-
Log\LoggerAwareInterface, Log\LoggerInterface, SimpleCache\CacheInterface
19+
use Psr\Log\{
20+
LoggerAwareInterface, LoggerAwareTrait, LoggerInterface
2221
};
22+
use Psr\SimpleCache\CacheInterface;
2323

2424
/**
25-
* @method setLogger(\Psr\Log\LoggerInterface $logger):DriverInterface
25+
*
2626
*/
2727
abstract class DriverAbstract implements DriverInterface, LoggerAwareInterface{
28-
use LogTrait;
28+
use LoggerAwareTrait;
2929

3030
/**
3131
* Holds the database resource object
@@ -67,7 +67,7 @@ abstract class DriverAbstract implements DriverInterface, LoggerAwareInterface{
6767
public function __construct(ContainerInterface $options, CacheInterface $cache = null, LoggerInterface $logger = null){
6868
$this->options = $options;
6969
$this->cache = $cache;
70-
$this->log = $logger;
70+
$this->logger = $logger;
7171

7272
// avoid unnecessary getter calls in long loops
7373
$this->cachekey_hash_algo = $this->options->cachekey_hash_algo;
@@ -155,7 +155,7 @@ public function escape($data = null){
155155
/** @inheritdoc */
156156
public function raw(string $sql, string $index = null, bool $assoc = null){
157157
$this->checkSQL($sql);
158-
$this->debug('DriverAbstract::raw()', ['method' => __METHOD__, 'sql' => $sql, 'index' => $index, 'assoc' => $assoc]);
158+
$this->logger->debug('DriverAbstract::raw()', ['method' => __METHOD__, 'sql' => $sql, 'index' => $index, 'assoc' => $assoc]);
159159

160160
try{
161161
return $this->raw_query($sql, $index, $assoc !== null ? $assoc : true);
@@ -169,7 +169,7 @@ public function raw(string $sql, string $index = null, bool $assoc = null){
169169
/** @inheritdoc */
170170
public function prepared(string $sql, array $values = null, string $index = null, bool $assoc = null){
171171
$this->checkSQL($sql);
172-
$this->debug('DriverAbstract::prepared()', ['method' => __METHOD__, 'sql' => $sql, 'val' => $values, 'index' => $index, 'assoc' => $assoc]);
172+
$this->logger->debug('DriverAbstract::prepared()', ['method' => __METHOD__, 'sql' => $sql, 'val' => $values, 'index' => $index, 'assoc' => $assoc]);
173173

174174
try{
175175
return $this->prepared_query(
@@ -270,6 +270,7 @@ protected function getResult(callable $callable, array $args, string $index = nu
270270
$out = new Result(null, $this->convert_encoding_src, $this->convert_encoding_dest);
271271
$i = 0;
272272

273+
/** @noinspection PhpAssignmentInConditionInspection */
273274
while($row = call_user_func_array($callable, $args)){
274275
$key = $assoc && !empty($index) ? $row[$index] : $i;
275276

src/Drivers/DriverInterface.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212

1313
namespace chillerlan\Database\Drivers;
1414

15-
use chillerlan\Database\{
16-
DatabaseOptions, Dialects\Dialect
17-
};
15+
use chillerlan\Database\Dialects\Dialect;
1816
use chillerlan\Traits\ContainerInterface;
19-
use Psr\{
20-
Log\LoggerInterface, SimpleCache\CacheInterface
21-
};
17+
use Psr\Log\LoggerInterface;
18+
use Psr\SimpleCache\CacheInterface;
2219

2320
interface DriverInterface{
2421

src/Dumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function dump(array $selection = null, array $not = null){
4141
$fh = fopen($this->options->storage_path.'/dump.sql', 'w');
4242

4343
foreach($tables as $table){
44-
$this->info('dumping table: '.$table);
44+
$this->logger->info('dumping table: '.$table);
4545
fwrite($fh, PHP_EOL.'--'.PHP_EOL.'-- '.$table.PHP_EOL.'--'.PHP_EOL.PHP_EOL.$this->query->show->create->table($table)->query()[0]->{'Create Table'}.';'.PHP_EOL.PHP_EOL);
4646

4747
$q = $this->query->select->from([$table]);
@@ -74,7 +74,7 @@ public function dump(array $selection = null, array $not = null){
7474

7575

7676
if($i > 0){
77-
$this->info('dumping data: '.round((100 / $pages) * $i, 2).'%');
77+
$this->logger->info('dumping data: '.round((100 / $pages) * $i, 2).'%');
7878
}
7979

8080
}

src/Query/MultiQueryTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function multi(array $values = null){
2323
$sql = $this->sql(true);
2424
$values = $values ?? ($this instanceof BindValues ? $this->getBindValues() : []);
2525

26-
$this->debug('MultiQueryTrait::multi()', ['method' => __METHOD__, 'sql' => $sql, 'val' => $values]);
26+
$this->logger->debug('MultiQueryTrait::multi()', ['method' => __METHOD__, 'sql' => $sql, 'val' => $values]);
2727

2828
return $this->db->multi($sql, $values);
2929
}
@@ -32,7 +32,7 @@ public function multi(array $values = null){
3232
public function callback(iterable $values, callable $callback){
3333
$sql = $this->sql(true);
3434

35-
$this->debug('MultiQueryTrait::callback()', ['method' => __METHOD__, 'sql' => $sql, 'val' => $values]);
35+
$this->logger->debug('MultiQueryTrait::callback()', ['method' => __METHOD__, 'sql' => $sql, 'val' => $values]);
3636

3737
return $this->db->multiCallback($sql, $values, $callback);
3838
}

src/Query/QueryBuilder.php

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
use chillerlan\Database\{
1616
Drivers\DriverInterface, ResultInterface
1717
};
18-
use chillerlan\Logger\LogTrait;
1918
use Psr\Log\{
20-
LoggerAwareInterface, LoggerInterface
19+
LoggerAwareInterface, LoggerAwareTrait, LoggerInterface
2120
};
2221

2322
/**
@@ -32,11 +31,9 @@
3231
* @property \chillerlan\Database\Query\Show $show
3332
* @property \chillerlan\Database\Query\Truncate $truncate
3433
* @property \chillerlan\Database\Query\Update $update
35-
*
36-
* @method setLogger(\Psr\Log\LoggerInterface $logger):QueryBuilder
3734
*/
3835
class QueryBuilder implements LoggerAwareInterface{
39-
use LogTrait;
36+
use LoggerAwareTrait;
4037

4138
protected const STATEMENTS = ['alter', 'create', 'delete', 'drop', 'insert', 'select', 'show', 'truncate', 'update'];
4239

@@ -57,7 +54,7 @@ class QueryBuilder implements LoggerAwareInterface{
5754
*/
5855
public function __construct(DriverInterface $db, LoggerInterface $logger = null){
5956
$this->db = $db;
60-
$this->log = $logger;
57+
$this->logger = $logger;
6158
$this->dialect = $this->db->getDialect();
6259
}
6360

@@ -81,19 +78,19 @@ public function __get(string $name){
8178
* @return \chillerlan\Database\Query\Alter
8279
*/
8380
public function alter():Alter{
84-
return new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements Alter{
81+
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Alter{
8582

8683
/** @inheritdoc */
8784
public function table(string $tablename):AlterTable{
88-
return (new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements AlterTable{
85+
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements AlterTable{
8986
use NameTrait;
9087

9188
})->name($tablename);
9289
}
9390

9491
/** @inheritdoc */
9592
public function database(string $dbname):AlterDatabase{
96-
return (new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements AlterDatabase{
93+
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements AlterDatabase{
9794
use NameTrait;
9895

9996
})->name($dbname);
@@ -106,11 +103,11 @@ public function database(string $dbname):AlterDatabase{
106103
* @return \chillerlan\Database\Query\Create
107104
*/
108105
public function create():Create{
109-
return new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements Create{
106+
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Create{
110107

111108
/** @inheritdoc */
112109
public function database(string $dbname):CreateDatabase{
113-
return (new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements CreateDatabase, Query{
110+
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements CreateDatabase, Query{
114111
use CharsetTrait, IfNotExistsTrait, NameTrait, QueryTrait;
115112

116113
/** @inheritdoc */
@@ -123,7 +120,7 @@ protected function getSQL():array{
123120

124121
/** @inheritdoc */
125122
public function table(string $tablename):CreateTable{
126-
return (new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements CreateTable, Query{
123+
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements CreateTable, Query{
127124
use CharsetTrait, IfNotExistsTrait, NameTrait, QueryTrait;
128125

129126
/** @var bool */
@@ -212,7 +209,7 @@ public function text(string $name, $defaultValue = null, bool $isNull = null):Cr
212209
* @return \chillerlan\Database\Query\Delete
213210
*/
214211
public function delete():Delete{
215-
return new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements Delete, Where, BindValues, Query{
212+
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Delete, Where, BindValues, Query{
216213
use WhereTrait, QueryTrait, NameTrait {
217214
name as from;
218215
}
@@ -229,11 +226,11 @@ protected function getSQL():array{
229226
* @return \chillerlan\Database\Query\Drop
230227
*/
231228
public function drop():Drop{
232-
return new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements Drop{
229+
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Drop{
233230

234231
/** @inheritdoc */
235232
public function database(string $dbname):DropItem{
236-
return (new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements DropItem, Query{
233+
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements DropItem, Query{
237234
use IfExistsTrait, NameTrait, QueryTrait;
238235

239236
/** @inheritdoc */
@@ -246,7 +243,7 @@ protected function getSQL():array{
246243

247244
/** @inheritdoc */
248245
public function table(string $tablename):DropItem{
249-
return (new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements DropItem, Query{
246+
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements DropItem, Query{
250247
use IfExistsTrait, NameTrait, QueryTrait;
251248

252249
/** @inheritdoc */
@@ -264,7 +261,7 @@ protected function getSQL():array{
264261
* @return \chillerlan\Database\Query\Insert
265262
*/
266263
public function insert():Insert{
267-
return new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements Insert, BindValues, MultiQuery{
264+
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Insert, BindValues, MultiQuery{
268265
use MultiQueryTrait, OnConflictTrait{
269266
name as into;
270267
}
@@ -302,7 +299,7 @@ public function values(iterable $values):Insert{
302299
* @return \chillerlan\Database\Query\Select
303300
*/
304301
public function select():Select{
305-
return new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements Select, Where, BindValues, Query{
302+
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Select, Where, BindValues, Query{
306303
use QueryTrait, WhereTrait;
307304

308305
/** @var bool */
@@ -392,7 +389,7 @@ public function count():int{
392389
* @return \chillerlan\Database\Query\Show
393390
*/
394391
public function show():Show{
395-
return new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements Show{
392+
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Show{
396393

397394
/**
398395
* @param string $name
@@ -412,7 +409,7 @@ public function __get(string $name){
412409

413410
/** @inheritdoc */
414411
public function databases():ShowItem{
415-
return new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements ShowItem, Query{
412+
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements ShowItem, Query{
416413
use QueryTrait;
417414

418415
/** @inheritdoc */
@@ -426,7 +423,7 @@ protected function getSQL():array{
426423
/** @inheritdoc */
427424
public function tables(string $from = null):ShowItem{
428425

429-
$showTables = new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements ShowItem, Where, Query{
426+
$showTables = new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements ShowItem, Where, Query{
430427
use QueryTrait, WhereTrait, NameTrait{
431428
name as from;
432429
}
@@ -460,11 +457,11 @@ public function pattern(string $pattern):ShowItem{
460457

461458
/** @inheritdoc */
462459
public function create():ShowCreate{
463-
return new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements ShowCreate{
460+
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements ShowCreate{
464461

465462
/** @inheritdoc */
466463
public function table(string $tablename):ShowItem{
467-
return (new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements ShowItem, Query{
464+
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements ShowItem, Query{
468465
use QueryTrait, NameTrait;
469466

470467
/** @inheritdoc */
@@ -485,11 +482,11 @@ protected function getSQL():array{
485482
* @return \chillerlan\Database\Query\Truncate
486483
*/
487484
public function truncate():Truncate{
488-
return new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements Truncate{
485+
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Truncate{
489486

490487
/** @inheritdoc */
491488
public function table(string $table):Truncate{
492-
return (new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements Truncate, Query{
489+
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Truncate, Query{
493490
use QueryTrait, NameTrait {
494491
name as table;
495492
}
@@ -510,7 +507,7 @@ protected function getSQL():array{
510507
*/
511508
public function update():Update{
512509

513-
return new class($this->db, $this->dialect, $this->log) extends StatementAbstract implements Update, Where, BindValues, MultiQuery{
510+
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Update, Where, BindValues, MultiQuery{
514511
use WhereTrait, MultiQueryTrait, NameTrait {
515512
name as table;
516513
}

0 commit comments

Comments
 (0)