1 | <?php |
||
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) |
||
35 | |||
36 | public function render() |
||
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 | $port = ''; |
||
56 | if ($uri->scheme == 'http' && $uri->port != 80) { |
||
57 | $port = ':' . $uri->port; |
||
58 | } |
||
59 | $url = $scheme . '://' . $uri->host . $port . '/api/' . $this->endpoint->getUrl(); |
||
60 | |||
61 | $form->addText('api_url', 'Api Url'); |
||
62 | $defaults['api_url'] = $url; |
||
63 | |||
64 | $form->addText('method', 'Method'); |
||
65 | $defaults['method'] = $this->endpoint->getMethod(); |
||
66 | |||
67 | if ($this->authorization instanceof BearerTokenAuthorization) { |
||
68 | $form->addText('token', 'Token') |
||
69 | ->setAttribute('placeholder', 'Enter token'); |
||
70 | } elseif ($this->authorization instanceof NoAuthorization) { |
||
71 | $form->addText('authorization', 'Authorization') |
||
72 | ->setDisabled(true); |
||
73 | $defaults['authorization'] = 'No authorization - global access'; |
||
74 | } |
||
75 | |||
76 | $form->addCheckbox('send_session_id', 'Send session id cookie'); |
||
77 | |||
78 | $params = $this->handler->params(); |
||
79 | $jsonField = null; |
||
80 | $jsonParams = []; |
||
81 | foreach ($params as $param) { |
||
82 | $count = $param->isMulti() ? 5 : 1; |
||
83 | for ($i = 0; $i < $count; $i++) { |
||
84 | $key = $param->getKey(); |
||
85 | if ($param->isMulti()) { |
||
86 | $key = $key . '___' . $i; |
||
87 | } |
||
88 | |||
89 | if ($param->getAvailableValues() && is_array($param->getAvailableValues())) { |
||
90 | $c = $form->addSelect($key, $this->getParamLabel($param), array_combine($param->getAvailableValues(), $param->getAvailableValues())); |
||
91 | if (!$param->isRequired()) { |
||
92 | $c->setPrompt('Select ' . $this->getLabel($param)); |
||
93 | } |
||
94 | } elseif ($param->getAvailableValues() && is_string($param->getAvailableValues())) { |
||
95 | $c = $form->addText($key, $this->getParamLabel($param))->setDisabled(true); |
||
96 | $defaults[$key] = $param->getAvailableValues(); |
||
97 | } elseif ($param->getType() == InputParam::TYPE_FILE) { |
||
98 | $c = $form->addUpload($key, $this->getParamLabel($param)); |
||
99 | } elseif ($param->getType() == InputParam::TYPE_POST_RAW) { |
||
100 | $c = $form->addTextArea('post_raw', $this->getParamLabel($param)) |
||
101 | ->setAttribute('rows', 10); |
||
102 | } elseif ($param->getType() == InputParam::TYPE_POST_JSON_KEY) { |
||
103 | if ($jsonField === null) { |
||
104 | $jsonField = $form->addTextArea('post_raw', 'JSON') |
||
105 | ->setOption('description', 'Empty string means "key is required", null means "key is optional"'); |
||
106 | } |
||
107 | $jsonParams[$key] = $param->isRequired() ? '' : null; |
||
108 | } else { |
||
109 | $c = $form->addText($key, $this->getParamLabel($param)); |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | if ($jsonField !== null && $jsonParams) { |
||
114 | $jsonField->setDefaultValue(json_encode($jsonParams, JSON_PRETTY_PRINT)); |
||
115 | } |
||
116 | |||
117 | $form->addSubmit('send', 'Otestuj') |
||
118 | ->getControlPrototype() |
||
119 | ->setName('button') |
||
120 | ->setHtml('<i class="fa fa-cloud-upload"></i> Try api'); |
||
121 | |||
122 | $form->setDefaults($defaults); |
||
123 | |||
124 | $form->onSuccess[] = array($this, 'formSucceeded'); |
||
125 | return $form; |
||
126 | } |
||
127 | |||
128 | private function getLabel(InputParam $param) |
||
132 | |||
133 | private function getParamLabel(InputParam $param) |
||
142 | |||
143 | public function formSucceeded($form, $values) |
||
167 | } |
||
168 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: