|
3 | 3 | namespace MLL\GraphiQL; |
4 | 4 |
|
5 | 5 | use Illuminate\Console\Command; |
| 6 | +use Illuminate\Container\Container; |
| 7 | +use Illuminate\Contracts\Routing\UrlGenerator; |
| 8 | +use Illuminate\Foundation\Application as LaravelApplication; |
| 9 | +use Laravel\Lumen\Application as LumenApplication; |
6 | 10 |
|
7 | 11 | class DownloadAssetsCommand extends Command |
8 | 12 | { |
@@ -30,41 +34,31 @@ class DownloadAssetsCommand extends Command |
30 | 34 |
|
31 | 35 | public function handle(): void |
32 | 36 | { |
33 | | - $this->fileForceContents( |
34 | | - self::publicPath(self::REACT_PATH_LOCAL), |
35 | | - file_get_contents('https:' . self::REACT_PATH_CDN) |
36 | | - ); |
37 | | - $this->fileForceContents( |
38 | | - self::publicPath(self::REACT_DOM_PATH_LOCAL), |
39 | | - file_get_contents('https:' . self::REACT_DOM_PATH_CDN) |
40 | | - ); |
41 | | - $this->fileForceContents( |
42 | | - self::publicPath(self::CSS_PATH_LOCAL), |
43 | | - file_get_contents('https:' . self::CSS_PATH_CDN) |
44 | | - ); |
45 | | - $this->fileForceContents( |
46 | | - self::publicPath(self::JS_PATH_LOCAL), |
47 | | - file_get_contents('https:' . self::JS_PATH_CDN) |
48 | | - ); |
49 | | - $this->fileForceContents( |
50 | | - self::publicPath(self::EXPLORER_PLUGIN_PATH_LOCAL), |
51 | | - file_get_contents('https:' . self::EXPLORER_PLUGIN_PATH_CDN) |
52 | | - ); |
53 | | - $this->fileForceContents( |
54 | | - self::publicPath(self::FAVICON_PATH_LOCAL), |
55 | | - file_get_contents('https:' . self::FAVICON_PATH_CDN) |
56 | | - ); |
| 37 | + $this->downloadFileFromCDN(self::REACT_PATH_LOCAL, self::REACT_PATH_CDN); |
| 38 | + $this->downloadFileFromCDN(self::REACT_DOM_PATH_LOCAL, self::REACT_DOM_PATH_CDN); |
| 39 | + $this->downloadFileFromCDN(self::CSS_PATH_LOCAL, self::CSS_PATH_CDN); |
| 40 | + $this->downloadFileFromCDN(self::JS_PATH_LOCAL, self::JS_PATH_CDN); |
| 41 | + $this->downloadFileFromCDN(self::EXPLORER_PLUGIN_PATH_LOCAL, self::EXPLORER_PLUGIN_PATH_CDN); |
| 42 | + $this->downloadFileFromCDN(self::FAVICON_PATH_LOCAL, self::FAVICON_PATH_CDN); |
57 | 43 | } |
58 | 44 |
|
59 | | - protected function fileForceContents(string $filePath, string $contents): void |
| 45 | + protected function downloadFileFromCDN(string $localPath, string $cdnPath): void |
60 | 46 | { |
| 47 | + $publicPath = self::publicPath($localPath); |
| 48 | + |
61 | 49 | // Ensure the directory exists |
62 | | - $directory = dirname($filePath); |
| 50 | + $directory = dirname($publicPath); |
63 | 51 | if (! is_dir($directory)) { |
64 | 52 | mkdir($directory, 0777, true); |
65 | 53 | } |
66 | 54 |
|
67 | | - file_put_contents($filePath, $contents); |
| 55 | + $contents = file_get_contents("https:{$cdnPath}"); |
| 56 | + if (false === $contents) { |
| 57 | + $error = error_get_last(); |
| 58 | + throw new \ErrorException($error['message'] ?? 'An error occurred', 0, $error['type'] ?? 1); |
| 59 | + } |
| 60 | + |
| 61 | + file_put_contents($publicPath, $cdnPath); |
68 | 62 | } |
69 | 63 |
|
70 | 64 | public static function reactPath(): string |
@@ -106,11 +100,16 @@ protected static function assetPath(string $local, string $cdn): string |
106 | 100 |
|
107 | 101 | protected static function asset(string $path): string |
108 | 102 | { |
109 | | - return app('url')->asset($path); |
| 103 | + $url = Container::getInstance()->make(UrlGenerator::class); |
| 104 | + |
| 105 | + return $url->asset($path); |
110 | 106 | } |
111 | 107 |
|
112 | 108 | protected static function publicPath(string $path): string |
113 | 109 | { |
114 | | - return app()->basePath("public/{$path}"); |
| 110 | + $container = Container::getInstance(); |
| 111 | + assert($container instanceof LaravelApplication || $container instanceof LumenApplication); |
| 112 | + |
| 113 | + return $container->basePath("public/{$path}"); |
115 | 114 | } |
116 | 115 | } |
0 commit comments