QueryList/src/Kernel.php

66 lines
1.3 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-21 02:20:28 +08:00
class Kernel
{
protected $providers = [
EncodeServiceProvider::class
];
2017-09-21 13:12:20 +08:00
protected $binds;
protected $ql;
/**
* Kernel constructor.
* @param $ql
*/
public function __construct(QueryList $ql)
{
$this->ql = $ql;
$this->binds = collect();
}
2017-09-21 02:20:28 +08:00
public function bootstrap()
{
$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;
}
public function getBind(string $name)
{
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);
}
}