Driver.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\common\library\storage;
  3. use think\Exception;
  4. /**
  5. * 存储模块驱动
  6. * Class driver
  7. * @package app\common\library\storage
  8. */
  9. class Driver
  10. {
  11. private $config; // upload 配置
  12. private $engine; // 当前存储引擎类
  13. /**
  14. * 构造方法
  15. * Driver constructor.
  16. * @param $config
  17. * @param null|string $storage 指定存储方式,如不指定则为系统默认
  18. * @throws Exception
  19. */
  20. public function __construct($config, $storage = null)
  21. {
  22. $this->config = $config;
  23. // 实例化当前存储引擎
  24. $this->engine = $this->getEngineClass($storage);
  25. }
  26. /**
  27. * 设置上传的文件信息
  28. * @param string $name
  29. * @return mixed
  30. */
  31. public function setUploadFile($name = 'iFile')
  32. {
  33. return $this->engine->setUploadFile($name);
  34. }
  35. /**
  36. * 设置上传的文件信息
  37. * @param string $filePath
  38. * @return mixed
  39. */
  40. public function setUploadFileByReal($filePath)
  41. {
  42. return $this->engine->setUploadFileByReal($filePath);
  43. }
  44. /**
  45. * 执行文件上传
  46. */
  47. public function upload()
  48. {
  49. return $this->engine->upload();
  50. }
  51. /**
  52. * 执行文件删除
  53. * @param $fileName
  54. * @return mixed
  55. */
  56. public function delete($fileName)
  57. {
  58. return $this->engine->delete($fileName);
  59. }
  60. /**
  61. * 获取错误信息
  62. * @return mixed
  63. */
  64. public function getError()
  65. {
  66. return $this->engine->getError();
  67. }
  68. /**
  69. * 获取文件路径
  70. * @return mixed
  71. */
  72. public function getFileName()
  73. {
  74. return $this->engine->getFileName();
  75. }
  76. /**
  77. * 返回文件信息
  78. * @return mixed
  79. */
  80. public function getFileInfo()
  81. {
  82. return $this->engine->getFileInfo();
  83. }
  84. /**
  85. * 获取当前的存储引擎
  86. * @param null|string $storage 指定存储方式,如不指定则为系统默认
  87. * @return mixed
  88. * @throws Exception
  89. */
  90. private function getEngineClass($storage = null)
  91. {
  92. $engineName = is_null($storage) ? $this->config['default'] : $storage;
  93. $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst($engineName);
  94. if (!class_exists($classSpace)) {
  95. throw new Exception('未找到存储引擎类: ' . $engineName);
  96. }
  97. return new $classSpace($this->config['engine'][$engineName]);
  98. }
  99. }