1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Controller; |
12
|
|
|
|
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
14
|
|
|
use Symfony\Component\Form\FormError; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Ynlo\GraphQLBundle\GraphiQL\AuthenticationFailedException; |
18
|
|
|
use Ynlo\GraphQLBundle\GraphiQL\GraphiQLAuthenticationProviderInterface; |
19
|
|
|
use Ynlo\GraphQLBundle\GraphiQL\GraphiQLRequest; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class ExplorerController |
23
|
|
|
*/ |
24
|
|
|
class ExplorerController extends Controller |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param Request $request |
28
|
|
|
* |
29
|
|
|
* @return Response |
30
|
|
|
*/ |
31
|
|
|
public function explorerAction(Request $request) |
32
|
|
|
{ |
33
|
|
|
$form = null; |
34
|
|
|
$authenticationError = null; |
35
|
|
|
$isAuthenticated = false; |
36
|
|
|
$config = $this->getParameter('graphql.graphiql'); |
37
|
|
|
|
38
|
|
|
if ($provider = $this->getAuthenticationProvider()) { |
39
|
|
|
if ($provider->requireUserData()) { |
40
|
|
|
$builder = $this->createFormBuilder(); |
41
|
|
|
$provider->buildUserForm($builder); |
42
|
|
|
$form = $builder->getForm(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($request->get('logout')) { |
46
|
|
|
$provider->logout(); |
47
|
|
|
|
48
|
|
|
return $this->redirectToRoute('api_explore'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$form->handleRequest($request); |
52
|
|
|
$response = null; |
|
|
|
|
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
if ($form && $form->isSubmitted() && $form->isValid()) { |
56
|
|
|
$provider->login($form); |
57
|
|
|
} elseif (!$form) { |
58
|
|
|
$provider->login(); |
59
|
|
|
} |
60
|
|
|
} catch (AuthenticationFailedException $exception) { |
61
|
|
|
if ($form) { |
62
|
|
|
$form->addError(new FormError($exception->getMessage())); |
63
|
|
|
} else { |
64
|
|
|
$authenticationError = $exception->getMessage(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$isAuthenticated = $provider->isAuthenticated(); |
69
|
|
|
} else { |
70
|
|
|
if ($config['authentication']['required']) { |
71
|
|
|
throw new \RuntimeException('Configure a valid provider to use GraphiQL with authentication'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->render( |
76
|
|
|
$config['template'], |
77
|
|
|
[ |
78
|
|
|
'form' => $form ? $form->createView() : null, |
79
|
|
|
'isAuthenticated' => $isAuthenticated, |
80
|
|
|
'title' => $config['title'], |
81
|
|
|
'authenticationEnabled' => (bool) $provider, |
82
|
|
|
'authenticationRequired' => $config['authentication']['required'], |
83
|
|
|
'authenticationError' => $authenticationError, |
84
|
|
|
'hasAuthenticationError' => $authenticationError || ($form && $form->getErrors(true)->count()), |
|
|
|
|
85
|
|
|
'loginMessage' => $config['authentication']['login_message'], |
86
|
|
|
'dataWarningMessage' => $config['data_warning_message'], |
87
|
|
|
'dataWarningDismissible' => $config['data_warning_dismissible'], |
88
|
|
|
'dataWarningStyle' => $config['data_warning_style'], |
89
|
|
|
] |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return Response |
95
|
|
|
*/ |
96
|
|
|
public function graphiQLAction() |
97
|
|
|
{ |
98
|
|
|
$request = new GraphiQLRequest( |
99
|
|
|
$this->generateUrl('api_root'), |
100
|
|
|
[], |
101
|
|
|
[ |
102
|
|
|
'Accept' => 'application/json', |
103
|
|
|
'Content-Type' => 'application/json', |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
if ($provider = $this->getAuthenticationProvider()) { |
107
|
|
|
$provider->prepareRequest($request); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$params = [ |
111
|
|
|
'url' => $request->getUrl(), |
112
|
|
|
'method' => 'post', |
113
|
|
|
'headers' => $request->getHeaders(), |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
return $this->render('@YnloGraphQL/graphiql.twig', $params); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return GraphiQLAuthenticationProviderInterface|object|null |
121
|
|
|
*/ |
122
|
|
|
protected function getAuthenticationProvider() |
123
|
|
|
{ |
124
|
|
|
$providerName = $this->getParameter('graphql.graphiql_auth_provider'); |
125
|
|
|
|
126
|
|
|
if ($providerName) { |
127
|
|
|
return $this->get($providerName); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return null; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|