leftypol/inc/context.php

105 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace Vichan;
2025-02-21 11:52:19 +01:00
use Vichan\Data\{IpNoteQueries, ReportQueries, UserPostQueries};
2024-10-04 01:01:47 +02:00
use Vichan\Data\Driver\{CacheDriver, HttpDriver, ErrorLogLogDriver, FileLogDriver, LogDriver, StderrLogDriver, SyslogLogDriver};
2025-03-18 09:59:35 +01:00
use Vichan\Service\Embed\EmbedService;
use Vichan\Service\Embed\OembedExtractor;
2024-04-07 21:10:39 +02:00
defined('TINYBOARD') or exit;
2024-04-07 21:10:39 +02:00
class Context {
private array $definitions;
public function __construct(array $definitions) {
$this->definitions = $definitions;
}
public function get(string $name): mixed {
if (!isset($this->definitions[$name])) {
throw new \RuntimeException("Could not find a dependency named $name");
}
$ret = $this->definitions[$name];
if (is_callable($ret) && !is_string($ret) && !is_array($ret)) {
$ret = $ret($this);
$this->definitions[$name] = $ret;
}
return $ret;
}
}
function build_context(array $config): Context {
return new Context([
'config' => $config,
2024-12-19 00:23:39 +01:00
LogDriver::class => function($c) {
$config = $c->get('config');
$name = $config['log_system']['name'];
$level = $config['debug'] ? LogDriver::DEBUG : LogDriver::NOTICE;
$backend = $config['log_system']['type'];
$legacy_syslog = isset($config['syslog']) && $config['syslog'];
2024-12-19 00:23:39 +01:00
// Check 'syslog' for backwards compatibility.
if ($legacy_syslog || $backend === 'syslog') {
$log_driver = new SyslogLogDriver($name, $level, $config['log_system']['syslog_stderr']);
if ($legacy_syslog) {
$log_driver->log(LogDriver::NOTICE, 'The configuration setting \'syslog\' is deprecated. Please use \'log_system\' instead');
}
return $log_driver;
2024-12-19 00:23:39 +01:00
} elseif ($backend === 'file') {
2024-12-11 15:18:26 +01:00
return new FileLogDriver($name, $level, $config['log_system']['file_path']);
2024-12-19 00:23:39 +01:00
} elseif ($backend === 'stderr') {
2024-10-04 12:57:37 +02:00
return new StderrLogDriver($name, $level);
} elseif ($backend === 'error_log') {
2024-10-04 12:57:37 +02:00
return new ErrorLogLogDriver($name, $level);
} else {
$log_driver = new ErrorLogLogDriver($name, $level);
$log_driver->log(LogDriver::ERROR, "Unknown 'log_system' value '$backend', using 'error_log' default");
return $log_driver;
2024-12-19 00:23:39 +01:00
}
},
2024-04-07 21:10:39 +02:00
CacheDriver::class => function($c) {
// Use the global for backwards compatibility.
return \cache::getCache();
},
2024-10-04 01:01:47 +02:00
HttpDriver::class => function($c) {
$config = $c->get('config');
return new HttpDriver($config['upload_by_url_timeout'], $config['max_filesize']);
},
\PDO::class => function($c) {
global $pdo;
// Ensure the PDO is initialized.
sql_open();
return $pdo;
2024-11-26 00:37:57 +01:00
},
ReportQueries::class => function($c) {
$auto_maintenance = (bool)$c->get('config')['auto_maintenance'];
$pdo = $c->get(\PDO::class);
return new ReportQueries($pdo, $auto_maintenance);
2024-12-11 17:31:57 +01:00
},
UserPostQueries::class => function($c) {
return new UserPostQueries($c->get(\PDO::class));
2025-02-21 11:52:19 +01:00
},
IpNoteQueries::class => fn($c) => new IpNoteQueries($c->get(\PDO::class), $c->get(CacheDriver::class)),
OembedExtractor::class => fn($c) => new OembedExtractor(
$c->get(CacheDriver::class),
$c->get(HttpDriver::class),
$c->get('config')['embed_thumb_timeout']
),
EmbedService::class => function($c) {
$config = $c->get('config');
return new EmbedService(
$c->get(LogDriver::class),
$c->get(OembedExtractor::class),
$c->get(HttpDriver::class),
$config['embedding_2'],
$config['embed_thumb_timeout'],
$config['tmp'],
);
}
]);
}