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\AbstractController; |
14
|
|
|
use Symfony\Component\Form\FormError; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
18
|
|
|
use Ynlo\GraphQLBundle\GraphiQL\AuthenticationFailedException; |
19
|
|
|
use Ynlo\GraphQLBundle\GraphiQL\GraphiQLAuthenticationProviderInterface; |
20
|
|
|
use Ynlo\GraphQLBundle\GraphiQL\GraphiQLRequest; |
21
|
|
|
|
22
|
|
|
class ExplorerController extends AbstractController |
23
|
|
|
{ |
24
|
|
|
private $config; |
25
|
|
|
private $provider; |
26
|
|
|
|
27
|
|
|
public function __construct(array $config, GraphiQLAuthenticationProviderInterface $provider = null) |
28
|
|
|
{ |
29
|
|
|
$this->config = $config; |
30
|
|
|
$this->provider = $provider; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function explorer(Request $request): Response |
34
|
|
|
{ |
35
|
|
|
$form = null; |
36
|
|
|
$authenticationError = null; |
37
|
|
|
$isAuthenticated = false; |
38
|
|
|
|
39
|
|
|
if ($this->provider) { |
40
|
|
|
if ($this->provider->requireUserData()) { |
41
|
|
|
$builder = $this->createFormBuilder(); |
42
|
|
|
$this->provider->buildUserForm($builder); |
43
|
|
|
$form = $builder->getForm(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($request->get('logout')) { |
47
|
|
|
$this->provider->logout(); |
48
|
|
|
|
49
|
|
|
return $this->redirectToRoute('api_explore'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$form->handleRequest($request); |
53
|
|
|
$response = null; |
|
|
|
|
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
if ($form && $form->isSubmitted() && $form->isValid()) { |
57
|
|
|
$this->provider->login($form); |
58
|
|
|
} elseif (!$form) { |
59
|
|
|
$this->provider->login(); |
60
|
|
|
} |
61
|
|
|
} catch (AuthenticationFailedException $exception) { |
62
|
|
|
if ($form) { |
63
|
|
|
$form->addError(new FormError($exception->getMessage())); |
64
|
|
|
} else { |
65
|
|
|
$authenticationError = $exception->getMessage(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$isAuthenticated = $this->provider->isAuthenticated(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($this->config['documentation']['link'] ?? null) { |
73
|
|
|
try { |
74
|
|
|
$this->config['documentation']['link'] = $this->container |
75
|
|
|
->get('router') |
76
|
|
|
->generate($this->config['documentation']['link']); |
77
|
|
|
} catch (RouteNotFoundException $exception) { |
78
|
|
|
//do nothing, use the link as is |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->render($this->config['template'], [ |
83
|
|
|
'form' => $form ? $form->createView() : null, |
84
|
|
|
'favicon' => $this->config['favicon'] ?? null, |
85
|
|
|
'documentation' => $this->config['documentation'] ?? [], |
86
|
|
|
'isAuthenticated' => $isAuthenticated, |
87
|
|
|
'title' => $this->config['title'], |
88
|
|
|
'authenticationEnabled' => (bool) $this->provider, |
89
|
|
|
'authenticationRequired' => $this->config['authentication']['required'], |
90
|
|
|
'authenticationError' => $authenticationError, |
91
|
|
|
'hasAuthenticationError' => $authenticationError || ($form && $form->getErrors(true)->count()), |
92
|
|
|
'loginMessage' => $this->config['authentication']['login_message'], |
93
|
|
|
'dataWarningMessage' => $this->config['data_warning_message'], |
94
|
|
|
'dataWarningDismissible' => $this->config['data_warning_dismissible'], |
95
|
|
|
'dataWarningStyle' => $this->config['data_warning_style'], |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function graphiQL() |
100
|
|
|
{ |
101
|
|
|
$params = []; |
102
|
|
|
if (isset($_COOKIE['XDEBUG_SESSION'])) { |
103
|
|
|
$params['XDEBUG_SESSION_START'] = 'PHPSTORM'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$request = new GraphiQLRequest( |
107
|
|
|
$this->generateUrl('api_root'), |
108
|
|
|
$params, |
109
|
|
|
[ |
110
|
|
|
'Accept' => 'application/json', |
111
|
|
|
'Content-Type' => 'application/json', |
112
|
|
|
] |
113
|
|
|
); |
114
|
|
|
if ($this->provider) { |
115
|
|
|
$this->provider->prepareRequest($request); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$defaultQuery = $this->config['default_query']; |
119
|
|
|
if ($defaultQuery) { |
120
|
|
|
//escape any simple quote |
121
|
|
|
//and convert newlines to javascript multiline concatenation |
122
|
|
|
$defaultQuery = str_replace(["'", "\n"], ["\'", "\\n' + \n'"], $this->config['default_query']); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->render('@YnloGraphQL/graphiql.html.twig', [ |
126
|
|
|
'url' => $request->getUrl(), |
127
|
|
|
'method' => 'post', |
128
|
|
|
'headers' => $request->getHeaders(), |
129
|
|
|
'defaultQuery' => $defaultQuery, |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|