ZF-7031: Zend_Loader_Autoloader_ServiceLoader


Hi, After version 1.8 and the new Autoloader system, I think we need Zend_Loader_Autoloader_ServiceLoader or PluginLoader, It will be more easy to plug and play service between modules for example the below workflow:

Module "admin" create Interface "Admin_Api_Registration"

Module "news" create class "News_Service_Administration" implements interface "Admin_Api_Registration"

Module "admin" creates new Zend_Loader_Autoloader_ServiceLoader("service","Administration");

Now Module admin can use foreach loader->getPlugins()



I fail to see how this differs from the current Zend_Loader_Autoloader_Resource and related Zend_Application_Module_Autoloader, which provide autoloading capabilities for precisely this sort of thing. Plugins may be autoloaded the same way as classes using this paradigm already.

The functionality already exist in the new Autoloader system, I'm talking about encapsulate it for loading across modules, maybe the below code explain more than me :)

$modules = Zend_Controller_Front::getInstance()->getControllerDirectory();
foreach (array_keys($modules) as $module) {
       $class = $this->_formatModuleName($module) . '_' . $this->_namespace . '_' . $this->_type;
        foreach(Zend_Loader_Autoloader::getInstance()->getClassAutoloaders($class) as $loader) {
                $types = $loader->getResourceTypes();
                 if(isset($types[$this->_namespace])) {
                      $path = $types[$this->_namespace]['path'] . '/' . str_replace('_', '/', $this->_type) . '.php';
                      if(is_file($path)) {
                          require_once $path;
                          if(!class_exists($class)) {
                              require_once 'Zend/Loader/Exception.php';
                              throw new Zend_Loader_Exception("Unable to find class: " . $class);
                          $this->_plugins[$module] = new $class();

Zend_Application_Module_Autoloader already fulfills this purpose.

If you use Zend_Application_Module_Bootstrap, it will create an instance of the above class that sets up a resource autoloader for that module; alternately, you can instantiate it yourself. Regardless, if used, you can then autoload that module's resources from anwywhere. As an example, if you have modules/foo/models/Bar.php with class 'Foo_Model_Bar', you can then simply instantiate the class: $bar = new Foo_Model_Bar(). This is easier and more intuitive than any other approach, and works cross-modules.