diff --git a/src/Exceptions/ServiceNotFoundException.php b/src/Exceptions/ServiceNotFoundException.php new file mode 100644 index 0000000..a39b428 --- /dev/null +++ b/src/Exceptions/ServiceNotFoundException.php @@ -0,0 +1,15 @@ + + * Date: 2017/9/21 + */ + +namespace QL\Exceptions; + +use Exception; + +class ServiceNotFoundException extends Exception +{ + +} \ No newline at end of file diff --git a/src/Kernel.php b/src/Kernel.php index 80d4686..616686e 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -8,8 +8,9 @@ namespace QL; use QL\Contracts\ServiceProviderContract; +use QL\Exceptions\ServiceNotFoundException; use QL\Providers\EncodeServiceProvider; - +use Closure; class Kernel { @@ -17,12 +18,22 @@ class Kernel EncodeServiceProvider::class ]; - protected $binds = []; + protected $binds; + protected $ql; + + /** + * Kernel constructor. + * @param $ql + */ + public function __construct(QueryList $ql) + { + $this->ql = $ql; + $this->binds = collect(); + } public function bootstrap() { $this->registerProviders(); - return $this; } @@ -33,9 +44,17 @@ class Kernel } } - public function bind($name, $provider) + public function bind(string $name,Closure $provider) { - $this->binds[$name] = value($provider); + $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]; } private function register(ServiceProviderContract $instance) diff --git a/src/Providers/EncodeServiceProvider.php b/src/Providers/EncodeServiceProvider.php index ae769f2..19e0da4 100644 --- a/src/Providers/EncodeServiceProvider.php +++ b/src/Providers/EncodeServiceProvider.php @@ -15,8 +15,8 @@ class EncodeServiceProvider implements ServiceProviderContract { public function register(Kernel $kernel) { - $kernel->bind('encoder',function (){ - return new EncodeService(); + $kernel->bind('encoding',function (string $outputEncoding,string $inputEncoding = null){ + return EncodeService::convert($this,$outputEncoding,$inputEncoding); }); } } \ No newline at end of file diff --git a/src/QueryList.php b/src/QueryList.php index 4f17075..a0dac3b 100644 --- a/src/QueryList.php +++ b/src/QueryList.php @@ -28,9 +28,15 @@ class QueryList */ public function __construct() { - $this->kernel = (new Kernel())->bootstrap(); + $this->kernel = (new Kernel($this))->bootstrap(); } + public function __call($name, $arguments) + { + return $this->kernel->getBind($name)->call($this,...$arguments); + } + + /** * @return mixed */ diff --git a/src/Services/EncodeService.php b/src/Services/EncodeService.php index fa16643..2c5a612 100644 --- a/src/Services/EncodeService.php +++ b/src/Services/EncodeService.php @@ -9,5 +9,14 @@ namespace QL\Services; class EncodeService { + public static function convert($ql,string $outputEncoding,string $inputEncoding = null) + { + dump($outputEncoding,$inputEncoding); + return $ql; + } + public static function detect() + { + + } } \ No newline at end of file