| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace WShafer\PSR11FlySystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use League\Flysystem\Cached\CachedAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use League\Flysystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use League\Flysystem\FilesystemInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use League\Flysystem\MountManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Psr\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use WShafer\PSR11FlySystem\Config\FileSystemConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use WShafer\PSR11FlySystem\Config\MainConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use WShafer\PSR11FlySystem\Exception\UnknownFileSystemException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use WShafer\PSR11FlySystem\Exception\UnknownPluginException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class FlySystemManager implements ContainerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** @var MainConfig  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var ContainerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $adaptorManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var ContainerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $cacheManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @var Filesystem[]|MountManager[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $systems = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @var ContainerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * Manager constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param MainConfig         $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param ContainerInterface $adaptorManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param ContainerInterface $cacheManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param ContainerInterface $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 12 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         MainConfig $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         ContainerInterface $adaptorManager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         ContainerInterface $cacheManager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ContainerInterface $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 12 |  |         $this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 12 |  |         $this->adaptorManager = $adaptorManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 12 |  |         $this->cacheManager = $cacheManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 12 |  |         $this->container = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return Filesystem|MountManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 6 |  |     public function get($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 6 |  |         if (key_exists($id, $this->systems)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |             return $this->systems[$id]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 6 |  |         $fileSystemConfig = $this->config->getFileSystemConfig($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 6 |  |         if (!$fileSystemConfig) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |             throw new UnknownFileSystemException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |                 'Unable to locate file system '.$id.'.  Please check your configuration.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 5 |  |         if (!$fileSystemConfig->isManager()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  |             return $this->getFileSystem($id, $fileSystemConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         $fileSystems = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         foreach ($fileSystemConfig->getFileSystems() as $name => $managerSystemConfig) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |             $fileSystems[$name] = $this->getFileSystem($name, $managerSystemConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         $manager = new MountManager($fileSystems); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         if ($fileSystemConfig->getPlugins()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |             $this->setPlugins($manager, $fileSystemConfig->getPlugins()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |         $this->systems[$id] = $manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |         return $this->systems[$id]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param FileSystemConfig $fileSystemConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return Filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 5 |  |     protected function getFileSystem($id, FileSystemConfig $fileSystemConfig) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 5 |  |         $adaptor = $this->adaptorManager->get($fileSystemConfig->getAdaptor()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 5 |  |         $cache = $this->cacheManager->get($fileSystemConfig->getCache()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 5 |  |         $cachedAdaptor = new CachedAdapter($adaptor, $cache); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 5 |  |         $fileSystem = new Filesystem($cachedAdaptor); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 5 |  |         if ($fileSystemConfig->getPlugins()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 3 |  |             $this->setPlugins($fileSystem, $fileSystemConfig->getPlugins()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 4 |  |         $this->systems[$id] = $fileSystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 4 |  |         return $fileSystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param FilesystemInterface|MountManager $filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param array $plugins | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 4 |  |     protected function setPlugins($filesystem, array $plugins = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 4 |  |         foreach ($plugins as $plugin) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 4 |  |             $this->addPlugin($filesystem, $plugin); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @param FilesystemInterface|MountManager $filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param string $pluginName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 4 |  |     protected function addPlugin($filesystem, string $pluginName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 4 |  |         if (!$this->container->has($pluginName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |             throw new UnknownPluginException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |                 'Unable to locate plugin service '.$pluginName.' in the container.  Please check your config.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 3 |  |         $filesystem->addPlugin($this->container->get($pluginName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 2 |  |     public function has($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 2 |  |         return $this->config->hasFileSystemConfig($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * Get the Adaptor manager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @return ContainerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 1 |  |     public function getAdaptorManager() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 1 |  |         return $this->adaptorManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * Get the Cache Manager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 156 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 157 | 1 |  |     public function getCacheManager() | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 159 | 1 |  |         return $this->cacheManager; | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 161 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |  |