diff options
Diffstat (limited to 'application/container')
-rw-r--r-- | application/container/ContainerBuilder.php | 165 | ||||
-rw-r--r-- | application/container/ShaarliContainer.php | 51 |
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 | |||
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 | } | ||
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 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Container; | ||
6 | |||
7 | use Shaarli\Bookmark\BookmarkServiceInterface; | ||
8 | use Shaarli\Config\ConfigManager; | ||
9 | use Shaarli\Feed\FeedBuilder; | ||
10 | use Shaarli\Formatter\FormatterFactory; | ||
11 | use Shaarli\History; | ||
12 | use Shaarli\Http\HttpAccess; | ||
13 | use Shaarli\Netscape\NetscapeBookmarkUtils; | ||
14 | use Shaarli\Plugin\PluginManager; | ||
15 | use Shaarli\Render\PageBuilder; | ||
16 | use Shaarli\Render\PageCacheManager; | ||
17 | use Shaarli\Security\CookieManager; | ||
18 | use Shaarli\Security\LoginManager; | ||
19 | use Shaarli\Security\SessionManager; | ||
20 | use Shaarli\Thumbnailer; | ||
21 | use Shaarli\Updater\Updater; | ||
22 | use 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 | */ | ||
48 | class ShaarliContainer extends Container | ||
49 | { | ||
50 | |||
51 | } | ||