2024-03-12 15:14:35 +01:00
|
|
|
<?php
|
|
|
|
namespace Vichan;
|
|
|
|
|
2024-04-07 21:10:39 +02:00
|
|
|
use Vichan\Data\Driver\CacheDriver;
|
2025-02-21 11:52:19 +01:00
|
|
|
use Vichan\Data\{IpNoteQueries, ReportQueries, UserPostQueries};
|
2024-12-19 00:23:39 +01:00
|
|
|
use Vichan\Data\Driver\{LogDriver, LogDrivers};
|
2024-04-07 21:10:39 +02:00
|
|
|
|
2024-03-12 15:14:35 +01:00
|
|
|
defined('TINYBOARD') or exit;
|
|
|
|
|
2024-04-07 21:10:39 +02:00
|
|
|
|
2024-03-12 15:14:35 +01: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'];
|
|
|
|
|
|
|
|
// Check 'syslog' for backwards compatibility.
|
|
|
|
if ((isset($config['syslog']) && $config['syslog']) || $backend === 'syslog') {
|
|
|
|
return LogDrivers::syslog($name, $level, $this->config['log_system']['syslog_stderr']);
|
|
|
|
} elseif ($backend === 'file') {
|
|
|
|
return LogDrivers::file($name, $level, $this->config['log_system']['file_path']);
|
|
|
|
} elseif ($backend === 'stderr') {
|
|
|
|
return LogDrivers::stderr($name, $level);
|
|
|
|
} elseif ($backend === 'none') {
|
|
|
|
return LogDrivers::none();
|
|
|
|
} else {
|
|
|
|
return LogDrivers::error_log($name, $level);
|
|
|
|
}
|
|
|
|
},
|
2024-04-07 21:10:39 +02:00
|
|
|
CacheDriver::class => function($c) {
|
|
|
|
// Use the global for backwards compatibility.
|
|
|
|
return \cache::getCache();
|
2024-11-26 00:34:03 +01:00
|
|
|
},
|
|
|
|
\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);
|
2024-12-08 13:30:12 +01:00
|
|
|
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)),
|
2024-03-12 15:14:35 +01:00
|
|
|
]);
|
|
|
|
}
|