haihv433 / sagoboot
SagoBoot | 可扩展PHP应用程序的迷你框架
Requires
- php: ~7.0
README
SagoBoot | 可扩展PHP应用程序的迷你框架
使用此包或其他包时,无需遵循任何原则,只需让你的思维自由发挥
关键档案
SagoBoot,设计得足够小巧,可以注入到任何复杂且不高度架构化的系统中,如WordPress、Drupal等,用于主题和插件。
SagoBoot,设计用于更高效地扩展,通过使用Composer包
和PSR-4: Autoloader
,无需额外配置,即可与Symfony
或Zend
等其他框架结合使用,进行首次路由或析构等操作。
SagoBoot,设计用于易于使用、快速启动、灵活且兼容任何想法和项目。
安装
通过Composer
安装
composer require haihv433/sagoboot
或直接从github
克隆仓库
git clone git@github.com:haihv433/sagoboot.git
应用程序和加载器
创建自己的应用程序时,最重要的一件事是App
类管理器,它持有所有实例
您的应用程序管理器需要继承自\SagoBoot\Application
在您的bootstrap.php
中,您可以在应用程序启动时控制操作,例如添加Autoloader
// Initlize your application
$app = new \SagoBoot\Sample\App\App();
// Set your new class as App manager
// that then, your system will cheat \SagoBoot\Sample\App as a Manager instead of \SagoBoot\Application
$app->bind('SagoBoot\Sample\App', 'App', 1);
// SagoBoot do call your Loader when system boot
// Before that, you can add the logic more
$app->addEvent('boot', function ( \SagoBoot\Sample\App\Loader $loader) use ($app) {
$app->make('Autoload')->addLoader($loader);
});
查看您的示例加载器\SagoBoot\Sample\App\Loader
// This method will call automatically when system boot
public function autoload(Modules\Loader $loader)
{
$loader->loadPath(__DIR__ . '/../autoload.php');
}
如您所见,Loader
使用一个名为autoload.php
的文件,请查看以下这些行
[
'abstract' => \SagoBoot\Sample\Car\Car::class,
'make' => false,
'singleton' => true,
'aliases' => [
\SagoBoot\Sample\Car\CarInterface::class
]
]
该文件包含一个数组,每个元素都有一个配置
abstract
:定义您的类
make
:如果您想系统自动构建类,则设置true
,否则不设置。
singleton
:将实例设置为Singleton,允许以相同的方式访问
aliases
:您可以使用aliases
模式隐藏实际逻辑,请查看类\SagoBoot\Sample\Car\CarInterface
的示例。
最后,不要忘记启动您的系统。
$app = require_once(__DIR__ . "/sample/bootstrap.php");
// Now booting
$app->boot();
依赖注入
现在,如果您已经完成了应用程序的声明,就无需再构建类,只需注入您需要的任何类即可
示例类MakeCar
namespace SagoBoot\Sample;
class MakeCar
{
public function __construct(
\SagoBoot\Sample\Garage $garage,
\SagoBoot\Sample\Car\CarInterface $car
)
{
}
}
示例类Garage
namespace SagoBoot\Sample;
class Garage
{
/**
* @var \SagoBoot\Sample\Car\CarInterface
*/
protected $car;
public function __construct(
\SagoBoot\Sample\Car\CarInterface $car
)
{
$this->car = $car;
}
}
如您所见,创建类时无需添加new
关键字,这已在autoload.php
中设置。
应用程序还会检查aliases
以从实际类到别名的反射
在这种情况下,调用接口\SagoBoot\Sample\Car\CarInterface
将返回\SagoBoot\Sample\Car\Car
实例
Singleton
如果您在autoload.php
中将类设置为singleton
或从\SagoBoot\Support\Singleton
实现,则应用程序将将其视为Singleton
对象,这意味着调用或注入将返回相同的对象。
请参见MakeCar.php::__construct()
$car->start()
->observerState()
->addFuel()
->observerState()
->stop();
$garage->maintenanceCar();
$car->observerState();
通过运行命令php index.sample.php
输出
Your car is: RUNNING.
Your car is: REFUEL.
Your car is: RUNNING.
Your car is: MAINTENANCE.
The car was destroyed.
对于调用Car
类的实例,以下方法可能很有用
- 在构造函数中注入接口
\SagoBoot\Sample\Car\CarInterface
- 调用
\SagoBoot\Sample\App\App::getInstance()->make(\SagoBoot\Sample\Car\CarInterface::class)
sgb_app(\SagoBoot\Sample\Car\CarInterface::class)
(检查API)
事件
如果你注意到了,你的应用只有在调用 $app->boot()
时才会启动,请更仔细地查看这个方法。
$this->make('EventsHelper')->fire('boot');
boot
事件是为启动设计的,因此,你可以在系统启动前后添加自定义逻辑,请参阅这里。
$app->addEvent('your_custome_event', function() {
// Your logical
});
$app->addEvent('boot', function() {
$this->make('EventsHelper')->fire('your_custome_event');
})
API 功能
为了更方便地与你的应用交互,请检查 helpers.php
以获取更多选项。
- 创建或检索实例
sgb_app()
- 调用一个
Helper
对象sgb_helper()
,(任何以Helper
关键字结尾的类) - 事件
sgb_event()
和sgb_add_event()
- 过滤器
sgb_filter
和sgb_add_filter()
实用
你喜欢设计模式及其实现吗?这些文章可能很有用。
实现 SagoBoot
时,无论使用任何模式,如工厂、代理、仓库等,都没有原则,你已经有一个骨架,只需这样做!!!