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 ;
10
6
11
7
class DownloadAssetsCommand extends Command
12
8
{
13
- public const REACT_PATH_LOCAL = 'vendor/graphiql/react.production.min.js ' ;
14
- public const REACT_PATH_CDN = '//cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js ' ;
15
-
16
- public const REACT_DOM_PATH_LOCAL = 'vendor/graphiql/react-dom.production.min.js ' ;
17
- public const REACT_DOM_PATH_CDN = '//cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js ' ;
18
-
19
- public const JS_PATH_LOCAL = 'vendor/graphiql/graphiql.min.js ' ;
20
- public const JS_PATH_CDN = '//cdn.jsdelivr.net/npm/graphiql/graphiql.min.js ' ;
21
-
22
- public const PLUGIN_EXPLORER_PATH_LOCAL = 'vendor/graphiql/graphiql-plugin-explorer.umd.js ' ;
23
- /** Pinned because the latest version broke, see https://github.com/mll-lab/laravel-graphiql/issues/25. */
24
- public const PLUGIN_EXPLORER_PATH_CDN =
'//cdn.jsdelivr.net/npm/@graphiql/[email protected] /dist/index.umd.js ' ;
25
-
26
- public const CSS_PATH_LOCAL = 'vendor/graphiql/graphiql.min.css ' ;
27
- public const CSS_PATH_CDN = '//cdn.jsdelivr.net/npm/graphiql/graphiql.min.css ' ;
28
-
29
- public const FAVICON_PATH_LOCAL = 'vendor/graphiql/favicon.ico ' ;
30
- public const FAVICON_PATH_CDN = '//raw.githubusercontent.com/graphql/graphql.github.io/source/public/favicon.ico ' ;
31
-
32
9
protected $ signature = 'graphiql:download-assets ' ;
33
10
34
11
protected $ description = 'Download the newest version of the GraphiQL assets to serve them locally. ' ;
35
12
36
13
public function handle (): void
37
14
{
38
- $ this ->downloadFileFromCDN (self :: REACT_PATH_LOCAL , self :: REACT_PATH_CDN );
39
- $ this ->downloadFileFromCDN (self :: REACT_DOM_PATH_LOCAL , self :: REACT_DOM_PATH_CDN );
40
- $ this ->downloadFileFromCDN (self :: CSS_PATH_LOCAL , self :: CSS_PATH_CDN );
41
- $ this ->downloadFileFromCDN (self :: JS_PATH_LOCAL , self :: JS_PATH_CDN );
42
- $ this ->downloadFileFromCDN (self :: PLUGIN_EXPLORER_PATH_LOCAL , self :: PLUGIN_EXPLORER_PATH_CDN );
43
- $ this ->downloadFileFromCDN (self :: FAVICON_PATH_LOCAL , self :: FAVICON_PATH_CDN );
15
+ $ this ->downloadFileFromCDN (GraphiQLAsset:: REACT_JS_LOCAL_PATH , GraphiQLAsset:: REACT_JS_SOURCE_URL );
16
+ $ this ->downloadFileFromCDN (GraphiQLAsset:: REACT_DOM_JS_LOCAL_PATH , GraphiQLAsset:: REACT_DOM_JS_SOURCE_URL );
17
+ $ this ->downloadFileFromCDN (GraphiQLAsset:: GRAPHIQL_CSS_LOCAL_PATH , GraphiQLAsset:: GRAPHIQL_CSS_SOURCE_URL );
18
+ $ this ->downloadFileFromCDN (GraphiQLAsset:: GRAPHIQL_JS_LOCAL_PATH , GraphiQLAsset:: GRAPHIQL_JS_SOURCE_URL );
19
+ $ this ->downloadFileFromCDN (GraphiQLAsset:: PLUGIN_EXPLORER_JS_LOCAL_PATH , GraphiQLAsset:: PLUGIN_EXPLORER_JS_SOURCE_URL );
20
+ $ this ->downloadFileFromCDN (GraphiQLAsset:: FAVICON_LOCAL_PATH , GraphiQLAsset:: FAVICON_SOURCE_URL );
44
21
}
45
22
46
23
protected function downloadFileFromCDN (string $ localPath , string $ cdnPath ): void
47
24
{
48
- $ publicPath = self ::publicPath ($ localPath );
25
+ $ publicPath = GraphiQLAsset ::publicPath ($ localPath );
49
26
50
27
// Ensure the directory exists
51
28
$ directory = dirname ($ publicPath );
52
29
if (! is_dir ($ directory )) {
53
30
mkdir ($ directory , 0777 , true );
54
31
}
55
32
56
- $ contents = file_get_contents (" https: { $ cdnPath}" );
33
+ $ contents = file_get_contents ($ cdnPath );
57
34
if ($ contents === false ) {
58
35
$ error = error_get_last ();
59
36
throw new \ErrorException ($ error ['message ' ] ?? 'An error occurred ' , 0 , $ error ['type ' ] ?? 1 );
@@ -62,60 +39,39 @@ protected function downloadFileFromCDN(string $localPath, string $cdnPath): void
62
39
file_put_contents ($ publicPath , $ contents );
63
40
}
64
41
42
+ /** @deprecated use GraphiQLAsset::reactJS, this alias will be removed in the next major version */
65
43
public static function reactPath (): string
66
44
{
67
- return self :: availablePath ( self :: REACT_PATH_LOCAL , self :: REACT_PATH_CDN );
45
+ return GraphiQLAsset:: reactJS ( );
68
46
}
69
47
48
+ /** @deprecated use GraphiQLAsset::reactDOMJS, this alias will be removed in the next major version */
70
49
public static function reactDOMPath (): string
71
50
{
72
- return self :: availablePath ( self :: REACT_DOM_PATH_LOCAL , self :: REACT_DOM_PATH_CDN );
51
+ return GraphiQLAsset:: reactDOMJS ( );
73
52
}
74
53
54
+ /** @deprecated use GraphiQLAsset::graphiQLJS, this alias will be removed in the next major version */
75
55
public static function jsPath (): string
76
56
{
77
- return self :: availablePath ( self :: JS_PATH_LOCAL , self :: JS_PATH_CDN );
57
+ return GraphiQLAsset:: graphiQLJS ( );
78
58
}
79
59
60
+ /** @deprecated use GraphiQLAsset::pluginExplorerJS, this alias will be removed in the next major version */
80
61
public static function pluginExplorerPath (): string
81
62
{
82
- return self :: availablePath ( self :: PLUGIN_EXPLORER_PATH_LOCAL , self :: PLUGIN_EXPLORER_PATH_CDN );
63
+ return GraphiQLAsset:: pluginExplorerJS ( );
83
64
}
84
65
66
+ /** @deprecated use GraphiQLAsset::graphiQLCSS, this alias will be removed in the next major version */
85
67
public static function cssPath (): string
86
68
{
87
- return self :: availablePath ( self :: CSS_PATH_LOCAL , self :: CSS_PATH_CDN );
69
+ return GraphiQLAsset:: graphiQLCSS ( );
88
70
}
89
71
72
+ /** @deprecated use GraphiQLAsset::favicon, this alias will be removed in the next major version */
90
73
public static function faviconPath (): string
91
74
{
92
- return self ::availablePath (self ::FAVICON_PATH_LOCAL , self ::FAVICON_PATH_CDN );
93
- }
94
-
95
- public static function availablePath (string $ local , string $ cdn ): string
96
- {
97
- return file_exists (self ::publicPath ($ local ))
98
- ? self ::localAssetURL ($ local )
99
- : self ::cdnURL ($ cdn );
100
- }
101
-
102
- public static function publicPath (string $ path ): string
103
- {
104
- $ container = Container::getInstance ();
105
- assert ($ container instanceof LaravelApplication || $ container instanceof LumenApplication);
106
-
107
- return $ container ->basePath ("public/ {$ path }" );
108
- }
109
-
110
- public static function localAssetURL (string $ path ): string
111
- {
112
- $ url = Container::getInstance ()->make (UrlGenerator::class);
113
-
114
- return $ url ->asset ($ path );
115
- }
116
-
117
- public static function cdnURL (string $ path ): string
118
- {
119
- return str_replace ('// ' , '/ ' , $ path );
75
+ return GraphiQLAsset::favicon ();
120
76
}
121
77
}
0 commit comments