2024-04-01 19:36:03 +02:00
|
|
|
<?php
|
|
|
|
namespace Vichan;
|
|
|
|
|
2024-04-03 23:42:05 +02:00
|
|
|
use Vichan\Driver\{HttpDriver, HttpDrivers, Log, LogDrivers};
|
2024-04-01 19:36:03 +02:00
|
|
|
|
2024-04-03 23:42:05 +02:00
|
|
|
defined('TINYBOARD') or exit;
|
2024-04-01 19:36:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
interface Context {
|
2024-04-03 23:42:05 +02:00
|
|
|
public function getLog(): Log;
|
2024-04-01 19:36:03 +02:00
|
|
|
public function getHttpDriver(): HttpDriver;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AppContext implements Context {
|
|
|
|
private array $config;
|
2024-04-03 23:42:05 +02:00
|
|
|
private ?Log $log;
|
2024-04-01 19:36:03 +02:00
|
|
|
private ?HttpDriver $http;
|
|
|
|
|
|
|
|
|
2024-04-03 23:42:05 +02:00
|
|
|
private function initLogDriver(): Log {
|
|
|
|
$name = $this->config['log_system']['name'];
|
|
|
|
$level = $this->config['debug'] ? Log::DEBUG : Log::NOTICE;
|
|
|
|
$backend = $this->config['log_system']['type'];
|
|
|
|
|
|
|
|
// Check 'syslog' for backwards compatibility.
|
|
|
|
if ((isset($this->config['syslog']) && $this->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-01 19:36:03 +02:00
|
|
|
public function __construct(array $config) {
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
2024-04-03 23:42:05 +02:00
|
|
|
public function getLog(): Log {
|
|
|
|
if (is_null($this->log)) {
|
|
|
|
$this->log = $this->initLogDriver();
|
|
|
|
}
|
|
|
|
return $this->log;
|
|
|
|
}
|
|
|
|
|
2024-04-01 19:36:03 +02:00
|
|
|
public function getHttpDriver(): HttpDriver {
|
|
|
|
if (is_null($this->http)) {
|
|
|
|
$this->http = HttpDrivers::getHttpDriver($this->config['upload_by_url_timeout'], $this->config['max_filesize']);
|
|
|
|
}
|
|
|
|
return $this->http;
|
|
|
|
}
|
|
|
|
}
|