ZF-10836: ZLAutoloader_Resource should check its constructors params order to make sure "namespace" is handled before "resourceTypes"

Issue Type: Bug Created: 2010-12-16T09:27:32.000+0000 Last Updated: 2010-12-20T00:16:03.000+0000 Status: Resolved Fix version(s): - 1.11.2 (30/Dec/10)

Reporter: julien PAULI (doctorrock83) Assignee: Adam Lundrigan (adamlundrigan) Tags: - Zend_Loader

Related issues: Attachments: - ZF-10836.patch


Assuming such a completely valid code :

<pre class="highlight">
$data = array('basePath' => '/foo', 'resourceTypes' => array(/* my stuff */), 'namespace' => 'Foo');
new Zend_Loader_Autoloader_Resource($data);

That wont work as you could imagine. The constructor invoke setters, but in $data input, setResourceTypes() will be treated before setNamespace(). The fact is that resourceTypes make use of the namespace, not set yet; the behavior is not what expected as resourceTypes will use a completly wrong namespace and autoloading will fail

A simple ksort() on the input array could sort the keys such that 'namespace' is beeing handled before 'resourceTypes'


Posted by Adam Lundrigan (adamlundrigan) on 2010-12-19T16:25:13.000+0000

Interesting find! I've attached a patch which reproduces the issue, and a possible fix. Essentially I updated Zend_Loader_Autoloader_Resource::setOptions to shift the 'namespace' key to the beginning of the array, so that it is processed first. This should be a more robust solution than sorting the keys. Comments?

<pre class="highlight">
// If namespace key exists, shift it to be beginning of the options array
// so that it is processed first
// @see ZF-10836
$namespaceIndex = array_search('namespace', array_keys($options));
if ( $namespaceIndex !== false )
    $namespace = $options['namespace'];
    $options = array('namespace'=>$namespace) + $options;

Posted by Ramon Henrique Ornelas (ramon) on 2010-12-19T16:30:06.000+0000

@Adam I think that could be but simple

<pre class="highlight">
if (array_key_exists('namespace', $options)) {

Thanks advance

Posted by Adam Lundrigan (adamlundrigan) on 2010-12-19T16:35:14.000+0000

@Ramon Thanks! That is much simpler. I've updated my patch with your code.

Posted by Adam Lundrigan (adamlundrigan) on 2010-12-19T16:48:56.000+0000

Removed @ from @see comment in patch, as it wasn't in a docblock. Changed array_key_exists to isset (namespace can't be null, and isset is faster)

Posted by Adam Lundrigan (adamlundrigan) on 2010-12-19T17:21:26.000+0000

Fixed a typo

Posted by Marc Hodgins (mjh_ca) on 2010-12-20T00:16:01.000+0000

Applied to trunk in r23567, merged to release branch 1.11 in r23568.

Have you found an issue?

See the Overview section for more details.


© 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.