Skip to content

Commit

Permalink
增加驱动管理类
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Jul 10, 2019
1 parent 35887c3 commit 88b9f05
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
],
"require": {
"php": ">=7.1.0",
"psr/container": "~1.0"
"psr/container": "~1.0",
"topthink/think-helper": "^3.1"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
Expand All @@ -22,4 +23,4 @@
"files": [
]
}
}
}
3 changes: 3 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ protected function bindParams($reflect, array $vars = []): array
/**
* 字符串命名风格转换
* type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
* @deprecated
* @access public
* @param string $name 字符串
* @param integer $type 转换类型
Expand All @@ -490,6 +491,7 @@ public static function parseName(string $name = null, int $type = 0, bool $ucfir

/**
* 获取类名(不包含命名空间)
* @deprecated
* @access public
* @param string|object $class
* @return string
Expand All @@ -502,6 +504,7 @@ public static function classBaseName($class): string

/**
* 创建工厂对象实例
* @deprecated
* @access public
* @param string $name 工厂类名
* @param string $namespace 默认命名空间
Expand Down
124 changes: 124 additions & 0 deletions src/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <[email protected]>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace think;

use InvalidArgumentException;
use think\helper\Str;

abstract class Manager
{
/**
* 驱动
* @var array
*/
protected $drivers = [];

/**
* 驱动的命名空间
* @var string
*/
protected $namespace = "";

/**
* 获取驱动实例
* @param null|string $name
* @return mixed
*/
public function driver($name = null)
{
$name = $name ?: $this->getDefaultDriver();

if (is_null($name)) {
throw new InvalidArgumentException(sprintf(
'Unable to resolve NULL driver for [%s].', static::class
));
}

return $this->drivers[$name] = $this->get($name);
}

/**
* 获取驱动实例
* @param $name
* @return mixed
*/
protected function get($name)
{
return $this->drivers[$name] ?? $this->createDriver($name);
}

/**
* 获取驱动类
* @param $driver
* @return string
*/
protected function resolveClass($driver)
{
if ($this->namespace || false !== strpos($driver, '\\')) {
$class = false !== strpos($driver, '\\') ? $driver : $this->namespace . Str::studly($driver);

if (class_exists($class)) {
return $class;
}
}

throw new InvalidArgumentException("Driver [$driver] not supported.");
}

/**
* 创建驱动
*
* @param string $driver
* @return mixed
*
*/
protected function createDriver($driver)
{
$method = 'create' . Str::studly($driver) . 'Driver';

if (method_exists($this, $method)) {
return $this->$method();
}

$class = $this->resolveClass($driver);

return $this->resolve($class);
}

/**
* 实例化驱动
* @param mixed ...$args
* @return mixed
*/
protected function resolve(...$args)
{
return Container::getInstance()->make(...$args);
}

/**
* 默认驱动
* @return string
*/
abstract public function getDefaultDriver();

/**
* 动态调用
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->driver()->$method(...$parameters);
}
}

0 comments on commit 88b9f05

Please sign in to comment.