BaseStorage.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\basic;
  12. /**
  13. * Class BaseStorage
  14. * @package crmeb\basic
  15. */
  16. abstract class BaseStorage
  17. {
  18. /**
  19. * 驱动名称
  20. * @var string
  21. */
  22. protected $name;
  23. /**
  24. * 驱动配置文件名
  25. * @var string
  26. */
  27. protected $configFile;
  28. /**
  29. * 错误信息
  30. * @var string
  31. */
  32. protected $error;
  33. /**
  34. * BaseStorage constructor.
  35. * @param string $name 驱动名
  36. * @param string $configFile 驱动配置名
  37. * @param array $config 其他配置
  38. */
  39. public function __construct(string $name, array $config = [], string $configFile = null)
  40. {
  41. $this->name = $name;
  42. $this->configFile = $configFile;
  43. $this->initialize($config);
  44. }
  45. /**
  46. * 设置错误信息
  47. * @param string|null $error
  48. * @return bool
  49. */
  50. protected function setError(?string $error = null)
  51. {
  52. $this->error = $error ?: '未知错误';
  53. return false;
  54. }
  55. /**
  56. * 获取错误信息
  57. * @return string
  58. */
  59. public function getError()
  60. {
  61. $error = $this->error;
  62. $this->error = null;
  63. return $error;
  64. }
  65. /**
  66. * 初始化
  67. * @param array $config
  68. * @return mixed
  69. */
  70. abstract protected function initialize(array $config);
  71. }