5
5
use Symfony \Bundle \FrameworkBundle \Command \ContainerAwareCommand ;
6
6
use Symfony \Component \Config \Resource \DirectoryResource ;
7
7
use Symfony \Component \Config \Resource \FileResource ;
8
- use Symfony \Component \Console \Input \InputArgument ;
9
8
use Symfony \Component \Console \Input \InputInterface ;
10
- use Symfony \Component \Console \Input \InputOption ;
11
9
use Symfony \Component \Console \Output \OutputInterface ;
12
10
use Symfony \Component \Console \Question \ConfirmationQuestion ;
13
11
14
12
class GraphQLConfigureCommand extends ContainerAwareCommand
15
13
{
14
+ const PROJECT_NAMESPACE = 'App ' ;
15
+
16
+ /**
17
+ * {@inheritdoc}
18
+ */
16
19
protected function configure ()
17
20
{
18
21
$ this
19
22
->setName ('graphql:configure ' )
20
23
->setDescription ('Generates GraphQL Schema class ' )
21
- ->addArgument ('bundle ' , InputArgument::OPTIONAL , 'Bundle to generate class to ' , 'AppBundle ' )
22
24
->addOption ('composer ' );
23
25
}
24
26
27
+ /**
28
+ * {@inheritdoc}
29
+ */
25
30
protected function execute (InputInterface $ input , OutputInterface $ output )
26
31
{
27
- $ bundleName = $ input ->getArgument ('bundle ' );
28
32
$ isComposerCall = $ input ->getOption ('composer ' );
29
- if (substr ($ bundleName , -6 ) != 'Bundle ' ) $ bundleName .= 'Bundle ' ;
30
-
31
- $ container = $ this ->getContainer ();
32
33
33
- $ rootDir = $ container ->getParameter ('kernel.root_dir ' );
34
- $ configFile = $ rootDir . '/../app/config/config.yml ' ;
35
- try {
36
- $ bundle = $ container ->get ('kernel ' )->getBundle ($ bundleName );
37
- } catch (\InvalidArgumentException $ e ) {
38
- $ output ->writeln ('There is no active bundleName: ' . $ bundleName );
39
- return ;
40
- }
34
+ $ container = $ this ->getContainer ();
35
+ $ rootDir = $ container ->getParameter ('kernel.root_dir ' );
36
+ $ configFile = $ rootDir . DIRECTORY_SEPARATOR . '.. ' . DIRECTORY_SEPARATOR . 'config/packages/graphql.yml ' ;
41
37
42
- $ className = 'Schema ' ;
43
- $ bundleNameSpace = $ bundle -> getNameSpace () . '\\GraphQL ' ;
44
- $ graphqlPath = $ bundle -> getPath ( ) . ' / GraphQL/ ' ;
45
- $ classPath = $ graphqlPath . ' / ' . $ className . '.php ' ;
38
+ $ className = 'Schema ' ;
39
+ $ schemaNamespace = self :: PROJECT_NAMESPACE . '\\GraphQL ' ;
40
+ $ graphqlPath = rtrim ( $ rootDir , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . ' GraphQL ' ;
41
+ $ classPath = $ graphqlPath . DIRECTORY_SEPARATOR . $ className . '.php ' ;
46
42
47
43
$ inputHelper = $ this ->getHelper ('question ' );
48
44
if (file_exists ($ classPath )) {
49
45
if (!$ isComposerCall ) {
50
- $ output ->writeln (sprintf ('Schema class %s was found. ' , $ bundleNameSpace . '\\' . $ className ));
46
+ $ output ->writeln (sprintf ('Schema class %s was found. ' , $ schemaNamespace . '\\' . $ className ));
51
47
}
52
48
} else {
53
- $ question = new ConfirmationQuestion (sprintf ('Confirm creating class at %s ? [Y/n] ' , $ bundleNameSpace . '\\' . $ className ), true );
49
+ $ question = new ConfirmationQuestion (sprintf ('Confirm creating class at %s ? [Y/n] ' , $ schemaNamespace . '\\' . $ className ), true );
54
50
if (!$ inputHelper ->ask ($ input , $ output , $ question )) {
55
51
return ;
56
52
}
57
53
58
54
if (!is_dir ($ graphqlPath )) {
59
55
mkdir ($ graphqlPath , 0777 , true );
60
56
}
61
- file_put_contents ($ classPath , $ this ->getSchemaClassTemplate ($ bundleNameSpace , $ className ));
57
+ file_put_contents ($ classPath , $ this ->getSchemaClassTemplate ($ schemaNamespace , $ className ));
62
58
63
59
$ output ->writeln ('Schema file has been created at ' );
64
60
$ output ->writeln ($ classPath . "\n" );
65
61
62
+ if (!file_exists ($ configFile )) {
63
+ $ question = new ConfirmationQuestion (sprintf ('Config file not found (look at %s). Create it? [Y/n] ' , $ configFile ), true );
64
+ if (!$ inputHelper ->ask ($ input , $ output , $ question )) {
65
+ return ;
66
+ }
67
+
68
+ touch ($ configFile );
69
+ }
70
+
66
71
$ originalConfigData = file_get_contents ($ configFile );
67
72
if (strpos ($ originalConfigData , 'graphql ' ) === false ) {
68
- $ configData = <<<CONFIG
73
+ $ projectNameSpace = self ::PROJECT_NAMESPACE ;
74
+ $ configData = <<<CONFIG
69
75
graphql:
70
- schema_class: " {$ bundleName }\\\\GraphQL\\\\ {$ className }"
71
-
76
+ schema_class: " {$ projectNameSpace }\\\\GraphQL\\\\ {$ className }"
72
77
73
78
CONFIG ;
74
79
file_put_contents ($ configFile , $ configData . $ originalConfigData );
@@ -77,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
77
82
if (!$ this ->graphQLRouteExists ()) {
78
83
$ question = new ConfirmationQuestion ('Confirm adding GraphQL route? [Y/n] ' , true );
79
84
$ resource = $ this ->getMainRouteConfig ();
80
- if ($ resource && ! $ inputHelper ->ask ($ input , $ output , $ question )) {
85
+ if ($ resource && $ inputHelper ->ask ($ input , $ output , $ question )) {
81
86
$ routeConfigData = <<<CONFIG
82
87
83
88
graphql:
@@ -93,25 +98,34 @@ protected function execute(InputInterface $input, OutputInterface $output)
93
98
}
94
99
}
95
100
101
+ /**
102
+ * @return null|string
103
+ *
104
+ * @throws \Exception
105
+ */
96
106
protected function getMainRouteConfig ()
97
107
{
98
108
$ routerResources = $ this ->getContainer ()->get ('router ' )->getRouteCollection ()->getResources ();
99
109
foreach ($ routerResources as $ resource ) {
100
110
/** @var FileResource|DirectoryResource $resource */
101
- if (substr ($ resource ->getResource (), -11 ) == 'routing.yml ' ) {
111
+ if (method_exists ( $ resource , ' getResource ' ) && substr ($ resource ->getResource (), -11 ) == 'routes.yaml ' ) {
102
112
return $ resource ->getResource ();
103
113
}
104
114
}
105
115
106
116
return null ;
107
117
}
108
118
119
+ /**
120
+ * @return bool
121
+ * @throws \Exception
122
+ */
109
123
protected function graphQLRouteExists ()
110
124
{
111
125
$ routerResources = $ this ->getContainer ()->get ('router ' )->getRouteCollection ()->getResources ();
112
126
foreach ($ routerResources as $ resource ) {
113
127
/** @var FileResource|DirectoryResource $resource */
114
- if (strpos ($ resource ->getResource (), 'GraphQLController.php ' ) !== false ) {
128
+ if (method_exists ( $ resource , ' getResource ' ) && strpos ($ resource ->getResource (), 'GraphQLController.php ' ) !== false ) {
115
129
return true ;
116
130
}
117
131
}
@@ -124,15 +138,15 @@ protected function generateRoutes()
124
138
125
139
}
126
140
127
- protected function getSchemaClassTemplate ($ bundleNameSpace , $ className = 'Schema ' )
141
+ protected function getSchemaClassTemplate ($ nameSpace , $ className = 'Schema ' )
128
142
{
129
143
$ tpl = <<<TEXT
130
144
<?php
131
145
/**
132
146
* This class was automatically generated by GraphQL Schema generator
133
147
*/
134
148
135
- namespace $ bundleNameSpace ;
149
+ namespace $ nameSpace ;
136
150
137
151
use Youshido\GraphQL\Schema\AbstractSchema;
138
152
use Youshido\GraphQL\Config\Schema\SchemaConfig;
@@ -165,5 +179,4 @@ public function build(SchemaConfig \$config)
165
179
166
180
return $ tpl ;
167
181
}
168
-
169
182
}
0 commit comments