|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* View Component Coffee Machine |
|
4
|
|
|
* Webino Example |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
use WebinoAppLib\Event\RouteEvent; |
|
8
|
|
|
use WebinoAppLib\Response\Content\SourcePreview; |
|
9
|
|
|
use WebinoAppLib\Response\ViewResponse; |
|
10
|
|
|
use WebinoAppLib\Router\DefaultRoute; |
|
11
|
|
|
use WebinoConfigLib\Feature\Route; |
|
12
|
|
|
use WebinoDomLib\Event\RenderEvent; |
|
13
|
|
|
use WebinoViewLib\Component\AbstractViewComponent; |
|
14
|
|
|
use WebinoViewLib\Component\OnRenderComponentInterface; |
|
15
|
|
|
use WebinoViewLib\Feature\CommonView; |
|
16
|
|
|
use WebinoViewLib\Feature\NodeView; |
|
17
|
|
|
use WebinoViewLib\Feature\ViewTemplateMap; |
|
18
|
|
|
|
|
19
|
|
|
require __DIR__ . '/../../vendor/autoload.php'; |
|
20
|
|
|
|
|
21
|
|
|
// TODO |
|
22
|
|
|
|
|
23
|
|
|
class CoffeeMachineComponent extends AbstractViewComponent implements |
|
|
|
|
|
|
24
|
|
|
OnRenderComponentInterface, |
|
25
|
|
|
\WebinoViewLib\Component\OnDispatchInterface, |
|
26
|
|
|
\WebinoAppLib\Service\Initializer\RoutingAwareInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use WebinoAppLib\Service\Initializer\RoutingAwareTrait; |
|
29
|
|
|
use \WebinoAppLib\Listener\RouteListenerTrait; |
|
30
|
|
|
|
|
31
|
|
|
private function getIndex() |
|
32
|
|
|
{ |
|
33
|
|
|
return 0; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function configure(NodeView $node) |
|
37
|
|
|
{ |
|
38
|
|
|
$node |
|
39
|
|
|
->setLocator('coffee-machine') |
|
40
|
|
|
->setSnippet('snippet', 'my/snippets/coffee-machine') |
|
41
|
|
|
->setReplace('{$snippet}') |
|
42
|
|
|
->setView([ |
|
43
|
|
|
(new NodeView('display')) |
|
44
|
|
|
->setRename('p') |
|
45
|
|
|
->setValue('Coffee Machine Display'), |
|
46
|
|
|
|
|
47
|
|
|
(new NodeView('buy')) |
|
48
|
|
|
->setRename('a') |
|
49
|
|
|
->setAttribute('href', '#') |
|
50
|
|
|
->setValue('Buy coffee'), |
|
51
|
|
|
]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function onBuyCoffee(RouteEvent $event) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
// TODO concept |
|
57
|
|
|
$this->setState(); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function onDispatch(\WebinoAppLib\Event\DispatchEvent $event) |
|
61
|
|
|
{ |
|
62
|
|
|
// TODO |
|
63
|
|
|
$state = $event->getRequest()->getQuery()->coffeeMachine[$this->getIndex()]; |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
dd($state); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// TODO concept |
|
69
|
|
|
public function getInitialState() |
|
70
|
|
|
{ |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function onRender(RenderEvent $event) |
|
75
|
|
|
{ |
|
76
|
|
|
// TODO common |
|
77
|
|
|
$href = $this->getRouter()->url(null)->setOption('query', ['coffeeMachine' => [$this->getIndex() => ['bought' => 1]]]); |
|
78
|
|
|
// die($href); |
|
|
|
|
|
|
79
|
|
|
$event->getNode('buy')->setAttribute('href', $href); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Initialize listener |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function init() |
|
86
|
|
|
{ |
|
87
|
|
|
parent::init(); |
|
88
|
|
|
|
|
89
|
|
|
// TODO common |
|
|
|
|
|
|
90
|
|
|
// $this->listenRoute('coffeeMachineBuy', 'onBuyCoffee'); |
|
91
|
|
|
// $this->listen(RouteEvent::MATCH, 'onBuyCoffee'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
public function toArray() |
|
99
|
|
|
{ |
|
100
|
|
|
// TODO common |
|
101
|
|
|
return parent::toArray(); |
|
102
|
|
|
// return parent::toArray() + (new Route('coffeeMachineBuy'))->setLiteral('/coffee-machine-buy')->toArray(); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$config = Webino::config([ |
|
107
|
|
|
/** |
|
108
|
|
|
* Configuring view |
|
109
|
|
|
* response route. |
|
110
|
|
|
*/ |
|
111
|
|
|
(new Route('viewTest'))->setLiteral('/view-test'), |
|
112
|
|
|
|
|
113
|
|
|
new ViewTemplateMap(__DIR__ . '/view'), |
|
114
|
|
|
|
|
115
|
|
|
new CommonView([ |
|
116
|
|
|
(new NodeView('content')) |
|
117
|
|
|
->setLocator('body') |
|
118
|
|
|
->setHtml('<coffee-machine/>'), |
|
119
|
|
|
|
|
120
|
|
|
new CoffeeMachineComponent, |
|
121
|
|
|
]), |
|
122
|
|
|
]); |
|
123
|
|
|
|
|
124
|
|
|
$app = Webino::application($config)->bootstrap(); |
|
125
|
|
|
|
|
126
|
|
|
$app->bindRoute('viewTest', function (RouteEvent $event) { |
|
127
|
|
|
/** |
|
128
|
|
|
* Responding |
|
129
|
|
|
* using view. |
|
130
|
|
|
*/ |
|
131
|
|
|
$event->setResponse(new ViewResponse); |
|
132
|
|
|
}); |
|
133
|
|
|
|
|
134
|
|
|
$app->bind(DefaultRoute::class, function (RouteEvent $event) { |
|
135
|
|
|
$event->setResponse([ |
|
136
|
|
|
$event->getApp()->url('viewTest')->html('View response!'), |
|
137
|
|
|
new SourcePreview(__FILE__), |
|
138
|
|
|
]); |
|
139
|
|
|
}); |
|
140
|
|
|
|
|
141
|
|
|
$app->dispatch(); |
|
142
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.