From ef6c7040a98515c75c445b78bc6802d1bf154875 Mon Sep 17 00:00:00 2001 From: Arman <407448+armanist@users.noreply.github.com> Date: Tue, 21 Apr 2026 13:04:46 +0400 Subject: [PATCH 1/2] [#459] Refactor view() helper to service locator and rename QtView to View Rename QtView class to View for naming consistency with the rest of the framework. Convert view() helper from returning a rendered string to returning the View instance, letting templates call view()->getContent() explicitly. Rename getView() to getContent() to avoid tautological naming. Also adopt debugbar() helper in ViewFactory, InitDebuggerStage, MessageAdapter and WebAppTrait. Made-with: Cursor --- src/App/Stages/InitDebuggerStage.php | 13 +------------ src/App/Traits/WebAppTrait.php | 6 ++---- src/Logger/Adapters/MessageAdapter.php | 18 +----------------- .../DefaultWeb/src/Views/layouts/main.php.tpl | 2 +- .../src/Controllers/BaseController.php.tpl | 6 +++--- .../DemoWeb/src/Views/layouts/main.php.tpl | 2 +- .../src/Controllers/BaseController.php.tpl | 6 +++--- .../Toolkit/src/Views/layouts/iframe.php.tpl | 2 +- .../Toolkit/src/Views/layouts/main.php.tpl | 2 +- src/View/Factories/ViewFactory.php | 15 +++++++-------- src/View/Helpers/view.php | 14 +++++++------- src/View/{QtView.php => View.php} | 8 ++++---- tests/Unit/View/Factories/ViewFactoryTest.php | 4 ++-- tests/Unit/View/Helpers/ViewHelperTest.php | 13 +++++++++---- .../Unit/View/{QtViewTest.php => ViewTest.php} | 14 +++++++------- tests/_root/modules/Test/Views/layout.php | 2 +- tests/_root/modules/Test/Views/layout.twig.php | 2 +- 17 files changed, 52 insertions(+), 77 deletions(-) rename src/View/{QtView.php => View.php} (98%) rename tests/Unit/View/{QtViewTest.php => ViewTest.php} (95%) diff --git a/src/App/Stages/InitDebuggerStage.php b/src/App/Stages/InitDebuggerStage.php index b6ef6aecb..f194b856c 100644 --- a/src/App/Stages/InitDebuggerStage.php +++ b/src/App/Stages/InitDebuggerStage.php @@ -17,11 +17,7 @@ namespace Quantum\App\Stages; use Quantum\App\Contracts\BootStageInterface; -use Quantum\Di\Exceptions\DiException; -use Quantum\Debugger\Debugger; use Quantum\App\AppContext; -use ReflectionException; -use Quantum\Di\Di; /** * Class InitDebuggerStage @@ -29,15 +25,8 @@ */ class InitDebuggerStage implements BootStageInterface { - /** - * @throws DiException|ReflectionException - */ public function process(AppContext $context): void { - if (!Di::isRegistered(Debugger::class)) { - Di::register(Debugger::class); - } - - Di::get(Debugger::class)->initStore(); + debugbar()->initStore(); } } diff --git a/src/App/Traits/WebAppTrait.php b/src/App/Traits/WebAppTrait.php index 605759035..253b4adcd 100644 --- a/src/App/Traits/WebAppTrait.php +++ b/src/App/Traits/WebAppTrait.php @@ -100,10 +100,8 @@ private function loadLanguage(): void */ private function logDebugInfo(): void { - $debugger = Di::get(Debugger::class); - - if ($debugger->isEnabled()) { - $debugger->addToStoreCell(Debugger::HOOKS, 'info', hook()->getRegistered()); + if (debugbar()->isEnabled()) { + debugbar()->addToStoreCell(Debugger::HOOKS, 'info', hook()->getRegistered()); } } diff --git a/src/Logger/Adapters/MessageAdapter.php b/src/Logger/Adapters/MessageAdapter.php index f880e4669..27a3f9cd0 100644 --- a/src/Logger/Adapters/MessageAdapter.php +++ b/src/Logger/Adapters/MessageAdapter.php @@ -17,11 +17,7 @@ namespace Quantum\Logger\Adapters; use Quantum\Logger\Contracts\ReportableInterface; -use Quantum\Di\Exceptions\DiException; -use DebugBar\DebugBarException; use Quantum\Debugger\Debugger; -use ReflectionException; -use Quantum\Di\Di; /** * Class MessageAdapter @@ -29,23 +25,11 @@ */ class MessageAdapter implements ReportableInterface { - /** - * @param array|null $context - * @throws DebugBarException - */ - - /** - * @throws DiException|ReflectionException - */ public function report(string $level, string $message, ?array $context = []): void { $tab = $context['tab'] ?? Debugger::MESSAGES; - if (!Di::isRegistered(Debugger::class)) { - Di::register(Debugger::class); - } - - $debugger = Di::get(Debugger::class); + $debugger = debugbar(); if ($debugger->isEnabled()) { $debugger->addToStoreCell($tab, $level, $message); diff --git a/src/Module/Templates/DefaultWeb/src/Views/layouts/main.php.tpl b/src/Module/Templates/DefaultWeb/src/Views/layouts/main.php.tpl index f0353a944..ee53a931a 100644 --- a/src/Module/Templates/DefaultWeb/src/Views/layouts/main.php.tpl +++ b/src/Module/Templates/DefaultWeb/src/Views/layouts/main.php.tpl @@ -12,7 +12,7 @@ -
+
getContent() ?>
render() ?> diff --git a/src/Module/Templates/DemoWeb/src/Controllers/BaseController.php.tpl b/src/Module/Templates/DemoWeb/src/Controllers/BaseController.php.tpl index 941a3e660..dfcd198b1 100644 --- a/src/Module/Templates/DemoWeb/src/Controllers/BaseController.php.tpl +++ b/src/Module/Templates/DemoWeb/src/Controllers/BaseController.php.tpl @@ -16,7 +16,7 @@ namespace {{MODULE_NAMESPACE}}\Controllers; use Quantum\View\Factories\ViewFactory; use Quantum\Asset\Asset; -use Quantum\View\QtView; +use Quantum\View\View; /** * Class BaseController @@ -26,9 +26,9 @@ abstract class BaseController { /** - * @var QtView + * @var View */ - protected QtView $view; + protected View $view; public function __before() { diff --git a/src/Module/Templates/DemoWeb/src/Views/layouts/main.php.tpl b/src/Module/Templates/DemoWeb/src/Views/layouts/main.php.tpl index 403784376..11e5a5104 100644 --- a/src/Module/Templates/DemoWeb/src/Views/layouts/main.php.tpl +++ b/src/Module/Templates/DemoWeb/src/Views/layouts/main.php.tpl @@ -12,7 +12,7 @@
-
+
getContent() ?>
diff --git a/src/Module/Templates/Toolkit/src/Controllers/BaseController.php.tpl b/src/Module/Templates/Toolkit/src/Controllers/BaseController.php.tpl index eca87bff8..953eb26cb 100644 --- a/src/Module/Templates/Toolkit/src/Controllers/BaseController.php.tpl +++ b/src/Module/Templates/Toolkit/src/Controllers/BaseController.php.tpl @@ -16,7 +16,7 @@ namespace Modules\Toolkit\Controllers; use Quantum\View\Factories\ViewFactory; use Quantum\Asset\Asset; -use Quantum\View\QtView; +use Quantum\View\View; /** * Class BaseController @@ -40,9 +40,9 @@ class BaseController protected const CURRENT_PAGE = 1; /** - * @var QtView + * @var View */ - protected QtView $view; + protected View $view; /** * Works before an action diff --git a/src/Module/Templates/Toolkit/src/Views/layouts/iframe.php.tpl b/src/Module/Templates/Toolkit/src/Views/layouts/iframe.php.tpl index 37cbcb876..83e3fceb4 100644 --- a/src/Module/Templates/Toolkit/src/Views/layouts/iframe.php.tpl +++ b/src/Module/Templates/Toolkit/src/Views/layouts/iframe.php.tpl @@ -17,7 +17,7 @@
- + getContent() ?>
diff --git a/src/Module/Templates/Toolkit/src/Views/layouts/main.php.tpl b/src/Module/Templates/Toolkit/src/Views/layouts/main.php.tpl index 7ac2d9c45..01d965563 100644 --- a/src/Module/Templates/Toolkit/src/Views/layouts/main.php.tpl +++ b/src/Module/Templates/Toolkit/src/Views/layouts/main.php.tpl @@ -29,7 +29,7 @@
- + getContent() ?>
diff --git a/src/View/Factories/ViewFactory.php b/src/View/Factories/ViewFactory.php index f1d3d652a..38acd90c5 100644 --- a/src/View/Factories/ViewFactory.php +++ b/src/View/Factories/ViewFactory.php @@ -21,24 +21,23 @@ use Quantum\App\Exceptions\BaseException; use Quantum\Di\Exceptions\DiException; use Quantum\ResourceCache\ViewCache; -use Quantum\Debugger\Debugger; -use Quantum\View\QtView; use ReflectionException; +use Quantum\View\View; use Quantum\Di\Di; /** * Class ViewFactory * @package Quantum\View - * @mixin QtView + * @mixin View */ class ViewFactory { - private ?QtView $instance = null; + private ?View $instance = null; /** * @throws ConfigException|BaseException|DiException|ReflectionException */ - public static function get(): QtView + public static function get(): View { if (!Di::isRegistered(self::class)) { Di::register(self::class); @@ -50,17 +49,17 @@ public static function get(): QtView /** * @throws ConfigException|BaseException|DiException|ReflectionException */ - public function resolve(): QtView + public function resolve(): View { if ($this->instance === null) { if (!Di::isRegistered(ViewCache::class)) { Di::register(ViewCache::class); } - $this->instance = new QtView( + $this->instance = new View( RendererFactory::get(), asset(), - Di::get(Debugger::class), + debugbar(), Di::get(ViewCache::class) ); } diff --git a/src/View/Helpers/view.php b/src/View/Helpers/view.php index 930bc9387..2d4b5a688 100644 --- a/src/View/Helpers/view.php +++ b/src/View/Helpers/view.php @@ -16,20 +16,20 @@ use League\CommonMark\Exception\CommonMarkException; use Quantum\Config\Exceptions\ConfigException; -use Quantum\View\Exceptions\ViewException; use League\CommonMark\CommonMarkConverter; use Quantum\App\Exceptions\BaseException; use Quantum\View\Factories\ViewFactory; use Quantum\Di\Exceptions\DiException; use Quantum\View\RawParam; +use Quantum\View\View; /** - * Rendered view - * @throws ViewException|ConfigException|DiException|BaseException|ReflectionException + * Gets the View instance + * @throws ConfigException|DiException|BaseException|ReflectionException */ -function view(): ?string +function view(): View { - return ViewFactory::get()->getView(); + return ViewFactory::get(); } /** @@ -39,7 +39,7 @@ function view(): ?string */ function partial(string $partial, array $args = []): string { - return ViewFactory::get()->renderPartial($partial, $args); + return view()->renderPartial($partial, $args); } /** @@ -52,7 +52,7 @@ function partial(string $partial, array $args = []): string */ function view_param(string $key) { - return ViewFactory::get()->getParam($key); + return view()->getParam($key); } /** diff --git a/src/View/QtView.php b/src/View/View.php similarity index 98% rename from src/View/QtView.php rename to src/View/View.php index 0af6a736a..a0bc733f8 100644 --- a/src/View/QtView.php +++ b/src/View/View.php @@ -31,10 +31,10 @@ use Psr\Log\LogLevel; /** - * Class QtView + * Class View * @package Quantum\View */ -class QtView +class View { private Renderer $renderer; @@ -221,10 +221,10 @@ public function renderPartial(string $viewFile, array $params = []): string } /** - * Gets the rendered view. + * Gets the rendered view content. * @throws ViewException */ - public function getView(): ?string + public function getContent(): ?string { if ($this->viewContent === null) { throw ViewException::viewNotRendered(); diff --git a/tests/Unit/View/Factories/ViewFactoryTest.php b/tests/Unit/View/Factories/ViewFactoryTest.php index ab4c7bf0d..6e0dc91f2 100644 --- a/tests/Unit/View/Factories/ViewFactoryTest.php +++ b/tests/Unit/View/Factories/ViewFactoryTest.php @@ -4,7 +4,7 @@ use Quantum\View\Factories\ViewFactory; use Quantum\Tests\Unit\AppTestCase; -use Quantum\View\QtView; +use Quantum\View\View; use Quantum\Di\Di; class ViewFactoryTest extends AppTestCase @@ -26,7 +26,7 @@ public function setUp(): void public function testGetInstance(): void { - $this->assertInstanceOf(QtView::class, ViewFactory::get()); + $this->assertInstanceOf(View::class, ViewFactory::get()); } public function testResolveReturnsSameInstance(): void diff --git a/tests/Unit/View/Helpers/ViewHelperTest.php b/tests/Unit/View/Helpers/ViewHelperTest.php index a03277d3d..b77ad0ab7 100644 --- a/tests/Unit/View/Helpers/ViewHelperTest.php +++ b/tests/Unit/View/Helpers/ViewHelperTest.php @@ -6,11 +6,11 @@ use Quantum\Router\MatchedRoute; use Quantum\Router\Route; use Quantum\Tests\Unit\AppTestCase; -use Quantum\View\QtView; +use Quantum\View\View; class ViewHelperTest extends AppTestCase { - private QtView $view; + private View $view; public function setUp(): void { @@ -36,13 +36,18 @@ public function tearDown(): void $this->view->setLayout(null); } - public function testView(): void + public function testViewReturnsInstance(): void + { + $this->assertInstanceOf(View::class, view()); + } + + public function testViewGetContent(): void { $this->view->setLayout('layout'); $this->view->render('index'); - $this->assertEquals('

Hello World, this is rendered html view

', view()); + $this->assertEquals('

Hello World, this is rendered html view

', view()->getContent()); } public function testPartial(): void diff --git a/tests/Unit/View/QtViewTest.php b/tests/Unit/View/ViewTest.php similarity index 95% rename from tests/Unit/View/QtViewTest.php rename to tests/Unit/View/ViewTest.php index a2047dcde..a5d777d3a 100644 --- a/tests/Unit/View/QtViewTest.php +++ b/tests/Unit/View/ViewTest.php @@ -6,14 +6,14 @@ use Quantum\View\Factories\ViewFactory; use Quantum\Tests\Unit\AppTestCase; use Quantum\Router\MatchedRoute; -use Quantum\View\QtView; use Quantum\View\RawParam; use Quantum\Router\Route; +use Quantum\View\View; use Quantum\Di\Di; -class QtViewTest extends AppTestCase +class ViewTest extends AppTestCase { - private QtView $view; + private View $view; public function setUp(): void { @@ -126,13 +126,13 @@ public function testRenderWithData(): void $this->view->render('index', ['name' => 'Lorem Ipsum']); - $this->assertEquals('

Hello Lorem Ipsum, this is rendered html view

', $this->view->getView()); + $this->assertEquals('

Hello Lorem Ipsum, this is rendered html view

', $this->view->getContent()); $this->view->setParam('name', 'dolor sit amet'); $this->view->render('index'); - $this->assertEquals('

Hello dolor sit amet, this is rendered html view

', $this->view->getView()); + $this->assertEquals('

Hello dolor sit amet, this is rendered html view

', $this->view->getContent()); } public function testRenderWithEscapedHtmlData(): void @@ -143,7 +143,7 @@ public function testRenderWithEscapedHtmlData(): void $this->view->render('post'); - $this->assertEquals('<h1>Hello</h1>', $this->view->getView()); + $this->assertEquals('<h1>Hello</h1>', $this->view->getContent()); } public function testRenderWithUnEscapedHtmlData(): void @@ -154,7 +154,7 @@ public function testRenderWithUnEscapedHtmlData(): void $this->view->render('post'); - $this->assertEquals('

Hello

', $this->view->getView()); + $this->assertEquals('

Hello

', $this->view->getContent()); } public function testRenderPartial(): void diff --git a/tests/_root/modules/Test/Views/layout.php b/tests/_root/modules/Test/Views/layout.php index e119b46df..51aeabc63 100644 --- a/tests/_root/modules/Test/Views/layout.php +++ b/tests/_root/modules/Test/Views/layout.php @@ -1,6 +1,6 @@ - +getContent() ?> diff --git a/tests/_root/modules/Test/Views/layout.twig.php b/tests/_root/modules/Test/Views/layout.twig.php index 521b7adda..8e2fc3fc8 100644 --- a/tests/_root/modules/Test/Views/layout.twig.php +++ b/tests/_root/modules/Test/Views/layout.twig.php @@ -1,6 +1,6 @@ -{{ view() }} +{{ view().getContent() }} From c4b5f03d4224ccbee05c947cd93112f66df6d377 Mon Sep 17 00:00:00 2001 From: Arman <407448+armanist@users.noreply.github.com> Date: Tue, 21 Apr 2026 13:30:49 +0400 Subject: [PATCH 2/2] [#459] Avoid duplicate debugbar() lookup in WebAppTrait::logDebugInfo() Made-with: Cursor --- src/App/Traits/WebAppTrait.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/App/Traits/WebAppTrait.php b/src/App/Traits/WebAppTrait.php index 253b4adcd..c819b6d8a 100644 --- a/src/App/Traits/WebAppTrait.php +++ b/src/App/Traits/WebAppTrait.php @@ -100,8 +100,10 @@ private function loadLanguage(): void */ private function logDebugInfo(): void { - if (debugbar()->isEnabled()) { - debugbar()->addToStoreCell(Debugger::HOOKS, 'info', hook()->getRegistered()); + $debugbar = debugbar(); + + if ($debugbar->isEnabled()) { + $debugbar->addToStoreCell(Debugger::HOOKS, 'info', hook()->getRegistered()); } }