2017-07-24 152 views
0

該文檔說明了如何集成Dropbox flysystem適配器,但沒有告訴您如何與ZipArchive適配器或類似集成。Laravel Flysystem與Zip Archive適配器集成

https://laravel.com/docs/5.4/filesystem#custom-filesystems

https://github.com/thephpleague/flysystem-ziparchive

我試圖讀數超過上FilesystemManager.php方法是含有在該文檔示例中使用的擴展方法的類。我發現了一種稱爲「適應」的方法,看起來可能是票證。

我試圖從存儲門面調用此方法像這樣:

return $storage_with_zip = Storage::adapt(new \League\Flysystem\ZipArchive\ZipArchiveAdapter($file->getRealPath()));

,但我得到這個錯誤:

BadMethodCallException Call to undefined method League\Flysystem\Filesystem::adapt

任何人都有成功整合Laravel文件系統與ZipArchiveAdapter?我知道我可以使用PHP本地ZipArchive,但是我想讓文件系統中的所有內容都使用Laravel包裝器。

謝謝!

***更新

我的最終目標是要能夠上傳拉鍊解壓到存儲/目錄或S3。

例1) $file = Storage::disk('local')->extractTo('unzipped/', $file);

,而不是這樣的:

$zip = new ZipArchive(); 
$zip->open($file->getRealPath()); 
$zip->extractTo(storage_path('app') . 'unzipped'); 

例2) $file = Storage::disk('s3')->extractTo('unzipped/', $file);

回答

1

對於你想要做什麼,這將更好地創造聯賽\插件Flyststem。 我不確定是否有一種「乾淨」的方式爲Laravel-Flyststem添加插件,這是聯盟\ Flyststem和Laravel之間的橋樑。 解決方法之一是爲local/s3註冊一個自定義驅動程序,該驅動程序將通過注入zip插件來擴展嵌入式驅動程序。

有幾步才達到它:

1)創建應用程序/文件系統/插件內

ZipExtractTo

namespace App\Filesystem\Plugins; 

use League\Flysystem\Plugin\AbstractPlugin; 
use ZipArchive; 

class ZipExtractTo extends AbstractPlugin 
{ 
    /** 
    * @inheritdoc 
    */ 
    public function getMethod() 
    { 
     return 'extractTo'; 
    } 

    /** 
    * Extract zip file into destination directory. 
    * 
    * @param string $path Destination directory 
    * @param string $zipFilePath The path to the zip file. 
    * 
    * @return bool True on success, false on failure. 
    */ 
    public function handle($path, $zipFilePath) 
    { 
     $path = $this->cleanPath($path); 

     $zipArchive = new ZipArchive(); 
     if ($zipArchive->open($zipFilePath) !== true) 
     { 
      return false; 
     } 

     for ($i = 0; $i < $zipArchive->numFiles; ++$i) 
     { 
      $zipEntryName = $zipArchive->getNameIndex($i); 
      $destination = $path . DIRECTORY_SEPARATOR . $this->cleanPath($zipEntryName); 
      if ($this->isDirectory($zipEntryName)) 
      { 
       $this->filesystem->createDir($destination); 
       continue; 
      } 
      $this->filesystem->putStream($destination, $zipArchive->getStream($zipEntryName)); 
     } 

     return true; 
    } 

    private function isDirectory($zipEntryName) 
    { 
     return substr($zipEntryName, -1) === '/'; 
    } 

    private function cleanPath($path) 
    { 
     return str_replace('/', DIRECTORY_SEPARATOR, $path); 
    } 

} 

2 ZipExtractTo插件)創建本地/ S3提供者:

ExtendedLocalServiceProvider

namespace App\Providers; 

use Storage; 
use Illuminate\Support\ServiceProvider; 
use App\Filesystem\Plugins\ZipExtractTo; 

class ExtendedLocalServiceProvider extends ServiceProvider 
{ 
    public function boot() 
    { 
     Storage::extend('local', function($app, $config) { 
      return Storage::createLocalDriver($config)->addPlugin(new ZipExtractTo()); 
     }); 
    } 
} 

ExtendedS3ServiceProvider

namespace App\Providers; 

use Storage; 
use Illuminate\Support\ServiceProvider; 
use App\Filesystem\Plugins\ZipExtractTo; 

class ExtendedS3ServiceProvider extends ServiceProvider 
{ 
    public function boot() 
    { 
     Storage::extend('s3', function($app, $config) { 
      return Storage::createS3Driver($config)->addPlugin(new ZipExtractTo()); 
     });   
    } 
} 

3)在應用程序內部註冊提供程序。PHP

'providers' => [ 
... 
App\Providers\ExtendedLocalServiceProvider::class, 
App\Providers\ExtendedS3ServiceProvider::class, 
], 

4)現在你能夠做這樣的事:

Storage::disk('local')->extractTo('unzipped/', $zipPath); 
Storage::disk('s3')->extractTo('unzipped/', $zipPath); 
+1

需要幾藉此在,感謝您的答覆 –