diff options
Diffstat (limited to 'application/container/ContainerBuilder.php')
-rw-r--r-- | application/container/ContainerBuilder.php | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/application/container/ContainerBuilder.php b/application/container/ContainerBuilder.php new file mode 100644 index 00000000..55bb51b5 --- /dev/null +++ b/application/container/ContainerBuilder.php | |||
@@ -0,0 +1,165 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Container; | ||
6 | |||
7 | use Shaarli\Bookmark\BookmarkFileService; | ||
8 | use Shaarli\Bookmark\BookmarkServiceInterface; | ||
9 | use Shaarli\Config\ConfigManager; | ||
10 | use Shaarli\Feed\FeedBuilder; | ||
11 | use Shaarli\Formatter\FormatterFactory; | ||
12 | use Shaarli\Front\Controller\Visitor\ErrorController; | ||
13 | use Shaarli\Front\Controller\Visitor\ErrorNotFoundController; | ||
14 | use Shaarli\History; | ||
15 | use Shaarli\Http\HttpAccess; | ||
16 | use Shaarli\Netscape\NetscapeBookmarkUtils; | ||
17 | use Shaarli\Plugin\PluginManager; | ||
18 | use Shaarli\Render\PageBuilder; | ||
19 | use Shaarli\Render\PageCacheManager; | ||
20 | use Shaarli\Security\CookieManager; | ||
21 | use Shaarli\Security\LoginManager; | ||
22 | use Shaarli\Security\SessionManager; | ||
23 | use Shaarli\Thumbnailer; | ||
24 | use Shaarli\Updater\Updater; | ||
25 | use Shaarli\Updater\UpdaterUtils; | ||
26 | |||
27 | /** | ||
28 | * Class ContainerBuilder | ||
29 | * | ||
30 | * Helper used to build a Slim container instance with Shaarli's object dependencies. | ||
31 | * Note that most injected objects MUST be added as closures, to let the container instantiate | ||
32 | * only the objects it requires during the execution. | ||
33 | * | ||
34 | * @package Container | ||
35 | */ | ||
36 | class ContainerBuilder | ||
37 | { | ||
38 | /** @var ConfigManager */ | ||
39 | protected $conf; | ||
40 | |||
41 | /** @var SessionManager */ | ||
42 | protected $session; | ||
43 | |||
44 | /** @var CookieManager */ | ||
45 | protected $cookieManager; | ||
46 | |||
47 | /** @var LoginManager */ | ||
48 | protected $login; | ||
49 | |||
50 | /** @var string|null */ | ||
51 | protected $basePath = null; | ||
52 | |||
53 | public function __construct( | ||
54 | ConfigManager $conf, | ||
55 | SessionManager $session, | ||
56 | CookieManager $cookieManager, | ||
57 | LoginManager $login | ||
58 | ) { | ||
59 | $this->conf = $conf; | ||
60 | $this->session = $session; | ||
61 | $this->login = $login; | ||
62 | $this->cookieManager = $cookieManager; | ||
63 | } | ||
64 | |||
65 | public function build(): ShaarliContainer | ||
66 | { | ||
67 | $container = new ShaarliContainer(); | ||
68 | |||
69 | $container['conf'] = $this->conf; | ||
70 | $container['sessionManager'] = $this->session; | ||
71 | $container['cookieManager'] = $this->cookieManager; | ||
72 | $container['loginManager'] = $this->login; | ||
73 | $container['basePath'] = $this->basePath; | ||
74 | |||
75 | $container['plugins'] = function (ShaarliContainer $container): PluginManager { | ||
76 | return new PluginManager($container->conf); | ||
77 | }; | ||
78 | |||
79 | $container['history'] = function (ShaarliContainer $container): History { | ||
80 | return new History($container->conf->get('resource.history')); | ||
81 | }; | ||
82 | |||
83 | $container['bookmarkService'] = function (ShaarliContainer $container): BookmarkServiceInterface { | ||
84 | return new BookmarkFileService( | ||
85 | $container->conf, | ||
86 | $container->history, | ||
87 | $container->loginManager->isLoggedIn() | ||
88 | ); | ||
89 | }; | ||
90 | |||
91 | $container['pageBuilder'] = function (ShaarliContainer $container): PageBuilder { | ||
92 | return new PageBuilder( | ||
93 | $container->conf, | ||
94 | $container->sessionManager->getSession(), | ||
95 | $container->bookmarkService, | ||
96 | $container->sessionManager->generateToken(), | ||
97 | $container->loginManager->isLoggedIn() | ||
98 | ); | ||
99 | }; | ||
100 | |||
101 | $container['pluginManager'] = function (ShaarliContainer $container): PluginManager { | ||
102 | $pluginManager = new PluginManager($container->conf); | ||
103 | |||
104 | $pluginManager->load($container->conf->get('general.enabled_plugins')); | ||
105 | |||
106 | return $pluginManager; | ||
107 | }; | ||
108 | |||
109 | $container['formatterFactory'] = function (ShaarliContainer $container): FormatterFactory { | ||
110 | return new FormatterFactory( | ||
111 | $container->conf, | ||
112 | $container->loginManager->isLoggedIn() | ||
113 | ); | ||
114 | }; | ||
115 | |||
116 | $container['pageCacheManager'] = function (ShaarliContainer $container): PageCacheManager { | ||
117 | return new PageCacheManager( | ||
118 | $container->conf->get('resource.page_cache'), | ||
119 | $container->loginManager->isLoggedIn() | ||
120 | ); | ||
121 | }; | ||
122 | |||
123 | $container['feedBuilder'] = function (ShaarliContainer $container): FeedBuilder { | ||
124 | return new FeedBuilder( | ||
125 | $container->bookmarkService, | ||
126 | $container->formatterFactory->getFormatter(), | ||
127 | $container->environment, | ||
128 | $container->loginManager->isLoggedIn() | ||
129 | ); | ||
130 | }; | ||
131 | |||
132 | $container['thumbnailer'] = function (ShaarliContainer $container): Thumbnailer { | ||
133 | return new Thumbnailer($container->conf); | ||
134 | }; | ||
135 | |||
136 | $container['httpAccess'] = function (): HttpAccess { | ||
137 | return new HttpAccess(); | ||
138 | }; | ||
139 | |||
140 | $container['netscapeBookmarkUtils'] = function (ShaarliContainer $container): NetscapeBookmarkUtils { | ||
141 | return new NetscapeBookmarkUtils($container->bookmarkService, $container->conf, $container->history); | ||
142 | }; | ||
143 | |||
144 | $container['updater'] = function (ShaarliContainer $container): Updater { | ||
145 | return new Updater( | ||
146 | UpdaterUtils::read_updates_file($container->conf->get('resource.updates')), | ||
147 | $container->bookmarkService, | ||
148 | $container->conf, | ||
149 | $container->loginManager->isLoggedIn() | ||
150 | ); | ||
151 | }; | ||
152 | |||
153 | $container['notFoundHandler'] = function (ShaarliContainer $container): ErrorNotFoundController { | ||
154 | return new ErrorNotFoundController($container); | ||
155 | }; | ||
156 | $container['errorHandler'] = function (ShaarliContainer $container): ErrorController { | ||
157 | return new ErrorController($container); | ||
158 | }; | ||
159 | $container['phpErrorHandler'] = function (ShaarliContainer $container): ErrorController { | ||
160 | return new ErrorController($container); | ||
161 | }; | ||
162 | |||
163 | return $container; | ||
164 | } | ||
165 | } | ||