|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2020 Jacob Siefer |
| 4 | + * |
| 5 | + * @see LICENSE |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Typesetsh\LaravelWrapper; |
| 10 | + |
| 11 | +use Illuminate\Contracts\Support\DeferrableProvider; |
| 12 | +use Illuminate\Support; |
| 13 | +use typesetsh\UriResolver; |
| 14 | + |
| 15 | +class ServiceProvider extends Support\ServiceProvider implements DeferrableProvider |
| 16 | +{ |
| 17 | + const CONFIG_PATH = __DIR__.'/../config/typesetsh.php'; |
| 18 | + |
| 19 | + /** |
| 20 | + * Register the service provider. |
| 21 | + */ |
| 22 | + public function register() |
| 23 | + { |
| 24 | + $this->mergeConfigFrom(self::CONFIG_PATH, 'typesetsh'); |
| 25 | + |
| 26 | + $this->registerPdfRenderer(); |
| 27 | + } |
| 28 | + |
| 29 | + protected function registerPdfRenderer(): void |
| 30 | + { |
| 31 | + $this->app->singleton('typesetsh', function ($app) { |
| 32 | + $allowedDirectories = $app['config']['typesetsh.allowed_directories'] ?? []; |
| 33 | + $allowProtocols = $app['config']['typesetsh.allowed_protocols'] ?? []; |
| 34 | + $cacheDir = $app['config']['typesetsh.cache_dir'] ?? null; |
| 35 | + $timeout = $app['config']['typesetsh.timeout'] ?? 15; |
| 36 | + |
| 37 | + $schemes = []; |
| 38 | + if ($allowProtocols) { |
| 39 | + $http = new UriResolver\Http($cacheDir, $timeout); |
| 40 | + foreach ($allowProtocols as $protocol) { |
| 41 | + $schemes[$protocol] = $http; |
| 42 | + } |
| 43 | + } |
| 44 | + if ($allowedDirectories) { |
| 45 | + $schemes['file'] = new UriResolver\File($allowedDirectories); |
| 46 | + } |
| 47 | + |
| 48 | + return new Typesetsh(new UriResolver($schemes)); |
| 49 | + }); |
| 50 | + |
| 51 | + $this->app->singleton('typesetsh.pdf', function ($app) { |
| 52 | + return new Pdf\Factory($app['typesetsh'], $app['view']); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get the services provided by the provider. |
| 58 | + */ |
| 59 | + public function provides(): array |
| 60 | + { |
| 61 | + return ['typesetsh', 'typesetsh.pdf']; |
| 62 | + } |
| 63 | +} |
0 commit comments