-
Notifications
You must be signed in to change notification settings - Fork 153
Sessions to be used only in controllers and blocks #36
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
Conversation
It is unsafe to use sessions anywhere but controllers and blocks due to multiple web APIs in Magento
https://devdocs.magento.com/guides/v2.2/coding-standards/technical-guidelines.html So, yes, this is correct and is in guidelines. And it would be great to cover it with a test. |
#### What has to be done | ||
Add PHPMD rule which will be triggered when a class has a constructor parameter of type | ||
SessionManagerInterface (and any of the implementations) and is not an instance of | ||
ActionInterface or AbstractBlock. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Also a plugin for a block/controller should be OK to use sessions.
- Would be good to expand the rule to cover "6.2.2. Request, Response, Session, Store Manager and Cookie objects MUST be used only in the Presentation layer.". Can be implemented as separate rules.
- Also, data providers for UI components are part of presentation layer, so they can depend on session.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- makes sense, but I don't know how a block plugin can be identified in a PHPMD rule. Controller plugin can be sort of identified by having before/after/aroundExecute/Dispatch but I'm not sure if that would be correct. I think it would be OK if devs would just have to put SuppressWarnings for those cases
- Ok, I'll update the proposal
- Sure, instances of Magento\Framework\View\Element\UiComponent\DataProvider\Document and Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface will be ignored by the rule then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- The plugin has
$subject
which can go through the same checks on whether it's a block or controller.
I don't think a warning should be issued when using store manager - it can be used outside of presentation layer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved from my side.
Though the proposed rules don't cover all the rules from the Tech Guidelines, they bring a good improvement and allow simpler implementation w/o dealing with exceptional cases. So it makes sense to implement it as described for now.
In general I agree, but I do have to say that I think It should stick around for at least a few years still, until the GraphQL integration is mature enough and has effectively replaces the frontend REST API uses in extensions. |
@Vinai CustomerSessionUserContext is an exception case when we can't identify automatically whether a session object is used properly - CustomerSessionUserContext is fine and will stay |
It is unsafe to use sessions anywhere but controllers and blocks due to multiple web APIs in Magento