Skip to content

Commit d064447

Browse files
committed
refactor: optimize provider
1 parent 8ae4dad commit d064447

File tree

15 files changed

+111
-195
lines changed

15 files changed

+111
-195
lines changed

.php-cs-fixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@
4242
->setFinder(
4343
PhpCsFixer\Finder::create()
4444
->exclude('vendor')
45-
->in([__DIR__.'/src/',__DIR__.'/tests/'])
45+
->in([__DIR__.'/src/',__DIR__.'/tests/',__DIR__.'/config/'])
4646
)
4747
;

CHANGELOG.md

Lines changed: 0 additions & 34 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ $client->appendObject($adapter->getBucket(), $adapter->applyPathPrefix('dir/path
127127
```
128128
129129
## Documentation
130-
- [Object storage OSS-aliyun](https://help.aliyun.com/product/31815.html)
130+
- [Object storage OSS-aliyun](https://www.alibabacloud.com/help/en/object-storage-service)
131131
132132
## Issues
133133
[Opening an Issue](https://github.com/alphasnow/aliyun-oss-laravel/issues/new)

config/config.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
return [
4+
"driver" => "aliyun",
5+
"access_id" => env("ALIYUN_OSS_ACCESS_ID"), // AccessKey ID, For example: LTAI4**************qgcsA
6+
"access_key" => env("ALIYUN_OSS_ACCESS_KEY"), // AccessKey Secret, For example: PkT4F********************Bl9or
7+
"bucket" => env("ALIYUN_OSS_BUCKET"), // For example: my-storage
8+
"endpoint" => env("ALIYUN_OSS_ENDPOINT"), // For example: oss-cn-shanghai.aliyuncs.com
9+
"internal" => env("ALIYUN_OSS_INTERNAL", null), // For example: oss-cn-shanghai-internal.aliyuncs.com
10+
"domain" => env("ALIYUN_OSS_DOMAIN", null), // For example: oss.my-domain.com
11+
"use_ssl" => env("ALIYUN_OSS_USE_SSL", false), // Whether to use https
12+
"prefix" => env("ALIYUN_OSS_PREFIX", null), // The prefix of the store path
13+
"security_token" => env("ALIYUN_OSS_TOKEN", null),// Used by \OSS\OssClient
14+
"request_proxy" => env("ALIYUN_OSS_PROXY", null),// Used by \OSS\OssClient
15+
"use_domain_endpoint" => env("ALIYUN_OSS_USE_DOMAIN_ENDPOINT", false), // Whether to upload using domain
16+
"signature_expires" => env("ALIYUN_OSS_SIGNATURE_EXPIRES", "+60 minutes"), // The valid time of the temporary url
17+
];

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ parameters:
55
- src
66

77
ignoreErrors:
8+
- '~Right side of && is always false.~'
89

910
checkMissingIterableValueType: false

phpunit.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<directory suffix=".php">src/</directory>
1919
<exclude>
2020
<directory suffix="ServiceProvider.php">src/</directory>
21-
<directory suffix="Exception.php">src/</directory>
2221
</exclude>
2322
</whitelist>
2423
</filter>

src/Adapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Adapter extends BaseAdapter
2323
public function __construct(OssClient $ossClient, Config $ossConfig)
2424
{
2525
$this->ossConfig = $ossConfig;
26-
parent::__construct($ossClient, $ossConfig->get('bucket'), ltrim($ossConfig->get('prefix', null), '/'), $ossConfig->get('options', []));
26+
parent::__construct($ossClient, $ossConfig->get("bucket"), ltrim($ossConfig->get("prefix", null), "/"), $ossConfig->get("options", []));
2727
}
2828

2929
/**
@@ -36,7 +36,7 @@ public function __construct(OssClient $ossClient, Config $ossConfig)
3636
public function getUrl($path)
3737
{
3838
$object = $this->applyPathPrefix($path);
39-
return $this->ossConfig->getUrlDomain() . '/' . ltrim($object, '/');
39+
return $this->ossConfig->getUrlDomain() . "/" . ltrim($object, "/");
4040
}
4141

4242
/**
@@ -56,9 +56,9 @@ public function getTemporaryUrl($path, $expiration = null, array $options = [])
5656
$clientOptions = $this->getOptionsFromConfig(new FlysystemConfig($options));
5757

5858
if (is_null($expiration)) {
59-
$expiration = new \DateTime($this->ossConfig->get('signature_expires'));
59+
$expiration = new \DateTime($this->ossConfig->get("signature_expires", "+60 minutes"));
6060
}
61-
$timeout = $expiration->getTimestamp() - (new \DateTime('now'))->getTimestamp();
61+
$timeout = $expiration->getTimestamp() - (new \DateTime("now"))->getTimestamp();
6262

6363
$url = $this->client->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $clientOptions);
6464
return $this->ossConfig->correctUrl($url);

src/Config.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Config extends Collection
1414
*/
1515
public function getUrlDomain()
1616
{
17-
if ($this->get('domain')) {
18-
return $this->getProtocol().'://'.$this->get('domain');
17+
if ($this->get("domain")) {
18+
return $this->getProtocol()."://".$this->get("domain");
1919
}
2020
return $this->getEndpointDomain();
2121
}
@@ -25,44 +25,45 @@ public function getUrlDomain()
2525
*/
2626
protected function getEndpointDomain()
2727
{
28-
return $this->getProtocol().'://'.$this->get('bucket').'.'.$this->get('endpoint');
28+
return $this->getProtocol()."://".$this->get("bucket").".".$this->get("endpoint");
2929
}
30+
3031
/**
3132
* @return string
3233
*/
3334
protected function getInternalDomain()
3435
{
35-
return $this->getProtocol().'://'.$this->get('bucket').'.'.$this->get('internal');
36+
return $this->getProtocol()."://".$this->get("bucket").".".$this->get("internal");
3637
}
3738

3839
/**
3940
* @return string
4041
*/
4142
protected function getProtocol()
4243
{
43-
return $this->get('use_ssl', false) ? 'https' : 'http';
44+
return $this->get("use_ssl", false) ? "https" : "http";
4445
}
4546

4647
/**
4748
* @return string
4849
*/
4950
public function getOssEndpoint()
5051
{
51-
if ($internal = $this->get('internal')) {
52+
if ($internal = $this->get("internal")) {
5253
return $internal;
5354
}
5455
if ($this->isCName()) {
55-
return $this->get('domain');
56+
return $this->get("domain");
5657
}
57-
return $this->get('endpoint');
58+
return $this->get("endpoint");
5859
}
5960

6061
/**
6162
* @return bool
6263
*/
63-
protected function isCName()
64+
public function isCName()
6465
{
65-
return $this->get('use_domain_endpoint') && $this->get('domain');
66+
return $this->get("use_domain_endpoint", false) && $this->get("domain");
6667
}
6768

6869
/**
@@ -71,11 +72,12 @@ protected function isCName()
7172
public function getOssClientParameters()
7273
{
7374
return [
74-
'accessKeyId' => $this->get('access_id'),
75-
'accessKeySecret' => $this->get('access_key'),
76-
'endpoint' => $this->getOssEndpoint(),
77-
'isCName' => $this->isCName(),
78-
'securityToken' => $this->get('security_token', null)
75+
"accessKeyId" => $this->get("access_id"),
76+
"accessKeySecret" => $this->get("access_key"),
77+
"endpoint" => $this->getOssEndpoint(),
78+
"isCName" => $this->isCName(),
79+
"securityToken" => $this->get("security_token", null),
80+
"requestProxy" => $this->get("request_proxy", null)
7981
];
8082
}
8183

@@ -85,13 +87,11 @@ public function getOssClientParameters()
8587
*/
8688
public function correctUrl($url)
8789
{
88-
// correct internal domain
89-
if ($this->get('internal')) {
90+
if ($this->get("internal")) {
9091
return str_replace($this->getInternalDomain(), $this->getUrlDomain(), $url);
9192
}
9293

93-
// correct domain
94-
if ($this->get('domain') && $this->get('use_domain_endpoint') == false) {
94+
if ($this->get("domain") && $this->get("use_domain_endpoint") == false) {
9595
return str_replace($this->getEndpointDomain(), $this->getUrlDomain(), $url);
9696
}
9797

src/ServiceProvider.php

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,21 @@ class ServiceProvider extends BaseServiceProvider
2020
*/
2121
public function boot()
2222
{
23-
$this->app->make('filesystem')
24-
->extend('aliyun', function ($app, array $config) {
25-
return $app->make('aliyun-oss.oss-filesystem', $config);
23+
$this->app->make("filesystem")
24+
->extend("aliyun", function ($app, array $config) {
25+
$ossConfig = new Config($config);
26+
27+
$ossClient = new OssClient($ossConfig->get("access_id"), $ossConfig->get("access_key"), $ossConfig->getOssEndpoint(), $ossConfig->isCName(), $ossConfig->get("security_token"), $ossConfig->get("request_proxy"));
28+
$ossConfig->has("use_ssl") && $ossClient->setUseSSL($ossConfig->get("use_ssl"));
29+
$ossConfig->has("max_retries") && $ossClient->setMaxTries($ossConfig->get("max_retries"));
30+
$ossConfig->has("enable_sts_in_url") && $ossClient->setSignStsInUrl($ossConfig->get("enable_sts_in_url"));
31+
$ossConfig->has("timeout") && $ossClient->setTimeout($ossConfig->get("timeout"));
32+
$ossConfig->has("connect_timeout") && $ossClient->setConnectTimeout($ossConfig->get("connect_timeout"));
33+
34+
$filesystem = new Filesystem(new Adapter($ossClient, $ossConfig), new FlysystemConfig(["disable_asserts" => true]));
35+
$filesystem->addPlugin(new AppendContent());
36+
37+
return $filesystem;
2638
});
2739
}
2840

@@ -31,46 +43,32 @@ public function boot()
3143
*/
3244
public function register()
3345
{
34-
$this->registerConfig();
35-
36-
$this->app->bind('aliyun-oss.oss-client', function ($app, array $config) {
37-
$ossConfig = new Config($config);
38-
39-
$client = $app->make(OssClient::class, $ossConfig->getOssClientParameters());
40-
$client->setUseSSL($ossConfig->get('use_ssl', false));
41-
return $client;
42-
});
43-
44-
$this->app->bind('aliyun-oss.oss-adapter', function ($app, array $config) {
45-
$client = $app->make('aliyun-oss.oss-client', $config);
46-
47-
return new Adapter($client, new Config($config));
48-
});
49-
50-
$this->app->bind('aliyun-oss.oss-filesystem', function ($app, array $config) {
51-
$adapter = $app->make('aliyun-oss.oss-adapter', $config);
46+
if (get_class($this->app) == "Laravel\Lumen\Application") {
47+
return;
48+
}
5249

53-
$filesystem = new Filesystem($adapter, new FlysystemConfig(['disable_asserts' => true]));
54-
$filesystem->addPlugin(new AppendContent());
55-
return $filesystem;
56-
});
50+
$this->registerConfig();
5751
}
5852

53+
/**
54+
* @throws \Illuminate\Contracts\Container\BindingResolutionException
55+
* @return void
56+
*/
5957
protected function registerConfig()
6058
{
6159
if ($this->app instanceof CachesConfiguration && $this->app->configurationIsCached()) {
6260
return;
6361
}
6462

65-
$config = $this->app->make('config');
63+
$config = $this->app->make("config");
6664
$disks = $config->get("filesystems.disks", []);
6765
$drivers = array_column($disks, "driver");
6866
if (in_array("aliyun", $drivers)) {
6967
return;
7068
}
7169

7270
$config->set("filesystems.disks.aliyun", array_merge(
73-
require __DIR__ . "/config/config.php",
71+
require __DIR__ . "/../config/config.php",
7472
$config->get("filesystems.disks.aliyun", [])
7573
));
7674
}

src/config/config.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)