Issues (38)

src/Upload.php (2 issues)

1
<?php
2
3
/**
4
 * 第三方上传实例抽象类
5
 * @author: JiaMeng <[email protected]>
6
 */
7
8
namespace tinymeng\uploads;
9
10
// 目录入口
11
if (!defined('UPLOADS_ROOT_PATH')) {
12
    define('UPLOADS_ROOT_PATH', dirname(__DIR__));
13
}
14
15
use tinymeng\uploads\Connector\GatewayInterface;
16
use tinymeng\uploads\Helper\Str;
17
/**
18
 * @method static \tinymeng\uploads\Gateways\Oss oss(array|null $config) 阿里云Oss
19
 * @method static \tinymeng\uploads\Gateways\Qiniu qiniu(array|null $config) 七牛云
20
 * @method static \tinymeng\uploads\Gateways\Cos cos(array|null $config) 腾讯云Cos
21
 */
22
abstract class Upload
23
{
24
25
    /**
26
     * Description:  init
27
     * @author: JiaMeng <[email protected]>
28
     * Updater:
29
     * @param $gateway
30
     * @param null $config
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $config is correct as it would always require null to be passed?
Loading history...
31
     * @return mixed
32
     * @throws \Exception
33
     */
34
    protected static function init($gateway, $config = null)
35
    {
36
        $gateway = Str::uFirst($gateway);
37
        $class = __NAMESPACE__ . '\\Gateways\\' . $gateway;
38
        if (class_exists($class)) {
39
            // 加载配置文件
40
            $configFile = UPLOADS_ROOT_PATH . "/config/TUploads.php";
41
            $baseConfig = [];
42
            if (file_exists($configFile)) {
43
                $allConfig = require $configFile;
44
                // 获取对应驱动的默认配置
45
                $driver = strtolower($gateway);
46
                if (isset($allConfig[$driver])) {
47
                    $baseConfig = $allConfig[$driver];
48
                }
49
            }
50
            
51
            // 合并配置:用户配置优先
52
            if ($config === null) {
0 ignored issues
show
The condition $config === null is always true.
Loading history...
53
                $config = [];
54
            }
55
            $finalConfig = array_replace_recursive($baseConfig, $config);
56
            
57
            $app = new $class($finalConfig);
58
            if ($app instanceof GatewayInterface) {
59
                return $app;
60
            }
61
            throw new \Exception("第三方上传类基类 [$class] 必须继承父类 [tinymeng\uploads\Connector\GatewayInterface]");
62
        }
63
        throw new \Exception("第三方上传基类 [$class] 不存在");
64
    }
65
66
    /**
67
     * Description:  __callStatic
68
     * @author: JiaMeng <[email protected]>
69
     * Updater:
70
     * @param $gateway
71
     * @param $config
72
     * @return mixed
73
     */
74
    public static function __callStatic($gateway, $config)
75
    {
76
        $config = isset($config[0]) ? $config[0] : null;
77
        return self::init($gateway, $config);
78
    }
79
80
}
81