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.

66 lines
1.6 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. $searchers[$fileName] = [
  22. 'id' => $fileName,
  23. 'name' => $result[1],
  24. 'param' => SearchersCollection::PARAM_REQUIRED
  25. ];
  26. } else if (count($result) === 3) {
  27. $searchers[$fileName] = [
  28. 'id' => $fileName,
  29. 'name' => $result[1],
  30. 'param' => $result[2]
  31. ];
  32. }
  33. }
  34. }
  35. return $searchers;
  36. }
  37. public function has($id)
  38. {
  39. return $this->storage->exists("searchers/$id.json");
  40. }
  41. public function get($id)
  42. {
  43. $contents = $this->storage->get("searchers/$id.json");
  44. return json_decode($contents, true);
  45. }
  46. public function destroy($id)
  47. {
  48. if ( ! $this->has($id)) {
  49. return false;
  50. }
  51. return $this->storage->delete("searchers/$id.json");
  52. }
  53. }