| @@ 52-82 (lines=31) @@ | ||
| 49 | /** |
|
| 50 | * Custom module invokable |
|
| 51 | */ |
|
| 52 | class MyInvokableModule |
|
| 53 | { |
|
| 54 | public function __invoke(AbstractApplication $app) |
|
| 55 | { |
|
| 56 | /** |
|
| 57 | * Binding to default route from |
|
| 58 | * the custom invokable module. |
|
| 59 | */ |
|
| 60 | $app->bind(DefaultRoute::class, function (RouteEvent $event) { |
|
| 61 | /** |
|
| 62 | * Obtaining custom |
|
| 63 | * module service. |
|
| 64 | */ |
|
| 65 | $myService = $event->getApp()->get(MyModuleService::class); |
|
| 66 | ||
| 67 | $event->setResponse([ |
|
| 68 | $myService->doSomething(), |
|
| 69 | new SourcePreview(__FILE__), |
|
| 70 | ]); |
|
| 71 | }); |
|
| 72 | ||
| 73 | /** |
|
| 74 | * The Configure event will be triggered |
|
| 75 | * only when the application configuration |
|
| 76 | * is not loaded from a cache. |
|
| 77 | */ |
|
| 78 | $app->onConfig(function () { |
|
| 79 | return [new Service(MyModuleService::class)]; |
|
| 80 | }); |
|
| 81 | } |
|
| 82 | } |
|
| 83 | ||
| 84 | /** |
|
| 85 | * Custom module service |
|
| @@ 33-63 (lines=31) @@ | ||
| 30 | /** |
|
| 31 | * Custom module |
|
| 32 | */ |
|
| 33 | class MyModule extends AbstractModule |
|
| 34 | { |
|
| 35 | const VERSION = '0.1.0'; |
|
| 36 | ||
| 37 | public function getDependencies() |
|
| 38 | { |
|
| 39 | return [ |
|
| 40 | MyModuleBase::class, |
|
| 41 | ]; |
|
| 42 | } |
|
| 43 | ||
| 44 | protected function init(AbstractApplication $app) |
|
| 45 | { |
|
| 46 | /** |
|
| 47 | * Binding to default route |
|
| 48 | * from the custom module. |
|
| 49 | */ |
|
| 50 | $app->bind(DefaultRoute::class, function (RouteEvent $event) { |
|
| 51 | /** |
|
| 52 | * Obtaining custom |
|
| 53 | * module service. |
|
| 54 | */ |
|
| 55 | $myService = $event->getApp()->get(MyModuleService::class); |
|
| 56 | ||
| 57 | $event->setResponse([ |
|
| 58 | $myService->doSomething(), |
|
| 59 | new SourcePreview(__FILE__), |
|
| 60 | ]); |
|
| 61 | }); |
|
| 62 | } |
|
| 63 | } |
|
| 64 | ||
| 65 | /** |
|
| 66 | * Custom module service |
|