This standalone software is meant to provide a global public directory of Friendica profiles across nodes.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
friendica-directory/src/dependencies.php

157 lines
5.4 KiB

<?php
use Psr\Container\ContainerInterface;
// DIC configuration
// l10n
$container['l10n'] = function (ContainerInterface $c): Gettext\TranslatorInterface {
$translator = new Gettext\Translator();
return $translator;
};
// simple cache
$container['simplecache'] = function (ContainerInterface $c): Sarahman\SimpleCache\FileSystemCache {
$settings = $c->get('settings')['simplecache'];
return new Sarahman\SimpleCache\FileSystemCache($settings['directory']);
};
// zend escaper
$container['escaper'] = function (ContainerInterface $c): Zend\Escaper\Escaper {
$settings = $c->get('settings')['escaper'];
return new Zend\Escaper\Escaper($settings['encoding']);
};
// view renderer
$container['renderer'] = function (ContainerInterface $c): Friendica\Directory\Views\PhpRenderer {
$settings = $c->get('settings')['renderer'];
return new Friendica\Directory\Views\PhpRenderer(
$c->get('escaper'),
$c->get('l10n'),
$c->get('router'),
$settings['template_path']
);
};
// monolog
$container['logger'] = function (ContainerInterface $c): Monolog\Logger {
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
switch ($settings['formatter']) {
case 'console':
$formatter = new Monolog\Formatter\LineFormatter("[%level_name%] %message% %context%\n");
break;
case 'logfile':
default:
$formatter = new Monolog\Formatter\LineFormatter("%datetime% - %level_name% %extra%: %message% %context%\n");
break;
}
$handler = new Monolog\Handler\StreamHandler($settings['path'], $settings['level']);
$handler->setFormatter($formatter);
$logger->pushHandler($handler);
return $logger;
};
// PDO wrapper
$container['atlas'] = function (ContainerInterface $c): Atlas\Pdo\Connection {
$args = $c->get('settings')['database'];
$dsn = "{$args['driver']}:dbname={$args['database']};host={$args['hostname']}";
$atlasConnection = Atlas\Pdo\Connection::new($dsn, $args['username'], $args['password']);
return $atlasConnection;
};
// Database migration manager
$container['migration'] = function (ContainerInterface $c): ByJG\DbMigration\Migration {
$args = $c->get('settings')['database'];
$connectionUri = new ByJG\Util\Uri("{$args['driver']}://{$args['username']}:{$args['password']}@{$args['hostname']}/{$args['database']}");
$migration = new ByJG\DbMigration\Migration($connectionUri, __DIR__ . '/sql');
$migration->addCallbackProgress(function (string $action, int $currentVersion) use ($c): void {
switch($action) {
case 'reset': $c->get('logger')->notice('Resetting database schema'); break;
case 'migrate': $c->get('logger')->notice('Migrating database schema to version ' . $currentVersion); break;
default:
$c->get('logger')->notice('Migration action: ' . $action . ' Current Version: ' . $currentVersion);
}
});
$migration->registerDatabase('mysql', ByJG\DbMigration\Database\MySqlDatabase::class);
return $migration;
};
$container['http'] = function (ContainerInterface $c): GuzzleHttp\ClientInterface {
$version = file_get_contents(__DIR__ . '/../VERSION');
if (!$version || !preg_match('/^\s*\d\.\d\.\d\s*$/', $version)) {
$version = '0.0.0';
}
return new GuzzleHttp\Client(['timeout' => 20, 'headers' => ['User-Agent' => 'FriendicaDirectory/' . trim($version) . ' ' . \GuzzleHttp\default_user_agent()]]);
};
// Internal Dependency Injection
$container[\Friendica\Directory\Models\Profile::class] = function (ContainerInterface $c): Friendica\Directory\Models\Profile {
return new Friendica\Directory\Models\Profile($c->get('atlas'));
};
$container[\Friendica\Directory\Models\ProfilePollQueue::class] = function (ContainerInterface $c): Friendica\Directory\Models\ProfilePollQueue {
return new Friendica\Directory\Models\ProfilePollQueue($c->get('atlas'));
};
$container[\Friendica\Directory\Models\Server::class] = function (ContainerInterface $c): Friendica\Directory\Models\Server {
return new Friendica\Directory\Models\Server($c->get('atlas'));
};
$container[\Friendica\Directory\Pollers\Directory::class] = function (ContainerInterface $c): Friendica\Directory\Pollers\Directory {
$settings = $c->get('settings')['poller'];
return new Friendica\Directory\Pollers\Directory(
$c->get('http'),
$c->get(\Friendica\Directory\Models\ProfilePollQueue::class),
$c->get('logger'),
$settings ?: []
);
};
$container[\Friendica\Directory\Pollers\Profile::class] = function (ContainerInterface $c): Friendica\Directory\Pollers\Profile {
$settings = $c->get('settings')['poller'];
return new Friendica\Directory\Pollers\Profile(
$c->get('atlas'),
$c->get('http'),
$c->get(\Friendica\Directory\Models\Server::class),
$c->get(\Friendica\Directory\Models\Profile::class),
$c->get('logger'),
$settings ?: []
);
};
$container[\Friendica\Directory\Pollers\Server::class] = function (ContainerInterface $c): Friendica\Directory\Pollers\Server {
$settings = $c->get('settings')['poller'];
return new Friendica\Directory\Pollers\Server(
$c->get('atlas'),
$c->get('http'),
$c->get(\Friendica\Directory\Models\ProfilePollQueue::class),
$c->get(\Friendica\Directory\Models\Server::class),
$c->get('simplecache'),
$c->get('logger'),
$settings ?: []
);
};
$container[\Friendica\Directory\Views\Widget\AccountTypeTabs::class] = function (ContainerInterface $c): Friendica\Directory\Views\Widget\AccountTypeTabs {
return new Friendica\Directory\Views\Widget\AccountTypeTabs(
$c->get('atlas'),
$c->get('renderer'),
$c->get('router')
);
};