-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed Dispatcher::dispatch() to ensure proper flow for all event handling, forwarding, exceptions, bubbling and performance improvements. Fixes #12931 #13112
Conversation
df243e4
to
f703a8e
Compare
…ling, forwarding, exceptions, bubbling and performance improvements. Fixes phalcon#12931
f703a8e
to
80123d3
Compare
Good job. Could you deal with failed test There was 1 failure:
--- Expected
+++ Actual
@@ @@
-'beforeDispatch-beforeExecuteRoute-afterExecuteRoute-afterDispatch'
+'beforeDispatch-beforeExecuteRoute-afterInitialize-afterExecuteRoute-afterDispatch' |
For clarity — the test expected value is marked in green and the new behavior is in red (Wording backwards). For reference — The failed test was prevalent on the first checkin in the old The reason the (now removed) old unit test failed was because of a nuance in the old test. Specifically, it did not contain an
My original thought is that the existing behavior is incorrect; however, I could be compelled to add a new test case to revert to the old behavior if you have some insight. E.g. If the use case was to somehow globally instantiate controllers with shared functionality via the In the case of the dispatcher, it depends how you read the afterInitialize event. Do you: A) interpret initialize as being specific to initializing via the
B) refer to it as being general such that the event should be fired in it's relative location even without a direct call into the
Side note --- there is a separate note in the code. The whole [ initialize/afterInitialize ] block is in the wrong location. The The issue here is the conflicting naming with the |
Yes, the test expected value is marked in green and the new behavior is in red . I just copied phpunit output from the terminal. Well, could you just remove wrong test? |
@sergeyklay I updated original comment with more info and questions. It is removed as noted now (hence passing); however, do you have thoughts on [A] vs. [B] above? I'm open to reverting back to the [B] model as I could see there be a global use case for performing actions from an eventhook globally in controllers regardless of the presence of |
Actually I prefer [B] but in general your PR looks really great. I will keep this open for a day or two to allow other contributors a chance to speak up, and to take a fresh look at it later. But at the moment it seems good to me. cc: @phalcon/core-team |
This looks great. I would also prefer [B]. Great work buddy, thank you so much for the contribution! |
…f the 'initialize' method and corresponding code notes/cleanup for reference.
Modified to include option [B] above. This means:
|
For 3.x we should keep things the way they are. If I need events to stop (subsequent listeners do not fire) we will need to do For 4.x this behavior needs to change. Simply The above will be OK behavior so long as we ensure that we highlight it in the documentation. |
This looks good to me @sergeyklay whenever you get a chance hit the magic button |
Thank you! |
@sergeyklay What's the 4.0 plan? Is 3.2.x getting merged up to 4.0 currently? Should I submit a new PR against the current 4.0 branch? or Should I wait until you've merged 3.2.x/master back down into 4.0? |
We're plan to release 3.2.3 in next days and start 3.3.0 branch. You can cherry-pick changes from 3.2.x to 4.0.x branch if you want or leave this job for me. I'll do this later (but can't say when exactly). |
Doesn't matter to me ... I can just help with the 4.0 changes related to this feature. Is the 4.0 branch working/building and is it PHP7.x only ? What would you prefer I do ? |
Yes, we'll drop support of the Zend Engine 2.x from Phalcon 4.x. |
It looks like this patch needs a small review. It broke an existing behaviour: after forwarding to an action within the same controller - its However, when forwarding to a different controller, controller's <?php
use Phalcon\Mvc\Controller;
class IndexController extends Controller
{
public function initialize()
{
$this->view->location = $this->di->get('dispatcher')->getHandlerClass();
$this->view->action = $this->di->get('dispatcher')->getActionName();
}
public function indexAction()
{
/**
* Before #13112 this forward will result in "Your location: IndexController/welcome".
*
* After #13112 this forward will result in "Your location: IndexController/index".
*
* @var IndexController
*/
$this->di['dispatcher']->forward([
'controller' => 'index',
'action' => 'welcome',
]);
}
public function welcomeAction()
{
}
} This line seems to be the key: https://github.com/phalcon/cphalcon/blob/v3.2.3/phalcon/dispatcher.zep#L520. Current controller is no longer considered a "fresh" instance. So, at the moment, a workaround for this problem is: $eventsManager->attach('dispatch:beforeForward', function(Phalcon\Events\Event $event, Dispatcher $dispatcher) {
$handlerClass = $dispatcher->getHandlerClass();
$dispatcher->getDi()->remove($handlerClass);
}); Code to reproduce is attached. Thanks! |
@virgofx Could you please take a look |
@temuri416 That's the correct behavior. The initialize() is only called ONCE ever per controller. It's more of a static onConstruct(). It was designed to parallel how other areas of Phalcon code do initialization - e.g. Models It's not great from a wording (which I updated in code) and perhaps the out-of-order issue is causing confusion? See note below (will be fixed in next version) Also .. this is how things were in the past, the patch didn't explicitly modify that behavior (although there were other bugs that may have blocked the intended behavior and thus it may appear different). Important:
As for the issue your experiencing, I believe the @temuri416 Let me know if you have issues moving your code into @sergeyklay With regards to Fix #1 above (and noted in above comments and inside code) .. do you want fix submitted into 3.3 branch? Or waiting until 4.0. Also, Which is the branch that is going to drop all the mixed types and go strict types (e.g. function (array $forward)) which will prevent a ton of is object checks everywhere? |
ok, no big deal. I can move my logic to |
@virgofx These changes already in 3.3.x branch. I'll deal with 4.0.0 a bit later |
This is a an update of previously closed PR #12209. It resolves all dispatch issues, adds additional notes in the code for future deprecations in 4.0. Various performance improvements in the resulting dispatch process as result of variable caching and dropping the global try/catch which previously hooked into the RVAL for every statement previously.
Fixes current issue: #12931
Related to (and ensures fixes for) previous issues:
#12154
#11819
cc @andresgutierrez
cc @temuri416
cc @sergeyklay
cc @niden