/
bootstrap.php
121 lines (110 loc) · 3.57 KB
/
bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
error_reporting(-1);
ini_set('html_errors', 1);
ini_set('display_errors', 1);
require_once __DIR__.'/vendor/autoload.php';
$app = new Silex\Application();
$app['debug'] = true;
/**
* This is a simple configuration parameter.
* It could come from a config file, or be hard-coded,
* like it is here.
*/
$app['data.path'] = __DIR__.'/data/hello.json';
/**
* This creates our data source.
* The datasource will be injected into any
* repositories so that they will have access to
* the data.
*/
$app['data.source'] = $app->share(function ($app) {
return new Lal\Datasource($app['data.path']);
});
/**
* These are a factory functions.
* Their ONLY purpose is to create new objects,
* and to inject their dependencies so that the
* callers of the factory will receive usable
* objects in return.
*
* If an object's constructor changes, or the
* object gets new setup requirements later,
* only the factory function needs to change.
*/
$app['user.factory'] = $app->protect(function () use ($app) {
return new Lal\User($app['user.repo'], $app['comment.repo'], $app['pageview.repo']);
});
$app['comment.factory'] = $app->protect(function () use ($app) {
return new Lal\Comment($app['user.repo']);
});
/**
* The repositories control access into and out of
* the datasource.
*
* They are injected with the data.source so that
* they can read/write data, and also get injected
* with factory functions so that they can
* translate raw data.source data into objects.
*
* Since they uses the factories, they do not need
* to know about an object's requirements. This is
* a main goal of DI!
*/
$app['user.repo'] = $app->share(function ($app) {
return new Lal\UserRepository($app['data.source'], $app['user.factory']);
});
$app['pageview.repo'] = $app->share(function ($app) {
return new Lal\PageViewRepository($app['data.source']);
});
/**
* Since the comment repo emits events when comments are saved
* it makes sense to register the listeners for those events
* when the repo is created.
*/
$app['comment.repo'] = $app->share(function ($app) {
$app['event.emitter']->on('comment.saved', array($app['comment.log'], 'logComment'));
return new Lal\CommentRepository($app['data.source'], $app['event.emitter'], $app['comment.factory']);
});
/**
* Custom view renderer
*
* A simple library. Like the datasource, it is
* injected with config parameters that are also
* stored in the DI container.
*
* This shouldn't be a singleton, so we don't use
* the `share` method when setting it up.
*/
$app['view.templates'] = __DIR__.'/templates/';
$app['view.renderer'] = function ($app) {
return new Lal\View($app['view.templates']);
};
/**
* This is a simple app controller.
*
* The app framework calls methods on this controller
* to do work and format the results. This controller
* is injected with the repositories and a custom view
* renderer.
*
* Since the controller emits events when pages are viewed
* it makes sense to register the listeners for those events
* when the controller is created.
*/
$app['hello.controller'] = $app->share(function ($app) {
$app['event.emitter']->on('user.viewed', array($app['pageview.repo'], 'incrementCount'));
return new Lal\HelloController($app['user.repo'], $app['comment.repo'], $app['view.renderer'], $app['event.emitter']);
});
/**
* Comment log listens for comment saved events
*/
$app['comment.log'] = $app->share(function () {
return new Lal\CommentLog('/tmp/hello.log');
});
/**
* Event manager for dispatching and emitting events
*/
$app['event.emitter'] = $app->share(function ($app) {
return new Evenement\EventEmitter();
});
return $app;