Skip to content

Commit 89b6f50

Browse files
committed
feat: Separate filesystem macro
1 parent 24488a6 commit 89b6f50

File tree

6 files changed

+81
-70
lines changed

6 files changed

+81
-70
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"extra": {
3535
"laravel": {
3636
"providers": [
37-
"AlphaSnow\\LaravelFilesystem\\Aliyun\\AliyunServiceProvider"
37+
"AlphaSnow\\LaravelFilesystem\\Aliyun\\AliyunServiceProvider"
3838
]
3939
}
4040
},

src/AliyunServiceProvider.php

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace AlphaSnow\LaravelFilesystem\Aliyun;
44

55
use AlphaSnow\Flysystem\Aliyun\AliyunFactory;
6-
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AliyunMacro;
7-
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendFile;
8-
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendObject;
6+
use Illuminate\Contracts\Container\BindingResolutionException;
97
use Illuminate\Contracts\Foundation\Application;
108
use Illuminate\Contracts\Foundation\CachesConfiguration;
119
use Illuminate\Filesystem\FilesystemAdapter;
@@ -16,29 +14,25 @@ class AliyunServiceProvider extends ServiceProvider
1614
{
1715
/**
1816
* @return void
17+
* @throws BindingResolutionException
1918
*/
2019
public function register()
2120
{
22-
$this->app->singleton(AliyunFactory::class, function ($app) {
23-
return new AliyunFactory();
24-
});
25-
26-
if ($this->app::class == "Laravel\Lumen\Application") {
27-
return;
28-
}
29-
30-
$this->registerConfig();
21+
$this->mergeOssConfig();
3122
}
3223

3324
/**
3425
* @return void
26+
* @throws BindingResolutionException
3527
*/
36-
protected function registerConfig()
28+
protected function mergeOssConfig()
3729
{
3830
if ($this->app instanceof CachesConfiguration && $this->app->configurationIsCached()) {
3931
return;
4032
}
4133

34+
// If a driver for OSS has been defined
35+
// Then configuration merge will not be performed
4236
$config = $this->app->make('config');
4337
$disks = $config->get("filesystems.disks", []);
4438
$drivers = array_column($disks, "driver");
@@ -54,46 +48,17 @@ protected function registerConfig()
5448

5549
/**
5650
* @return void
51+
* @throws BindingResolutionException
5752
*/
5853
public function boot()
5954
{
6055
$this->app->make("filesystem")
6156
->extend("oss", function (Application $app, array $config) {
62-
$adapter = $app->make(AliyunFactory::class)->createAdapter($config);
57+
$adapter = $this->app->make(AliyunFactory::class)->createAdapter($config);
6358
$driver = new Filesystem($adapter);
6459
$filesystem = new FilesystemAdapter($driver, $adapter, $config);
65-
$macros = array_merge($this->defaultMacros, $config["macros"] ?? []);
66-
$this->registerMicros($filesystem, $macros);
60+
(new FilesystemMacroManager($filesystem))->defaultRegister()->register($config["macros"] ?? []);
6761
return $filesystem;
6862
});
6963
}
70-
71-
/**
72-
* @var array
73-
*/
74-
protected $defaultMacros = [
75-
AppendFile::class,
76-
AppendObject::class,
77-
];
78-
79-
/**
80-
* @param FilesystemAdapter $filesystemAdapter
81-
* @param array $macros
82-
*/
83-
protected function registerMicros(FilesystemAdapter $filesystemAdapter, array $macros): void
84-
{
85-
foreach ($macros as $macro) {
86-
if (!class_exists($macro)) {
87-
continue;
88-
}
89-
$aliyunMacro = new $macro();
90-
if (!$aliyunMacro instanceof AliyunMacro) {
91-
continue;
92-
}
93-
if ($filesystemAdapter->hasMacro($aliyunMacro->name())) {
94-
continue;
95-
}
96-
$filesystemAdapter::macro($aliyunMacro->name(), $aliyunMacro->macro());
97-
}
98-
}
9964
}

src/FilesystemMacroManager.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace AlphaSnow\LaravelFilesystem\Aliyun;
4+
5+
use AlphaSnow\Flysystem\Aliyun\AliyunException;
6+
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendFile;
7+
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendObject;
8+
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AliyunMacro;
9+
use Illuminate\Filesystem\FilesystemAdapter;
10+
11+
class FilesystemMacroManager
12+
{
13+
/**
14+
* @var FilesystemAdapter
15+
*/
16+
protected FilesystemAdapter $filesystemAdapter;
17+
18+
/**
19+
* @var array
20+
*/
21+
protected array $defaultMacros = [
22+
AppendFile::class,
23+
AppendObject::class,
24+
];
25+
26+
/**
27+
* @param FilesystemAdapter $filesystemAdapter
28+
*/
29+
public function __construct(FilesystemAdapter $filesystemAdapter)
30+
{
31+
$this->filesystemAdapter = $filesystemAdapter;
32+
}
33+
34+
/**
35+
* @return $this
36+
*/
37+
public function defaultRegister(): FilesystemMacroManager
38+
{
39+
$this->register($this->defaultMacros);
40+
return $this;
41+
}
42+
43+
/**
44+
* @param array $macros
45+
* @return $this
46+
*/
47+
public function register(array $macros): FilesystemMacroManager
48+
{
49+
foreach ($macros as $macro) {
50+
$filesystemMacro = new $macro();
51+
if (!$filesystemMacro instanceof AliyunMacro) {
52+
throw new AliyunException("FilesystemMacroManager register want AliyunMacro, But got ".$filesystemMacro::class, 0);
53+
}
54+
55+
$this->filesystemAdapter::macro($filesystemMacro->name(), $filesystemMacro->macro());
56+
}
57+
return $this;
58+
}
59+
}

src/OssClientAdapter.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
use AlphaSnow\Flysystem\Aliyun\AliyunAdapter;
66
use AlphaSnow\Flysystem\Aliyun\AliyunException;
77
use Illuminate\Filesystem\FilesystemAdapter;
8+
use JetBrains\PhpStorm\Pure;
89
use League\Flysystem\Config;
10+
use OSS\OssClient;
911

1012
class OssClientAdapter
1113
{
1214
/**
1315
* @var AliyunAdapter
1416
*/
15-
protected $adapter;
17+
protected AliyunAdapter $adapter;
1618

1719
/**
1820
* @param FilesystemAdapter $filesystemAdapter
@@ -21,23 +23,24 @@ public function __construct(FilesystemAdapter $filesystemAdapter)
2123
{
2224
$adapter = $filesystemAdapter->getAdapter();
2325
if (!$adapter instanceof AliyunAdapter) {
24-
throw new AliyunException("Adapter expect AliyunAdapter, But got ".$adapter::class, 0);
26+
throw new AliyunException("OssClientAdapter construct want AliyunAdapter, But got ".$adapter::class, 0);
2527
}
28+
2629
$this->adapter = $adapter;
2730
}
2831

2932
/**
30-
* @return \OSS\OssClient
33+
* @return OssClient
3134
*/
32-
public function client()
35+
#[Pure] public function client(): OssClient
3336
{
3437
return $this->adapter->getClient();
3538
}
3639

3740
/**
3841
* @return string
3942
*/
40-
public function bucket()
43+
#[Pure] public function bucket(): string
4144
{
4245
return $this->adapter->getBucket();
4346
}
@@ -46,7 +49,7 @@ public function bucket()
4649
* @param string $path
4750
* @return string
4851
*/
49-
public function path($path = "")
52+
#[Pure] public function path(string $path = ""): string
5053
{
5154
return $this->adapter->getPrefixer()->prefixPath($path);
5255
}
@@ -55,7 +58,7 @@ public function path($path = "")
5558
* @param array $options
5659
* @return array
5760
*/
58-
public function options($options = [])
61+
public function options(array $options = []): array
5962
{
6063
return $this->adapter->getOptions()->mergeConfig(new Config($options));
6164
}

tests/ProviderTest.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,19 @@
22

33
namespace AlphaSnow\LaravelFilesystem\Aliyun\Tests;
44

5-
use AlphaSnow\Flysystem\Aliyun\AliyunFactory;
6-
use AlphaSnow\LaravelFilesystem\Aliyun\AliyunServiceProvider;
75
use Illuminate\Contracts\Filesystem\Filesystem;
86
use Illuminate\Support\Facades\Storage;
97

108
class ProviderTest extends TestCase
119
{
12-
/**
13-
* @test
14-
*/
15-
public function aliyun_factory_singleton()
16-
{
17-
$factory1 = $this->app->make(AliyunFactory::class);
18-
$factory2 = $this->app->make(AliyunFactory::class);
19-
20-
$this->assertSame($factory1, $factory2);
21-
}
22-
2310
/**
2411
* @test
2512
*/
2613
public function merge_default_config()
2714
{
2815
$config = require __DIR__ . "/../config/config.php";
2916
$appCfg = $this->app["config"]->get("filesystems.disks.oss");
30-
$this->assertSame($config,$appCfg);
17+
$this->assertSame($config, $appCfg);
3118
}
3219

3320
/**
@@ -38,5 +25,4 @@ public function default_filesystem_disk()
3825
$storage = Storage::disk("oss");
3926
$this->assertInstanceOf(Filesystem::class, $storage);
4027
}
41-
4228
}

tests/ProviderWithExistConfigTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace AlphaSnow\LaravelFilesystem\Aliyun\Tests;
44

5-
use AlphaSnow\Flysystem\Aliyun\AliyunFactory;
65
use AlphaSnow\LaravelFilesystem\Aliyun\AliyunServiceProvider;
76
use Illuminate\Contracts\Filesystem\Filesystem;
87
use Illuminate\Support\Facades\Storage;
@@ -26,5 +25,4 @@ public function without_default_filesystem_disk()
2625
$this->expectException(\InvalidArgumentException::class);
2726
Storage::disk("oss");
2827
}
29-
3028
}

0 commit comments

Comments
 (0)