how to add a 3rd party library to magento?

前端 未结 2 1524
抹茶落季
抹茶落季 2020-12-28 15:57

The library doesn\'t need to integrate with magento, it\'s mostly a wrapper that communicates with an API.

I would like to be able to use this library and make these

2条回答
  •  遥遥无期
    2020-12-28 16:42

    As a solution that works perfectly: you can extend the varien_event_observer, create your own autoloader function and and by using the controller_front_init_before event you push this autoloader in front of the __autoload stack. this example of integrating solarium and symphony event dispatcher can explain it :

    class JeroenVermeulen_Solarium_Model_Observer_Autoloader extends Varien_Event_Observer {
    
        /**
         * This an observer function for the event 'controller_front_init_before'.
         * It prepends our autoloader, so we can load the extra libraries.
         *
         * @param Varien_Event_Observer $event
         */
        public function controllerFrontInitBefore( $event ) {
            spl_autoload_register( array($this, 'load'), true, true );
        }
    
        /**
         * This function can autoloads classes starting with:
         * - Solarium
         * - Symfony\Component\EventDispatcher
         *
         * @param string $class
         */
        public static function load( $class )
        {
            if ( preg_match( '#^(Solarium|Symfony\\\\Component\\\\EventDispatcher)\b#', $class ) ) {
                $phpFile = Mage::getBaseDir('lib') . '/' . str_replace( '\\', '/', $class ) . '.php';
                require_once( $phpFile );
            }
        }
    
    }
    

    and surely your libraries shoud be in the lib pool ! this solution is provided by @Jeroen Vermeulen, and i thank him :)

提交回复
热议问题