Skip to content

Commit 8bee72a

Browse files
committed
Adding Events To Call Action
Updating Changelog Adding Unit Test
1 parent ca9be48 commit 8bee72a

File tree

3 files changed

+187
-4
lines changed

3 files changed

+187
-4
lines changed

CHANGELOG-5.0.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- Changed `Phalcon\Filter\Validation\Validator\Email` to allow UTF8 in local part. [#16637](https://github.com/phalcon/cphalcon/issues/16637)
88

99
### Added
10-
10+
- Added `dispatch:beforeCallAction` and `dispatch:afterCallAction` to last-minute modifications to handler and method (mostly for debugging).
1111

1212
### Fixed
1313

@@ -17,6 +17,7 @@
1717
- Fixed `Phalcon\Filter\Validation\Validator\File\MimeType::validate` to close the handle when using `finfo` [#16647](https://github.com/phalcon/cphalcon/issues/16647)
1818
- Fixed `Phalcon\Mvc\Model\Manager::getRelationRecords` to explicitly set the `referencedModel` in the conditions along with the `referencedFields` [#16655](https://github.com/phalcon/cphalcon/pull/16655)
1919
- Fixed `Phalcon\Image\Adapters\AbstractAdapter::watermark` to correctly calculate the Y offset [#16658](https://github.com/phalcon/cphalcon/issues/16658)
20+
- Fixed `Phalcon\Dispatcher\AbstractDispatcher` when calling action methods that do not define parameters to prevent `Unknown named parameter` error.
2021

2122
### Removed
2223

phalcon/Dispatcher/AbstractDispatcher.zep

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use Phalcon\Events\ManagerInterface;
1919
use Phalcon\Filter\FilterInterface;
2020
use Phalcon\Mvc\Model\Binder;
2121
use Phalcon\Mvc\Model\BinderInterface;
22+
use Phalcon\Support\Collection;
2223

2324
/**
2425
* This is the base class for Phalcon\Mvc\Dispatcher and Phalcon\Cli\Dispatcher.
@@ -154,10 +155,52 @@ abstract class AbstractDispatcher extends AbstractInjectionAware implements Disp
154155

155156
public function callActionMethod(handler, string actionMethod, array! params = [])
156157
{
157-
return call_user_func_array(
158-
[handler, actionMethod],
159-
params
158+
var result, observer, altHandler, altAction, altParams;
159+
160+
let altHandler = handler;
161+
let altAction = actionMethod;
162+
let altParams = params;
163+
164+
if this->eventsManager !== null && this->eventsManager instanceof ManagerInterface {
165+
let observer = <Collection> this->getDi()->get(
166+
"Phalcon\Support\Collection",
167+
[[
168+
"handler": handler,
169+
"action": actionMethod,
170+
"params": params
171+
]]
172+
);
173+
174+
this->eventsManager->fire(
175+
"dispatch:beforeCallAction",
176+
this,
177+
observer
178+
);
179+
180+
let altHandler = observer->get("handler");
181+
let altAction = observer->get("action");
182+
let altParams = observer->get("params", [], "array");
183+
}
184+
185+
let result = call_user_func_array(
186+
[
187+
altHandler,
188+
altAction
189+
],
190+
array_values(altParams)
160191
);
192+
193+
if this->eventsManager !== null && this->eventsManager instanceof ManagerInterface {
194+
let observer["result"] = result;
195+
196+
this->eventsManager->fire(
197+
"dispatch:afterCallAction",
198+
this,
199+
observer
200+
);
201+
}
202+
203+
return result;
161204
}
162205

163206
/**
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon Framework.
5+
*
6+
* (c) Phalcon Team <team@phalcon.io>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Tests\Unit\Dispatcher;
15+
16+
use Phalcon\Di\Di;
17+
use Phalcon\Events\Event;
18+
use Phalcon\Events\Manager;
19+
use Phalcon\Mvc\Dispatcher;
20+
use Phalcon\Support\Collection;
21+
use UnitTester;
22+
23+
class CallActionMethodCest
24+
{
25+
private bool $wasCalled = false;
26+
private bool $altCalled = false;
27+
private string $paramCalled = '';
28+
29+
public function _before(UnitTester $I)
30+
{
31+
$this->wasCalled = false;
32+
$this->altCalled = false;
33+
$this->paramCalled = '';
34+
}
35+
36+
/**
37+
* Tests Phalcon\Dispatcher :: callActionMethod()
38+
*
39+
* @author Phalcon Team <team@phalcon.io>
40+
* @since 2025-01-06
41+
*/
42+
public function dispatcherCallActionMethod(UnitTester $I)
43+
{
44+
$I->wantToTest('Dispatcher - callActionMethod()');
45+
46+
$dispatcher = new Dispatcher();
47+
48+
$dispatcher->callActionMethod(
49+
$this,
50+
'_wasCalled'
51+
);
52+
53+
$I->assertTrue($this->wasCalled);
54+
$I->assertFalse($this->altCalled);
55+
}
56+
57+
/**
58+
* Tests Phalcon\Dispatcher :: callActionMethod()
59+
*
60+
* @author Phalcon Team <team@phalcon.io>
61+
* @since 2025-01-06
62+
*/
63+
public function dispatcherCallActionMethodWithParams(UnitTester $I)
64+
{
65+
$I->wantToTest('Dispatcher - callActionMethod() - Params');
66+
67+
$dispatcher = new Dispatcher();
68+
69+
$dispatcher->callActionMethod(
70+
$this,
71+
'_paramCalled',
72+
[
73+
'something' => 'else'
74+
]
75+
);
76+
77+
$I->assertFalse($this->wasCalled);
78+
$I->assertFalse($this->altCalled);
79+
$I->assertEquals('else', $this->paramCalled);
80+
81+
$dispatcher->callActionMethod(
82+
$this,
83+
'_paramCalled',
84+
[
85+
'something'
86+
]
87+
);
88+
89+
$I->assertFalse($this->wasCalled);
90+
$I->assertFalse($this->altCalled);
91+
$I->assertEquals('something', $this->paramCalled);
92+
}
93+
94+
/**
95+
* Tests Phalcon\Dispatcher :: callActionMethod() Events
96+
*
97+
* @author Phalcon Team <team@phalcon.io>
98+
* @since 2025-01-06
99+
*/
100+
public function dispatcherCallActionMethodWithEvents(UnitTester $I)
101+
{
102+
$I->wantToTest('Dispatcher - callActionMethod() - Events');
103+
104+
$eventsManager = new Manager();
105+
$eventsManager->attach(
106+
'dispatch:beforeCallAction',
107+
function (Event $event, Dispatcher $dispatcher, Collection $observer) {
108+
$observer->action = "_altCalled";
109+
}
110+
);
111+
112+
$dispatcher = new Dispatcher();
113+
$dispatcher->setEventsManager($eventsManager);
114+
$dispatcher->setDi(new Di());
115+
116+
$dispatcher->callActionMethod(
117+
$this,
118+
'_wasCalled'
119+
);
120+
121+
$I->assertTrue($this->altCalled);
122+
$I->assertFalse($this->wasCalled);
123+
}
124+
125+
public function _wasCalled(): void
126+
{
127+
$this->wasCalled = true;
128+
}
129+
130+
public function _altCalled(): void
131+
{
132+
$this->altCalled = true;
133+
}
134+
135+
public function _paramCalled(string $param): void
136+
{
137+
$this->paramCalled = $param;
138+
}
139+
}

0 commit comments

Comments
 (0)