Skip to content

Commit

Permalink
Add auto-detection of debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Rixafy committed Jan 6, 2025
1 parent 715f358 commit d7d5fcf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ extensions:
nettrine.dbal: Nettrine\DBAL\DI\DbalExtension
```

Debug mode can be passed in extension constructor `DbalExtension(%debugMode%)`, but it's optional and determined automatically from `Tracy\Debugger::$productionMode`.

> [!NOTE]
> This is just **DBAL**, for **ORM** please use [nettrine/orm](https://github.com/contributte/doctrine-orm).
Expand Down Expand Up @@ -85,7 +87,7 @@ Here is the list of all available options with their types.
```neon
nettrine.dbal:
debug:
panel: <boolean>
panel: <boolean> # optional, it's determined automatically or passed to constructor when extension is registered
types: array<string, class-string>
typesMapping: array<string, class-string>
Expand Down
11 changes: 9 additions & 2 deletions src/DI/DbalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Nettrine\DBAL\DI\Pass\ConsolePass;
use Nettrine\DBAL\DI\Pass\DoctrinePass;
use stdClass;
use Tracy\Debugger;

/**
* @property-read stdClass $config
Expand Down Expand Up @@ -79,8 +80,14 @@ class DbalExtension extends CompilerExtension
/** @var AbstractPass[] */
protected array $passes = [];

public function __construct()
public function __construct(
private ?bool $debugMode = null
)
{
if ($this->debugMode === null) {
$this->debugMode = class_exists(Debugger::class) && Debugger::$productionMode === false;
}

$this->passes[] = new DoctrinePass($this);
$this->passes[] = new ConsolePass($this);
$this->passes[] = new ConnectionPass($this);
Expand All @@ -107,7 +114,7 @@ public function getConfigSchema(): Schema

return Expect::structure([
'debug' => Expect::structure([
'panel' => Expect::bool(false),
'panel' => Expect::bool($this->debugMode),
'sourcePaths' => Expect::arrayOf('string'),
]),
'types' => Expect::arrayOf('string', 'string'),
Expand Down
58 changes: 54 additions & 4 deletions tests/Cases/DI/DbalExtension.debugMode.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,36 @@ use Tracy\Debugger;

require_once __DIR__ . '/../../bootstrap.php';

// Debug mode
// Disabled debug mode
Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(static function (Compiler $compiler): void {
$compiler->addExtension('nettrine.dbal', new DbalExtension());
$compiler->addExtension('nette.tracy', new TracyExtension());
$compiler->addConfig(Neonkit::load(<<<'NEON'
nettrine.dbal:
connections:
default:
driver: pdo_sqlite
password: test
user: test
path: ":memory:"
NEON
));
})->build();

$container->getByName('nettrine.dbal.connections.default.connection');
$blueScreen = Debugger::getBlueScreen();
$panels = Liberator::of($blueScreen)->panels;

Assert::count(0, $panels);
});

// Debug mode auto-detection from Tracy/Debugger with multiple panels
Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(static function (Compiler $compiler): void {
Debugger::$productionMode = false;
$compiler->addExtension('nettrine.dbal', new DbalExtension());
$compiler->addExtension('nette.tracy', new TracyExtension());
$compiler->addConfig(Neonkit::load(<<<'NEON'
Expand All @@ -36,10 +62,9 @@ Toolkit::test(function (): void {
password: test
user: test
path: ":memory:"
debug:
panel: true
NEON
));
Debugger::$productionMode = null;
})->build();

$container->getByName('nettrine.dbal.connections.default.connection');
Expand All @@ -50,7 +75,32 @@ Toolkit::test(function (): void {
Assert::count(2, $panels);
});

// Debug mode
// Debug mode passed via constructor
Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(static function (Compiler $compiler): void {
$compiler->addExtension('nettrine.dbal', new DbalExtension(true));
$compiler->addExtension('nette.tracy', new TracyExtension());
$compiler->addConfig(Neonkit::load(<<<'NEON'
nettrine.dbal:
connections:
default:
driver: pdo_sqlite
password: test
user: test
path: ":memory:"
NEON
));
})->build();

$container->getByName('nettrine.dbal.connections.default.connection');
$blueScreen = Debugger::getBlueScreen();
$panels = Liberator::of($blueScreen)->panels;

Assert::count(3, $panels); // 2 are from previous test
});

// Debug mode defined in debug.panel, testing DebugStack middleware
Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(static function (Compiler $compiler): void {
Expand Down

0 comments on commit d7d5fcf

Please sign in to comment.