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.

56 lines
1.6 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) ||
  20. ! $this->storage->exists($this->infoFilePath)
  21. ) {
  22. // Handle this case, must report result to user.
  23. return;
  24. }
  25. $documentContent = $this->storage->get($this->documentFilePath);
  26. $info = json_decode($this->storage->get($this->infoFilePath), true);
  27. $searchAndDisplace = new SearchAndDisplace($documentContent, $info);
  28. $originalDocumentPath = $info['document_path'];
  29. $pathDetails = pathinfo($originalDocumentPath);
  30. $resultedDocumentPath = $pathDetails['dirname'] . '/' . $pathDetails['filename'] . '-displaced.md';
  31. try {
  32. $resultedDocumentContent = $searchAndDisplace->execute();
  33. file_put_contents($resultedDocumentPath, $resultedDocumentContent);
  34. } catch (\Exception $exception) {
  35. \Illuminate\Support\Facades\Log::info('exception: ' . $exception->getMessage());
  36. return;
  37. } finally {
  38. $this->storage->delete($this->documentFilePath);
  39. $this->storage->delete($this->infoFilePath);
  40. }
  41. }
  42. }