So I'm on D7, and trying to get my head around the autoloading challenges.

Do composer_manager and xautoload work together, or are they different solutions to the same problem, and should I choose one and then use it?

Next, do I need even straight composer for xautoload, or xautoload doing what composer does?

Finally, if these are different solutions, could somebody perhaps briefly explain what the advantages/disadvantages of each approach?

Thanks!

Comments

donquixote’s picture

Both modules have their right to exist.

It is a question of how you want to organize your modules and libraries.
Or if you are developing a contrib module, it is a question of which dependency you are willing to add to it.

xautoload can help you with autoloading for 3rd party libraries, but it does not help you to download these libraries. The downloading has to be done with e.g. Libraries API.

Using xautoload for modules in Drupal 7 feels a bit like you are writing a Drupal 8 module. If you use the PSR-4 file structure of D8 modules, then you don't need to tell the module where your class files are.
Using composer manager, it feels more like you are writing a library for Composer. Without a composer.json in your module, composer manager won't do anything.

xautoload is only for class loading.
composer_manager does also handle dependency management, so that modules can depend on 3rd party libraries.
Maybe composer_manager is a bit more fragile than xautoload (last time I checked, but this is long ago), but this is only because it does more "adventurous" stuff. 3rd party dependency management with Drupal 7 modules is a hard chicken-and-egg problem.

xautoload works out of the box, and does not really restrict you in your usual Drupal ways of doing things. Except the PHP version requirement.
If you want to enable a contrib module with a dependency to xautoload, "drush en -y MODULENAME" will download both this module and xautoload, enable both, and everything will be fine.

With composer_manager, afaik, you need a bit more manual configuration, and think about how you download your modules.

Both modules can be used together on one site without conflict. But each class will be loaded either by the Composer class loader, or by the xautoload class loader. They work side by side, but not hand in hand.

There is also a 3rd option which I learned on a conference session a while ago, where you need neither xautoload nor composer_manager. Instead you work with Composer itself. But this requires that you rethink your file structure, and the way you work.

------

Can you explain your own use case?

donquixote’s picture

donquixote’s picture

Status: Active » Closed (fixed)