Factory.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think;
  13. use InvalidArgumentException;
  14. use think\helper\Str;
  15. abstract class Factory
  16. {
  17. protected $app;
  18. /**
  19. * 驱动
  20. * @var array
  21. */
  22. protected $drivers = [];
  23. /**
  24. * 驱动的命名空间
  25. * @var string
  26. */
  27. protected $namespace = "";
  28. public function __construct(App $app)
  29. {
  30. $this->app = $app;
  31. }
  32. /**
  33. * 获取驱动实例
  34. * @param null $driver
  35. * @return mixed
  36. */
  37. public function driver($driver = null)
  38. {
  39. $driver = $driver ?: $this->getDefaultDriver();
  40. if (is_null($driver)) {
  41. throw new InvalidArgumentException(sprintf(
  42. 'Unable to resolve NULL driver for [%s].', static::class
  43. ));
  44. }
  45. if (!isset($this->drivers[$driver])) {
  46. $this->drivers[$driver] = $this->createDriver($driver);
  47. }
  48. return $this->drivers[$driver];
  49. }
  50. /**
  51. * 获取驱动类
  52. * @param $driver
  53. * @return string
  54. */
  55. protected function resolveClass($driver)
  56. {
  57. if ($this->namespace || false !== strpos($driver, '\\')) {
  58. $class = false !== strpos($driver, '\\') ? $driver : $this->namespace . Str::studly($driver);
  59. if (class_exists($class)) {
  60. return $class;
  61. }
  62. }
  63. throw new InvalidArgumentException("Driver [$driver] not supported.");
  64. }
  65. /**
  66. * 创建驱动
  67. *
  68. * @param string $driver
  69. * @return mixed
  70. *
  71. */
  72. protected function createDriver($driver)
  73. {
  74. $method = 'create' . Str::studly($driver) . 'Driver';
  75. if (method_exists($this, $method)) {
  76. return $this->$method();
  77. }
  78. $class = $this->resolveClass($driver);
  79. return $this->app->make($class);
  80. }
  81. /**
  82. * 默认驱动
  83. * @return string
  84. */
  85. abstract public function getDefaultDriver();
  86. /**
  87. * 动态调用
  88. * @param string $method
  89. * @param array $parameters
  90. * @return mixed
  91. */
  92. public function __call($method, $parameters)
  93. {
  94. return $this->driver()->$method(...$parameters);
  95. }
  96. }