-
-
Notifications
You must be signed in to change notification settings - Fork 26
Improve dbal config #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Improve dbal config #228
Changes from all commits
bb45e11
709bf9c
0a152fc
757e09d
7ea38b9
43b5053
7f0658e
4525510
1913fb6
6375c36
5015ae7
9e93b28
2006c4d
893764b
9692402
864d231
a5d9546
1581486
fdf81df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,78 +4,29 @@ | |
|
||
namespace Yiisoft\Yii\Cycle\Factory; | ||
|
||
use Cycle\Database\Driver\Driver; | ||
use Exception; | ||
use Cycle\Database\Config\DatabaseConfig; | ||
use Cycle\Database\DatabaseManager; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Log\LoggerInterface; | ||
use RuntimeException; | ||
|
||
final class DbalFactory | ||
{ | ||
private readonly array|DatabaseConfig $dbalConfig; | ||
|
||
/** @var LoggerInterface|string|null */ | ||
private mixed $logger = null; | ||
|
||
public function __construct(array|DatabaseConfig $config) | ||
{ | ||
if (is_array($config) && array_key_exists('query-logger', $config)) { | ||
$this->logger = $config['query-logger']; | ||
unset($config['query-logger']); | ||
} | ||
$this->dbalConfig = $config; | ||
public function __construct( | ||
private readonly ?LoggerInterface $logger = null | ||
) { | ||
} | ||
|
||
public function __invoke(ContainerInterface $container): DatabaseManager | ||
public function create(array $dbalConfig): DatabaseManager | ||
{ | ||
$loggingEnabled = $dbalConfig['query-logging'] ?? false; | ||
$dbal = new DatabaseManager( | ||
$this->prepareConfig($this->dbalConfig) | ||
new DatabaseConfig($dbalConfig) | ||
); | ||
|
||
if ($this->logger !== null) { | ||
$logger = $this->prepareLogger($container, $this->logger); | ||
if ($this->logger !== null && $loggingEnabled === true) { | ||
$logger = $this->logger; | ||
$dbal->setLogger($logger); | ||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** Remove when issue is resolved {@link https://github.com/cycle/orm/issues/60} */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue is opened yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But it works, see tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Which test checks driver logger? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
$drivers = $dbal->getDrivers(); | ||
array_walk($drivers, static fn (Driver $driver) => $driver->setLogger($logger)); | ||
} | ||
|
||
return $dbal; | ||
} | ||
|
||
/** | ||
* @param LoggerInterface|string $logger | ||
* | ||
* @throws Exception | ||
* | ||
* @return LoggerInterface | ||
*/ | ||
private function prepareLogger(ContainerInterface $container, mixed $logger): LoggerInterface | ||
{ | ||
if (is_string($logger)) { | ||
$logger = $container->get($logger); | ||
} | ||
if (!$logger instanceof LoggerInterface) { | ||
throw new RuntimeException( | ||
sprintf('Logger definition should be subclass of %s.', LoggerInterface::class) | ||
); | ||
} | ||
return $logger; | ||
} | ||
|
||
/** | ||
* @param array|DatabaseConfig $config | ||
* | ||
* @return DatabaseConfig | ||
*/ | ||
private function prepareConfig(array|DatabaseConfig $config): DatabaseConfig | ||
{ | ||
if ($config instanceof DatabaseConfig) { | ||
return $config; | ||
} | ||
|
||
return new DatabaseConfig($config); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.