Skip to content

Commit

Permalink
feat: Separate filesystem macro
Browse files Browse the repository at this point in the history
  • Loading branch information
alphasnow committed Aug 3, 2022
1 parent 24488a6 commit 89b6f50
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 70 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"extra": {
"laravel": {
"providers": [
"AlphaSnow\\LaravelFilesystem\\Aliyun\\AliyunServiceProvider"
"AlphaSnow\\LaravelFilesystem\\Aliyun\\AliyunServiceProvider"
]
}
},
Expand Down
55 changes: 10 additions & 45 deletions src/AliyunServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
namespace AlphaSnow\LaravelFilesystem\Aliyun;

use AlphaSnow\Flysystem\Aliyun\AliyunFactory;
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AliyunMacro;
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendFile;
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendObject;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Foundation\CachesConfiguration;
use Illuminate\Filesystem\FilesystemAdapter;
Expand All @@ -16,29 +14,25 @@ class AliyunServiceProvider extends ServiceProvider
{
/**
* @return void
* @throws BindingResolutionException
*/
public function register()
{
$this->app->singleton(AliyunFactory::class, function ($app) {
return new AliyunFactory();
});

if ($this->app::class == "Laravel\Lumen\Application") {
return;
}

$this->registerConfig();
$this->mergeOssConfig();
}

/**
* @return void
* @throws BindingResolutionException
*/
protected function registerConfig()
protected function mergeOssConfig()
{
if ($this->app instanceof CachesConfiguration && $this->app->configurationIsCached()) {
return;
}

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

/**
* @return void
* @throws BindingResolutionException
*/
public function boot()
{
$this->app->make("filesystem")
->extend("oss", function (Application $app, array $config) {
$adapter = $app->make(AliyunFactory::class)->createAdapter($config);
$adapter = $this->app->make(AliyunFactory::class)->createAdapter($config);
$driver = new Filesystem($adapter);
$filesystem = new FilesystemAdapter($driver, $adapter, $config);
$macros = array_merge($this->defaultMacros, $config["macros"] ?? []);
$this->registerMicros($filesystem, $macros);
(new FilesystemMacroManager($filesystem))->defaultRegister()->register($config["macros"] ?? []);
return $filesystem;
});
}

/**
* @var array
*/
protected $defaultMacros = [
AppendFile::class,
AppendObject::class,
];

/**
* @param FilesystemAdapter $filesystemAdapter
* @param array $macros
*/
protected function registerMicros(FilesystemAdapter $filesystemAdapter, array $macros): void
{
foreach ($macros as $macro) {
if (!class_exists($macro)) {
continue;
}
$aliyunMacro = new $macro();
if (!$aliyunMacro instanceof AliyunMacro) {
continue;
}
if ($filesystemAdapter->hasMacro($aliyunMacro->name())) {
continue;
}
$filesystemAdapter::macro($aliyunMacro->name(), $aliyunMacro->macro());
}
}
}
59 changes: 59 additions & 0 deletions src/FilesystemMacroManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace AlphaSnow\LaravelFilesystem\Aliyun;

use AlphaSnow\Flysystem\Aliyun\AliyunException;
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendFile;
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendObject;
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AliyunMacro;
use Illuminate\Filesystem\FilesystemAdapter;

class FilesystemMacroManager
{
/**
* @var FilesystemAdapter
*/
protected FilesystemAdapter $filesystemAdapter;

/**
* @var array
*/
protected array $defaultMacros = [
AppendFile::class,
AppendObject::class,
];

/**
* @param FilesystemAdapter $filesystemAdapter
*/
public function __construct(FilesystemAdapter $filesystemAdapter)
{
$this->filesystemAdapter = $filesystemAdapter;
}

/**
* @return $this
*/
public function defaultRegister(): FilesystemMacroManager
{
$this->register($this->defaultMacros);
return $this;
}

/**
* @param array $macros
* @return $this
*/
public function register(array $macros): FilesystemMacroManager
{
foreach ($macros as $macro) {
$filesystemMacro = new $macro();
if (!$filesystemMacro instanceof AliyunMacro) {
throw new AliyunException("FilesystemMacroManager register want AliyunMacro, But got ".$filesystemMacro::class, 0);
}

$this->filesystemAdapter::macro($filesystemMacro->name(), $filesystemMacro->macro());
}
return $this;
}
}
17 changes: 10 additions & 7 deletions src/OssClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
use AlphaSnow\Flysystem\Aliyun\AliyunAdapter;
use AlphaSnow\Flysystem\Aliyun\AliyunException;
use Illuminate\Filesystem\FilesystemAdapter;
use JetBrains\PhpStorm\Pure;
use League\Flysystem\Config;
use OSS\OssClient;

class OssClientAdapter
{
/**
* @var AliyunAdapter
*/
protected $adapter;
protected AliyunAdapter $adapter;

/**
* @param FilesystemAdapter $filesystemAdapter
Expand All @@ -21,23 +23,24 @@ public function __construct(FilesystemAdapter $filesystemAdapter)
{
$adapter = $filesystemAdapter->getAdapter();
if (!$adapter instanceof AliyunAdapter) {
throw new AliyunException("Adapter expect AliyunAdapter, But got ".$adapter::class, 0);
throw new AliyunException("OssClientAdapter construct want AliyunAdapter, But got ".$adapter::class, 0);
}

$this->adapter = $adapter;
}

/**
* @return \OSS\OssClient
* @return OssClient
*/
public function client()
#[Pure] public function client(): OssClient
{
return $this->adapter->getClient();
}

/**
* @return string
*/
public function bucket()
#[Pure] public function bucket(): string
{
return $this->adapter->getBucket();
}
Expand All @@ -46,7 +49,7 @@ public function bucket()
* @param string $path
* @return string
*/
public function path($path = "")
#[Pure] public function path(string $path = ""): string
{
return $this->adapter->getPrefixer()->prefixPath($path);
}
Expand All @@ -55,7 +58,7 @@ public function path($path = "")
* @param array $options
* @return array
*/
public function options($options = [])
public function options(array $options = []): array
{
return $this->adapter->getOptions()->mergeConfig(new Config($options));
}
Expand Down
16 changes: 1 addition & 15 deletions tests/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,19 @@

namespace AlphaSnow\LaravelFilesystem\Aliyun\Tests;

use AlphaSnow\Flysystem\Aliyun\AliyunFactory;
use AlphaSnow\LaravelFilesystem\Aliyun\AliyunServiceProvider;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Storage;

class ProviderTest extends TestCase
{
/**
* @test
*/
public function aliyun_factory_singleton()
{
$factory1 = $this->app->make(AliyunFactory::class);
$factory2 = $this->app->make(AliyunFactory::class);

$this->assertSame($factory1, $factory2);
}

/**
* @test
*/
public function merge_default_config()
{
$config = require __DIR__ . "/../config/config.php";
$appCfg = $this->app["config"]->get("filesystems.disks.oss");
$this->assertSame($config,$appCfg);
$this->assertSame($config, $appCfg);
}

/**
Expand All @@ -38,5 +25,4 @@ public function default_filesystem_disk()
$storage = Storage::disk("oss");
$this->assertInstanceOf(Filesystem::class, $storage);
}

}
2 changes: 0 additions & 2 deletions tests/ProviderWithExistConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace AlphaSnow\LaravelFilesystem\Aliyun\Tests;

use AlphaSnow\Flysystem\Aliyun\AliyunFactory;
use AlphaSnow\LaravelFilesystem\Aliyun\AliyunServiceProvider;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Storage;
Expand All @@ -26,5 +25,4 @@ public function without_default_filesystem_disk()
$this->expectException(\InvalidArgumentException::class);
Storage::disk("oss");
}

}

0 comments on commit 89b6f50

Please sign in to comment.