From e83d4665a23e2d7c5297e5388a40c4a304a85bf1 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Sun, 26 May 2024 22:36:07 -0300 Subject: [PATCH] dispatch pre actions from Front --- system/engine/api/preactions.php | 6 ++++++ system/engine/front.php | 27 ++++++++++++++++----------- system/engine/preactions.php | 8 ++++++++ system/system.php | 27 ++------------------------- system/url/seo_url.php | 3 ++- 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/system/engine/api/preactions.php b/system/engine/api/preactions.php index 397f224..46381ba 100644 --- a/system/engine/api/preactions.php +++ b/system/engine/api/preactions.php @@ -11,4 +11,10 @@ namespace Phacil\Framework\Api; interface PreActions { /** @return \Phacil\Framework\Action[] */ public function getHandlers(); + + /** + * @param int $actionKey + * @return void + */ + public function executedActionHandler($actionKey); } \ No newline at end of file diff --git a/system/engine/front.php b/system/engine/front.php index e80c603..e03bfdf 100644 --- a/system/engine/front.php +++ b/system/engine/front.php @@ -23,21 +23,25 @@ class Front implements frontInterface { */ protected $registry; + protected $error; + + protected $pre_action = []; + /** * - * @var array + * @var \Phacil\Framework\Api\PreActions */ - protected $pre_action = array(); - - protected $error; + private $pre_actions; /** * * @param Registry $registry * @return void */ - public function __construct(Registry $registry) { + public function __construct(Registry $registry, \Phacil\Framework\Api\PreActions $preActions) { $this->registry = $registry; + $this->pre_actions = $preActions; + } @@ -61,18 +65,19 @@ class Front implements frontInterface { try { $this->registry->set('route', $action->getRoute()); - foreach ($this->pre_action as $pre_action) { + foreach ($this->pre_actions->getHandlers() as $key => $pre_action) { $result = $this->execute($pre_action); + $this->pre_actions->executedActionHandler($key); - if ($result) { - $action = $result; + if ($result && $result instanceof \Phacil\Framework\Interfaces\Action) { + return $this->dispatch($result, $error); break; } } - while ($action) { - $action = $this->execute($action); + if ($action) { + $this->execute($action); } } catch (\Exception $th) { throw $th; @@ -154,6 +159,6 @@ class Front implements frontInterface { } protected function injectionClass($class){ - return $this->registry->injectionClass($class); + return $this->registry->getInstance($class); } } diff --git a/system/engine/preactions.php b/system/engine/preactions.php index aed0401..762c8a0 100644 --- a/system/engine/preactions.php +++ b/system/engine/preactions.php @@ -40,4 +40,12 @@ class PreActions implements \Phacil\Framework\Api\PreActions { public function getHandlers() { return $this->handlers; } + + /** + * @param int $actionKey + * @return void + */ + public function executedActionHandler($actionKey){ + unset($this->handlers[$actionKey]); + } } \ No newline at end of file diff --git a/system/system.php b/system/system.php index a4dd2b8..c0d7472 100644 --- a/system/system.php +++ b/system/system.php @@ -329,29 +329,6 @@ final class startEngineExacTI { return $constant; } - /** - * Add controller system pre-actions - * @return \Phacil\Framework\Action[] - * @since 1.5.1 - */ - public function controllerPreActions() { - /** @var \Phacil\Framework\Api\PreActions */ - $preactions = $this->registry->getInstance(\Phacil\Framework\Api\PreActions::class); - return $preactions->getHandlers(); - } - - /** - * Add route to pre actions array - * - * @param string $route - * @return void - * @since 2.0.0 - */ - public function addControllerPreAction($route) { - $this->preActions[] = $route; - return; - } - /** * Check the registry element * @@ -633,11 +610,11 @@ try { //$frontController->addPreAction($engine->registry->create(\Phacil\Framework\Action::class, array((string) 'url/seo_url', [], \Phacil\Framework\Interfaces\Action::SYSTEM))); //extraPreActions - if ($engine->controllerPreActions()) { + /* if ($engine->controllerPreActions()) { foreach ($engine->controllerPreActions() as $action) { $frontController->addPreAction($action); } - } + } */ // Router $action = $engine->registry->create(\Phacil\Framework\Action::class, array(startEngineExacTI::getRoute())); diff --git a/system/url/seo_url.php b/system/url/seo_url.php index 3121a96..307ee2a 100644 --- a/system/url/seo_url.php +++ b/system/url/seo_url.php @@ -129,7 +129,8 @@ class SystemUrlSeoUrl extends ControllerController { if (Request::GET('route')) { - return $this->forward(Request::GET('route'), $match); + \Phacil\Framework\Registry::addPreferenceByRoute(\Phacil\Framework\startEngineExacTI::getRoute()); + return $this->registry->create(\Phacil\Framework\Action::class, [Request::GET('route'), $match]); } } }