1111use Nette \Utils \FileSystem ;
1212use SplFileInfo ;
1313use function array_merge , defined , extension_loaded , file_get_contents , file_put_contents , filemtime , flock , fopen , function_exists , hash , is_array , is_dir , is_file , realpath , rename , serialize , spl_autoload_register , sprintf , strlen , unlink , var_export ;
14- use const LOCK_EX , LOCK_SH , LOCK_UN , T_CLASS , T_COMMENT , T_DOC_COMMENT , T_ENUM , T_INTERFACE , T_NAME_QUALIFIED , T_NAMESPACE , T_STRING , T_TRAIT , T_WHITESPACE , TOKEN_PARSE ;
1514
1615
1716/**
18- * Nette auto loader is responsible for loading classes and interfaces.
17+ * Scans directories for PHP classes, interfaces, traits, and enums and autoloads them on demand.
18+ * Unlike PSR-4, does not require any naming conventions — classes are discovered by content.
1919 *
2020 * <code>
2121 * $loader = new Nette\Loaders\RobotLoader;
@@ -65,6 +65,9 @@ public function __construct()
6565 }
6666
6767
68+ /**
69+ * Saves the cache if it was modified during the request.
70+ */
6871 public function __destruct ()
6972 {
7073 if ($ this ->needSave ) {
@@ -74,7 +77,8 @@ public function __destruct()
7477
7578
7679 /**
77- * Register autoloader.
80+ * Registers the autoloader via spl_autoload_register().
81+ * @param bool $prepend Prepend instead of append to the autoloader stack.
7882 */
7983 public function register (bool $ prepend = false ): static
8084 {
@@ -84,7 +88,7 @@ public function register(bool $prepend = false): static
8488
8589
8690 /**
87- * Handles autoloading of classes, interfaces or traits .
91+ * Autoloads the requested class, interface, trait, or enum .
8892 */
8993 public function tryLoad (string $ type ): void
9094 {
@@ -126,7 +130,7 @@ public function tryLoad(string $type): void
126130
127131
128132 /**
129- * Add path or paths to list .
133+ * Adds one or more directories (or individual files) to scan for classes .
130134 */
131135 public function addDirectory (string ...$ paths ): static
132136 {
@@ -135,6 +139,9 @@ public function addDirectory(string ...$paths): static
135139 }
136140
137141
142+ /**
143+ * Controls whether PHP parse errors in scanned files are rethrown. Enabled by default.
144+ */
138145 public function reportParseErrors (bool $ state = true ): static
139146 {
140147 $ this ->reportParseErrors = $ state ;
@@ -143,7 +150,7 @@ public function reportParseErrors(bool $state = true): static
143150
144151
145152 /**
146- * Excludes path or paths from list .
153+ * Excludes one or more directories (or individual files) from scanning .
147154 */
148155 public function excludeDirectory (string ...$ paths ): static
149156 {
@@ -153,6 +160,7 @@ public function excludeDirectory(string ...$paths): static
153160
154161
155162 /**
163+ * Returns all indexed classes with their file paths.
156164 * @return array<class-string, string> class => filename
157165 */
158166 public function getIndexedClasses (): array
@@ -168,7 +176,7 @@ public function getIndexedClasses(): array
168176
169177
170178 /**
171- * Rebuilds class list cache .
179+ * Rebuilds the class index from scratch, discarding any previously cached data .
172180 */
173181 public function rebuild (): void
174182 {
@@ -182,7 +190,7 @@ public function rebuild(): void
182190
183191
184192 /**
185- * Refreshes class list cache .
193+ * Loads the cached index and incrementally updates it for any changed files .
186194 */
187195 public function refresh (): void
188196 {
@@ -195,7 +203,7 @@ public function refresh(): void
195203
196204
197205 /**
198- * Refreshes $this->classes & $this->emptyFiles .
206+ * Scans all configured paths and updates the class index, reusing cached mtimes to skip unchanged files .
199207 */
200208 private function refreshClasses (): void
201209 {
@@ -247,8 +255,8 @@ private function refreshClasses(): void
247255
248256
249257 /**
250- * Creates an iterator scanning directory for PHP files and subdirectories .
251- * @throws Nette\IOException if path is not found
258+ * Creates a recursive file iterator for the given directory, applying ignore and exclude filters .
259+ * @throws Nette\IOException If the directory does not exist.
252260 */
253261 private function createFileIterator (string $ dir ): Nette \Utils \Finder
254262 {
@@ -272,6 +280,9 @@ private function createFileIterator(string $dir): Nette\Utils\Finder
272280 }
273281
274282
283+ /**
284+ * Re-scans a single file and updates the class index accordingly.
285+ */
275286 private function updateFile (string $ file ): void
276287 {
277288 foreach ($ this ->classes as $ class => [$ prevFile ]) {
@@ -305,7 +316,7 @@ private function updateFile(string $file): void
305316
306317
307318 /**
308- * Searches classes, interfaces and traits in PHP file.
319+ * Extracts class, interface, trait, and enum names from a PHP file using token parsing .
309320 * @return list<class-string>
310321 */
311322 private function scanPhp (string $ file ): array
@@ -380,7 +391,8 @@ private function scanPhp(string $file): array
380391
381392
382393 /**
383- * Sets auto-refresh mode.
394+ * Enables or disables automatic cache refresh on every autoload attempt. Enabled by default.
395+ * Disable in production for better performance; clear the cache manually on deployment.
384396 */
385397 public function setAutoRefresh (bool $ state = true ): static
386398 {
@@ -390,7 +402,7 @@ public function setAutoRefresh(bool $state = true): static
390402
391403
392404 /**
393- * Sets the directory for storing cache.
405+ * Sets the directory for storing the class index cache. Must be an absolute path .
394406 */
395407 public function setCacheDirectory (string $ dir ): static
396408 {
@@ -411,7 +423,7 @@ public function setTempDirectory(string $dir): static
411423
412424
413425 /**
414- * Loads class list from cache.
426+ * Loads the class index from the cache file, building it from scratch if it does not exist yet .
415427 */
416428 private function loadCache (): void
417429 {
@@ -459,8 +471,8 @@ private function loadCache(): void
459471
460472
461473 /**
462- * Writes class list to cache.
463- * @param ?resource $lock
474+ * Writes the class index to the cache file atomically .
475+ * @param ?resource $lock An already-acquired exclusive lock, or null to acquire one.
464476 */
465477 private function saveCache ($ lock = null ): void
466478 {
@@ -483,6 +495,7 @@ private function saveCache($lock = null): void
483495
484496
485497 /**
498+ * Opens a lock file and acquires a shared or exclusive lock on it.
486499 * @param LOCK_SH|LOCK_EX $mode
487500 * @return resource
488501 */
0 commit comments