Modern Overrides – How to Rewrite Library Files

coreoverrides

The problem is well known: lib classes are loaded exclusively through the autoloader, and we cannot change them other than:

  • Copying them entirely to a codePool that is checked earlier than lib.
  • Installing the PSR-0 autoloader, specifying an autoload classmap, and then copying the file entirely into that folder structure instead. [ my current solution ]

I'm in a tough spot because I want to potentially touch many of these files – but for the sake of my sanity and stability/upgradeability of the store don't want to copy entire library classes.

Now obviously there are potential solutions to this problem, but they all come with their own set of problems:

  • Go the AOP route and use a PHP-based library like Go! AOP: Last I checked this will require Magento classes to be loaded by the composer autoloader, not just one being available. Flyingmana has done some work in this area but it's definitely not ready for production usage and my needs are more immediate. I also want to ship as an extension, and that would require more composer setup.
  • Go the AOP route and use a native PHP extension: Probably the most favorable at this point, but it would require installing a separate extension, not to mention that it would not work with HHVM.
  • Use PHP's classkit and/or runkit: It's another native PHP extension so it has the same problem as above.
  • Patch call sites to use my own namespaced (\Danslo\Varien_X) version, then extend from the original (\Varien_X): There are just way too many callsites to patch and it would require a silly amount of rewrites. Not an option.
  • Roll my own: It should be possible to:

    1. Write my own autoloader.
    2. Copy the original class to a separate folder ({root_dir}/var/tmp), wrap it in namespace \Magento { < original contents > }.
    3. Include that file.
    4. Include my modified class OriginalClass extends Magento\OriginalClass {}

The downside of this is obvious: dynamic code generation, regex, a little overhead for loading rewritten classes. But I'm almost sure that at this point it would beat copying ~5000 lines of code when I just want to touch/add ~100 lines.

I know I'm asking a lot, but is there something modern and relatively clean out there that helps solve this problem?

Best Answer

Decided to implement Go! AOP framework in Magento.

See Danslo_Aop on github.

Related Topic