-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Adding a validation before adding or executing layout generator class. #22324
Adding a validation before adding or executing layout generator class. #22324
Conversation
Hi @tiagosampaio. Thank you for your contribution
For more details, please, review the Magento Contributor Assistant documentation |
Hi @dmytro-ch, thank you for the review. |
@@ -77,6 +77,10 @@ public function process(Reader\Context $readerContext, Generator\Context $genera | |||
{ | |||
$this->buildStructure($readerContext->getScheduledStructure(), $generatorContext->getStructure()); | |||
foreach ($this->generators as $generator) { | |||
if (!$this->validateGenerator($generator)) { | |||
continue; |
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.
Fixed Issues (if relevant)
It fixes no issues.
What kind of issue it fixes then? What happened before if corresponding interface is not implemented?
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.
Hi @orlangur.
If any 3rd party module overrides or include a generator in GeneratorPool and doesn't declare the methods getType() and process() the user will be given an exception. That's why I think it's a good idea to force the implementation of GeneratorInterface
.
Examples:
Call to undefined method Any\Class::getType()
Call to undefined method Magento\Framework\View\Page\Config\Generator\Head::process()
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.
@tiagosampaio so user already receives an exception and by this change you actually hide it. What should really be done is throwing exception in case of incorrect interface and only in addGenerators
method.
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.
@orlangur you're right. I'll do it.
*/ | ||
public function process(Reader\Context $readerContext, Generator\Context $generatorContext) | ||
{ | ||
$this->buildStructure($readerContext->getScheduledStructure(), $generatorContext->getStructure()); | ||
foreach ($this->generators as $generator) { | ||
$this->validateGenerator($generator); |
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.
Please validate in addGenerators
only and get rid of this method.
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.
@orlangur I believe this validation should be there too since the argument $generators
can be injected via DI as well as using addGenerator
method. Please let me know what you think about it.
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.
How? As I see https://github.com/magento/magento2/blob/9c4e1758640ef6be4458929fc61d2619a78d9fc4/lib/internal/Magento/Framework/View/Layout/GeneratorPool.php#L51 it is still added via addGenerators
this way.
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.
Forget what I've said. It really doesn't make sense and I just made the commit.
private function validateGenerator($generator) | ||
{ | ||
if (!($generator instanceof GeneratorInterface)) { | ||
throw new LocalizedException(__('Generator classes must be instances of %1', GeneratorInterface::class)); |
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.
Do we really use LocalizedException
in such cases? Please grep for some similar check, I believe it should be InvalidArgumentException
and no need to translate.
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.
@tiagosampaio and now please inline the method as we need this check only once 😉
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.
After changes are applied and all builds are green, please squash them into a single commit so that we have perfectly clean history 😉
@orlangur it's done. |
* @return void | ||
*/ | ||
protected function addGenerators(array $generators) | ||
{ | ||
foreach ($generators as $generator) { | ||
if (!($generator instanceof GeneratorInterface)) { |
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.
Please get rid of unnecessary parentheses.
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.
Done.
Hi @orlangur, thank you for the review. |
Hi @tiagosampaio during testing PR changes I got fatal error instead of exception error. |
* @return void | ||
*/ | ||
protected function addGenerators(array $generators) | ||
{ | ||
foreach ($generators as $generator) { | ||
if (!$generator instanceof GeneratorInterface) { | ||
throw new \InvalidArgumentException( | ||
'Generator class must be an instance of %1', GeneratorInterface::class |
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.
Yep, better use concatenation here (looks like you intended to use sprintf
).
Commit amend + force push, please.
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.
@orlangur I'm just using sprintf()
now.
Hi @orlangur, thank you for the review. |
✔️ QA passed |
Hi @tiagosampaio, thank you for your contribution! |
Before adding or executing any layout generator it's important to validate the class before.
Fixed Issues (if relevant)
It fixes no issues.
Contribution checklist (*)