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
|
|
|
|
16
|
|
|
class ApiConsoleControl extends Control |
17
|
|
|
{ |
18
|
|
|
private $endpoint; |
19
|
|
|
|
20
|
|
|
private $handler; |
21
|
|
|
|
22
|
|
|
private $authorization; |
23
|
|
|
|
24
|
|
|
private $request; |
25
|
|
|
|
26
|
|
|
public function __construct(Request $request, EndpointIdentifier $endpoint, ApiHandlerInterface $handler, ApiAuthorizationInterface $authorization) |
27
|
|
|
{ |
28
|
|
|
parent::__construct(null, 'apiconsolecontrol'); |
29
|
|
|
$this->endpoint = $endpoint; |
30
|
|
|
$this->handler = $handler; |
31
|
|
|
$this->authorization = $authorization; |
32
|
|
|
$this->request = $request; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function render() |
36
|
|
|
{ |
37
|
|
|
$this->template->setFile(__DIR__ . '/console.latte'); |
|
|
|
|
38
|
|
|
$this->template->render(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function createComponentConsoleForm() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$form = new Form(); |
44
|
|
|
|
45
|
|
|
$defaults = []; |
46
|
|
|
|
47
|
|
|
$form->setRenderer(new BootstrapRenderer()); |
48
|
|
|
|
49
|
|
|
$uri = $this->request->getUrl(); |
50
|
|
|
$scheme = $uri->scheme; |
51
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { |
52
|
|
|
$scheme = $_SERVER['HTTP_X_FORWARDED_PROTO']; |
53
|
|
|
} |
54
|
|
|
$url = $scheme . '://' . $uri->host . '/api/' . $this->endpoint->getUrl(); |
55
|
|
|
|
56
|
|
|
$form->addText('api_url', 'Api Url'); |
57
|
|
|
$defaults['api_url'] = $url; |
58
|
|
|
|
59
|
|
|
$form->addText('method', 'Method'); |
60
|
|
|
$defaults['method'] = $this->endpoint->getMethod(); |
61
|
|
|
|
62
|
|
|
if ($this->authorization instanceof BearerTokenAuthorization) { |
63
|
|
|
$form->addText('token', 'Token:') |
64
|
|
|
->setAttribute('placeholder', 'napište token'); |
|
|
|
|
65
|
|
|
} elseif ($this->authorization instanceof NoAuthorization) { |
66
|
|
|
$form->addText('authorization', 'Authorization') |
67
|
|
|
->setDisabled(true); |
68
|
|
|
$defaults['authorization'] = 'No authorization - global access'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$params = $this->handler->params(); |
72
|
|
|
foreach ($params as $param) { |
73
|
|
|
$count = $param->isMulti() ? 5 : 1; |
74
|
|
|
for ($i = 0; $i < $count; $i++) { |
75
|
|
|
$key = $param->getKey(); |
76
|
|
|
if ($param->isMulti()) { |
77
|
|
|
$key = $key . '___' . $i; |
78
|
|
|
} |
79
|
|
|
$c = $form->addText($key, $param->getKey()); |
80
|
|
|
if ($param->getAvailableValues()) { |
81
|
|
|
$c->setOption('description', 'available values: ' . implode(' | ', $param->getAvailableValues())); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$form->addSubmit('send', 'Otestuj') |
87
|
|
|
->getControlPrototype() |
88
|
|
|
->setName('button') |
89
|
|
|
->setHtml('<i class="fa fa-cloud-upload"></i> Try api'); |
90
|
|
|
|
91
|
|
|
$form->setDefaults($defaults); |
92
|
|
|
|
93
|
|
|
$form->onSuccess[] = array($this, 'formSucceeded'); |
94
|
|
|
return $form; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function formSucceeded($form, $values) |
98
|
|
|
{ |
99
|
|
|
$url = $values['api_url']; |
100
|
|
|
|
101
|
|
|
$token = false; |
102
|
|
|
if (isset($values['token'])) { |
103
|
|
|
$token = $values['token']; |
104
|
|
|
unset($values['token']); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$method = $values['method']; |
108
|
|
|
unset($values['method']); |
109
|
|
|
|
110
|
|
|
$consoleRequest = new ConsoleRequest($this->handler); |
111
|
|
|
$result = $consoleRequest->makeRequest($url, $method, $values, $token); |
112
|
|
|
|
113
|
|
|
$this->template->response = $result; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |
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@property
annotation 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.