You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Like the Google robot crawls and indexes websites, RobotLoader crawls all PHP scripts and records what classes and interfaces were found in them.
39
-
These records are then saved in cache and used during all subsequent requests.
40
-
41
-
Documentation can be found on the [website](https://doc.nette.org/robotloader).
42
-
43
-
44
31
Installation
45
32
------------
46
33
47
34
The recommended way to install is via Composer:
48
35
49
-
```
36
+
```shell
50
37
composer require nette/robot-loader
51
38
```
52
39
53
-
It requires PHP version 7.1.
40
+
It requires PHP version 7.1 and supports PHP up to 7.4.
54
41
55
42
56
43
Usage
57
44
-----
58
45
59
-
You just need to specifiy what directories to index and where to save the cache:
46
+
Like the Google robot crawls and indexes websites, [RobotLoader](https://api.nette.org/3.0/Nette/Loaders/RobotLoader.html) crawls all PHP scripts and records what classes and interfaces were found in them. These records are then saved in cache and used during all subsequent requests. You just need to specify what directories to index and where to save the cache:
60
47
61
48
```php
62
49
$loader = new Nette\Loaders\RobotLoader;
63
50
64
-
// Add directories for RobotLoader to index
51
+
// directories to be indexed by RobotLoader (including subdirectories)
65
52
$loader->addDirectory(__DIR__ . '/app');
66
53
$loader->addDirectory(__DIR__ . '/libs');
67
54
68
-
// And set caching to the 'temp' directory
55
+
// use 'temp' directory for cache
69
56
$loader->setTempDirectory(__DIR__ . '/temp');
70
57
$loader->register(); // Run the RobotLoader
71
58
```
72
59
73
60
And that's all. From now on, you don't need to use `require`. Great, isn't it?
74
61
75
-
When RobotLoader encounters duplicate class name during indexing, it throws an exception and informs you about it.
62
+
When RobotLoader encounters duplicate class name during indexing, it throws an exception and informs you about it. RobotLoader also automatically updates the cache when it has to load a class it doesn't know. We recommend disabling this on production servers, see [Caching](#Caching).
76
63
77
-
The `$loader->setAutoRefresh(true or false)` determines whether RobotLoader should reindex files if asked for nonexistent class.
78
-
This feature should be disabled on production server.
79
-
80
-
If you want RobotLoader to skip some directory, use `$loader->excludeDirectory('temp')`.
64
+
If you want RobotLoader to skip some directories, use `$loader->excludeDirectory('temp')` (it can be called multiple times or you can pass multiple directories).
81
65
82
66
By default, RobotLoader reports errors in PHP files by throwing exception `ParseError`. It can be disabled via `$loader->reportParseErrors(false)`.
83
67
84
68
85
-
PHP files analyzer
69
+
PHP Files Analyzer
86
70
------------------
87
71
88
-
RobotLoader can also be used to find classes, interfaces, and trait in PHP files without using the autoloading feature:
72
+
RobotLoader can also be used purely to find classes, interfaces, and trait in PHP files **without** using the autoloading feature:
89
73
90
74
```php
91
75
$loader = new Nette\Loaders\RobotLoader;
@@ -98,7 +82,7 @@ $loader->rebuild();
98
82
$res = $loader->getIndexedClasses();
99
83
```
100
84
101
-
When scanning files again, we can use the cache and unmodified files will not be analyzed repeatedly:
85
+
Even with such use, you can use the cache. As a result, unmodified files will not be repeatedly analyzed when rescanning:
102
86
103
87
```php
104
88
$loader = new Nette\Loaders\RobotLoader;
@@ -112,4 +96,26 @@ $loader->refresh();
112
96
$res = $loader->getIndexedClasses();
113
97
```
114
98
115
-
Enjoy RobotLoader!
99
+
Caching
100
+
-------
101
+
102
+
RobotLoader is very fast because it cleverly uses the cache.
103
+
104
+
When developing with it, you have practically no idea that it runs on the background. It continuously updates the cache because it knows that classes and files can be created, deleted, renamed, etc. And it doesn't repeatedly scan unmodified files.
105
+
106
+
When used on a production server, on the other hand, we recommend disabling the cache update using `$loader->setAutoRefresh(false)`, because the files are not changing. At the same time, it is necessary to **clear the cache** when uploading a new version on the hosting.
107
+
108
+
Of course, the initial scanning of files, when the cache does not already exist, may take a few seconds for larger applications. RobotLoader has built-in prevention against [cache stampede](https://en.wikipedia.org/wiki/Cache_stampede).
109
+
This is a situation where production server receives a large number of concurrent requests and because RobotLoader's cache does not yet exist, they would all start scanning the files. Which spikes CPU and filesystem usage.
110
+
Fortunately, RobotLoader works in such a way that for multiple concurrent requests, only the first thread indexes the files, creates a cache, the others wait, and then use the cache.
111
+
112
+
113
+
PSR-4
114
+
-----
115
+
116
+
Today, Composer can be used for autoloading in compliance with PSR-4. Simply saying, it is a system where the namespaces and class names correspond to the directory structure and file names, ie `App\Router\RouterFactory` is located in the file `/path/to/App/Router/RouterFactory.php`.
117
+
118
+
RobotLoader is not tied to any fixed structure, therefore, it is useful in situations where it does not suit you to have the directory structure designed as namespaces in PHP, or when you are developing an application that has historically not used such conventions. It is also possible to use both loaders together.
119
+
120
+
121
+
If you like RobotLoader, **[please make a donation now](https://nette.org/donate)**. Thank you!
0 commit comments