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;
|
|
|
|
|
use Kiri;
|
2023-08-11 00:12:33 +08:00
|
|
|
use Kiri\Events\EventInterface;
|
2023-05-25 16:59:20 +08:00
|
|
|
use Kiri\Di\LocalService;
|
|
|
|
|
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-11 00:12:33 +08:00
|
|
|
use ReflectionException;
|
2023-08-14 21:09:45 +08:00
|
|
|
use Monolog\Logger;
|
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
|
|
|
*/
|
|
|
|
|
abstract class BaseApplication extends Component
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public string $storage = APP_PATH . 'storage';
|
|
|
|
|
|
2023-07-31 23:09:00 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var LocalService|mixed
|
|
|
|
|
*/
|
2023-05-25 16:59:20 +08:00
|
|
|
public LocalService $localService;
|
|
|
|
|
|
2023-07-31 23:09:00 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var ContainerInterface
|
|
|
|
|
*/
|
|
|
|
|
public ContainerInterface $container;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var EventProvider
|
|
|
|
|
*/
|
|
|
|
|
public EventProvider $provider;
|
|
|
|
|
|
2023-05-25 16:59:20 +08:00
|
|
|
/**
|
|
|
|
|
* Init constructor.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
2023-07-31 23:09:00 +08:00
|
|
|
$this->container = Kiri::getContainer();
|
|
|
|
|
$this->localService = $this->container->get(LocalService::class);
|
|
|
|
|
|
|
|
|
|
$this->provider = $this->container->get(EventProvider::class);
|
2023-05-25 16:59:20 +08:00
|
|
|
|
2023-07-31 23:09:00 +08:00
|
|
|
$config = $this->container->get(ConfigProvider::class);
|
2023-05-25 16:59:20 +08:00
|
|
|
|
|
|
|
|
$this->mapping($config);
|
|
|
|
|
$this->parseStorage($config);
|
|
|
|
|
$this->parseEvents($config);
|
2023-08-14 21:09:45 +08:00
|
|
|
|
2023-05-25 16:59:20 +08:00
|
|
|
parent::__construct();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-08-14 21:09:45 +08:00
|
|
|
const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG];
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($config->get('components', []) as $id => $component) {
|
2023-07-31 23:09:00 +08:00
|
|
|
$this->container->set($id, $component);
|
2023-05-25 16:59:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ConfigProvider $config
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws InitException
|
|
|
|
|
*/
|
|
|
|
|
public function parseStorage(ConfigProvider $config): void
|
|
|
|
|
{
|
2023-08-11 00:12:33 +08:00
|
|
|
$storage = $config->get('storage', 'storage');
|
|
|
|
|
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)) {
|
|
|
|
|
throw new InitException("Directory {$storage} does not have write permission");
|
2023-05-25 16:59:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ConfigProvider $config
|
|
|
|
|
* @return void
|
2023-08-11 00:12:33 +08:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
* @throws ReflectionException
|
2023-05-25 16:59:20 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return mixed|null
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function __get(string $name)
|
|
|
|
|
{
|
|
|
|
|
if ($this->localService->has($name)) {
|
|
|
|
|
return $this->localService->get($name);
|
|
|
|
|
}
|
|
|
|
|
return parent::__get($name); // TODO: Change the autogenerated stub
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $id
|
|
|
|
|
* @param $definition
|
|
|
|
|
*/
|
|
|
|
|
public function set($id, $definition): void
|
|
|
|
|
{
|
|
|
|
|
$this->localService->set($id, $definition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $id
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function has($id): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->localService->has($id);
|
|
|
|
|
}
|
|
|
|
|
}
|