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