Issues

ZF-6484: Resource Autoloader does not take any namespace prefix

Issue Type: Bug Created: 2009-05-01T10:05:56.000+0000 Last Updated: 2011-07-05T15:19:05.000+0000 Status: Resolved Fix version(s): - 1.11.2 (30/Dec/10)

Reporter: Christian Münch (cmuench) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Loader

Related issues: - ZF-7815

Attachments:

Description

It's not possible to set a namespace prefix like "Foo_Bar" to the class Zend_Loader_Autoloader_Resource. The class checks the namespace too restrictive.

The problem is that array_shift only returns the first part. So my namespace must be "Foo".

Line: 142

<pre class="highlight"> 
if (!empty($namespaceTopLevel)) {
    $namespace = array_shift($segments);
    if ($namespace != $this->getNamespace()) {
         // wrong prefix? we're done
         return false;
    }
}

Comments

Posted by Dolf Schimmel (Freeaqingme) (freak) on 2009-05-01T10:08:20.000+0000

Assigning to Matthew.

Posted by Gildas de Cadoudal (gildas.de.cadoudal) on 2009-08-02T13:14:18.000+0000

I have encoutred the same problem following you can find a prosition of solution which replace the portion of code quote in issue description :

    if (!empty($namespaceTopLevel)) {
        if (strpos($class, $namespaceTopLevel) !== 0) {
            // wrong prefix? we're done
            return false;
        }
        $namespace = substr($class, strlen($namespaceTopLevel)+1);
        $segments = explode('_', $namespace);
        $namespace = $namespaceTopLevel;
    }

Posted by Matthew Weier O'Phinney (matthew) on 2010-06-18T09:30:02.000+0000

Can you demonstrate what your use case is for this? I'm not entirely convinced it makes sense for us to support this, particularly since you can override the resource autoloader yourself and override the appropriate method to drop in the functionality you desire.

Posted by Christian Münch (cmuench) on 2010-06-18T10:51:32.000+0000

I'm writing an CMS application with a plugin system. Each plugin should have an own namespace. The CMS plugin loader iterates over all plugins and registrates the namespace during runtime. All plugins have a namespace prefix "Plugin_". So i have i.E. a namespace like "Plugin_Frontend_Twitter". I overwritten the resource loader with my own Loader (Inmon_Cms_Loader_Autoloader_Resource) extended by Zend_Loader_Autoloader_Resource. So i have currently no problem. I think it's ok so. For ZF2 i have to port the CMS to the new plugin system with native PHP namespaces. So you can close the ticket IMHO.

Posted by Christian Opitz (metti) on 2010-06-20T18:37:26.000+0000

Hi guys,

had the same issue. I'm currently writing an implementation of ZF for TYPO3-extensions and there the "module" classes (for ZF they are modules; for TYPO3 plugins) are prefixed Tx_MyExt_...

I registered these modules with the Zend_Application_Module_Autoloader and everything worked fine except that getClassPath returned to early because it checked the toplevel namespace to be 'Tx' while the real one was Tx_ZfextSample_. My hack was then:

<pre class="highlight">
<?php
class ZfExt_Application_Module_Autoloader extends Zend_Application_Module_Autoloader
{
    /**
     * Overriding the parent method because it behaves wrong when using
     * prefixed namespaces.
     * 
     * $loader = new Zend_Loader_Autoloader_Resource(array(
     *     'namespace' => 'Tx_MyExt',
     *     'basePath'  => '/path/to/tx_myextension/plugin/',
     * ))
     * 
     * The class names in there are f.i. Tx_MyExt_Model_DbTable_Pages
     * Problem:
     * Parent method detects 'Tx' to be it's namespace and not 'Tx_MyExt'
     * Hacked that here.
     * 
     * @param string $class
     * @return string|boolean False if not matched other wise the correct path
     */
    public function getClassPath($class)
    {
        $namespace = $this->getNamespace();
        if (strpos($class, $namespace) !== 0)
        {
            return false;
        }
        $this->_namespace = null;
        $classPath = parent::getClassPath($class);
        $this->setNamespace($namespace);
        return $classPath;
    }
}

Cheers, Christian

Posted by Robert Goldsmith (far-blue) on 2010-11-01T01:53:26.000+0000

I've been trying to configure a per-module autoloader in a post-router plugin (after the module has been calculated) so I can have per-module forms, models etc. and consider the following format for the module namespace to be the 'correct' one: appNameSpace_moduleName. However, as has already bean mentioned, the getClassPath method in the 'module' autoloader assumes that the namespace for an autoloader does not contain the '' char. Instead of exploding the class path on the '' char and then comparing with the namespace, the following patch checks and correctly adjusts for any namespace before then continuing as normal by building the file path of a class based only on splitting on '_' chars after the namespace prefix.

<pre class="highlight">
--- Loader/Autoloader/Resource.php  2010-10-29 16:27:36.000000000 +0100
+++ Loader/Autoloader/Resource.new.php  2010-10-29 17:01:22.000000000 +0100
@@ -139,16 +139,24 @@
      */
     public function getClassPath($class)
     {
-        $segments          = explode('_', $class);
         $namespaceTopLevel = $this->getNamespace();
         $namespace         = '';
 
         if (!empty($namespaceTopLevel)) {
-            $namespace = array_shift($segments);
-            if ($namespace != $namespaceTopLevel) {
-                // wrong prefix? we're done
+            if (strncmp($class, $namespaceTopLevel, strlen($namespaceTopLevel)) != 0) {
+                // wrong namespace
                 return false;
+            } else {
+                $segments = explode('_', substr($class, strlen($namespaceTopLevel)));
+                $namespace = $namespaceTopLevel;
             }
+        } else {
+            $segments = explode('_', $class);
+        }
+        
+
+        if($segments[0] == '') {
+            array_shift($segments);
         }
 
         if (count($segments) < 2) {

Posted by Kim Blomqvist (kblomqvist) on 2011-05-28T07:17:57.000+0000

Seems like this is already resolved in ZF-11219?

Posted by Matthew Weier O'Phinney (matthew) on 2011-07-05T15:19:05.000+0000

Fixed with ZF-11219 for version 1.11.2.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts