Search code examples
phpmagentowsdl

Magento with non-convention class names - generated WSDL class names within Magento


Were using WSDL2PHPGenerator to generate classes for a webservice the store needs to call in various events. Because they are generated and serialized 1 to 1 by their name, the classes names cannot be changed. For that reason, they are not named under the convention of Company_Module_ClassName - so even though we manage to require the files, when trying to create a new instance, the Magento auto-loader function has an include statement in the end of it which will fire an exception for each of those classes as it cannot find them using the name convention.

What is the right approach for this issue? Did someone handle this flow?

If breaked into smaller questions that perhaps could help resolve the bigger flow.

1.Is there a way to disable the autoloader for specific classes ? We tried to disable / enable via:

 $autoloadFuncs = spl_autoload_functions();
 foreach ($autoloadFuncs as $unregisterFunc) {
     spl_autoload_unregister($unregisterFunc);
 }

But after enabling it back, the Magento loader stopped loading new classes ...

2.Is there a way to load classes without the naming convention ?

3.Is there a way perhaps to use SoapClient with classes that are named differently then the classes that should be serialized to in the final XML format?


Solution

  • There's probably no "right" way to do this, but when I've encountered similar situations I've

    1. Written an autoloader function that loads the special classes correctly

    2. Inserted an autoloader function in front the of Varien_Autoload autoloader

    Re: number 2, something like this should work (pseudo code)

     //get all current autoload functions
     $autoloadFuncs = spl_autoload_functions();
    
     //unregister all autoload functions (autoload queue will be empty)
     foreach ($autoloadFuncs as $unregisterFunc) {
         spl_autoload_unregister($unregisterFunc);
     }    
    
     //add your autoloading code here -- doesn't need to be an
     //anonymous function, any valid PHP callback will do
     spl_autoload_register(function(){
          //your autoloader code
     });
    
     //add the old autoloader functions back
     foreach ($autoloadFuncs as $unregisterFunc) {
         spl_autoload_register($unregisterFunc);
     }    
    

    This will ensure the autoloader for your functions will be first, and that way Magento's exception throwing autoloader never fires. Just make sure your autoloader silently fails if it can't find a class.

    Also, if you're using an even remotely modern version of PHP, the spl_auto_reguster functions's third parameter will let you add a new autoloader functions before all the others (prepend it). This will let you avoid the unregister/register code above

    spl_autoload_register(function(){
        //your autoload code here
    },true,true);
    

    Update per the comments -- a small sample program that shows the autoloader will stop calling callbacks on the stack once a match is found.

    <?php
        function one()
        {
            class Foo{
            }
            echo __FUNCTION__,"\n";
        }
    
        function two()
        {
            echo __FUNCTION__,"\n";
        }    
    
        function main()
        {
            spl_autoload_register('one');
            spl_autoload_register('two');
        
            $foo = new Foo;
        }
        main();