From abafe1b970b188364e03f135b9ee7c835c16322b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Nikolaou?= Date: Sun, 25 Oct 2020 20:39:29 +0200 Subject: [PATCH] Resolve the Blob Client through the container Bind the `BlobRestProxy` to the container, so it can be resolved when the driver is created, but also when users need to access the client outside of the driver/adapter. For example: `$client = app(BlobRestProxy::class)` --- src/AzureStorageServiceProvider.php | 23 +++++++++++++---------- tests/ServiceProviderTest.php | 11 +++++++++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/AzureStorageServiceProvider.php b/src/AzureStorageServiceProvider.php index fb873d2..81f667a 100644 --- a/src/AzureStorageServiceProvider.php +++ b/src/AzureStorageServiceProvider.php @@ -20,15 +20,7 @@ final class AzureStorageServiceProvider extends ServiceProvider public function boot() { Storage::extend('azure', function ($app, $config) { - $endpoint = sprintf( - 'DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;', - $config['name'], - $config['key'] - ); - if (isset($config['endpoint'])) { - $endpoint .= sprintf("BlobEndpoint=%s;", $config['endpoint']); - } - $client = BlobRestProxy::createBlobService($endpoint); + $client = $app->make(BlobRestProxy::class, $config); $adapter = new AzureBlobStorageAdapter( $client, $config['container'], @@ -47,6 +39,17 @@ public function boot() */ public function register() { - // + $this->app->bind(BlobRestProxy::class, function ($app, $config) { + $config = empty($config) ? $app->make('config')->get('filesystems.disks.azure') : $config; + $endpoint = sprintf( + 'DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;', + $config['name'], + $config['key'] + ); + if (isset($config['endpoint'])) { + $endpoint .= sprintf("BlobEndpoint=%s;", $config['endpoint']); + } + return BlobRestProxy::createBlobService($endpoint); + }); } } diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index 481e5b1..49a14bc 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -3,6 +3,7 @@ namespace Tests; use Illuminate\Support\Facades\Storage; +use MicrosoftAzure\Storage\Blob\BlobRestProxy; final class ServiceProviderTest extends TestCase { @@ -46,4 +47,14 @@ public function custom_url_overrides_endpoint() $this->assertEquals("$customUrl/$container/a.txt", Storage::url('a.txt')); } + + /** @test */ + public function it_resolves_the_azure_client() + { + $this->assertTrue($this->app->bound(BlobRestProxy::class)); + + Storage::disk(); + + $this->assertTrue($this->app->resolved(BlobRestProxy::class)); + } }