|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tomaj\NetteApi\Component; |
|
4
|
|
|
|
|
5
|
|
|
use Nette\Application\UI\Control; |
|
6
|
|
|
use Nette\Application\UI\Form; |
|
7
|
|
|
use Nette\Http\Request; |
|
8
|
|
|
use Tomaj\Form\Renderer\BootstrapRenderer; |
|
9
|
|
|
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; |
|
10
|
|
|
use Tomaj\NetteApi\Authorization\BearerTokenAuthorization; |
|
11
|
|
|
use Tomaj\NetteApi\Authorization\NoAuthorization; |
|
12
|
|
|
use Tomaj\NetteApi\EndpointIdentifier; |
|
13
|
|
|
use Tomaj\NetteApi\Handlers\ApiHandlerInterface; |
|
14
|
|
|
use Tomaj\NetteApi\Misc\ConsoleRequest; |
|
15
|
|
|
use Tomaj\NetteApi\Params\InputParam; |
|
16
|
|
|
|
|
17
|
|
|
class ApiConsoleControl extends Control |
|
18
|
|
|
{ |
|
19
|
|
|
private $endpoint; |
|
20
|
|
|
|
|
21
|
|
|
private $handler; |
|
22
|
|
|
|
|
23
|
|
|
private $authorization; |
|
24
|
|
|
|
|
25
|
|
|
private $request; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(Request $request, EndpointIdentifier $endpoint, ApiHandlerInterface $handler, ApiAuthorizationInterface $authorization) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct(null, 'apiconsolecontrol'); |
|
30
|
|
|
$this->endpoint = $endpoint; |
|
31
|
|
|
$this->handler = $handler; |
|
32
|
|
|
$this->authorization = $authorization; |
|
33
|
|
|
$this->request = $request; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function render() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->template->setFile(__DIR__ . '/console.latte'); |
|
|
|
|
|
|
39
|
|
|
$this->template->render(); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function createComponentConsoleForm() |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$form = new Form(); |
|
45
|
|
|
|
|
46
|
|
|
$defaults = []; |
|
47
|
|
|
|
|
48
|
|
|
$form->setRenderer(new BootstrapRenderer()); |
|
49
|
|
|
|
|
50
|
|
|
$uri = $this->request->getUrl(); |
|
51
|
|
|
$scheme = $uri->scheme; |
|
52
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { |
|
53
|
|
|
$scheme = $_SERVER['HTTP_X_FORWARDED_PROTO']; |
|
54
|
|
|
} |
|
55
|
|
|
$url = $scheme . '://' . $uri->host . '/api/' . $this->endpoint->getUrl(); |
|
56
|
|
|
|
|
57
|
|
|
$form->addText('api_url', 'Api Url'); |
|
58
|
|
|
$defaults['api_url'] = $url; |
|
59
|
|
|
|
|
60
|
|
|
$form->addText('method', 'Method'); |
|
61
|
|
|
$defaults['method'] = $this->endpoint->getMethod(); |
|
62
|
|
|
|
|
63
|
|
|
if ($this->authorization instanceof BearerTokenAuthorization) { |
|
64
|
|
|
$form->addText('token', 'Token:') |
|
65
|
|
|
->setAttribute('placeholder', 'napište token'); |
|
|
|
|
|
|
66
|
|
|
} elseif ($this->authorization instanceof NoAuthorization) { |
|
67
|
|
|
$form->addText('authorization', 'Authorization') |
|
68
|
|
|
->setDisabled(true); |
|
69
|
|
|
$defaults['authorization'] = 'No authorization - global access'; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$params = $this->handler->params(); |
|
73
|
|
|
foreach ($params as $param) { |
|
74
|
|
|
$count = $param->isMulti() ? 5 : 1; |
|
75
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
76
|
|
|
$key = $param->getKey(); |
|
77
|
|
|
if ($param->isMulti()) { |
|
78
|
|
|
$key = $key . '___' . $i; |
|
79
|
|
|
} |
|
80
|
|
|
$c = $form->addText($key, $this->getParamLabel($param)); |
|
81
|
|
|
if ($param->getAvailableValues()) { |
|
82
|
|
|
$c->setOption('description', 'available values: ' . implode(' | ', $param->getAvailableValues())); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$form->addSubmit('send', 'Otestuj') |
|
88
|
|
|
->getControlPrototype() |
|
89
|
|
|
->setName('button') |
|
90
|
|
|
->setHtml('<i class="fa fa-cloud-upload"></i> Try api'); |
|
91
|
|
|
|
|
92
|
|
|
$form->setDefaults($defaults); |
|
93
|
|
|
|
|
94
|
|
|
$form->onSuccess[] = array($this, 'formSucceeded'); |
|
95
|
|
|
return $form; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function getParamLabel(InputParam $param) |
|
99
|
|
|
{ |
|
100
|
|
|
$title = $param->getKey(); |
|
101
|
|
|
if ($param->isRequired()) { |
|
102
|
|
|
$title .= ' *'; |
|
103
|
|
|
} |
|
104
|
|
|
return $title; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function formSucceeded($form, $values) |
|
108
|
|
|
{ |
|
109
|
|
|
$url = $values['api_url']; |
|
110
|
|
|
|
|
111
|
|
|
$token = false; |
|
112
|
|
|
if (isset($values['token'])) { |
|
113
|
|
|
$token = $values['token']; |
|
114
|
|
|
unset($values['token']); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$method = $values['method']; |
|
118
|
|
|
unset($values['method']); |
|
119
|
|
|
|
|
120
|
|
|
$consoleRequest = new ConsoleRequest($this->handler); |
|
121
|
|
|
$result = $consoleRequest->makeRequest($url, $method, $values, $token); |
|
122
|
|
|
|
|
123
|
|
|
$this->template->add('response', $result); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.