Complex classes like FlysystemDriver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FlysystemDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class FlysystemDriver extends AbstractHierarchicalFilesystemDriver |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var FilesystemInterface |
||
| 26 | */ |
||
| 27 | protected $filesystem; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var AdapterInterface |
||
| 31 | */ |
||
| 32 | protected $adapter; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $entryPath; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * FlysystemDriver constructor. |
||
| 41 | * @param array $configuration |
||
| 42 | */ |
||
| 43 | 51 | public function __construct(array $configuration = []) |
|
| 44 | { |
||
| 45 | 51 | parent::__construct($configuration); |
|
| 46 | // The capabilities default of this driver. See CAPABILITY_* constants for possible values |
||
| 47 | 51 | $this->capabilities = |
|
| 48 | 17 | ResourceStorage::CAPABILITY_BROWSABLE |
|
| 49 | 51 | | ResourceStorage::CAPABILITY_PUBLIC |
|
| 50 | 51 | | ResourceStorage::CAPABILITY_WRITABLE; |
|
| 51 | 51 | } |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Processes the configuration for this driver. |
||
| 55 | * @return void |
||
| 56 | */ |
||
| 57 | public function processConfiguration() |
||
| 58 | { |
||
| 59 | $this->entryPath = $this->configuration['path']; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Merges the capabilities merged by the user at the storage |
||
| 64 | * configuration into the actual capabilities of the driver |
||
| 65 | * and returns the result. |
||
| 66 | * |
||
| 67 | * @param int $capabilities |
||
| 68 | * @return int |
||
| 69 | */ |
||
| 70 | public function mergeConfigurationCapabilities($capabilities) |
||
| 71 | { |
||
| 72 | // TODO: Implement mergeConfigurationCapabilities() method. |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns the identifier of the root level folder of the storage. |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getRootLevelFolder() |
||
| 81 | { |
||
| 82 | return '/'; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns the identifier of the default folder new files should be put into. |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | 3 | public function getDefaultFolder() |
|
| 91 | { |
||
| 92 | 3 | $identifier = '/user_upload/'; |
|
| 93 | 3 | $createFolder = !$this->folderExists($identifier); |
|
| 94 | 3 | if (true === $createFolder) { |
|
| 95 | 3 | $identifier = $this->createFolder('user_upload'); |
|
| 96 | 2 | } |
|
| 97 | 3 | return $identifier; |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Checks if a folder exists. |
||
| 102 | * |
||
| 103 | * @param string $folderIdentifier |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | 6 | public function folderExists($folderIdentifier) |
|
| 107 | { |
||
| 108 | 6 | if ('/' === $folderIdentifier) { |
|
| 109 | return true; |
||
| 110 | } else { |
||
| 111 | 6 | return ($this->filesystem->has('/' . $folderIdentifier) && $this->filesystem->get('/' . $folderIdentifier)->isDir()); |
|
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Creates a folder, within a parent folder. |
||
| 117 | * If no parent folder is given, a root level folder will be created |
||
| 118 | * |
||
| 119 | * @param string $newFolderName |
||
| 120 | * @param string $parentFolderIdentifier |
||
| 121 | * @param bool $recursive |
||
| 122 | * @return string the Identifier of the new folder |
||
| 123 | */ |
||
| 124 | 9 | public function createFolder($newFolderName, $parentFolderIdentifier = '', $recursive = false) |
|
| 125 | { |
||
| 126 | 9 | $parentFolderIdentifier = $this->canonicalizeAndCheckFolderIdentifier($parentFolderIdentifier); |
|
| 127 | 9 | $newFolderName = trim($newFolderName, '/'); |
|
| 128 | 9 | if (false === $recursive) { |
|
| 129 | 9 | $newFolderName = $this->sanitizeFileName($newFolderName); |
|
| 130 | 9 | $newIdentifier = $parentFolderIdentifier . $newFolderName . '/'; |
|
| 131 | 9 | $this->filesystem->createDir($newIdentifier); |
|
| 132 | 6 | } else { |
|
| 133 | $parts = GeneralUtility::trimExplode('/', $newFolderName); |
||
| 134 | $parts = array_map(array($this, 'sanitizeFileName'), $parts); |
||
| 135 | $newFolderName = implode('/', $parts); |
||
| 136 | $newIdentifier = $parentFolderIdentifier . $newFolderName . '/'; |
||
| 137 | } |
||
| 138 | 9 | return $newIdentifier; |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Returns the public URL to a file. |
||
| 143 | * Either fully qualified URL or relative to PATH_site (rawurlencoded). |
||
| 144 | * |
||
| 145 | * @param string $identifier |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function getPublicUrl($identifier) |
||
| 149 | { |
||
| 150 | return '/'; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Renames a folder in this storage. |
||
| 155 | * |
||
| 156 | * @param string $folderIdentifier |
||
| 157 | * @param string $newName |
||
| 158 | * @return array A map of old to new file identifiers of all affected resources |
||
| 159 | */ |
||
| 160 | 3 | public function renameFolder($folderIdentifier, $newName) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Removes a folder in filesystem. |
||
| 173 | * |
||
| 174 | * @param string $folderIdentifier |
||
| 175 | * @param bool $deleteRecursively |
||
| 176 | * @return bool |
||
| 177 | * @throws FileOperationErrorException |
||
| 178 | */ |
||
| 179 | public function deleteFolder($folderIdentifier, $deleteRecursively = false) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Checks if a file exists. |
||
| 194 | * |
||
| 195 | * @param string $fileIdentifier |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | 12 | public function fileExists($fileIdentifier) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Checks if a folder contains files and (if supported) other folders. |
||
| 208 | * |
||
| 209 | * @param string $folderIdentifier |
||
| 210 | * @return bool TRUE if there are no files and folders within $folder |
||
| 211 | */ |
||
| 212 | 3 | public function isFolderEmpty($folderIdentifier) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Adds a file from the local server hard disk to a given path in TYPO3s |
||
| 219 | * virtual file system. This assumes that the local file exists, so no |
||
| 220 | * further check is done here! After a successful the original file must |
||
| 221 | * not exist anymore. |
||
| 222 | * |
||
| 223 | * @param string $localFilePath (within PATH_site) |
||
| 224 | * @param string $targetFolderIdentifier |
||
| 225 | * @param string $newFileName optional, if not given original name is used |
||
| 226 | * @param bool $removeOriginal if set the original file will be removed |
||
| 227 | * after successful operation |
||
| 228 | * @return string the identifier of the new file |
||
| 229 | */ |
||
| 230 | 3 | public function addFile($localFilePath, $targetFolderIdentifier, $newFileName = '', $removeOriginal = true) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Creates a new (empty) file and returns the identifier. |
||
| 259 | * |
||
| 260 | * @param string $fileName |
||
| 261 | * @param string $parentFolderIdentifier |
||
| 262 | * @return string |
||
| 263 | * @throws InvalidFileNameException |
||
| 264 | */ |
||
| 265 | 3 | public function createFile($fileName, $parentFolderIdentifier) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Copies a file *within* the current storage. |
||
| 291 | * Note that this is only about an inner storage copy action, |
||
| 292 | * where a file is just copied to another folder in the same storage. |
||
| 293 | * |
||
| 294 | * @param string $fileIdentifier |
||
| 295 | * @param string $targetFolderIdentifier |
||
| 296 | * @param string $fileName |
||
| 297 | * @return string the Identifier of the new file |
||
| 298 | */ |
||
| 299 | public function copyFileWithinStorage($fileIdentifier, $targetFolderIdentifier, $fileName) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Renames a file in this storage. |
||
| 311 | * |
||
| 312 | * @param string $fileIdentifier |
||
| 313 | * @param string $newName The target path (including the file name!) |
||
| 314 | * @return string The identifier of the file after renaming |
||
| 315 | */ |
||
| 316 | public function renameFile($fileIdentifier, $newName) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Replaces a file with file in local file system. |
||
| 327 | * |
||
| 328 | * @param string $fileIdentifier |
||
| 329 | * @param string $localFilePath |
||
| 330 | * @return bool TRUE if the operation succeeded |
||
| 331 | */ |
||
| 332 | public function replaceFile($fileIdentifier, $localFilePath) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Removes a file from the filesystem. This does not check if the file is |
||
| 343 | * still used or if it is a bad idea to delete it for some other reason |
||
| 344 | * this has to be taken care of in the upper layers (e.g. the Storage)! |
||
| 345 | * |
||
| 346 | * @param string $fileIdentifier |
||
| 347 | * @return bool TRUE if deleting the file succeeded |
||
| 348 | */ |
||
| 349 | 3 | public function deleteFile($fileIdentifier) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Creates a hash for a file. |
||
| 356 | * |
||
| 357 | * @param string $fileIdentifier |
||
| 358 | * @param string $hashAlgorithm The hash algorithm to use |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | 2 | public function hash($fileIdentifier, $hashAlgorithm) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Moves a file *within* the current storage. |
||
| 368 | * Note that this is only about an inner-storage move action, |
||
| 369 | * where a file is just moved to another folder in the same storage. |
||
| 370 | * |
||
| 371 | * @param string $fileIdentifier |
||
| 372 | * @param string $targetFolderIdentifier |
||
| 373 | * @param string $newFileName |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public function moveFileWithinStorage($fileIdentifier, $targetFolderIdentifier, $newFileName) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Folder equivalent to moveFileWithinStorage(). |
||
| 383 | * |
||
| 384 | * @param string $sourceFolderIdentifier |
||
| 385 | * @param string $targetFolderIdentifier |
||
| 386 | * @param string $newFolderName |
||
| 387 | * @return array All files which are affected, map of old => new file identifiers |
||
| 388 | */ |
||
| 389 | public function moveFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Folder equivalent to copyFileWithinStorage(). |
||
| 396 | * |
||
| 397 | * @param string $sourceFolderIdentifier |
||
| 398 | * @param string $targetFolderIdentifier |
||
| 399 | * @param string $newFolderName |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | public function copyFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Returns the contents of a file. Beware that this requires to load the |
||
| 409 | * complete file into memory and also may require fetching the file from an |
||
| 410 | * external location. So this might be an expensive operation (both in terms |
||
| 411 | * of processing resources and money) for large files. |
||
| 412 | * |
||
| 413 | * @param string $fileIdentifier |
||
| 414 | * @return string The file contents |
||
| 415 | */ |
||
| 416 | 3 | public function getFileContents($fileIdentifier) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Sets the contents of a file to the specified value. |
||
| 423 | * |
||
| 424 | * @param string $fileIdentifier |
||
| 425 | * @param string $contents |
||
| 426 | * @return int The number of bytes written to the file |
||
| 427 | */ |
||
| 428 | 3 | public function setFileContents($fileIdentifier, $contents) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Checks if a file inside a folder exists |
||
| 437 | * |
||
| 438 | * @param string $fileName |
||
| 439 | * @param string $folderIdentifier |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | 3 | public function fileExistsInFolder($fileName, $folderIdentifier) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Checks if a folder inside a folder exists. |
||
| 451 | * |
||
| 452 | * @param string $folderName |
||
| 453 | * @param string $folderIdentifier |
||
| 454 | * @return bool |
||
| 455 | */ |
||
| 456 | 3 | public function folderExistsInFolder($folderName, $folderIdentifier) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Returns a path to a local copy of a file for processing it. When changing the |
||
| 465 | * file, you have to take care of replacing the current version yourself! |
||
| 466 | * |
||
| 467 | * @param string $fileIdentifier |
||
| 468 | * @param bool $writable Set this to FALSE if you only need the file for read |
||
| 469 | * operations. This might speed up things, e.g. by using |
||
| 470 | * a cached local version. Never modify the file if you |
||
| 471 | * have set this flag! |
||
| 472 | * @return string The path to the file on the local disk |
||
| 473 | */ |
||
| 474 | public function getFileForLocalProcessing($fileIdentifier, $writable = true) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Returns the permissions of a file/folder as an array |
||
| 485 | * (keys r, w) of boolean flags |
||
| 486 | * |
||
| 487 | * @param string $identifier |
||
| 488 | * @return array |
||
| 489 | */ |
||
| 490 | public function getPermissions($identifier) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Directly output the contents of the file to the output |
||
| 500 | * buffer. Should not take care of header files or flushing |
||
| 501 | * buffer before. Will be taken care of by the Storage. |
||
| 502 | * |
||
| 503 | * @param string $identifier |
||
| 504 | * @return void |
||
| 505 | */ |
||
| 506 | public function dumpFileContents($identifier) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Checks if a given identifier is within a container, e.g. if |
||
| 516 | * a file or folder is within another folder. |
||
| 517 | * This can e.g. be used to check for web-mounts. |
||
| 518 | * |
||
| 519 | * Hint: this also needs to return TRUE if the given identifier |
||
| 520 | * matches the container identifier to allow access to the root |
||
| 521 | * folder of a filemount. |
||
| 522 | * |
||
| 523 | * @param string $folderIdentifier |
||
| 524 | * @param string $identifier identifier to be checked against $folderIdentifier |
||
| 525 | * @return bool TRUE if $content is within or matches $folderIdentifier |
||
| 526 | */ |
||
| 527 | public function isWithin($folderIdentifier, $identifier) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Returns information about a file. |
||
| 544 | * |
||
| 545 | * @param string $fileIdentifier |
||
| 546 | * @param array $propertiesToExtract Array of properties which are be extracted |
||
| 547 | * If empty all will be extracted |
||
| 548 | * @return array |
||
| 549 | */ |
||
| 550 | public function getFileInfoByIdentifier($fileIdentifier, array $propertiesToExtract = []) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Returns information about a file. |
||
| 563 | * |
||
| 564 | * @param string $folderIdentifier |
||
| 565 | * @return array |
||
| 566 | */ |
||
| 567 | 3 | public function getFolderInfoByIdentifier($folderIdentifier) |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Returns the identifier of a file inside the folder |
||
| 580 | * |
||
| 581 | * @param string $fileName |
||
| 582 | * @param string $folderIdentifier |
||
| 583 | * @return string file identifier |
||
| 584 | */ |
||
| 585 | public function getFileInFolder($fileName, $folderIdentifier) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Returns a list of files inside the specified path |
||
| 592 | * |
||
| 593 | * @param string $folderIdentifier |
||
| 594 | * @param int $start |
||
| 595 | * @param int $numberOfItems |
||
| 596 | * @param bool $recursive |
||
| 597 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
| 598 | * @param string $sort Property name used to sort the items. |
||
| 599 | * Among them may be: '' (empty, no sorting), name, |
||
| 600 | * fileext, size, tstamp and rw. |
||
| 601 | * If a driver does not support the given property, it |
||
| 602 | * should fall back to "name". |
||
| 603 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
| 604 | * @return array of FileIdentifiers |
||
| 605 | */ |
||
| 606 | 3 | public function getFilesInFolder( |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Returns the identifier of a folder inside the folder |
||
| 633 | * |
||
| 634 | * @param string $folderName The name of the target folder |
||
| 635 | * @param string $folderIdentifier |
||
| 636 | * @return string folder identifier |
||
| 637 | */ |
||
| 638 | 3 | public function getFolderInFolder($folderName, $folderIdentifier) |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Returns a list of folders inside the specified path |
||
| 646 | * |
||
| 647 | * @param string $folderIdentifier |
||
| 648 | * @param int $start |
||
| 649 | * @param int $numberOfItems |
||
| 650 | * @param bool $recursive |
||
| 651 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
| 652 | * @param string $sort Property name used to sort the items. |
||
| 653 | * Among them may be: '' (empty, no sorting), name, |
||
| 654 | * fileext, size, tstamp and rw. |
||
| 655 | * If a driver does not support the given property, it |
||
| 656 | * should fall back to "name". |
||
| 657 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
| 658 | * @return array of Folder Identifier |
||
| 659 | * @TODO: Implement pagination with $start and $numberOfItems |
||
| 660 | * @TODO: Implement directory filter callbacks |
||
| 661 | * @TODO: Implement sorting |
||
| 662 | */ |
||
| 663 | 3 | public function getFoldersInFolder( |
|
| 688 | |||
| 689 | /** |
||
| 690 | * Returns the number of files inside the specified path |
||
| 691 | * |
||
| 692 | * @param string $folderIdentifier |
||
| 693 | * @param bool $recursive |
||
| 694 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
| 695 | * @return int Number of files in folder |
||
| 696 | * @TODO: Implement recursive count |
||
| 697 | * @TODO: Implement filename filtering |
||
| 698 | */ |
||
| 699 | public function countFilesInFolder($folderIdentifier, $recursive = false, array $filenameFilterCallbacks = []) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Returns the number of folders inside the specified path |
||
| 707 | * |
||
| 708 | * @param string $folderIdentifier |
||
| 709 | * @param bool $recursive |
||
| 710 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
| 711 | * @return int Number of folders in folder |
||
| 712 | */ |
||
| 713 | 3 | public function countFoldersInFolder($folderIdentifier, $recursive = false, array $folderNameFilterCallbacks = []) |
|
| 726 | |||
| 727 | /** |
||
| 728 | * Extracts information about a file from the filesystem. |
||
| 729 | * |
||
| 730 | * @param string $filePath The absolute path to the file |
||
| 731 | * @param string $containerPath The relative path to the file's container |
||
| 732 | * @param array $propertiesToExtract array of properties which should be returned, if empty all will be extracted |
||
| 733 | * @return array |
||
| 734 | */ |
||
| 735 | protected function extractFileInformation($filePath, $containerPath, array $propertiesToExtract = array()) |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Extracts a specific FileInformation from the FileSystems. |
||
| 761 | * |
||
| 762 | * @param string $fileIdentifier |
||
| 763 | * @param string $containerPath |
||
| 764 | * @param string $property |
||
| 765 | * |
||
| 766 | * @return bool|int|string |
||
| 767 | * @throws \InvalidArgumentException |
||
| 768 | */ |
||
| 769 | public function getSpecificFileInformation($fileIdentifier, $containerPath, $property) |
||
| 799 | } |
||
| 800 |