aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/container
diff options
context:
space:
mode:
Diffstat (limited to 'application/container')
-rw-r--r--application/container/ContainerBuilder.php165
-rw-r--r--application/container/ShaarliContainer.php51
2 files changed, 216 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
3declare(strict_types=1);
4
5namespace Shaarli\Container;
6
7use Shaarli\Bookmark\BookmarkFileService;
8use Shaarli\Bookmark\BookmarkServiceInterface;
9use Shaarli\Config\ConfigManager;
10use Shaarli\Feed\FeedBuilder;
11use Shaarli\Formatter\FormatterFactory;
12use Shaarli\Front\Controller\Visitor\ErrorController;
13use Shaarli\Front\Controller\Visitor\ErrorNotFoundController;
14use Shaarli\History;
15use Shaarli\Http\HttpAccess;
16use Shaarli\Netscape\NetscapeBookmarkUtils;
17use Shaarli\Plugin\PluginManager;
18use Shaarli\Render\PageBuilder;
19use Shaarli\Render\PageCacheManager;
20use Shaarli\Security\CookieManager;
21use Shaarli\Security\LoginManager;
22use Shaarli\Security\SessionManager;
23use Shaarli\Thumbnailer;
24use Shaarli\Updater\Updater;
25use 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 */
36class 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}
diff --git a/application/container/ShaarliContainer.php b/application/container/ShaarliContainer.php
new file mode 100644
index 00000000..66e669aa
--- /dev/null
+++ b/application/container/ShaarliContainer.php
@@ -0,0 +1,51 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Container;
6
7use Shaarli\Bookmark\BookmarkServiceInterface;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Feed\FeedBuilder;
10use Shaarli\Formatter\FormatterFactory;
11use Shaarli\History;
12use Shaarli\Http\HttpAccess;
13use Shaarli\Netscape\NetscapeBookmarkUtils;
14use Shaarli\Plugin\PluginManager;
15use Shaarli\Render\PageBuilder;
16use Shaarli\Render\PageCacheManager;
17use Shaarli\Security\CookieManager;
18use Shaarli\Security\LoginManager;
19use Shaarli\Security\SessionManager;
20use Shaarli\Thumbnailer;
21use Shaarli\Updater\Updater;
22use Slim\Container;
23
24/**
25 * Extension of Slim container to document the injected objects.
26 *
27 * @property string $basePath Shaarli's instance base path (e.g. `/shaarli/`)
28 * @property BookmarkServiceInterface $bookmarkService
29 * @property CookieManager $cookieManager
30 * @property ConfigManager $conf
31 * @property mixed[] $environment $_SERVER automatically injected by Slim
32 * @property callable $errorHandler Overrides default Slim exception display
33 * @property FeedBuilder $feedBuilder
34 * @property FormatterFactory $formatterFactory
35 * @property History $history
36 * @property HttpAccess $httpAccess
37 * @property LoginManager $loginManager
38 * @property NetscapeBookmarkUtils $netscapeBookmarkUtils
39 * @property callable $notFoundHandler Overrides default Slim exception display
40 * @property PageBuilder $pageBuilder
41 * @property PageCacheManager $pageCacheManager
42 * @property callable $phpErrorHandler Overrides default Slim PHP error display
43 * @property PluginManager $pluginManager
44 * @property SessionManager $sessionManager
45 * @property Thumbnailer $thumbnailer
46 * @property Updater $updater
47 */
48class ShaarliContainer extends Container
49{
50
51}