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 Ynlo\GraphQLBundle\GraphiQL\AuthenticationFailedException; |
18
|
|
|
use Ynlo\GraphQLBundle\GraphiQL\GraphiQLAuthenticationProviderInterface; |
19
|
|
|
use Ynlo\GraphQLBundle\GraphiQL\GraphiQLRequest; |
20
|
|
|
|
21
|
|
|
class ExplorerController extends AbstractController |
22
|
|
|
{ |
23
|
|
|
private $config; |
24
|
|
|
private $provider; |
25
|
|
|
|
26
|
|
|
public function __construct(array $config, GraphiQLAuthenticationProviderInterface $provider = null) |
27
|
|
|
{ |
28
|
|
|
$this->config = $config; |
29
|
|
|
$this->provider = $provider; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function explorer(Request $request): Response |
33
|
|
|
{ |
34
|
|
|
$form = null; |
35
|
|
|
$authenticationError = null; |
36
|
|
|
$isAuthenticated = false; |
37
|
|
|
|
38
|
|
|
if ($this->provider) { |
39
|
|
|
if ($this->provider->requireUserData()) { |
40
|
|
|
$builder = $this->createFormBuilder(); |
41
|
|
|
$this->provider->buildUserForm($builder); |
42
|
|
|
$form = $builder->getForm(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($request->get('logout')) { |
46
|
|
|
$this->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
|
|
|
$this->provider->login($form); |
57
|
|
|
} elseif (!$form) { |
58
|
|
|
$this->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 = $this->provider->isAuthenticated(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->render($this->config['template'], [ |
72
|
|
|
'form' => $form ? $form->createView() : null, |
73
|
|
|
'isAuthenticated' => $isAuthenticated, |
74
|
|
|
'title' => $this->config['title'], |
75
|
|
|
'authenticationEnabled' => (bool) $this->provider, |
76
|
|
|
'authenticationRequired' => $this->config['authentication']['required'], |
77
|
|
|
'authenticationError' => $authenticationError, |
78
|
|
|
'hasAuthenticationError' => $authenticationError || ($form && $form->getErrors(true)->count()), |
|
|
|
|
79
|
|
|
'loginMessage' => $this->config['authentication']['login_message'], |
80
|
|
|
'dataWarningMessage' => $this->config['data_warning_message'], |
81
|
|
|
'dataWarningDismissible' => $this->config['data_warning_dismissible'], |
82
|
|
|
'dataWarningStyle' => $this->config['data_warning_style'], |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function graphiQL() |
87
|
|
|
{ |
88
|
|
|
$request = new GraphiQLRequest( |
89
|
|
|
$this->generateUrl('api_root'), |
90
|
|
|
[], |
91
|
|
|
[ |
92
|
|
|
'Accept' => 'application/json', |
93
|
|
|
'Content-Type' => 'application/json', |
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
if ($this->provider) { |
97
|
|
|
$this->provider->prepareRequest($request); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->render('@YnloGraphQL/graphiql.html.twig', [ |
101
|
|
|
'url' => $request->getUrl(), |
102
|
|
|
'method' => 'post', |
103
|
|
|
'headers' => $request->getHeaders(), |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|