ZF-8205: Zend_Loader_Autoloader_Resource Namespace Separator
Description
Add getter / setter for the namespace separator in the Zend_Loader_Autoloader_Resource class, this then makes it easy to switch to loading 5.3 namespaced resources.
This would help with Doctrine 2 integration as it loads 5.3 namespaced entities.
Comments
Posted by Keith Pope (mute) on 2009-12-22T09:30:12.000+0000
Hi,
I am attaching a prototype patch for this, this is only a very quick first implementation so see what you think. I may need to expand the tests etc...
Thx
Keith
Posted by Andrea Turso (trashofmasters) on 2010-03-06T19:17:10.000+0000
Hi,
I tried to roll my own patch to Zend_Loader_Autoloader_Resource when I found this report and then applied the diff to avoid reinventing the proverbial wheel.
With the files successfully patched, loading namespaced classes is working as expected, although the resource loader doesn't seem to be able to load class files
For instance, in a module bootstrap file:
We should find a way to make the two implementation to cooperate when PHP5.3 namespaced classes are requested together with classic namespaced classes.
When I was brainstorming for the patch a solution that popped in my mind was:
The first call will load \Forums\Resource\ when the second will load Forums_Resource_.
The patch could do something like this:
In {{Zend_Loader_Autoloader_Resource#addResourceType}} * Check if the $namespace has the heading \ ** Remove the heading \ in order return to the classic $namespace ** Mark the $resource as namespaced
In {{Zend_Loader_Autoloader_Resource#load}} * Before instantiating the the resource check if it's namespaced ** Alter the $class name to reflect the Fully Qualified Name.
With this trick the autoloader will autoload the file in the classical way because the class is underscored and the namespace is only "unveiled" when instantiating the resource. For instance:
What do you think about it?
Posted by Andrea Turso (trashofmasters) on 2010-03-07T07:58:25.000+0000
This is a naïve implementation of my solution, it does compose the namespace in the correct class path and does tell namespaced resources apart although it cannot autoload the file and therefore instantiate the Fully Qualified Name class.
Posted by Andrea Turso (trashofmasters) on 2010-03-07T08:14:18.000+0000
I had to hardcode a call to {{Zend_Loader_Autoloader_Resource#autoload($class)}} to load the required file and then instantiate the FQN class.
I attached the file Resource.diff, it contains all the changes I made to the {{Zend_Loader_Autoloader_Resource}} class, the test is in the previous {{ResourceTest.diff}} file. The ResourceTest has just one more test method I'm no Test-Driven so if you find this solution appropriate I suggest you to write another bunch of tests, just to be sure that the patch works with a wide range of use-cases.
Cya
Posted by Andrea Turso (trashofmasters) on 2010-03-09T11:50:38.000+0000
As I said in my previous comments, my patch doesn't seem to be able to autoload classes when instantiated directly, for example:
ended in Fatal Error: class \Forums\Model\Post not found ... while loading it with the load($resource, $class) worked.
I hacked the Zend_Loader_Autoloader and found out that applying the following patch made the Autoloader autoload my classes flawlessly.
I wouldn't rely on my previous patch to {{Zend/Loader/Autoloader/Resource.php}} as it was just a proof of concept to test resource loading.
I'd like to hear your opinions and if I'm heading the right way.
Bye
Posted by Dmitry (dv) on 2011-01-08T08:25:16.000+0000
I'm pretty sure there is no need to "hack" Zend_Loader_Autoloader.
Just use your custom autoloader. Also don't forget about resource autoloader for accessing classes with "Application_Model" into directory "applicatiom/models".