Compare commits
No commits in common. "18650ba7ad03821205b19f18f7cca0bb8921f033" and "6f162102ef7ceca41822d18c3e694abd926f550b" have entirely different histories.
18650ba7ad
...
6f162102ef
|
@ -10,14 +10,7 @@ use Log;
|
||||||
|
|
||||||
abstract class BaseCompiler implements Responsable
|
abstract class BaseCompiler implements Responsable
|
||||||
{
|
{
|
||||||
public function __construct(protected ?File $file = null)
|
protected File $file;
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function fromFile(string $file): static
|
|
||||||
{
|
|
||||||
return new static(new File($file, false));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function compile(Document $document): self
|
public function compile(Document $document): self
|
||||||
{
|
{
|
||||||
|
@ -48,32 +41,6 @@ abstract class BaseCompiler implements Responsable
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array<int, Documents> $documents
|
|
||||||
*/
|
|
||||||
public function merge(array $documents): self
|
|
||||||
{
|
|
||||||
$outfile = '/tmp/' . Str::uuid()->toString() . '.pdf';
|
|
||||||
$paths = array_map(fn ($document) => static::compile($document)->getPath(), $documents);
|
|
||||||
$command = collect([
|
|
||||||
'pdfjam --nup 1x1 --outfile ' . $outfile,
|
|
||||||
...array_map(fn ($path) => escapeshellarg($path), $paths),
|
|
||||||
])->implode(' ');
|
|
||||||
|
|
||||||
exec($command, $output, $returnVar);
|
|
||||||
|
|
||||||
foreach ($paths as $path) {
|
|
||||||
@unlink($path);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (0 !== $returnVar) {
|
|
||||||
Log::error('Merging failed', ['output' => $output]);
|
|
||||||
throw (new CompilerException('Compilation failed.'))->setOutput($output);
|
|
||||||
}
|
|
||||||
|
|
||||||
return static::fromFile($outfile);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function storeAs(string $directory, string $name, string $disk): string
|
public function storeAs(string $directory, string $name, string $disk): string
|
||||||
{
|
{
|
||||||
$contents = $this->file->getContent();
|
$contents = $this->file->getContent();
|
||||||
|
@ -105,7 +72,7 @@ abstract class BaseCompiler implements Responsable
|
||||||
|
|
||||||
protected function prepareForCompilation(Document $document): void
|
protected function prepareForCompilation(Document $document): void
|
||||||
{
|
{
|
||||||
$workDir = '/tmp/' . Str::uuid()->toString();
|
$workDir = '/tmp/'.Str::random(32);
|
||||||
mkdir($workDir, 0777, true);
|
mkdir($workDir, 0777, true);
|
||||||
$this->file = new File($workDir.'/'.$document->compiledFilename(), false);
|
$this->file = new File($workDir.'/'.$document->compiledFilename(), false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,8 @@ use PHPUnit\Framework\Assert;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
use ReflectionMethod;
|
use ReflectionMethod;
|
||||||
use ReflectionProperty;
|
use ReflectionProperty;
|
||||||
use Spatie\LaravelData\Data;
|
|
||||||
|
|
||||||
abstract class Document extends Data
|
abstract class Document
|
||||||
{
|
{
|
||||||
abstract public function basename(): string;
|
abstract public function basename(): string;
|
||||||
|
|
||||||
|
@ -36,15 +35,6 @@ abstract class Document extends Data
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function assertIsContent(string $content): void
|
|
||||||
{
|
|
||||||
Assert::assertEquals(
|
|
||||||
$content,
|
|
||||||
$this->renderBody(),
|
|
||||||
"Failed asserting that Content is {$content}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<int, string> $content
|
* @param array<int, string> $content
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -30,15 +30,6 @@ trait FakesCompilation
|
||||||
Assert::assertNotEmpty($validDocuments, 'Failed that TeX Document "'.$documentClass.'" has been compiled with given check.');
|
Assert::assertNotEmpty($validDocuments, 'Failed that TeX Document "'.$documentClass.'" has been compiled with given check.');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param class-string<Document> $documentClass
|
|
||||||
* @param callable(Document): bool $check
|
|
||||||
*/
|
|
||||||
public function assertCompiledContent(string $documentClass, string $content): void
|
|
||||||
{
|
|
||||||
$this->assertCompiled($documentClass, fn ($document) => $document->renderBody() === $content);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param class-string<Document> $documentClass
|
* @param class-string<Document> $documentClass
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue