Repository navigation
Labels
bugSomething isn't workingSomething isn't workinggood first issueGood for newcomersGood for newcomershelp wantedExtra attention is neededExtra attention is needed
Milestone
Description
Refactor debugbar() helper for structural consistency
Description
The debugbar() helper method currently violates two major architectural conventions in the quantum-php-core framework:
- Incorrect Location: The function is currently defined in
src/View/Helpers/view.php. The Debugger is a separated component, and this tightly couples theViewmodule toDebuggerdependencies. - Inconsistent Behavior: The helper method currently executes business logic (
$debugger->render()) instead of acting as a typical service locator. Other helper functions in the framework (e.g.,config(),session(),request()) intuitively return the underlying service instance, allowing the developer to interact with the service fluently.
Current Implementation
Location: src/View/Helpers/view.php
function debugbar(): ?string
{
if (!Di::isRegistered(Debugger::class)) {
Di::register(Debugger::class);
}
$debugger = Di::get(Debugger::class);
if ($debugger->isEnabled()) {
return $debugger->render();
}
return null;
}Proposed Solution
- Relocate the Helper: Move the
debugbar()function to a dedicated helper file inside the Debugger module:src/Debugger/Helpers/debugger.php. - Update the Return Value: Alter the helper logic so it strictly returns a
Debuggerinstance. - Protect the Render Method: Currently
Debugger::render()does not check if debugging is enabled. We must update it to return an empty string ifisEnabled()is false. Without this, callingdebugbar()->render()in a layout file would render the bar even in production!
Changes for src/Debugger/Helpers/debugger.php
// Exception catching / PhpDoc omitted for brevity
function debugbar(): Debugger
{
if (!Di::isRegistered(Debugger::class)) {
Di::register(Debugger::class);
}
return Di::get(Debugger::class);
}Changes for src/Debugger/Debugger.php
public function render(): string
{
if (!$this->isEnabled()) {
return '';
}
// ... existing render logic ...
}After these changes, rendering the debug bar should be explicitly called via method chaining inside templates: debugbar()->render(). This will be perfectly safe since render() will now respect the debug configuration.
Considerations
- Update the
composer.jsonorLoadHelpersStageclass to ensuresrc/Debugger/Helpers/debugger.phpis properly autoloaded. - Scan the codebase for any implicit HTML rendering calls relying on
debugbar()and update them to usedebugbar()->render()if necessary (for instance, in main layouts).
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workinggood first issueGood for newcomersGood for newcomershelp wantedExtra attention is neededExtra attention is needed
Activity
[quantum-php#458] Refactor debugbar() helper for structural and behav…
Merge pull request #463 from armanist/issue/458-refactor-debugbar-helper