Hi!
If I create a PSR-0 style autoload module for D7, would that have a chance to be accepted as a D7 version of this module?
It feels wrong introducing this under a different name.

I am thinking of a pattern similar to what is being discussed here,
#1290658: Move all module-provided classes to PHP namespaces (PSR-0 or similar), and autoload them without the registry [policy, no patch]
#867772-78: Use PHP 5.3 namespaces
where the module maintainer can choose between PHP 5.3 namespace-per-module or prefix-based class names.

I can start this as a sandbox thing, np, just want to know if there is any interest.

UPDATE:
This is now available as X Autoload module.

CommentFileSizeAuthor
#2 xautoload.module.txt2.83 KBdonquixote

Comments

michelle’s picture

I'm interested... I don't know if I want an actual module as a dependency but it would serve as a great example either way. I'm trying to keep up with D8 in D7 and this is one of the more confusing ones for me.

Michelle

donquixote’s picture

StatusFileSize
new2.83 KB

Here is a simple (D6) module that does work both standalone and in combination with the existing autoload module.

Classes should be named
class [module name]_CamelCase[whatever] {}
They are to be placed in files within
[module dir]/lib/...

Yes, maybe it would be more psr-0-like to call them ".php" instead of ".inc". Undecided about that.

For instance, my own classes typically look like this:

// In [nodeaspect module dir]/lib/PageInfoCache.inc
class nodeaspect_PageInfoCache {..}

// In [nodeaspect module dir]/lib/InjectedAPI/hookNodeaspectPage.inc
class nodeaspect_InjectedAPI_hookNodeaspectPage {..}

// In [dqx_adminmenu module dir]/lib/InjectedAPI/structureAlter.inc
class dqx_adminmenu_InjectedAPI_structureAlter {..}

The module name MUST be all-lowercase, with underscore allowed.
The module name MUST be followed by underscore + a capital letter.
The capitalization of the rest does not matter.
Any further underscores will be turned into directory separators.

Having the last fragment lower camel case is a stupid habit of mine. Irrelevant for the autoloader.

---------

The autoloader has two parts:
1) Active discovery / directory scanning.
2) Passive lookup.

By default, it just does the passive lookups. This will happen for any module, no matter what the info file says.

If the original (D6) autoload module is enabled, it will scan the lib folder of those modules that have a autoload hint in their info file. The result is added to the registry, thus using it as a cache. I suppose this has performance benefits - but that depends on the implementation of the registry.

The module maintainer can now choose if the lib directory should be actively scanned, or just passively looked up.

donquixote’s picture

I should mention that I have not tested this thing in combination with the existing autoload module :)
Probably needs some module weight adjustment, to decide which autoloader should have priority.

donquixote’s picture

Title: PSR-0 style autoload for D7 » pseudo-PSR-0 style autoload for D6 and D7

More accurate title :)

donquixote’s picture

The big question is, should I start a separate module, or rather have this included in the existing autoload module?
And if I go for a separate module, what should the name be? xautoload is a bit meaningless.
I refuse doing the files[] game in any of my own code. It just feels plain stupid.

donquixote’s picture

Is this ever going to be considered for autoload module?
I would hate to fork / make yet another module.

donquixote’s picture

Status: Active » Needs review

See http://drupal.org/sandbox/donquixote/1420754
I mark this as "needs review" to get some attention... please let me know if you want to see a patch.

donquixote’s picture

X Autoload now has a public D7 release.

dave reid’s picture

Yes, I would think this would make a good D7 version of Autoload to support PSR-0.

donquixote’s picture

I am already using xautoload in a number of modules (basically every new module I write, or old module that I revamp), and I am quite happy about it.

This said, none of these modules use PSR-0, they all use a pseudo PEAR standard.
E.g.,
class my_module_Foo_Bar -> "$my_module_dir/lib/Foo/Bar.php"

PSR-0 is implemented, but I don't want to make my D7 modules require PHP 5.3.
Still, this is a huge improvement over the autoload registry, imo. And it will be quite straightforward to port this structure to PSR-0.

