Skip to content
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

Replaced hard coded "new" object creation with Magento friendly way #993

Closed
wants to merge 5 commits into from
Closed

Replaced hard coded "new" object creation with Magento friendly way #993

wants to merge 5 commits into from

Conversation

woutersamaey
Copy link
Contributor

@woutersamaey woutersamaey commented May 20, 2020

Replaced hard coded "new" object creation with Magento friendly way, allowing other modules to rewrite these objects.

@woutersamaey woutersamaey changed the title Replaced hard coded "new" object reaction with Magento friendly way Replaced hard coded "new" object creation with Magento friendly way May 20, 2020
@Flyingmana
Copy link
Member

Please use @deprecated only for classes, which are intended to get removed completely, not as a workaround to mark direct instanciation.

Also for reviewers: check usage is only changed, where magento is already loaded, there are probably uses where this will fail now, because magento is not loaded yet.

@woutersamaey
Copy link
Contributor Author

So I remove the @deprecated word?

@Flyingmana
Copy link
Member

yes, please :)

…allowing other modules to rewrite these objects.
@woutersamaey
Copy link
Contributor Author

I removed the 3 @deprecated words

@sreichel
Copy link
Contributor

Also for reviewers: check usage is only changed, where magento is already loaded, there are probably uses where this will fail now, because magento is not loaded yet.

I dont sse problems with changes inside app/core/Mage, but there schould be no calls to Mage:: inside lib/Varien ...

@colinmollenhour
Copy link
Member

Thanks for the PR!

Can you explain the use case for this in more detail?

