biurad / 法兰盘
一个快速、可编译和可扩展的PHP框架
v1.1.3
2019-03-30 05:54 UTC
Requires
- php: >=5.6.0
- maximebf/debugbar: >=1.0.0
README
PHP法兰盘框架
法兰盘是一个基于PSR-7、PSR-11、PSR-14和PSR-15的PHP 8.0+框架,支持注解/属性,由Divine Niiquaye创建,是一个非常快速、可编译和可扩展的框架。
这个库包含了许多适合开发者开发Web应用程序的功能。法兰盘真正可扩展,依赖项少,性能高。
法兰盘v2.0处于早期开发阶段,不建议用于生产环境。
📦 安装与基本用法
此项目需要PHP 8.0或更高版本。推荐使用Composer安装。只需运行
$ composer require biurad/flange 2.0.*
法兰盘基于Flight Routing、Biurad DI、Symfony组件和Biurad库构建。法兰盘是一个完全遵守PSR的PHP框架,可完全自定义,甚至可以用于开发从小到大的项目。
require_once __DIR__ . '/vendor/autoload.php'; // Boot the application. $app = new Flange\Application(); // Add a route to application $app->match('/hello/{name:\w+}', to: fn (string $name): string => 'Hello ' . $app->escape()->escapeHtml($name)); $extensions = [ [Flange\Extensions\CoreExtension::class, [__DIR__]], // You can add more extensions here ... ]; //If you want to use extensions, here is an example: $app->loadExtensions($extensions, ['config' => '%project_dir%/config']); // You can set custom pages for caught exceptions, using default event dispatcher, or your custom event dispatcher. $app->getDispatcher()?->addListener(Flange\Events::EXCEPTION, new ErrorListener(), -8); $app->run();
在大型项目中工作!建议使用应用程序的缓存版本。这比使用带有扩展的不可缓存应用程序类提供了超过60% - 100%的性能提升。
use function Rade\DI\Loader\{param, phpCode, wrap}; $config = [ 'cacheDir' => __DIR__ . '/caches', 'debug' => $_ENV['APP_DEBUG'] ?? false, // Set the debug mode environment ]; // Setup cache for application. $app = Flange\AppBuilder::build(static function (Flange\AppBuilder $creator): void { // Add resource to re-compile if changes are made to this file. $creator->addResource(new FileResource(__FILE__)); // Adding routes requires the Rade\DI\Extensions\RoutingExtension to be loaded. // Routes should always be added before Rade\DI\Extensions\RoutingExtension is booted, else it will not be compiled. $creator->match('/hello/{name:\w+}', to: phpCode('fn (string $name): string => \'Hello \' . $this->escape()->escapeHtml($name);')); $extensions = [ [Flange\Extensions\CoreExtension::class, [__DIR__]], // You can add more extensions here ... ]; //If you want to use extensions, here is an example as its recommended to use extensions to build your application. $creator->loadExtensions($extensions, ['config' => '%project_dir%/config']); // You can set custom pages for caught exceptions, using default event dispatcher, or your custom event dispatcher. $creator->definition('events.dispatcher')->bind('addListener', [Flange\Events::EXCEPTION, wrap(ErrorListener::class), -8]); }, $config); $app->run(); // Boot the application.
以下是一个你可以为你的应用程序使用的自定义错误示例。
use Biurad\Http\Response\HtmlResponse; use Flange\Event\ExceptionEvent; class ErrorListener { public function __invoke(ExceptionEvent $event): void { // If extensions were loaded, the %project_dir% will exist, else replace will absolute path $errorsPath = $event->getApplication()->parameter('%project_dir%/errors/'); $code = $event->getThrowable()->getCode(); $templates = [ $errorsPath . \substr($code, 0, 2) . 'x.html.php', // 40x.html.php format ... $errorsPath . \substr($code, 0, 1) . 'xx.html.php', // 4xx.html.php format ... $errorsPath . $code . '.html.php', // 404.html.php format ... $errorsPath . 'default.html.php', ]; // Tries to load a template file from a list of error templates. foreach ($template as $template) { if (\file_exists($template)) { $event->setResponse( (static function () use ($template, $code) { \ob_start(); include __DIR__ . $template; return new HtmlResponse(\ob_get_clean(), (int) $code); })() ); } } } }
值得注意的是,使用PSR-15中间件堆栈时,使用PHP SPL队列类,算法如下:LAST <- FIRST : FIRST -> LAST。默认情况下加载扩展和事件监听器使用优先级堆叠算法(这意味着优先级越高,扩展或事件监听器在链中的触发越早),默认为0。
📓 文档
关于如何使用此库的详细文档,请参阅此库的文档。还建议浏览tests目录中的单元测试。
🙌 赞助者
如果这个库已经进入你的项目,或者你对支持我们感兴趣,请考虑捐赠以支持未来的开发。
👥 致谢与认可
- Divine Niiquaye Ibok是这个库的作者。
- 所有贡献者为该项目做出贡献。
📄 许可证
Flange 完全免费,并按照 BSD 3 许可证发布。