ListWith.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace League\Flysystem\Plugin;
  3. class ListWith extends AbstractPlugin
  4. {
  5. /**
  6. * Get the method name.
  7. *
  8. * @return string
  9. */
  10. public function getMethod()
  11. {
  12. return 'listWith';
  13. }
  14. /**
  15. * List contents with metadata.
  16. *
  17. * @param string[] $keys
  18. * @param string $directory
  19. * @param bool $recursive
  20. *
  21. * @return array listing with metadata
  22. */
  23. public function handle(array $keys = [], $directory = '', $recursive = false)
  24. {
  25. $contents = $this->filesystem->listContents($directory, $recursive);
  26. foreach ($contents as $index => $object) {
  27. if ($object['type'] === 'file') {
  28. $missingKeys = array_diff($keys, array_keys($object));
  29. $contents[$index] = array_reduce($missingKeys, [$this, 'getMetadataByName'], $object);
  30. }
  31. }
  32. return $contents;
  33. }
  34. /**
  35. * Get a meta-data value by key name.
  36. *
  37. * @param array $object
  38. * @param string $key
  39. *
  40. * @return array
  41. */
  42. protected function getMetadataByName(array $object, $key)
  43. {
  44. $method = 'get' . ucfirst($key);
  45. if ( ! method_exists($this->filesystem, $method)) {
  46. throw new \InvalidArgumentException('Could not get meta-data for key: ' . $key);
  47. }
  48. $object[$key] = $this->filesystem->{$method}($object['path']);
  49. return $object;
  50. }
  51. }