定制 nette/robot-loader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nette/robot-loader

Composer 安装命令:

composer require nette/robot-loader

包简介

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

README 文档

README

RobotLoader

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

Thus, we can forget about these familiar 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

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.

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

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

If you're building an application using Composer, you can install it via:

composer require nette/robot-loader

It requires PHP version 8.1 and supports PHP up to 8.5.

 

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:

$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->setCacheDirectory(__DIR__ . '/temp');
$loader->register(); // Activate RobotLoader

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

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

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

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

 

PHP Files Analyzer

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

$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:

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

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

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

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

 

Caching

RobotLoader is very fast because it cleverly uses caching.

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.

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.

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.

 

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.

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.

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

nette/robot-loader 适用场景与选型建议

nette/robot-loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 55.15M 次下载、GitHub Stars 达 893, 最近一次更新时间为 2014 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「autoload」 「interface」 「nette」 「class」 「trait」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nette/robot-loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nette/robot-loader 我们能提供哪些服务?
定制开发 / 二次开发

基于 nette/robot-loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 55.15M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 899
  • 点击次数: 32
  • 依赖项目数: 364
  • 推荐数: 3

GitHub 信息

  • Stars: 893
  • Watchers: 37
  • Forks: 34
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2014-03-20