| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace kalanis\kw_files\Processing\Storage\Dirs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use kalanis\kw_files\FilesException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use kalanis\kw_files\Interfaces\IFLTranslations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use kalanis\kw_files\Interfaces\ITypes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use kalanis\kw_files\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use kalanis\kw_paths\PathsException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use kalanis\kw_storage\Interfaces\IPassDirs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use kalanis\kw_storage\StorageException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Class CanDirRecursive | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package kalanis\kw_files\Processing\Storage\Dirs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Process dirs via predefined api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class CanDirRecursive extends ADirs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var IPassDirs */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $storage = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 12 |  |     public function __construct(IPassDirs $storage, ?IFLTranslations $lang = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 12 |  |         $this->storage = $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 12 |  |         $this->setLang($lang); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 3 |  |     public function createDir(array $entry, bool $deep = false): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 3 |  |         $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 3 |  |             return $this->storage->mkDir($path, $deep); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |         } catch (StorageException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |             throw new FilesException($this->getLang()->flCannotCreateDir($path), $ex->getCode(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string[] $entry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param bool $loadRecursive | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param bool $wantSize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string[] $previousPaths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @throws FilesException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @throws PathsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return Node[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 6 |  |     public function readDir(array $entry, bool $loadRecursive = false, bool $wantSize = false, array $previousPaths = []): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 6 |  |         $entryPath = $this->compactName($entry, $this->getStorageSeparator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 6 |  |             if (!$this->storage->isDir($this->getStorageSeparator() . $entryPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |                 throw new FilesException($this->getLang()->flCannotReadDir($entryPath)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 4 |  |             $files = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 4 |  |             $sepLen = mb_strlen($this->getStorageSeparator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 4 |  |             foreach ($this->storage->lookup($this->getStorageSeparator() . $entryPath) as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 4 |  |                 $usePath = mb_substr($item, $sepLen); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 4 |  |                 $currentPath = $this->compactName(array_merge($entry, [$usePath]), $this->getStorageSeparator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |                 $sub = new Node(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 4 |  |                 if ('' == $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |                     if (!empty($previousPaths)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |                         continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 4 |  |                     $sub->setData( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  |                         [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 4 |  |                         0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 4 |  |                         ITypes::TYPE_DIR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 4 |  |                 } elseif ($this->storage->isDir($this->getStorageSeparator() . $currentPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |                     $sub->setData( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 3 |  |                         array_merge($previousPaths, $this->expandName($usePath)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 3 |  |                         0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 3 |  |                         ITypes::TYPE_DIR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     // normal node - file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 4 |  |                     $sub->setData( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |                         array_merge($previousPaths, $this->expandName($usePath)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  |                         $wantSize ? intval($this->storage->size($this->getStorageSeparator() . $currentPath)) : 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |                         ITypes::TYPE_FILE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  |                 $files[] = $sub; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 4 |  |                 if ($loadRecursive && $sub->isDir() && ('' !== $item)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |                     $files = array_merge($files, $this->readDir(array_merge($entry, $sub->getPath()), $loadRecursive, $wantSize, $sub->getPath())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 4 |  |             return $files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  |         } catch (StorageException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |             throw new FilesException($this->getLang()->flCannotReadDir($entryPath), $ex->getCode(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |     public function copyDir(array $source, array $dest): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 2 |  |         $src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |         $dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |             return $this->storage->copy($src, $dst); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |         } catch (StorageException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |             throw new FilesException($this->getLang()->flCannotCopyDir($src, $dst), $ex->getCode(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |     public function moveDir(array $source, array $dest): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |         $src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |         $dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |             return $this->storage->move($src, $dst); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |         } catch (StorageException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |             throw new FilesException($this->getLang()->flCannotMoveDir($src, $dst), $ex->getCode(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 120 | 2 |  |     public function deleteDir(array $entry, bool $deep = false): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 | 2 |  |         $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 124 | 2 |  |             if ($this->storage->isDir($path)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 125 | 1 |  |                 return $this->storage->rmDir($path, $deep); | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 127 | 1 |  |                 return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 129 | 1 |  |         } catch (StorageException $ex) { | 
            
                                                                        
                            
            
                                    
            
            
                | 130 | 1 |  |             throw new FilesException($this->getLang()->flCannotRemoveDir($path), $ex->getCode(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @codeCoverageIgnore only when path fails | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     protected function noDirectoryDelimiterSet(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         return $this->getLang()->flNoDirectoryDelimiterSet(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 142 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |  |