| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Halalsoft\LaravelGoogleCloudStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Aws\S3\S3Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Contracts\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Filesystem\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Filesystem\FilesystemAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Filesystem\FilesystemManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Support\ServiceProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use League\Flysystem\AdapterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use League\Flysystem\AwsS3v3\AwsS3Adapter as S3Adapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use League\Flysystem\Cached\CachedAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use League\Flysystem\Cached\CacheInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use League\Flysystem\Cached\Storage\Memory as MemoryStore; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use League\Flysystem\Filesystem as Flysystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use League\Flysystem\FilesystemInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class GoogleCloudStorageServiceProvider extends ServiceProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Perform post-registration booting of services. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function boot() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $factory = $this->app->make('filesystem'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         /* @var FilesystemManager $factory */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $factory->extend( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             'gcs', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             function ($app, array $config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 $config['endpoint'] = $config['endpoint'] ?? "https://storage.googleapis.com"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 $config['base_url'] = $config['base_url'] ?? "https://storage.googleapis.com"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 $config['region']   = $config['region'] ?? "none"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 $s3Config = $this->formatS3Config($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 $root     = $s3Config['root'] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 $options = $config['options'] ?? []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 return $this->adapt( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     $this->createFlysystem( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                         new S3Adapter(new S3Client($s3Config), $s3Config['bucket'], $root, $options), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                         $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     protected function formatS3Config(array $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $config += ['version' => 'latest']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if (!empty($config['key']) && !empty($config['secret'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $config['credentials'] = array_intersect_key($config, array_flip(['key', 'secret', 'token'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * Adapt the filesystem implementation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param FilesystemInterface  $filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return Filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     protected function adapt(FilesystemInterface $filesystem) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return new FilesystemAdapter($filesystem); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Create a Flysystem instance with the given adapter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param AdapterInterface  $adapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param array  $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return FilesystemInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     protected function createFlysystem(AdapterInterface $adapter, array $config) | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $cache = $config["cache"] ?? null; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         $config = array_intersect_key($config, array_flip((array) ['visibility', 'disable_asserts', 'url'])); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         if ($cache) { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             $adapter = new CachedAdapter($adapter, $this->createCacheStore($cache)); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         return new Flysystem($adapter, count($config) > 0 ? $config : null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Create a cache store instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param mixed  $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return CacheInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     protected function createCacheStore($config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         if ($config === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             return new MemoryStore; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         return new Cache( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $this->app['cache']->store($config['store']), $config['prefix'] ?? 'flysystem', $config['expire'] ?? null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * Register bindings in the container. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function register() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         // | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  | } |