If you want to help, you could add some unit tests.
Also some benchmarks to compare with symfony loader would be nice. In theory, uncached xautoload should be faster than symfony, because it does not loop through all namespaces for every non-cached lookup.

sun’s picture

I checked the code of all current class loader modules and did not find one that is a clean and minimal backport of D8's class loader.

Therefore, I created http://drupal.org/project/classloader

Now @Crell made me aware of this issue in #1693466: Duplicate module?

In case there is any intention to throw away all of autoload module's current/custom code and to replace it with the code in classloader module, then I'd be happy to kill that project.

robloach’s picture

Also, why "pseudo-PSR-0"? PSR-0 has large adoption at this point, and is a well-documented standard, with a large pleathura of different class-loaders out there already. Stop beating around the bush and just go straight up PSR-0.

donquixote’s picture

The "pseudo" was from an argument that is now history.
Both xautoload and classloader support straight PSR-0.

classloader is the clean and minimal backport, xautoload is more custom-ish with additional support for other stuff and claims to scale better with many namespaces (needs benchmark).

The question in #11 still holds.

sun’s picture

In case there is any intention to throw away all of autoload module's current/custom code and to replace it with the code in classloader module, then I'd be happy to kill that project.

Any feedback from the Autoload module maintainers on this? @Dave Reid, @Crell?

OTOH, since there is no D7 branch at all yet, we could as well leave autoload alone...

dave reid’s picture

Please see comment #9

sun’s picture

Sorry, but that does not sufficiently answer the question.

The question is whether maintainers have any intention to keep/retain the Autoload module's current functionality, which involves some kind of custom registration mechanism.

If that's not the case, then we can move the classloader project's code into autoload. Which means there is PSR-0 class loader support only.

However, if the old code is to be kept for autoload 7.x-x.x, then I'm not interested in merging classloader into autoload.

donquixote’s picture

Another argument to not make this a D7 of autoload would be to avoid any false assumption that autoload-6.x and classloader-7.x would have anything in common. Because they don't.

dave reid’s picture

Not sure why I'm so hard to understand. The primary purpose of autoload is to backport from the next version of core the class loading functionality. The D7 version would do exactly the same, which is backport from D8, which is a PSR-0 autoloader.

donquixote’s picture

Title: pseudo-PSR-0 style autoload for D6 and D7 » PSR-0 style autoload for D7

Fair point.
Changing issue title.

Crell’s picture

The code in the D6 branch of autoload is a backport of D7's autloader, so yes it's all custom Drupal stuff. :-) Originally we figured there was no need for a D7 version of this module, but using that for a backport of D8's PSR-0 class loader makes total sense to me. There's no reason to keep the D7-ish class loader in the D7 version, because, er, it's already in core. :-)

sun’s picture

Component: Miscellaneous » Code
Category: support » feature
Status: Needs review » Active

Thanks for clarifying! :)

If we want to do this, then I don't think that a patch-based workflow makes sense. I'd simply go ahead and dump classloader's code into a new autoload 7.x-?.x branch.

(The new major version would have to be discussed/clarified. Technically, it should and would have to be 3.x, since the functionality and feature-set is entirely different to 1.x and 2.x.)

The name "autoload" is a bit unfortunate, as it doesn't really clarify that it is about a PHP class loader (and also, since the current classloader module code is backporting the drupal_classloader() function literally), but I also don't really care that much.

We'd definitely have to rewrite (and kinda "split") Autoload's project page to provide a clean description and instructions for D7 as on Class Loader's project page.

But of course, the alternative would still be to just add a pointer to Class Loader on Autoload's project page for people seeking the functionality for D7... Perhaps that's easier/cleaner?

Crell’s picture

I can't speak for Dave, but I have no problem with leveraging Git to bring things back together, and yes 3.x makes total sense. I've mostly stepped out of this module anyway; I just want to see fewer modules in the world. :-)

Crell’s picture

Issue summary: View changes

Link to published xautoload module.