When parsing a file using namespace, the autoloader receives only the name of the classes relative to the files namespace, not the fully qualified name that PHP will pass when trying to autoload the class.

It's relatively easy to determine the namespace of a given file, so I will attach a patch to do that shortly. I prefer we use a PSR-0 autoloader long-term, but that is still a work in progress for contrib.

Comments

glennpratt’s picture

StatusFileSize
new972 bytes

D8 patch, could probably be more robust and needs tests.

glennpratt’s picture

Status: Active » Needs review
glennpratt’s picture

D7 patch is identical except paths.

glennpratt’s picture

Cross reference for contrib autoload module. http://drupal.org/node/1400160

eric_a’s picture

Version: 8.x-dev » 7.x-dev
Issue tags: +PSR-0

Soon the registry won't be with us anymore in D8 (#1541674: Remove the registry), so moving this to D7. Tagging PSR-0.

Status: Needs review » Needs work

The last submitted patch, registry_parse_file-namespace-1399974-3-D7.patch, failed testing.

adamwight’s picture

Here's a patch against 7.23, including tests.

Unfortunately, multiple namespaces in a file cannot be supported without much more intensive parsing, so this isn't a complete solution.

Glad to hear the registry will go away in D8!

adamwight’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, registry_parse_file-namespace-1399974-4-D7.patch, failed testing.

adamwight’s picture

Status: Needs work » Needs review
StatusFileSize
new3.01 KB

Patch now tracks the 7.x branch.

adamwight’s picture

TODO:
* patch documentation
* announce new feature in release notes

adamwight’s picture

Issue tags: +PSR-4

Tagging with PSR-4

adamwight’s picture

Title: _registry_parse_file doesn't correctly identify the name of classes in a namespace. » _registry_parse_file doesn't correctly register classes outside of the global namespace
adamwight’s picture

Title: _registry_parse_file doesn't correctly register classes outside of the global namespace » Registry does not support namespaced classes
fabianx’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

RTBC works great for me, I created Registry Autoload module to support this without patching core:

https://www.drupal.org/sandbox/fabianx/2354855

One namespace per file should be enough.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 10: registry_parse_file-namespace-1399974-5-D7.patch, failed testing.

fabianx’s picture

Besides no longer applying the regexp also needs work.

We cannot expect people to put the namespace on the same line as the <?php part and this is a violation of our code standards.

donquixote’s picture

Just saying, this problem has been solved by other people already.

Composer:
https://github.com/composer/composer/blob/master/src/Composer/Autoload/C...
("This file is copied from the Symfony package.")

xautoload:
http://cgit.drupalcode.org/xautoload/tree/src/Discovery/FileInspector.ph...
(This reminds me that I should put some credit into this file, since I have most of this from Composer.)

Registry Autoload:
http://cgit.drupalcode.org/sandbox-Fabianx-2354855/tree/registry_autoloa...

donquixote’s picture

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.