1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Service Factory Config Override |
4
|
|
|
* Webino Example |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
use WebinoAppLib\Event\RouteEvent; |
8
|
|
|
use WebinoAppLib\Factory\AbstractFactory; |
9
|
|
|
use WebinoAppLib\Feature\Service; |
10
|
|
|
use WebinoAppLib\Response\Content\SourcePreview; |
11
|
|
|
use WebinoAppLib\Router\DefaultRoute; |
12
|
|
|
|
13
|
|
|
require __DIR__ . '/../../vendor/autoload.php'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Example service dependency |
17
|
|
|
*/ |
18
|
|
|
class MySubService |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
public function doSomething($msg) |
21
|
|
|
{ |
22
|
|
|
return $msg; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Custom service |
28
|
|
|
*/ |
29
|
|
|
class MyService |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var MySubService |
33
|
|
|
*/ |
34
|
|
|
private $someDependency; |
35
|
|
|
|
36
|
|
|
public function __construct(MySubService $someDependency) |
37
|
|
|
{ |
38
|
|
|
$this->someDependency = $someDependency; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function doSomething() |
42
|
|
|
{ |
43
|
|
|
return $this->someDependency->doSomething('My service response!'); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Custom service factory |
49
|
|
|
*/ |
50
|
|
|
class MyServiceFactory extends AbstractFactory |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
protected function create() |
53
|
|
|
{ |
54
|
|
|
/** @var MySubService $someDependency */ |
55
|
|
|
$someDependency = $this->getServices()->get(MySubService::class); |
56
|
|
|
return new MyService($someDependency); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$config = Webino::config([ |
61
|
|
|
/** |
62
|
|
|
* Registering service |
63
|
|
|
* factory via config. |
64
|
|
|
*/ |
65
|
|
|
new Service(MyService::class, stdClass::class), |
66
|
|
|
/** |
67
|
|
|
* Overriding service |
68
|
|
|
* factory via config. |
69
|
|
|
*/ |
70
|
|
|
new Service(MyService::class, MyServiceFactory::class), |
71
|
|
|
|
72
|
|
|
// registering example dependency |
73
|
|
|
new Service(MySubService::class), |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
$app = Webino::application($config)->bootstrap(); |
77
|
|
|
|
78
|
|
|
$app->bind(DefaultRoute::class, function (RouteEvent $event) { |
79
|
|
|
/** |
80
|
|
|
* Obtaining service |
81
|
|
|
* instance. |
82
|
|
|
*/ |
83
|
|
|
$myService = $event->getApp()->get(MyService::class); |
84
|
|
|
|
85
|
|
|
$event->setResponse([ |
86
|
|
|
$myService->doSomething(), |
87
|
|
|
new SourcePreview(__FILE__), |
88
|
|
|
]); |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
$app->dispatch(); |
92
|
|
|
|
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.