Namespace support for JLoader (with tests)#1400
Namespace support for JLoader (with tests)#1400florianv wants to merge 2 commits intojoomla:stagingfrom florianv:jloader
Conversation
|
Wouldn't it be better to add a PSR-0 (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) mode? |
|
Hello, it works like that. Each namespace must have a top-level namespace ("Vendor Name"). The registerNamespace is used to register the path to the root namespace : "Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case." For this, I'm not sure if file_exists and include are case sensitive (if it depends on the system). |
|
Just an up. If you look at this PSR-0 Loader https://github.com/composer/composer/blob/master/src/Composer/Autoload/ClassLoader.php, they use an array for namespaces (same namespace possible for different paths) too and it looks like the one in this pull request. The difference is that you can declarate namespaces camelcased and have lower case or camelcased directory and file names. Also it ensures it will work properly on case sensitive and insensitive systems. |
|
So the comment I added earlier, did it get deleted? Or maybe it's an issue with github, since the site has been experiencing problems recently. |
|
I got the email for your comment, but never saw it online. It was right around the time they started experiencing database issues, so perhaps it's being missing is related. Maybe you are the one who broke GitHub ;-) |
|
@florianv this pull request isn't mergeable any longer. Would you mind rebasing it (and maybe even squashing the two commits into one) so we can finish the discussion about it. I'm inclined to merge it, but I want to play with it a little bit and see what the overall impact on performance may look like. I really love the initiative in doing this though. I'm going to close the pull request for now. Once you get it rebased (double check code styling please) please re-open it so we can finish the conversation. Thanks a bunch! |
|
Ok, I will do it asap. This is because I take only the first part of the namespace to avoid iterating the whole namespaces stack. |
|
I'm quite comfortable with that. |
This pull request adds support for class loading and autoloading based on namespaces.
If your class is located in :
BASE_PATH/Chess/Piece/Pawn.php:You can register it to the auto loader, by registering the ROOT namespace
Chess:JLoader::registerNamespace('Chess', BASE_PATH . '/Chess');All classes respecting the naming convention in a given namespace :
namespace Folder\SubFolder;for classes inBASE_PATH/Folder/SubFolder/will be autoloaded.If you have lower case directory names and class names
BASE_PATH/folder/subfolder/, you can either declarate the namespace with lower or camel cases, and it will work too.But note that the first param of JLoader::registerNamespace is case sensitive and must match the namespace declaration case.
Examples:
namespace Chess;JLoader::registerNamespace('Chess', PATH_TO_CHESS);namespace chess;JLoader::registerNamespace('chess', PATH_TO_CHESS);You can also register multiple lookup paths for a given namespace (like the prefix).