2020-01-18 17:50:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shaarli\Container;
|
|
|
|
|
2020-09-26 14:18:01 +02:00
|
|
|
use malkusch\lock\mutex\FlockMutex;
|
2020-01-18 17:50:11 +01:00
|
|
|
use Shaarli\Bookmark\BookmarkFileService;
|
|
|
|
use Shaarli\Bookmark\BookmarkServiceInterface;
|
|
|
|
use Shaarli\Config\ConfigManager;
|
2020-05-18 17:17:36 +02:00
|
|
|
use Shaarli\Feed\FeedBuilder;
|
2020-01-26 11:15:15 +01:00
|
|
|
use Shaarli\Formatter\FormatterFactory;
|
2020-08-21 10:50:44 +02:00
|
|
|
use Shaarli\Front\Controller\Visitor\ErrorController;
|
2020-09-12 12:42:19 +02:00
|
|
|
use Shaarli\Front\Controller\Visitor\ErrorNotFoundController;
|
2020-01-18 17:50:11 +01:00
|
|
|
use Shaarli\History;
|
2020-06-06 14:01:03 +02:00
|
|
|
use Shaarli\Http\HttpAccess;
|
2020-06-17 15:55:31 +02:00
|
|
|
use Shaarli\Netscape\NetscapeBookmarkUtils;
|
2020-01-18 17:50:11 +01:00
|
|
|
use Shaarli\Plugin\PluginManager;
|
|
|
|
use Shaarli\Render\PageBuilder;
|
2020-01-23 21:52:03 +01:00
|
|
|
use Shaarli\Render\PageCacheManager;
|
2020-07-07 10:15:56 +02:00
|
|
|
use Shaarli\Security\CookieManager;
|
2020-01-18 17:50:11 +01:00
|
|
|
use Shaarli\Security\LoginManager;
|
|
|
|
use Shaarli\Security\SessionManager;
|
2020-06-06 14:01:03 +02:00
|
|
|
use Shaarli\Thumbnailer;
|
2020-07-06 08:04:35 +02:00
|
|
|
use Shaarli\Updater\Updater;
|
|
|
|
use Shaarli\Updater\UpdaterUtils;
|
2020-01-18 17:50:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ContainerBuilder
|
|
|
|
*
|
|
|
|
* Helper used to build a Slim container instance with Shaarli's object dependencies.
|
|
|
|
* Note that most injected objects MUST be added as closures, to let the container instantiate
|
|
|
|
* only the objects it requires during the execution.
|
|
|
|
*
|
|
|
|
* @package Container
|
|
|
|
*/
|
|
|
|
class ContainerBuilder
|
|
|
|
{
|
|
|
|
/** @var ConfigManager */
|
|
|
|
protected $conf;
|
|
|
|
|
|
|
|
/** @var SessionManager */
|
|
|
|
protected $session;
|
|
|
|
|
2020-07-07 10:15:56 +02:00
|
|
|
/** @var CookieManager */
|
|
|
|
protected $cookieManager;
|
|
|
|
|
2020-01-18 17:50:11 +01:00
|
|
|
/** @var LoginManager */
|
|
|
|
protected $login;
|
|
|
|
|
2020-06-13 13:08:01 +02:00
|
|
|
/** @var string|null */
|
|
|
|
protected $basePath = null;
|
2020-01-23 21:52:03 +01:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ConfigManager $conf,
|
|
|
|
SessionManager $session,
|
2020-07-07 10:15:56 +02:00
|
|
|
CookieManager $cookieManager,
|
2020-06-13 13:08:01 +02:00
|
|
|
LoginManager $login
|
2020-01-23 21:52:03 +01:00
|
|
|
) {
|
2020-01-18 17:50:11 +01:00
|
|
|
$this->conf = $conf;
|
|
|
|
$this->session = $session;
|
|
|
|
$this->login = $login;
|
2020-07-07 10:15:56 +02:00
|
|
|
$this->cookieManager = $cookieManager;
|
2020-01-18 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function build(): ShaarliContainer
|
|
|
|
{
|
|
|
|
$container = new ShaarliContainer();
|
2020-01-23 21:52:03 +01:00
|
|
|
|
2020-01-18 17:50:11 +01:00
|
|
|
$container['conf'] = $this->conf;
|
|
|
|
$container['sessionManager'] = $this->session;
|
2020-07-07 10:15:56 +02:00
|
|
|
$container['cookieManager'] = $this->cookieManager;
|
2020-01-18 17:50:11 +01:00
|
|
|
$container['loginManager'] = $this->login;
|
2020-06-13 13:08:01 +02:00
|
|
|
$container['basePath'] = $this->basePath;
|
2020-01-23 21:52:03 +01:00
|
|
|
|
2020-01-18 17:50:11 +01:00
|
|
|
$container['plugins'] = function (ShaarliContainer $container): PluginManager {
|
|
|
|
return new PluginManager($container->conf);
|
|
|
|
};
|
|
|
|
|
|
|
|
$container['history'] = function (ShaarliContainer $container): History {
|
|
|
|
return new History($container->conf->get('resource.history'));
|
|
|
|
};
|
|
|
|
|
|
|
|
$container['bookmarkService'] = function (ShaarliContainer $container): BookmarkServiceInterface {
|
|
|
|
return new BookmarkFileService(
|
|
|
|
$container->conf,
|
|
|
|
$container->history,
|
2020-09-26 14:18:01 +02:00
|
|
|
new FlockMutex(fopen(SHAARLI_MUTEX_FILE, 'r'), 2),
|
2020-01-18 17:50:11 +01:00
|
|
|
$container->loginManager->isLoggedIn()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
$container['pageBuilder'] = function (ShaarliContainer $container): PageBuilder {
|
|
|
|
return new PageBuilder(
|
|
|
|
$container->conf,
|
|
|
|
$container->sessionManager->getSession(),
|
|
|
|
$container->bookmarkService,
|
|
|
|
$container->sessionManager->generateToken(),
|
|
|
|
$container->loginManager->isLoggedIn()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-01-23 20:06:32 +01:00
|
|
|
$container['pluginManager'] = function (ShaarliContainer $container): PluginManager {
|
2020-06-20 15:14:24 +02:00
|
|
|
$pluginManager = new PluginManager($container->conf);
|
|
|
|
|
|
|
|
$pluginManager->load($container->conf->get('general.enabled_plugins'));
|
|
|
|
|
|
|
|
return $pluginManager;
|
2020-01-23 20:06:32 +01:00
|
|
|
};
|
|
|
|
|
2020-01-26 11:15:15 +01:00
|
|
|
$container['formatterFactory'] = function (ShaarliContainer $container): FormatterFactory {
|
2020-07-28 20:46:11 +02:00
|
|
|
return new FormatterFactory(
|
|
|
|
$container->conf,
|
|
|
|
$container->loginManager->isLoggedIn()
|
|
|
|
);
|
2020-01-26 11:15:15 +01:00
|
|
|
};
|
|
|
|
|
2020-01-23 21:52:03 +01:00
|
|
|
$container['pageCacheManager'] = function (ShaarliContainer $container): PageCacheManager {
|
2020-05-17 14:16:32 +02:00
|
|
|
return new PageCacheManager(
|
|
|
|
$container->conf->get('resource.page_cache'),
|
|
|
|
$container->loginManager->isLoggedIn()
|
|
|
|
);
|
2020-01-23 21:52:03 +01:00
|
|
|
};
|
|
|
|
|
2020-05-18 17:17:36 +02:00
|
|
|
$container['feedBuilder'] = function (ShaarliContainer $container): FeedBuilder {
|
|
|
|
return new FeedBuilder(
|
|
|
|
$container->bookmarkService,
|
|
|
|
$container->formatterFactory->getFormatter(),
|
|
|
|
$container->environment,
|
|
|
|
$container->loginManager->isLoggedIn()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-06 14:01:03 +02:00
|
|
|
$container['thumbnailer'] = function (ShaarliContainer $container): Thumbnailer {
|
|
|
|
return new Thumbnailer($container->conf);
|
|
|
|
};
|
|
|
|
|
|
|
|
$container['httpAccess'] = function (): HttpAccess {
|
|
|
|
return new HttpAccess();
|
|
|
|
};
|
|
|
|
|
2020-06-17 15:55:31 +02:00
|
|
|
$container['netscapeBookmarkUtils'] = function (ShaarliContainer $container): NetscapeBookmarkUtils {
|
|
|
|
return new NetscapeBookmarkUtils($container->bookmarkService, $container->conf, $container->history);
|
|
|
|
};
|
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
$container['updater'] = function (ShaarliContainer $container): Updater {
|
|
|
|
return new Updater(
|
|
|
|
UpdaterUtils::read_updates_file($container->conf->get('resource.updates')),
|
|
|
|
$container->bookmarkService,
|
|
|
|
$container->conf,
|
|
|
|
$container->loginManager->isLoggedIn()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-09-12 12:42:19 +02:00
|
|
|
$container['notFoundHandler'] = function (ShaarliContainer $container): ErrorNotFoundController {
|
|
|
|
return new ErrorNotFoundController($container);
|
|
|
|
};
|
2020-08-21 10:50:44 +02:00
|
|
|
$container['errorHandler'] = function (ShaarliContainer $container): ErrorController {
|
|
|
|
return new ErrorController($container);
|
|
|
|
};
|
2020-08-27 12:04:36 +02:00
|
|
|
$container['phpErrorHandler'] = function (ShaarliContainer $container): ErrorController {
|
|
|
|
return new ErrorController($container);
|
|
|
|
};
|
2020-08-21 10:50:44 +02:00
|
|
|
|
2020-01-18 17:50:11 +01:00
|
|
|
return $container;
|
|
|
|
}
|
|
|
|
}
|