Skip to content

Commit 9ac9cd8

Browse files
committed
readme: improved
1 parent 3a947ef commit 9ac9cd8

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

readme.md

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ Introduction
1313

1414
RobotLoader is a tool that gives you comfort of automated class loading for your entire application including third-party libraries.
1515

16-
- get rid of all `require`
17-
- does not require strict directory or file naming conventions
18-
- extremely fast
19-
- no manual cache updates, everything runs automatically
20-
- highly mature, stable and widely used library
16+
get rid of all `require`<br>
17+
✅ doesn't require strict naming conventions for directories or files<br>
18+
extremely fast<br>
19+
no manual cache updates, everything runs automatically<br>
20+
mature, stable and widely used library<br>
2121

22-
So we can forget about those famous code blocks:
22+
Thus, we can forget about these familiar code blocks:
2323

2424
```php
2525
require_once 'Utils/Page.php';
@@ -41,7 +41,16 @@ Thank you!
4141
Installation
4242
------------
4343

44-
The recommended way to install is via Composer:
44+
You can download RobotLoader as a [single standalone file `RobotLoader.php`](https://github.com/nette/robot-loader/raw/standalone/src/RobotLoader/RobotLoader.php), which you include using `require` in your script, and instantly enjoy comfortable autoloading for the entire application.
45+
46+
```php
47+
require '/path/to/RobotLoader.php';
48+
49+
$loader = new Nette\Loaders\RobotLoader;
50+
// ...
51+
```
52+
53+
If you're building an application using [Composer](https://doc.nette.org/en/best-practices/composer), you can install it via:
4554

4655
```shell
4756
composer require nette/robot-loader
@@ -53,79 +62,82 @@ It requires PHP version 8.0 and supports PHP up to 8.3.
5362
Usage
5463
-----
5564

56-
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:
65+
Similar to how the Google robot crawls and indexes web pages, the [RobotLoader](https://api.nette.org/robot-loader/master/Nette/Loaders/RobotLoader.html) goes through all PHP scripts and notes which classes, interfaces, traits and enums it found. It then stores the results in cache for use in subsequent requests. You just need to specify which directories it should go through and where to store the cache:
5766

5867
```php
5968
$loader = new Nette\Loaders\RobotLoader;
6069

61-
// directories to be indexed by RobotLoader (including subdirectories)
70+
// Directories for RobotLoader to index (including subdirectories)
6271
$loader->addDirectory(__DIR__ . '/app');
6372
$loader->addDirectory(__DIR__ . '/libs');
6473

65-
// use 'temp' directory for cache
74+
// Set caching to the 'temp' directory
6675
$loader->setTempDirectory(__DIR__ . '/temp');
67-
$loader->register(); // Run the RobotLoader
76+
$loader->register(); // Activate RobotLoader
6877
```
6978

70-
And that's all. From now on, you don't need to use `require`. Great, isn't it?
79+
And that's it, from this point on, we don't need to use `require`. Awesome!
7180

72-
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).
81+
If RobotLoader encounters a duplicate class name during indexing, it will throw an exception and notify you. RobotLoader also automatically updates the cache when it needs to load an unknown class. We recommend turning this off on production servers, see [#Caching].
7382

74-
If you want RobotLoader to skip some directories, use `$loader->excludeDirectory('temp')` (it can be called multiple times or you can pass multiple directories).
83+
If you want RobotLoader to skip certain directories, use `$loader->excludeDirectory('temp')` (can be called multiple times or pass multiple directories).
7584

76-
By default, RobotLoader reports errors in PHP files by throwing exception `ParseError`. It can be disabled via `$loader->reportParseErrors(false)`.
85+
By default, RobotLoader reports errors in PHP files by throwing a `ParseError` exception. This can be suppressed using `$loader->reportParseErrors(false)`.
7786

7887

7988
PHP Files Analyzer
8089
------------------
8190

82-
RobotLoader can also be used purely to find classes, interfaces, and trait in PHP files **without** using the autoloading feature:
91+
RobotLoader can also be used purely for finding classes, interfaces, traits and enums in PHP files **without** using the autoloading function:
8392

8493
```php
8594
$loader = new Nette\Loaders\RobotLoader;
8695
$loader->addDirectory(__DIR__ . '/app');
8796

88-
// Scans directories for classes / intefaces / traits
97+
// Scans directories for classes/interfaces/traits/enums
8998
$loader->rebuild();
9099

91-
// Returns array of class => filename pairs
100+
// Returns an array of class => filename pairs
92101
$res = $loader->getIndexedClasses();
93102
```
94103

95-
Even with such use, you can use the cache. As a result, unmodified files will not be repeatedly analyzed when rescanning:
104+
Even with such usage, you can utilize caching. This ensures that unchanged files won't be rescanned:
96105

97106
```php
98107
$loader = new Nette\Loaders\RobotLoader;
99108
$loader->addDirectory(__DIR__ . '/app');
109+
110+
// Set caching to the 'temp' directory
100111
$loader->setTempDirectory(__DIR__ . '/temp');
101112

102-
// Scans directories using a cache
113+
// Scans directories using cache
103114
$loader->refresh();
104115

105-
// Returns array of class => filename pairs
116+
// Returns an array of class => filename pairs
106117
$res = $loader->getIndexedClasses();
107118
```
108119

120+
109121
Caching
110122
-------
111123

112-
RobotLoader is very fast because it cleverly uses the cache.
124+
RobotLoader is very fast because it cleverly uses caching.
113125

114-
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.
126+
During development, you hardly notice it running in the background. It continuously updates its cache, considering that classes and files can be created, deleted, renamed, etc. And it doesn't rescan unchanged files.
115127

116-
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.
128+
On a production server, on the other hand, we recommend turning off cache updates using `$loader->setAutoRefresh(false)` (in a Nette Application, this happens automatically), because files don't change. At the same time, it's necessary to **clear the cache** when uploading a new version to hosting.
117129

118-
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).
119-
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.
120-
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.
130+
The initial file scanning, when the cache doesn't exist yet, can naturally take a moment for larger applications. RobotLoader has built-in prevention against [cache stampede](https://en.wikipedia.org/wiki/Cache_stampede).
131+
This is a situation where a large number of concurrent requests on a production server would trigger RobotLoader, and since the cache doesn't exist yet, they would all start scanning files, which would overload the server.
132+
Fortunately, RobotLoader works in such a way that only the first thread indexes the files, creates the cache, and the rest wait and then use the cache.
121133

122134

123135
PSR-4
124136
-----
125137

126-
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`.
138+
Nowadays, you can use [Composer for autoloading](https://doc.nette.org/en/best-practices/composer#toc-autoloading) while adhering to PSR-4. Simply put, it's a system where namespaces and class names correspond to the directory structure and file names, e.g., `App\Router\RouterFactory` will be in the file `/path/to/App/Router/RouterFactory.php`.
127139

128-
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.
140+
RobotLoader isn't tied to any fixed structure, so it's useful in situations where you don't want to have the directory structure designed exactly like the PHP namespaces, or when developing an application that historically doesn't use such conventions. It's also possible to use both loaders together.
129141

130142

131143
If you like RobotLoader, **[please make a donation now](https://nette.org/donate)**. Thank you!

0 commit comments

Comments
 (0)