11<?php
2+
23/**
34 * Date: 25.11.15
45 *
78
89namespace Youshido \GraphQLBundle \Controller ;
910
10- use Symfony \Bundle \FrameworkBundle \Controller \Controller ;
11+ use Psr \Container \ContainerInterface ;
12+ use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
1113use Symfony \Component \DependencyInjection \ContainerAwareInterface ;
14+ use Symfony \Component \DependencyInjection \ParameterBag \ParameterBagInterface ;
1215use Symfony \Component \HttpFoundation \JsonResponse ;
1316use Symfony \Component \Routing \Annotation \Route ;
14- use Youshido \GraphQL \Exception \ConfigurationException ;
1517use Youshido \GraphQLBundle \Exception \UnableToInitializeSchemaServiceException ;
1618use Youshido \GraphQLBundle \Execution \Processor ;
1719
18- class GraphQLController extends Controller
20+ class GraphQLController extends AbstractController
1921{
22+ protected $ container ;
23+ protected $ params ;
24+
25+ public function __construct (ContainerInterface $ container , ParameterBagInterface $ params )
26+ {
27+ $ this ->container = $ container ;
28+ $ this ->params = $ params ;
29+ }
30+
2031 /**
2132 * @Route("/graphql")
2233 *
@@ -36,13 +47,13 @@ public function defaultAction()
3647 );
3748 }
3849
39- if ($ this ->get ('request_stack ' )->getCurrentRequest ()->getMethod () == 'OPTIONS ' ) {
50+ if ($ this ->container -> get ('request_stack ' )->getCurrentRequest ()->getMethod () == 'OPTIONS ' ) {
4051 return $ this ->createEmptyResponse ();
4152 }
4253
4354 list ($ queries , $ isMultiQueryRequest ) = $ this ->getPayload ();
4455
45- $ queryResponses = array_map (function ($ queryData ) {
56+ $ queryResponses = array_map (function ($ queryData ) {
4657 return $ this ->executeQuery ($ queryData ['query ' ], $ queryData ['variables ' ]);
4758 }, $ queries );
4859
@@ -55,15 +66,15 @@ public function defaultAction()
5566 return $ response ;
5667 }
5768
58- private function createEmptyResponse ()
69+ protected function createEmptyResponse ()
5970 {
6071 return new JsonResponse ([], 200 , $ this ->getResponseHeaders ());
6172 }
6273
63- private function executeQuery ($ query , $ variables )
74+ protected function executeQuery ($ query , $ variables )
6475 {
6576 /** @var Processor $processor */
66- $ processor = $ this ->get ('graphql.processor ' );
77+ $ processor = $ this ->container -> get ('graphql.processor ' );
6778 $ processor ->processPayload ($ query , $ variables );
6879
6980 return $ processor ->getResponseData ();
@@ -74,9 +85,9 @@ private function executeQuery($query, $variables)
7485 *
7586 * @throws \Exception
7687 */
77- private function getPayload ()
88+ protected function getPayload ()
7889 {
79- $ request = $ this ->get ('request_stack ' )->getCurrentRequest ();
90+ $ request = $ this ->container -> get ('request_stack ' )->getCurrentRequest ();
8091 $ query = $ request ->get ('query ' , null );
8192 $ variables = $ request ->get ('variables ' , []);
8293 $ isMultiQueryRequest = false ;
@@ -135,7 +146,7 @@ private function getPayload()
135146 /**
136147 * @throws \Exception
137148 */
138- private function initializeSchemaService ()
149+ protected function initializeSchemaService ()
139150 {
140151 if ($ this ->container ->initialized ('graphql.schema ' )) {
141152 return ;
@@ -149,9 +160,9 @@ private function initializeSchemaService()
149160 *
150161 * @throws \Exception
151162 */
152- private function makeSchemaService ()
163+ protected function makeSchemaService ()
153164 {
154- if ($ this ->container ->has ($ this ->getSchemaService ())) {
165+ if ($ this ->getSchemaService () && $ this -> container ->has ($ this ->getSchemaService ())) {
155166 return $ this ->container ->get ($ this ->getSchemaService ());
156167 }
157168
@@ -175,27 +186,32 @@ private function makeSchemaService()
175186 /**
176187 * @return string
177188 */
178- private function getSchemaClass ()
189+ protected function getSchemaClass ()
179190 {
180191 return $ this ->getParameter ('graphql.schema_class ' );
181192 }
182193
183194 /**
184195 * @return string
185196 */
186- private function getSchemaService ()
197+ protected function getSchemaService ()
187198 {
188199 $ serviceName = $ this ->getParameter ('graphql.schema_service ' );
189200
190- if (substr ($ serviceName , 0 , 1 ) === '@ ' ) {
201+ if (substr ($ serviceName ?: '' , 0 , 1 ) === '@ ' ) {
191202 return substr ($ serviceName , 1 , strlen ($ serviceName ) - 1 );
192203 }
193204
194205 return $ serviceName ;
195206 }
196207
197- private function getResponseHeaders ()
208+ protected function getResponseHeaders ()
198209 {
199210 return $ this ->getParameter ('graphql.response.headers ' );
200211 }
212+
213+ protected function getParameter (string $ name ): array |bool |string |int |float |\UnitEnum |null
214+ {
215+ return $ this ->params ->get ($ name );
216+ }
201217}
0 commit comments