Files
kiri-core/kiri-engine/Abstracts/BaseApplication.php
T

117 lines
2.8 KiB
PHP
Raw Normal View History

2023-05-25 16:59:20 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/10/7 0007
* Time: 2:13
*/
declare(strict_types=1);
namespace Kiri\Abstracts;
2023-05-26 09:20:31 +08:00
use Database\DatabasesProviders;
2023-05-25 16:59:20 +08:00
use Exception;
2023-08-11 00:12:33 +08:00
use Kiri\Events\EventInterface;
2023-05-25 16:59:20 +08:00
use Kiri\Config\ConfigProvider;
use Kiri\Exception\{InitException};
2023-08-11 00:12:33 +08:00
use Psr\Container\ContainerExceptionInterface;
2023-07-31 23:09:00 +08:00
use Psr\Container\ContainerInterface;
2023-08-11 00:12:33 +08:00
use Psr\Container\NotFoundExceptionInterface;
2023-05-25 16:59:20 +08:00
use Psr\Log\LoggerInterface;
use Kiri\Events\EventProvider;
2023-08-14 22:10:38 +08:00
use Kiri\Error\StdoutLogger;
2023-05-25 16:59:20 +08:00
/**
* Class BaseApplication
* @package Kiri\Base
2023-05-26 09:20:31 +08:00
* @property DatabasesProviders $connections
2023-05-25 16:59:20 +08:00
*/
2023-11-30 17:46:50 +08:00
abstract class BaseApplication extends LocalService
2023-05-25 16:59:20 +08:00
{
/**
* @var string
*/
public string $storage = APP_PATH . 'storage';
2023-07-31 23:09:00 +08:00
/**
2023-11-30 17:02:21 +08:00
* @param EventProvider $provider
* @param ConfigProvider $config
* @param ContainerInterface $container
2023-12-12 15:35:38 +08:00
* @throws
2023-05-25 16:59:20 +08:00
*/
2023-11-30 17:02:21 +08:00
public function __construct(public EventProvider $provider, public ConfigProvider $config, public ContainerInterface $container)
2023-05-25 16:59:20 +08:00
{
$this->mapping($config);
parent::__construct();
}
2026-06-12 23:57:25 +08:00
/**
* @return void
* @throws
*/
public function init(): void
{
$this->parseStorage($this->config);
$this->parseEvents($this->config);
}
2023-05-25 16:59:20 +08:00
/**
* @param ConfigProvider $config
* @return void
*/
public function mapping(ConfigProvider $config): void
{
2023-08-14 22:10:38 +08:00
$this->container->bind(LoggerInterface::class, new StdoutLogger());
2023-05-25 16:59:20 +08:00
foreach ($config->get('mapping', []) as $interface => $class) {
2023-07-31 23:09:00 +08:00
$this->container->set($interface, $class);
2023-05-25 16:59:20 +08:00
}
}
/**
* @param ConfigProvider $config
* @return void
2023-12-12 15:35:38 +08:00
* @throws
2023-05-25 16:59:20 +08:00
*/
public function parseStorage(ConfigProvider $config): void
{
2025-12-18 21:30:00 +08:00
$storage = $config->get('site.log.storage', 'storage');
2023-08-11 00:12:33 +08:00
if (!str_contains($storage, APP_PATH)) {
$storage = APP_PATH . $storage . '/';
}
if (!is_dir($storage)) {
mkdir($storage, 0777, true);
}
if (!is_dir($storage) || !is_writeable($storage)) {
2023-11-24 10:22:56 +08:00
throw new InitException("Directory $storage does not have write permission");
2023-05-25 16:59:20 +08:00
}
}
/**
* @param ConfigProvider $config
* @return void
2023-12-12 15:35:38 +08:00
* @throws
2023-05-25 16:59:20 +08:00
*/
public function parseEvents(ConfigProvider $config): void
{
$events = $config->get('events', []);
foreach ($events as $key => $value) {
if (is_string($value)) {
2023-08-11 00:12:33 +08:00
$value = $this->container->get($value);
if (!($value instanceof EventInterface)) {
throw new Exception("Event listen must implement " . EventInterface::class);
}
$value = [$value, 'process'];
2023-05-25 16:59:20 +08:00
}
2023-07-31 23:09:00 +08:00
$this->provider->on($key, $value, 0);
2023-05-25 16:59:20 +08:00
}
}
}