robot-loader icon indicating copy to clipboard operation
robot-loader copied to clipboard

Get rid of dependencies and make the autoloader self-contained

Open meglio opened this issue 3 years ago • 6 comments

It is an ironic situation that in order to use RobotLoader I have to use composer, so that:

  1. Dependencies are fetched (currently there are two of them - nette/finder and nette/utils)
  2. Dependencies can be autoloaded by the composer's autoloading

In your documentation, you're claiming that RobotLoader can replace composer autoloading, but the only easy way to use it in this mode is to ... install it and autoload it with using composer.

My suggestion is to make RobotLoader a self-contained file with zero dependencies. So then the simplest use case becomes possible: download the single RobotLoader.php file manually, throw it into the source folder, and start using it as the autoloader in the user project.

meglio avatar Dec 09 '20 08:12 meglio

Of course, the robot loader can be modified so that it has no dependencies, but its purpose is not to replace a Composer. Composer autoloading is the best way to autoload the contents of the vendor/ folder.

dg avatar Dec 09 '20 12:12 dg

So, what's the point then - how do I use RobotLoader if I don't use Composer? And why would I use RobotLoader if I do use Composer?

The RobotLoader implies it can be used with or without Composer. But then it turns out that you can't use RobotLoader without composer out of the box. But if I introduce Composer to my project, rarely will it be also the case that I use something else besides the composer itself.

At the end of the day, it is not clear what problem RobotLoader is trying to solve. Could you elaborate a use-case where it will make sense, please?

meglio avatar Dec 10 '20 00:12 meglio

It's like a composer classmap autoloader with automatic reindexing, so you don't need to call composer dump-autoload everytime you add new class.

JanTvrdik avatar Dec 10 '20 08:12 JanTvrdik

This is an over 13-year-old library, so it was created much earlier than Composer and was originally used to autoload all classes. Today, it is standard to install and load libraries by Composer. RobotLoader is used to autoload other classes, such as classes that are part of the application. It can do this better than Composer due to automatic reindexing, see comment above.

RobotLoader can also be used to search for classes in directories without using autoloding feature. https://doc.nette.org/en/3.0/robotloader#toc-php-files-analyzer

It would be possible to get rid of all dependencies, but this is not my priority at the moment, because RobotLoader is not an alternative to the Composer.

dg avatar Dec 10 '20 11:12 dg

@JanTvrdik , in the development mode, I only ever need to composer dump-autoload after installing a new library - not a big deal since I'd be running composer from command line anyway. If my understanding is wrong, please correct me.

Trying to understand what's the point with "automatic reindexing". Is this for when an authoritative classmap is used in composer, i.e.:

"config": {
    "classmap-authoritative": true
}

... if not, then for what cases?

Without the authoritative classmap loading ON, I only ever have to declare my src application folder once, and then the autoloading happens for whatever folders/subfolders I create after that while developing. In production, if I want to go faster, I'd then dump-autoload, but that's (most of the time) part of the CI/CD anyway (not a manual step).

I'm not trying to be unkind, just can't understand why I would use the library and how it can help me, and I still cannot figure it out. I'm happily using some other libraries from Nette. But this one is confusing, and the documentation does not help. It teaches one how to use it, but has no examples of when it would make sense to add it on top of composer.

Hope my question and investigation makes sense to you, and hope you can see the problem I'm trying to describe.

meglio avatar Dec 10 '20 13:12 meglio

classmap autoloader refers to https://getcomposer.org/doc/04-schema.md#classmap (i.e. not PSR-4 and PSR-0)

JanTvrdik avatar Dec 10 '20 13:12 JanTvrdik

Here is standalone version of RobotLoader https://github.com/nette/robot-loader/tree/standalone/src/RobotLoader

dg avatar Sep 26 '23 18:09 dg