1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tomaj\NetteApi\Component; |
4
|
|
|
|
5
|
|
|
use Nette\Application\UI\Control; |
6
|
|
|
use Nette\ComponentModel\IContainer; |
7
|
|
|
use Tomaj\NetteApi\ApiDecider; |
8
|
|
|
use Closure; |
9
|
|
|
use Exception; |
10
|
|
|
|
11
|
|
|
class ApiListingControl extends Control |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ApiDecider |
15
|
|
|
*/ |
16
|
|
|
private $apiDecider; |
17
|
|
|
|
18
|
|
|
private $clickCallback; |
19
|
|
|
|
20
|
|
|
public function __construct(IContainer $parent, $name, ApiDecider $apiDecider) |
21
|
|
|
{ |
22
|
|
|
parent::__construct(null, $name); |
23
|
|
|
$this->apiDecider = $apiDecider; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
public function onClick(Closure $callback) |
28
|
|
|
{ |
29
|
|
|
$this->clickCallback = $callback; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function render() |
33
|
|
|
{ |
34
|
|
|
$handlers = $this->apiDecider->getHandlers(); |
35
|
|
|
$this->template->handlers = $this->sortHandlers($handlers); |
|
|
|
|
36
|
|
|
$this->template->setFile(__DIR__ . '/api_listing.latte'); |
|
|
|
|
37
|
|
|
$this->template->render(); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function handleSelect($method, $version, $package, $apiAction) |
41
|
|
|
{ |
42
|
|
|
if (!$this->clickCallback) { |
43
|
|
|
throw new Exception('You have to set onClick callback to component!'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->clickCallback->__invoke($method, $version, $package, $apiAction); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function sortHandlers($handlers) |
50
|
|
|
{ |
51
|
|
|
$versionHandlers = []; |
52
|
|
|
foreach ($handlers as $handler) { |
53
|
|
|
$endPoint = $handler['endpoint']; |
54
|
|
|
if (!isset($versionHandlers[$endPoint->getVersion()])) { |
55
|
|
|
$versionHandlers[$endPoint->getVersion()] = []; |
56
|
|
|
} |
57
|
|
|
$versionHandlers[$endPoint->getVersion()][] = $handler; |
58
|
|
|
} |
59
|
|
|
return $versionHandlers; |
60
|
|
|
} |
61
|
|
|
} |
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.