1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Advanced usage example index file |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
use WebinoAppLib\Application\CoreConfig; |
7
|
|
|
use WebinoAppLib\Event\AppEvent; |
8
|
|
|
use WebinoAppLib\Event\DispatchEvent; |
9
|
|
|
use WebinoAppLib\Event\RouteEvent; |
10
|
|
|
use WebinoAppLib\Feature as AppFeature; |
11
|
|
|
use WebinoAppLib\Listener\AbstractRouteListener; |
12
|
|
|
use WebinoAppLib\Router\DefaultRoute; |
13
|
|
|
use WebinoConfigLib\Feature as ConfigFeature; |
14
|
|
|
|
15
|
|
|
require 'vendor/autoload.php'; |
16
|
|
|
|
17
|
|
|
class MyCustomFeature extends ConfigFeature\AbstractFeature |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
public function setAnything() |
20
|
|
|
{ |
21
|
|
|
return $this; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
class MyCustomListener extends AbstractRouteListener |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
public function init() |
28
|
|
|
{ |
29
|
|
|
$this->listen(AppEvent::DISPATCH, function (DispatchEvent $event) { |
30
|
|
|
$event->setResponse($event->getApp()->file()->read('docs/common.html')); |
31
|
|
|
}, AppEvent::BEGIN); |
32
|
|
|
|
33
|
|
|
$this->listen(AppEvent::DISPATCH, function (DispatchEvent $event) { |
34
|
|
|
$event->setResponse($event->getApp()->file()->read('docs/footer.html')); |
35
|
|
|
}, AppEvent::FINISH); |
36
|
|
|
|
37
|
|
|
$this->listen(DefaultRoute::class, function (RouteEvent $event) { |
38
|
|
|
$event->setResponse([ |
39
|
|
|
'<h1>Webino Application Basic Usage Example</h1>', |
40
|
|
|
$event->getApp()->url('myRoute')->html('My Route Example'), |
41
|
|
|
'<br />', |
42
|
|
|
$event->getApp()->url('myRuntimeRoute')->html('My Runtime Route Example') |
43
|
|
|
]); |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
$this->listen(RouteEvent::MATCH, function (RouteEvent $event) { |
47
|
|
|
$routeName = $event->getRouteMatch()->getMatchedRouteName(); |
48
|
|
|
if (DefaultRoute::class === $routeName) { |
49
|
|
|
$routeName = 'default'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$file = $event->getApp()->file(); |
53
|
|
|
$path = 'docs/' . $routeName . '.html'; |
54
|
|
|
|
55
|
|
|
if ($file->has($path)) { |
56
|
|
|
$docs = $file->read($path); |
57
|
|
|
$event->setResponse($docs); |
58
|
|
|
} |
59
|
|
|
}, RouteEvent::FINISH); |
60
|
|
|
|
61
|
|
|
$this->listen(RouteEvent::MATCH, function (RouteEvent $event) { |
62
|
|
|
if (DefaultRoute::class !== $event->getRouteMatch()->getMatchedRouteName()) { |
63
|
|
|
$event->setResponse($event->getApp()->url(DefaultRoute::class)->html('Go Home')); |
64
|
|
|
} |
65
|
|
|
}, RouteEvent::FINISH); |
66
|
|
|
|
67
|
|
|
$this->listenRoute('myRoute', function (RouteEvent $event) { |
|
|
|
|
68
|
|
|
$event->setResponse('<h1>My Route Example Content</h1>'); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
$this->listenRoute('myRuntimeRoute', function (RouteEvent $event) { |
|
|
|
|
72
|
|
|
$event->setResponse([ |
73
|
|
|
'<h1>My Runtime Route Example Content</h1>', |
74
|
|
|
]); |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
$config = new CoreConfig([ |
81
|
|
|
new ConfigFeature\Log, |
82
|
|
|
new ConfigFeature\FirePhpLog, |
83
|
|
|
new AppFeature\FilesystemCache, |
84
|
|
|
|
85
|
|
|
(new MyCustomFeature) |
86
|
|
|
->setAnything(), |
87
|
|
|
|
88
|
|
|
new AppFeature\Listener(MyCustomListener::class), |
89
|
|
|
|
90
|
|
|
(new ConfigFeature\Route('myRoute'))->setLiteral('/my-route'), |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
$appCore = Webino::application($config); |
95
|
|
|
$app = $appCore->bootstrap(); |
96
|
|
|
|
97
|
|
|
// runtime route example |
98
|
|
|
$app->route('myRuntimeRoute')->setLiteral('/my-runtime-route'); |
99
|
|
|
|
100
|
|
|
require __DIR__ . '/dispatch.php'; |
101
|
|
|
|
102
|
|
|
$app->dispatch(); |
103
|
|
|
|
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.