class RobotLoader

Scans directories for PHP classes, interfaces, traits, and enums and autoloads them on demand.

Unlike PSR-4, does not require any naming conventions — classes are discovered by content.

$loader = new Nette\Loaders\RobotLoader;
$loader->addDirectory('app');
$loader->excludeDirectory('app/exclude');
$loader->setCacheDirectory('temp');
$loader->register();

Constants

private RetryLimit

Properties

list<string> $ignoreDirs
list<string> $acceptFiles

Methods

__construct()

No description

__destruct()

Saves the cache if it was modified during the request.

register(bool $prepend = false)

Registers the autoloader via spl_autoload_register().

void
tryLoad(string $type)

Autoloads the requested class, interface, trait, or enum.

addDirectory(string ...$paths)

Adds one or more directories (or individual files) to scan for classes.

reportParseErrors(bool $state = true)

Controls whether PHP parse errors in scanned files are rethrown. Enabled by default.

excludeDirectory(string ...$paths)

Excludes one or more directories (or individual files) from scanning.

array
getIndexedClasses()

Returns all indexed classes with their file paths.

void
rebuild()

Rebuilds the class index from scratch, discarding any previously cached data.

void
refresh()

Loads the cached index and incrementally updates it for any changed files.

setAutoRefresh(bool $state = true)

Enables or disables automatic cache refresh on every autoload attempt. Enabled by default.

setCacheDirectory(string $dir)

Sets the directory for storing the class index cache. Must be an absolute path.

setTempDirectory(string $dir) deprecated

No description

array
generateCacheKey()

No description

Details

at line 60
__construct()

No description

at line 71
__destruct()

Saves the cache if it was modified during the request.

at line 83
RobotLoader register(bool $prepend = false)

Registers the autoloader via spl_autoload_register().

Parameters

bool $prepend

Prepend instead of append to the autoloader stack.

Return Value

RobotLoader

at line 93
void tryLoad(string $type)

Autoloads the requested class, interface, trait, or enum.

Parameters

string $type

Return Value

void

at line 135
RobotLoader addDirectory(string ...$paths)

Adds one or more directories (or individual files) to scan for classes.

Parameters

string ...$paths

Return Value

RobotLoader

at line 145
RobotLoader reportParseErrors(bool $state = true)

Controls whether PHP parse errors in scanned files are rethrown. Enabled by default.

Parameters

bool $state

Return Value

RobotLoader

at line 155
RobotLoader excludeDirectory(string ...$paths)

Excludes one or more directories (or individual files) from scanning.

Parameters

string ...$paths

Return Value

RobotLoader

at line 166
array getIndexedClasses()

Returns all indexed classes with their file paths.

Return Value

array

class => filename

at line 181
void rebuild()

Rebuilds the class index from scratch, discarding any previously cached data.

Return Value

void

at line 195
void refresh()

Loads the cached index and incrementally updates it for any changed files.

Return Value

void

at line 397
RobotLoader setAutoRefresh(bool $state = true)

Enables or disables automatic cache refresh on every autoload attempt. Enabled by default.

Disable in production for better performance; clear the cache manually on deployment.

Parameters

bool $state

Return Value

RobotLoader

at line 407
RobotLoader setCacheDirectory(string $dir)

Sets the directory for storing the class index cache. Must be an absolute path.

Parameters

string $dir

Return Value

RobotLoader

at line 419
RobotLoader setTempDirectory(string $dir) deprecated

deprecated use setCacheDirectory()

No description

Parameters

string $dir

Return Value

RobotLoader

at line 531
protected array generateCacheKey()

No description

Return Value

array