• Stars
    star
    871
  • Rank 52,385 (Top 2 %)
  • Language
    PHP
  • License
    Other
  • Created over 10 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🍀 RobotLoader: high performance and comfortable autoloader that will search and autoload classes within your application.

RobotLoader: comfortable autoloading

Downloads this Month Tests Coverage Status Latest Stable Version License

Introduction

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

  • get rid of all require
  • does not require strict directory or file naming conventions
  • extremely fast
  • no manual cache updates, everything runs automatically
  • highly mature, stable and widely used library

So we can forget about those famous code blocks:

require_once 'Utils/Page.php';
require_once 'Utils/Style.php';
require_once 'Utils/Paginator.php';
...

Support Me

Do you like RobotLoader? Are you looking forward to the new features?

Buy me a coffee

Thank you!

Installation

The recommended way to install is via Composer:

composer require nette/robot-loader

It requires PHP version 8.0 and supports PHP up to 8.2.

Usage

Like the Google robot crawls and indexes websites, RobotLoader 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:

$loader = new Nette\Loaders\RobotLoader;

// directories to be indexed by RobotLoader (including subdirectories)
$loader->addDirectory(__DIR__ . '/app');
$loader->addDirectory(__DIR__ . '/libs');

// use 'temp' directory for cache
$loader->setTempDirectory(__DIR__ . '/temp');
$loader->register(); // Run the RobotLoader

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

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.

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

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

PHP Files Analyzer

RobotLoader can also be used purely to find classes, interfaces, and trait in PHP files without using the autoloading feature:

$loader = new Nette\Loaders\RobotLoader;
$loader->addDirectory(__DIR__ . '/app');

// Scans directories for classes / intefaces / traits
$loader->rebuild();

// Returns array of class => filename pairs
$res = $loader->getIndexedClasses();

Even with such use, you can use the cache. As a result, unmodified files will not be repeatedly analyzed when rescanning:

$loader = new Nette\Loaders\RobotLoader;
$loader->addDirectory(__DIR__ . '/app');
$loader->setTempDirectory(__DIR__ . '/temp');

// Scans directories using a cache
$loader->refresh();

// Returns array of class => filename pairs
$res = $loader->getIndexedClasses();

Caching

RobotLoader is very fast because it cleverly uses the cache.

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.

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.

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

PSR-4

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.

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.

If you like RobotLoader, please make a donation now. Thank you!

More Repositories

1

php-generator

🐘 Generates neat PHP code for you. Supports new PHP 8.3 features.
PHP
2,109
star
2

utils

🛠 Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
PHP
1,988
star
3

tracy

😎 Tracy: the addictive tool to ease debugging PHP code for cool developers. Friendly design, logging, profiler, advanced features like debugging AJAX calls or CLI support. You will love it.
PHP
1,755
star
4

nette

👪 METAPACKAGE for Nette Framework components
PHP
1,531
star
5

latte

☕ Latte: the safest & truly intuitive templates for PHP. Engine for those who want the most secure PHP sites.
PHP
1,137
star
6

finder

🔍 Finder: find files and directories with an intuitive API.
932
star
7

neon

🍸 Encodes and decodes NEON file format.
PHP
904
star
8

schema

📐 Validating data structures against a given Schema.
PHP
896
star
9

di

💎 Flexible, compiled and full-featured Dependency Injection Container with perfectly usable autowiring and support for all new PHP 8 features.
PHP
869
star
10

bootstrap

🅱 The simple way to configure and bootstrap your Nette application.
PHP
666
star
11

database

💾 A database layer with a familiar PDO-like API but much more powerful. Building queries, advanced joins, drivers for MySQL, PostgreSQL, SQLite, MS SQL Server and Oracle.
PHP
507
star
12

forms

📝 Generating, validating and processing secure forms in PHP. Handy API, fully customizable, server & client side validation and mature design.
PHP
495
star
13

mail

A handy library for creating and sending emails in PHP
PHP
466
star
14

tester

Tester: enjoyable unit testing in PHP with code coverage reporter. 🍏🍏🍎🍏
PHP
460
star
15

http

🌐 Abstraction for HTTP request, response and session. Provides careful data sanitization and utility for URL and cookies manipulation.
PHP
456
star
16

caching

⏱ Caching library with easy-to-use API and many cache backends.
PHP
411
star
17

application

🏆 A full-stack component-based MVC kernel for PHP that helps you write powerful and modern web applications. Write less, have cleaner code and your work will bring you joy.
PHP
407
star
18

security

🔑 Provides authentication, authorization and a role-based access control management via ACL (Access Control List)
PHP
354
star
19

component-model

⚛ Component model foundation for Nette.
PHP
266
star
20

routing

Nette Routing: two-ways URL conversion
PHP
236
star
21

tokenizer

[DISCONTINUED] Source code tokenizer
PHP
141
star
22

sandbox

141
star
23

docs

📖 The Nette documentation
116
star
24

safe-stream

SafeStream: atomic and safe manipulation with files via native PHP functions.
PHP
115
star
25

web-project

Standard Web Project: a simple skeleton application using the Nette
Latte
106
star
26

reflection

[DISCONTINUED] Docblock annotations parser and common reflection classes
PHP
92
star
27

examples

🎓 Examples demonstrating the Nette Framework.
88
star
28

code-checker

✅ A simple tool to check source code against a set of Nette coding standards.
PHP
84
star
29

web-addons.nette.org

[DISCONTINUED] Website https://addons.nette.org source code.
PHP
55
star
30

coding-standard

Nette Coding Standard code checker & fixer
PHP
39
star
31

command-line

⌨ Command line options and arguments parser.
PHP
36
star
32

type-fixer

🆙 A tool to automatically update typehints in your code.
PHP
29
star
33

resources

Client-side resources for Nette Framework.
23
star
34

latte-tools

Twig & HTML to Latte converters
PHP
22
star
35

grunt-nette-tester

Grunt plugin for Nette Tester
JavaScript
20
star
36

middleware

PHP
20
star
37

deprecated

[DISCONTINUED] APIs and features removed from Nette Framework
PHP
19
star
38

safe

🛡 PHP functions smarten up to throw exceptions instead of returning false or triggering errors.
PHP
17
star
39

nette-minified

[DISCONTINUED] Minified version of Nette Framework.
PHP
16
star
40

tutorial-todo

[DISCONTINUED] Tutorial for simple task manager.
PHP
10
star
41

union

[READ-ONLY] Subtree union of Nette repositories
PHP
7
star
42

assistant

PHP
3
star
43

.github

1
star