33namespace MLL \GraphiQL ;
44
55use 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 ;
106
117class DownloadAssetsCommand extends Command
128{
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-
329 protected $ signature = 'graphiql:download-assets ' ;
3310
3411 protected $ description = 'Download the newest version of the GraphiQL assets to serve them locally. ' ;
3512
3613 public function handle (): void
3714 {
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 );
4421 }
4522
4623 protected function downloadFileFromCDN (string $ localPath , string $ cdnPath ): void
4724 {
48- $ publicPath = self ::publicPath ($ localPath );
25+ $ publicPath = GraphiQLAsset ::publicPath ($ localPath );
4926
5027 // Ensure the directory exists
5128 $ directory = dirname ($ publicPath );
5229 if (! is_dir ($ directory )) {
5330 mkdir ($ directory , 0777 , true );
5431 }
5532
56- $ contents = file_get_contents (" https: { $ cdnPath}" );
33+ $ contents = file_get_contents ($ cdnPath );
5734 if ($ contents === false ) {
5835 $ error = error_get_last ();
5936 throw new \ErrorException ($ error ['message ' ] ?? 'An error occurred ' , 0 , $ error ['type ' ] ?? 1 );
@@ -62,60 +39,39 @@ protected function downloadFileFromCDN(string $localPath, string $cdnPath): void
6239 file_put_contents ($ publicPath , $ contents );
6340 }
6441
42+ /** @deprecated use GraphiQLAsset::reactJS, this alias will be removed in the next major version */
6543 public static function reactPath (): string
6644 {
67- return self :: availablePath ( self :: REACT_PATH_LOCAL , self :: REACT_PATH_CDN );
45+ return GraphiQLAsset:: reactJS ( );
6846 }
6947
48+ /** @deprecated use GraphiQLAsset::reactDOMJS, this alias will be removed in the next major version */
7049 public static function reactDOMPath (): string
7150 {
72- return self :: availablePath ( self :: REACT_DOM_PATH_LOCAL , self :: REACT_DOM_PATH_CDN );
51+ return GraphiQLAsset:: reactDOMJS ( );
7352 }
7453
54+ /** @deprecated use GraphiQLAsset::graphiQLJS, this alias will be removed in the next major version */
7555 public static function jsPath (): string
7656 {
77- return self :: availablePath ( self :: JS_PATH_LOCAL , self :: JS_PATH_CDN );
57+ return GraphiQLAsset:: graphiQLJS ( );
7858 }
7959
60+ /** @deprecated use GraphiQLAsset::pluginExplorerJS, this alias will be removed in the next major version */
8061 public static function pluginExplorerPath (): string
8162 {
82- return self :: availablePath ( self :: PLUGIN_EXPLORER_PATH_LOCAL , self :: PLUGIN_EXPLORER_PATH_CDN );
63+ return GraphiQLAsset:: pluginExplorerJS ( );
8364 }
8465
66+ /** @deprecated use GraphiQLAsset::graphiQLCSS, this alias will be removed in the next major version */
8567 public static function cssPath (): string
8668 {
87- return self :: availablePath ( self :: CSS_PATH_LOCAL , self :: CSS_PATH_CDN );
69+ return GraphiQLAsset:: graphiQLCSS ( );
8870 }
8971
72+ /** @deprecated use GraphiQLAsset::favicon, this alias will be removed in the next major version */
9073 public static function faviconPath (): string
9174 {
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 ();
12076 }
12177}
0 commit comments