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.

61 lines
1.8 KiB

  1. <?php
  2. namespace App\SearchDisplace;
  3. use Illuminate\Support\Facades\Storage;
  4. class SearchAndDisplaceFromFiles
  5. {
  6. protected $id;
  7. protected $storage;
  8. protected $documentFilePath;
  9. protected $infoFilePath;
  10. public function __construct($id)
  11. {
  12. $this->id = $id;
  13. $this->storage = Storage::disk('local');
  14. $this->documentFilePath = "contracts/$this->id.md";
  15. $this->infoFilePath = "searchers/$this->id.json";
  16. }
  17. public function execute()
  18. {
  19. if ( ! $this->storage->exists($this->documentFilePath) || ! $this->storage->exists($this->infoFilePath)) {
  20. // Handle this case, must report result to user.
  21. return;
  22. }
  23. try {
  24. $documentContent = $this->storage->get($this->documentFilePath);
  25. $searchersContent = json_decode($this->storage->get($this->infoFilePath), true);
  26. $documentPath = $searchersContent['document_path'];
  27. $searchers = $searchersContent['searchers'];
  28. $this->storage->put($this->infoFilePath, json_encode($searchers[0]['content']));
  29. $searchAndDisplace = new SearchAndDisplace($documentContent, [
  30. 'searchers' => [
  31. [
  32. 'key' => $this->id,
  33. 'replace_with' => $searchers[0]['replace_with'],
  34. ]
  35. ],
  36. ]);
  37. $result = $searchAndDisplace->execute();
  38. file_put_contents($documentPath, $result['content']);
  39. } catch (\Exception $exception) {
  40. \Illuminate\Support\Facades\Log::info('EXCEPTION: ' . $exception->getMessage());
  41. return;
  42. } finally {
  43. $this->storage->delete($this->documentFilePath);
  44. $this->storage->delete($this->infoFilePath);
  45. }
  46. }
  47. }