2017 © Pedro Peláez
 

library robot-loader

image

nette/robot-loader

  • Friday, June 22, 2018
  • by david@grudl.com
  • Repository
  • 41 Watchers
  • 203 Stars
  • 2,747,890 Installations
  • PHP
  • 309 Dependents
  • 4 Suggesters
  • 15 Forks
  • 0 Open issues
  • 21 Versions
  • 15 % Grown

The README.md

RobotLoader, (*1)

Downloads this Month Tests Coverage Status Latest Stable Version License, (*2)

Introduction

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

✅ get rid of all require
✅ doesn't require strict naming conventions for directories or files
✅ extremely fast
✅ no manual cache updates, everything runs automatically
✅ mature, stable and widely used library
, (*4)

Thus, we can forget about these familiar code blocks:, (*5)

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

 , (*6)

Support Me

Do you like RobotLoader? Are you looking forward to the new features?, (*7)

Buy me a coffee, (*8)

Thank you!, (*9)

 , (*10)

Installation

You can download RobotLoader as a single standalone file RobotLoader.php, which you include using require in your script, and instantly enjoy comfortable autoloading for the entire application., (*11)

require '/path/to/RobotLoader.php';

$loader = new Nette\Loaders\RobotLoader;
// ...

If you're building an application using Composer, you can install it via:, (*12)

composer require nette/robot-loader

It requires PHP version 8.0 and supports PHP up to 8.4., (*13)

 , (*14)

Usage

Similar to how the Google robot crawls and indexes web pages, the RobotLoader 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:, (*15)

$loader = new Nette\Loaders\RobotLoader;

// Directories for RobotLoader to index (including subdirectories)
$loader->addDirectory(__DIR__ . '/app');
$loader->addDirectory(__DIR__ . '/libs');

// Set caching to the 'temp' directory
$loader->setTempDirectory(__DIR__ . '/temp');
$loader->register(); // Activate RobotLoader

And that's it, from this point on, we don't need to use require. Awesome!, (*16)

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]., (*17)

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

By default, RobotLoader reports errors in PHP files by throwing a ParseError exception. This can be suppressed using $loader->reportParseErrors(false)., (*19)

 , (*20)

PHP Files Analyzer

RobotLoader can also be used purely for finding classes, interfaces, traits and enums in PHP files without using the autoloading function:, (*21)

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

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

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

Even with such usage, you can utilize caching. This ensures that unchanged files won't be rescanned:, (*22)

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

// Set caching to the 'temp' directory
$loader->setTempDirectory(__DIR__ . '/temp');

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

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

 , (*23)

Caching

RobotLoader is very fast because it cleverly uses caching., (*24)

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., (*25)

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., (*26)

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. 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. 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., (*27)

 , (*28)

PSR-4

Nowadays, you can use Composer for 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., (*29)

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., (*30)

If you like RobotLoader, please make a donation now. Thank you!, (*31)

The Versions

22/06 2018

dev-master

9999999-dev https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

trait class nette interface autoload

22/06 2018

v3.0.4

3.0.4.0 https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

trait class nette interface autoload

26/09 2017

v3.0.3

3.0.3.0 https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

trait class nette interface autoload

14/08 2017

v2.4.x-dev

2.4.9999999.9999999-dev https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

trait class nette interface autoload

14/08 2017

v2.4.4

2.4.4.0 https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

trait class nette interface autoload

18/07 2017

v3.0.2

3.0.2.0 https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

trait class nette interface autoload

11/07 2017

v2.4.3

2.4.3.0 https://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

11/07 2017

v3.0.1

3.0.1.0 https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

trait class nette interface autoload

10/02 2017

v3.0.0

3.0.0.0 https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

trait class nette interface autoload

23/01 2017

v3.0.0-RC

3.0.0.0-RC https://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

02/01 2017

v2.4.2

2.4.2.0 https://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

29/09 2016

v2.4.1

2.4.1.0 https://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

17/05 2016

v2.4.0

2.4.0.0 https://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

17/05 2016

v2.3.x-dev

2.3.9999999.9999999-dev https://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

17/05 2016

v2.3.2

2.3.2.0 https://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

06/02 2016

2.2.x-dev

2.2.9999999.9999999-dev https://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

19/07 2015

v2.2.2

2.2.2.0 http://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

11/07 2015

v2.3.1

2.3.1.0 http://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

05/02 2015

v2.3.0

2.3.0.0 http://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

25/05 2014

v2.2.1

2.2.1.0 http://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

11/05 2014

v2.2.0

2.2.0.0 http://nette.org

Nette RobotLoader: comfortable autoloading

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires