ForcedCopy.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace League\Flysystem\Plugin;
  3. use League\Flysystem\FileExistsException;
  4. use League\Flysystem\FileNotFoundException;
  5. class ForcedCopy extends AbstractPlugin
  6. {
  7. /**
  8. * @inheritdoc
  9. */
  10. public function getMethod()
  11. {
  12. return 'forceCopy';
  13. }
  14. /**
  15. * Copies a file, overwriting any existing files.
  16. *
  17. * @param string $path Path to the existing file.
  18. * @param string $newpath The new path of the file.
  19. *
  20. * @throws FileExistsException
  21. * @throws FileNotFoundException Thrown if $path does not exist.
  22. *
  23. * @return bool True on success, false on failure.
  24. */
  25. public function handle($path, $newpath)
  26. {
  27. try {
  28. $deleted = $this->filesystem->delete($newpath);
  29. } catch (FileNotFoundException $e) {
  30. // The destination path does not exist. That's ok.
  31. $deleted = true;
  32. }
  33. if ($deleted) {
  34. return $this->filesystem->copy($path, $newpath);
  35. }
  36. return false;
  37. }
  38. }