|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Codebarista\LaravelEcharts\Actions; |
| 4 | + |
| 5 | +use Codebarista\LaravelEcharts\Enums\MimeTypes; |
| 6 | +use Codebarista\LaravelEcharts\Exceptions\StoreChartImageException; |
| 7 | +use Codebarista\LaravelEcharts\Makeable; |
| 8 | +use Illuminate\Support\Arr; |
| 9 | +use Illuminate\Support\Facades\File; |
| 10 | +use Illuminate\Support\Facades\Process; |
| 11 | +use Illuminate\Support\Str; |
| 12 | +use Throwable; |
| 13 | + |
| 14 | +class StoreChartImage |
| 15 | +{ |
| 16 | + use Makeable; |
| 17 | + |
| 18 | + private MimeTypes $mimeType; |
| 19 | + |
| 20 | + private string $fileName; |
| 21 | + |
| 22 | + private string $filePath; |
| 23 | + |
| 24 | + private string $fullPath; |
| 25 | + |
| 26 | + private bool $optimize; |
| 27 | + |
| 28 | + private int $width; |
| 29 | + |
| 30 | + private int $height; |
| 31 | + |
| 32 | + public function mimeType(MimeTypes $value): static |
| 33 | + { |
| 34 | + $this->mimeType = $value; |
| 35 | + |
| 36 | + return $this; |
| 37 | + } |
| 38 | + |
| 39 | + public function fileName(string $value): static |
| 40 | + { |
| 41 | + $this->fileName = $value; |
| 42 | + |
| 43 | + return $this; |
| 44 | + } |
| 45 | + |
| 46 | + public function filePath(string $value): static |
| 47 | + { |
| 48 | + $this->filePath = $value; |
| 49 | + |
| 50 | + return $this; |
| 51 | + } |
| 52 | + |
| 53 | + public function optimize(bool $value): static |
| 54 | + { |
| 55 | + $this->optimize = $value; |
| 56 | + |
| 57 | + return $this; |
| 58 | + } |
| 59 | + |
| 60 | + public function width(int $value): static |
| 61 | + { |
| 62 | + $this->width = $value; |
| 63 | + |
| 64 | + return $this; |
| 65 | + } |
| 66 | + |
| 67 | + public function height(int $value): static |
| 68 | + { |
| 69 | + $this->height = $value; |
| 70 | + |
| 71 | + return $this; |
| 72 | + } |
| 73 | + |
| 74 | + public function __construct() |
| 75 | + { |
| 76 | + $this->filePath(config('echarts.storage.path', 'app/public/vendor/echarts')); |
| 77 | + $this->mimeType(config('echarts.canvas.mime_type', MimeTypes::PNG)); |
| 78 | + $this->optimize(config('echarts.optimize.png', false)); |
| 79 | + $this->height(config('echarts.canvas.height', 600)); |
| 80 | + $this->width(config('echarts.canvas.width', 600)); |
| 81 | + $this->fileName(Str::random()); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @throws StoreChartImageException|Throwable |
| 86 | + */ |
| 87 | + public function handle(array $options): string |
| 88 | + { |
| 89 | + $process = Process::path(codebarista_path('tools/echarts')) |
| 90 | + ->run($this->getCommand($options)); |
| 91 | + |
| 92 | + throw_if($process->failed(), |
| 93 | + new StoreChartImageException($process->errorOutput())); |
| 94 | + |
| 95 | + if ($this->optimize && $this->mimeType === MimeTypes::PNG) { |
| 96 | + pngcrush($this->fullPath); |
| 97 | + } |
| 98 | + |
| 99 | + return $this->fullPath; |
| 100 | + } |
| 101 | + |
| 102 | + private function getCommand(array $options): array |
| 103 | + { |
| 104 | + $filePath = storage_path($this->filePath); |
| 105 | + |
| 106 | + File::ensureDirectoryExists($filePath); |
| 107 | + |
| 108 | + $fileName = $this->fileName.match ($this->mimeType) { |
| 109 | + MimeTypes::PDF => '.pdf', |
| 110 | + MimeTypes::JPG => '.jpg', |
| 111 | + MimeTypes::PNG => '.png', |
| 112 | + MimeTypes::SVG => '.svg' |
| 113 | + }; |
| 114 | + |
| 115 | + $this->fullPath = $filePath.DIRECTORY_SEPARATOR.$fileName; |
| 116 | + |
| 117 | + return [ |
| 118 | + 'node', 'render.js', |
| 119 | + '--options', Arr::toJson($options), |
| 120 | + '--type', $this->mimeType->value, |
| 121 | + '--path', $this->fullPath, |
| 122 | + '--height', $this->height, |
| 123 | + '--width', $this->width, |
| 124 | + ]; |
| 125 | + } |
| 126 | +} |
0 commit comments