1

So when I develop extensions I usually do this outside of Magento and just pull in the packages I need. Once I have developed the extension I integrate it at the end.

Because of this I manually create factory methods, is this best practice or not? I don't really want to couple to Magento so I'm not sure what to do in this scenario.

I'm coupled to the Magento/Framework module. Do you know if it generates these for you when not plugged into Magento?

  • 2
    It's complicated, and it depends. IMO, if at the end of the day you're writing something that is used within Magento, you're already coupled to the framework, and therefor don't bother. If you're worrying about creating a class that gets generated in Magento, again you're already coupled. If the code can/will be used elsewhere then it should be it's own package, and a different package should be made for the Magento integration. But if you are just developing a Magento extension, then you're tied to the framework by definition. – Ash Smith Oct 10 '16 at 23:19
  • Yeah I'm coupled to the Magento/Framework module. Do you know if it generates these for you when not plugged into magento? – Brideo Oct 10 '16 at 23:23
2

So when I develop extensions I usually do this outside of Magento and just pull in the packages I need. Once I have developed the extension I integrate it at the end.

...

Yeah I'm coupled to the Magento/Framework module. Do you know if it generates these for you when not plugged into magento?

Generation is handled by Magento/Framework, yes. To utilize that in your tests, you can use the Magento unit test framework or mock the generated classes. Both methods are described here: Generate factory or proxy in unit tests: "ReflectionException: Class ...Factory does not exist"

But you can go even further and develop as much as possible of your extensions without any dependency to Magento. You will have two packages in the end, one independent library and one "dumb" Magento module that integrates the library into Magento.

For the Magento module you will mostly write integration tests within Magento (as in your "integrate it at the end" step). The library can define interfaces for interaction with the Magento core and the module implements these (acting as an adapter).

If you follow this approach, you have different options regarding factories in the library package, which should be choosen case by case:

  • write them yourselves. This makes sense for classes that are implemented and used within the library
  • only write the interfaces, then implement these in the module (possibly proxying to generated factories). This makes sense for the adapter classes that are not implemented in the library.
  • let them generate by Magento when integrating the module. This makes sense for classes that are implemented within the library but only used from the integration module

If you want to read more about the framework independent approach, I wrote a few blog articles on how and why to do this that you can find here: http://integer-net.com/m1m2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.