QueryList/src/Kernel.php

74 lines
1.6 KiB
PHP
Raw Normal View History

2017-09-21 02:20:28 +08:00
<?php
/**
* Created by PhpStorm.
* User: Jaeger <JaegerCode@gmail.com>
* Date: 2017/9/21
*/
namespace QL;
use QL\Contracts\ServiceProviderContract;
2017-09-21 13:12:20 +08:00
use QL\Exceptions\ServiceNotFoundException;
2017-09-21 02:20:28 +08:00
use QL\Providers\EncodeServiceProvider;
2017-09-21 13:12:20 +08:00
use Closure;
2017-09-22 02:38:46 +08:00
use QL\Providers\HttpServiceProvider;
2017-09-22 19:09:43 +08:00
use QL\Providers\PluginServiceProvider;
2017-09-22 02:38:46 +08:00
use QL\Providers\SystemServiceProvider;
2020-04-03 17:33:32 +08:00
use Tightenco\Collect\Support\Collection;
2017-09-21 02:20:28 +08:00
class Kernel
{
protected $providers = [
2017-09-22 02:38:46 +08:00
SystemServiceProvider::class,
HttpServiceProvider::class,
2017-09-22 19:09:43 +08:00
EncodeServiceProvider::class,
PluginServiceProvider::class
2017-09-21 02:20:28 +08:00
];
2017-09-21 13:12:20 +08:00
protected $binds;
protected $ql;
/**
* Kernel constructor.
* @param $ql
*/
public function __construct(QueryList $ql)
{
$this->ql = $ql;
2020-04-03 17:33:32 +08:00
$this->binds = new Collection();
2017-09-21 13:12:20 +08:00
}
2017-09-21 02:20:28 +08:00
public function bootstrap()
{
2017-09-22 19:09:43 +08:00
//注册服务提供者
2017-09-21 02:20:28 +08:00
$this->registerProviders();
return $this;
}
public function registerProviders()
{
foreach ($this->providers as $provider) {
$this->register(new $provider());
}
}
2017-09-21 13:12:20 +08:00
public function bind(string $name,Closure $provider)
2017-09-21 02:20:28 +08:00
{
2017-09-21 13:12:20 +08:00
$this->binds[$name] = $provider;
}
2017-09-22 01:51:46 +08:00
public function getService(string $name)
2017-09-21 13:12:20 +08:00
{
if(!$this->binds->offsetExists($name)){
throw new ServiceNotFoundException("Service: {$name} not found!");
}
return $this->binds[$name];
2017-09-21 02:20:28 +08:00
}
private function register(ServiceProviderContract $instance)
{
$instance->register($this);
}
}