Skip to content

Commit 737ff8e

Browse files
committed
readme: updated
1 parent 58a999b commit 737ff8e

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

readme.md

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,11 @@ Introduction
1414
RobotLoader is a tool that gives you comfort of automated class loading for your entire application including third-party libraries.
1515

1616
- get rid of all `require`
17-
- requires no strict file naming conventions
18-
- allows more classes in single file
17+
- does not require strict directory or file naming conventions
1918
- extremely fast
2019
- no manual cache updates, everything runs automatically
2120
- highly mature, stable and widely used library
2221

23-
RobotLoader is incredibly comfortable and addictive!
24-
25-
It requires PHP version 7.1 and supports PHP up to 7.4.
26-
27-
If you like Nette, **[please make a donation now](https://nette.org/donate)**. Thank you!
28-
2922
So we can forget about those famous code blocks:
3023

3124
```php
@@ -35,57 +28,48 @@ require_once 'Utils/Paginator.php';
3528
...
3629
```
3730

38-
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-
4431
Installation
4532
------------
4633

4734
The recommended way to install is via Composer:
4835

49-
```
36+
```shell
5037
composer require nette/robot-loader
5138
```
5239

53-
It requires PHP version 7.1.
40+
It requires PHP version 7.1 and supports PHP up to 7.4.
5441

5542

5643
Usage
5744
-----
5845

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:
6047

6148
```php
6249
$loader = new Nette\Loaders\RobotLoader;
6350

64-
// Add directories for RobotLoader to index
51+
// directories to be indexed by RobotLoader (including subdirectories)
6552
$loader->addDirectory(__DIR__ . '/app');
6653
$loader->addDirectory(__DIR__ . '/libs');
6754

68-
// And set caching to the 'temp' directory
55+
// use 'temp' directory for cache
6956
$loader->setTempDirectory(__DIR__ . '/temp');
7057
$loader->register(); // Run the RobotLoader
7158
```
7259

7360
And that's all. From now on, you don't need to use `require`. Great, isn't it?
7461

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).
7663

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).
8165

8266
By default, RobotLoader reports errors in PHP files by throwing exception `ParseError`. It can be disabled via `$loader->reportParseErrors(false)`.
8367

8468

85-
PHP files analyzer
69+
PHP Files Analyzer
8670
------------------
8771

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:
8973

9074
```php
9175
$loader = new Nette\Loaders\RobotLoader;
@@ -98,7 +82,7 @@ $loader->rebuild();
9882
$res = $loader->getIndexedClasses();
9983
```
10084

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:
10286

10387
```php
10488
$loader = new Nette\Loaders\RobotLoader;
@@ -112,4 +96,26 @@ $loader->refresh();
11296
$res = $loader->getIndexedClasses();
11397
```
11498

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

Comments
 (0)