Repo for the search and displace core module including the interface to select files and search and displace operations to run on them. https://searchanddisplace.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB

  1. <?php
  2. namespace App\SearchDisplace\Searchers;
  3. use Illuminate\Support\Facades\Storage;
  4. class SearchersStorage
  5. {
  6. protected $storage;
  7. protected $searchers = [];
  8. public function __construct()
  9. {
  10. $this->storage = Storage::disk('local');
  11. }
  12. public function all()
  13. {
  14. $searchers = [];
  15. $files = $this->storage->files('searchers');
  16. foreach ($files as $file) {
  17. if (pathinfo($file, PATHINFO_EXTENSION) === 'json') {
  18. $fileName = pathinfo($file, PATHINFO_FILENAME);
  19. $result = explode('_', $fileName);
  20. if (count($result) < 2) {
  21. continue;
  22. }
  23. $contents = json_decode($this->storage->get($file), true);
  24. $description = $contents['description'];
  25. $searchers[$fileName] = [
  26. 'id' => $fileName,
  27. 'name' => $result[1],
  28. 'description' => $description,
  29. 'tag' => '',
  30. 'param' => SearchersCollection::PARAM_REQUIRED,
  31. ];
  32. if (count($result) === 3) {
  33. $searchers[$fileName]['tag'] = $result[2];
  34. // $searchers[$fileName]['param'] = SearchersCollection::PARAM_REQUIRED;
  35. }
  36. // if (count($result) === 3) {
  37. // $searchers[$fileName]['param'] = $result[2];
  38. // }
  39. }
  40. }
  41. return $searchers;
  42. }
  43. public function has($id)
  44. {
  45. return $this->storage->exists("searchers/$id.json");
  46. }
  47. public function get($id)
  48. {
  49. $contents = $this->storage->get("searchers/$id.json");
  50. return json_decode($contents, true);
  51. }
  52. public function destroy($id)
  53. {
  54. if ( ! $this->has($id)) {
  55. return false;
  56. }
  57. return $this->storage->delete("searchers/$id.json");
  58. }
  59. }