|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* View Component Counter Ajax |
|
4
|
|
|
* Webino Example |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
use WebinoAppLib\Event\RouteEvent; |
|
8
|
|
|
use WebinoAppLib\Response\ViewResponse; |
|
9
|
|
|
use WebinoAppLib\Router\DefaultRoute; |
|
10
|
|
|
use WebinoAppLib\View\SourcePreviewComponent; |
|
11
|
|
|
use WebinoDomLib\Event\RenderEvent; |
|
12
|
|
|
use WebinoViewLib\Component\AbstractViewComponent; |
|
13
|
|
|
use WebinoViewLib\Component\Javascript; |
|
14
|
|
|
use WebinoViewLib\Component\Stylesheet; |
|
15
|
|
|
use WebinoViewLib\Feature\CommonView; |
|
16
|
|
|
use WebinoViewLib\Feature\NodeView; |
|
17
|
|
|
|
|
18
|
|
|
require __DIR__ . '/../../vendor/autoload.php'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Custom view component |
|
22
|
|
|
*/ |
|
23
|
|
View Code Duplication |
class CounterComponent extends AbstractViewComponent |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
public function initState(\stdClass $state) |
|
26
|
|
|
{ |
|
27
|
|
|
$state->count = 0; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function configure(NodeView $node) |
|
31
|
|
|
{ |
|
32
|
|
|
$node |
|
33
|
|
|
->setLocator('counter') |
|
34
|
|
|
->setReplace('<div class="ajax-fragment btn-group"><display/><plus/><minus/></div>') |
|
35
|
|
|
->setView([ |
|
36
|
|
|
(new NodeView('display')) |
|
37
|
|
|
->setRename('span') |
|
38
|
|
|
->setAddClass('btn btn-default disabled'), |
|
39
|
|
|
|
|
40
|
|
|
(new NodeView('plus')) |
|
41
|
|
|
->setRename('a') |
|
42
|
|
|
->setAddClass('btn btn-info ajax') |
|
43
|
|
|
->setValue('+'), |
|
44
|
|
|
|
|
45
|
|
|
(new NodeView('minus')) |
|
46
|
|
|
->setRename('a') |
|
47
|
|
|
->setAddClass('btn btn-info ajax') |
|
48
|
|
|
->setValue('-'), |
|
49
|
|
|
]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function onRender(RenderEvent $event) |
|
53
|
|
|
{ |
|
54
|
|
|
$event->getNode('display') |
|
55
|
|
|
->setValue($this->getState()->count); |
|
56
|
|
|
|
|
57
|
|
|
$this->onStateChange('plus', function () { |
|
58
|
|
|
$this->getState()->count++; |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
$this->onStateChange('minus', function () { |
|
62
|
|
|
$this->getState()->count--; |
|
63
|
|
|
}); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$config = Webino::config([ |
|
68
|
|
|
new CommonView([ |
|
69
|
|
|
new Stylesheet\BootstrapV3, |
|
70
|
|
|
new Javascript\WebinoAjaxJQueryV0, |
|
71
|
|
|
new SourcePreviewComponent(__FILE__), |
|
72
|
|
|
|
|
73
|
|
|
(new NodeView('content')) |
|
74
|
|
|
->setLocator('body') |
|
75
|
|
|
->setHtml('<div class="container jumbotron"><counter/></div><source-preview/>'), |
|
76
|
|
|
|
|
77
|
|
|
new CounterComponent, |
|
78
|
|
|
]), |
|
79
|
|
|
]); |
|
80
|
|
|
|
|
81
|
|
|
$app = Webino::application($config)->bootstrap(); |
|
82
|
|
|
|
|
83
|
|
|
$app->bind(DefaultRoute::class, function (RouteEvent $event) { |
|
84
|
|
|
$event->setResponse(new ViewResponse); |
|
85
|
|
|
}); |
|
86
|
|
|
|
|
87
|
|
|
$app->dispatch(); |
|
88
|
|
|
|
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.