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
2.1 KiB

<?php
namespace App\SearchDisplace;
use App\Events\SDAppliedOnMarkdownDocument;
use Illuminate\Support\Facades\Storage;
class SearchAndDisplaceFromFiles
{
protected $id;
protected $storage;
protected $directoryPath;
protected $infoFilePath;
public function __construct($id)
{
$this->id = $id;
$this->storage = Storage::disk('local');
$this->directoryPath = "contracts/$this->id";
$this->infoFilePath = "searchers/$this->id.json";
}
public function execute()
{
// The files don't exist, so we don't have to apply S&D.
if ( ! $this->storage->exists($this->directoryPath) || ! $this->storage->exists($this->infoFilePath)) {
return;
}
try {
$documentContent = $this->storage->get("$this->directoryPath/document.md");
$searchersContent = json_decode($this->storage->get($this->infoFilePath), true);
$documentPath = $searchersContent['document_path'];
$searchers = $searchersContent['searchers'];
$this->storage->put($this->infoFilePath, json_encode($searchers[0]['content']));
$searchAndDisplace = new SearchAndDisplace($documentContent, [
'searchers' => [
[
'key' => $this->id,
'type' => $searchers[0]['type'],
'value' => $searchers[0]['value'],
]
],
]);
$result = $searchAndDisplace->execute();
file_put_contents($documentPath, $result['content']);
SDAppliedOnMarkdownDocument::dispatch($this->id);
} catch (\Exception $exception) {
\Illuminate\Support\Facades\Log::info('EXCEPTION: ' . $exception->getMessage());
\Illuminate\Support\Facades\Log::info('EXCEPTION: ' . $exception->getTraceAsString());
return;
} finally {
$this->storage->deleteDirectory($this->directoryPath);
$this->storage->delete($this->infoFilePath);
}
}
}