Also, did you consider that you can override any class file by dropping classes with the same name into app/code/* directories (due to ordering of include paths)?

My thinking is that the lib/Varien files are utility libraries (akin to the Zend libs) and not really meant to be extended with new user-specific functionality like the models and blocks are, and if there are bugs or lacking features it is best to just submit fixes/improvements to the core code rather than override them. And if you must override them or can't wait for a PR to be merged there is always the copy/modify option with the include path.

@woutersamaey
Copy link
Contributor Author

We have created a module that uses Amazon S3 as primary media storage without using the media dir on the disk. Other modules have S3 or other storage options too, but in our case the media dir is completely empty.

To build this, we had to dig deep in Maganto and found these models to be not easily rewriteable.

You’re right about the include path etc, but I would prefer Magento to load models in the same way, as Magento tells us to do. Why the ‘new’ keyword is used at all seams to break Magento’s own rules without valid reason.

So I can understand your workaround, but my thinking was to have an easy and clean solution that does not need any workarounds.

I’d even endeavor to remove all uses of ‘new’ as much as possible. Let devs rewrite any and all
models they like.

I understand Varien is a lib, but Magento always extends lib files and then uses these, while also loading them the Magento way. Some examples here are the Zend_Db classes. These (and others) are all wrapped in ‘Mage_’ classes.

So hope you like this PR. There is always an alternative, but my goal is following best practises even when Magento forgot their own rules.

@colinmollenhour
Copy link
Member

Thanks for the additional detail. You make a good case and I think I am more convinced than before so no objections.

As an aside, any chance your S3 module will be open-source? :)

@woutersamaey
Copy link
Contributor Author

The S3 module actually needs more core edits, but it's very hard for me to post them here and get everyone to agree on them. So, let's first try to get this one in...

Copy link
Contributor

@sreichel sreichel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the changes to lib/Varien required if you use your added wrapper classes?

@tmotyl
Copy link
Contributor

tmotyl commented Jun 20, 2020

This kind of change has a significant risk of introducing some side effects.
Maybe we are missing some api or event which would make the whole architecture of your extension clearer and you would not need to override core classes any more?

Can you describe implementation details of your change?

@github-actions github-actions bot added Component: Adminhtml Relates to Mage_Adminhtml Component: Api PageRelates to Mage_Api Component: Backup Relates to Mage_Backup Component: Captcha Relates to Mage_Captcha Component: Catalog Relates to Mage_Catalog Component: Cms Relates to Mage_Cms Component: ConfigurableSwatches Relates to Mage_ConfigurableSwatches Component: Connect Component: Core Relates to Mage_Core labels Jul 24, 2020
@Flyingmana Flyingmana changed the base branch from 1.9.4.x to 20.0 August 4, 2021 23:39
fballiano
fballiano previously approved these changes Aug 5, 2021
app/code/core/Mage/Catalog/Helper/Image.php Outdated Show resolved Hide resolved
app/code/core/Mage/XmlConnect/Helper/Image.php Outdated Show resolved Hide resolved
app/code/core/Mage/XmlConnect/Model/Images.php Outdated Show resolved Hide resolved
lib/Varien/Convert/Adapter/Http.php Outdated Show resolved Hide resolved
lib/Varien/Image.php Outdated Show resolved Hide resolved
lib/Varien/Io/File.php Outdated Show resolved Hide resolved
@github-actions github-actions bot added Component: Dataflow Relates to Mage_Dataflow Component: Downloadable Relates to Mage_Downloadable Component: Eav Relates to Mage_Eav Component: lib/Varien Relates to lib/Varien Component: lib/* Relates to lib/* Component: PayPal Relates to Mage_Paypal Component: Sales Relates to Mage_Sales Component: Shipping Relates to Mage_Shipping Component: Sitemap Relates to Mage_Sitemap Component: XmlConnect labels Sep 16, 2021
@woutersamaey
Copy link
Contributor Author

@luigifab I fixed these few nit-picks

@fballiano
Copy link
Contributor

I love how @luigifab is our syntax police (I really do, that's not sarcastic) :-)

@woutersamaey
Copy link
Contributor Author

We should use PHP CS Fixer for these nit-picks. Devs really have better things to do imo.

@ADDISON74
Copy link
Contributor

From what I understand this PR comes as a proposal to solve a particular situation encountered in Magento. At first glance, the changes are made correctly, but what are the implications in the wrong sense?

If this PR will be merged I would change the phrase bellow is by removing the comma:

Instead of creating instances using "new", use Mage::getModel('core/varien_file_uploader')

@fballiano
Copy link
Contributor

Are the changes to lib/Varien required if you use your added wrapper classes?

I agree with @sreichel that it's very weird to see Mage:: in "lib/", is it absolutely necessary for some reason?

Copy link
Collaborator

@luigifab luigifab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this:

echo get_class(new Varien_Io_File()),"\n";
echo get_class(Mage::getModel('Varien_Io_File')),"\n";
echo get_class(Mage::getModel('varien/io_file')),"\n";

Results are:

Varien_Io_File
Varien_Io_File
Varien_Io_File

So, Mage_Core_Model_Varien_File_Uploader + Mage_Core_Model_Varien_Image + Mage_Core_Model_Varien_Io_File are not necessary?

@luigifab luigifab mentioned this pull request Oct 20, 2022
3 tasks
@fballiano
Copy link
Contributor

I wouldn't have this in v20 only, it will create too many merging problems.

Also, there are conflicts to be fixed, I'm setting it to draft.

@fballiano fballiano marked this pull request as draft December 19, 2022 11:35
@@ -30,6 +30,8 @@
* ATTENTION! This class must be used like abstract class and must added
* validation by protected file extension list to extended class
*
* Instead of creating instances using "new", use Mage::getModel('core/varien_file_uploader')
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Mage::getModel('core/varien_file_uploader') instead of creating instances with "new"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anyway files in "lib/" should never have references to "Mage::" as Sven pointed out

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only wanted to correct a few sentences, This PR has some important issues that need to be discussed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep for sure, if we merge luigifab PR (which is similar) I'll take this one further but I without the "lib/" part ;-)

@@ -26,6 +26,7 @@

/**
* Image handler library
* Instead of creating instances using "new", use Mage::getModel('core/varien_image')
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Mage::getModel('core/varien_image') instead of creating instances with "new"

@@ -27,6 +27,7 @@

/**
* Filesystem client
* Instead of creating instances using "new", use Mage::getModel('core/varien_io_file')
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Mage::getModel('core/varien_io_file') instead of creating instances with "new"

@storefront-bvba storefront-bvba closed this by deleting the head repository Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Adminhtml Relates to Mage_Adminhtml Component: Api PageRelates to Mage_Api Component: Backup Relates to Mage_Backup Component: Captcha Relates to Mage_Captcha Component: Catalog Relates to Mage_Catalog Component: Cms Relates to Mage_Cms Component: ConfigurableSwatches Relates to Mage_ConfigurableSwatches Component: Core Relates to Mage_Core Component: Dataflow Relates to Mage_Dataflow Component: Downloadable Relates to Mage_Downloadable Component: Eav Relates to Mage_Eav Component: lib/Varien Relates to lib/Varien Component: lib/* Relates to lib/* Component: PayPal Relates to Mage_Paypal Component: Sales Relates to Mage_Sales Component: Shipping Relates to Mage_Shipping Component: Sitemap Relates to Mage_Sitemap
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants