Skip to content

Commit

Permalink
connection pool
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Aug 13, 2023
1 parent aa7f96e commit 2a2c7aa
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 40 deletions.
3 changes: 3 additions & 0 deletions src/Core/Application/ApplicationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Windwalker\Core\Application;

use JetBrains\PhpStorm\NoReturn;
use Psr\Log\LogLevel;
use Windwalker\Core\Console\Process\ProcessRunnerInterface;
use Windwalker\DI\Container;
use Windwalker\Event\EventAwareInterface;
Expand Down Expand Up @@ -85,6 +86,8 @@ public function loadConfig(mixed $source, ?string $format = null, array $options

public function addMessage(string|array $messages, ?string $type = 'info'): static;

public function log(string|\Stringable $message, array $context = [], string $level = LogLevel::INFO): static;

/**
* Close this request.
*
Expand Down
4 changes: 3 additions & 1 deletion src/Core/Application/ApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
use Closure;
use OutOfRangeException;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use ReflectionException;
use Windwalker\Attributes\AttributesAccessor;
use Windwalker\Core\Console\Process\ProcessRunnerTrait;
use Windwalker\Core\Event\CoreEventAwareTrait;
use Windwalker\Core\Event\EventDispatcherRegistry;
use Windwalker\Core\Event\CoreEventEmitter;
use Windwalker\Core\Runtime\Config;
use Windwalker\Core\Runtime\Runtime;
use Windwalker\Core\Utilities\Base64Url;
Expand All @@ -43,6 +43,8 @@ trait ApplicationTrait
use CoreEventAwareTrait;
use ProcessRunnerTrait;

public LoggerInterface $logger;

/**
* @var array<ServiceProviderInterface>
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Application/WebApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function boot(): void
// Start profiler for Web type
$profilerFactory = null;

if ($this->getType() === 'web') {
if ($this->getType() === AppType::WEB) {
$profilerFactory = new ProfilerFactory();
$profilerFactory->get('main')->mark('boot', ['system']);
}
Expand Down
31 changes: 31 additions & 0 deletions src/Core/Application/WebApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace Windwalker\Core\Application;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use Windwalker\Core\Manager\LoggerManager;

/**
* Trait WebApplicationTrait
*/
Expand All @@ -37,4 +42,30 @@ public function getClient(): AppClient
{
return AppClient::WEB;
}

public function log(string|\Stringable $message, array $context = [], string $level = LogLevel::INFO): static
{
$this->logger ??= $this->getLogger();

$this->logger->log($level, $message, $context);

return $this;
}

private function getLogger(): LoggerInterface
{
$manager = $this->container->get(LoggerManager::class);

$name = 'web';

if ($this->isCliRuntime()) {
$name = 'cli-web';
}

if (!$manager->has($name)) {
return new NullLogger();
}

return $manager->get($name);
}
}
24 changes: 24 additions & 0 deletions src/Core/Console/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use JetBrains\PhpStorm\NoReturn;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Application as SymfonyApp;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
Expand All @@ -37,6 +40,7 @@
use Windwalker\Core\Events\Console\ConsoleLogEvent;
use Windwalker\Core\Events\Console\ErrorMessageOutputEvent;
use Windwalker\Core\Events\Console\MessageOutputEvent;
use Windwalker\Core\Manager\LoggerManager;
use Windwalker\Core\Provider\AppProvider;
use Windwalker\Core\Provider\ConsoleProvider;
use Windwalker\Core\Provider\WebProvider;
Expand Down Expand Up @@ -400,4 +404,24 @@ protected function bootProvidersBeforeRequest(Container $container)
}
}
}

public function log(\Stringable|string $message, array $context = [], string $level = LogLevel::INFO): static
{
$this->logger ??= $this->getLogger();

$this->logger->log($level, $message, $context);

return $this;
}

protected function getLogger(): LoggerInterface
{
$manager = $this->container->get(LoggerManager::class);

if ($manager->has('console')) {
return $manager->get('console');
}

return new NullLogger();
}
}
8 changes: 5 additions & 3 deletions src/Core/Controller/DelegatingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public function execute(string $task, array $args = []): mixed

return $nav->back();
} catch (Throwable $e) {
$this->logError($e);
if ($this->app->isCliRuntime()) {
$this->logError($e);
}

if (
$this->app->isDebug()
Expand Down Expand Up @@ -195,12 +197,12 @@ protected function logError(Throwable $e): void
return;
}

$message = ErrorLogHandler::handleExceptionLogText($e, $this->app->path('@root'));
// $message = ErrorLogHandler::handleExceptionLogText($e, $this->app->path('@root'));

$this->app->service(LoggerService::class)
->error(
$this->app->config('error.log_channel') ?? 'error',
$message,
$e->getMessage(),
['exception' => $e]
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Error/ErrorLogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public function __invoke(Throwable $e): void
$code = $e->getCode();

if ($code < 400 || $code >= 500) {
$message = static::handleExceptionLogText($e, $this->config->get('@root'));
// $message = static::handleExceptionLogText($e, $this->config->get('@root'));

$this->logger->error(
$this->config->getDeep('error.log_channel') ?? 'error',
$message,
$e->getMessage(),
['exception' => $e]
);
}
Expand Down
65 changes: 45 additions & 20 deletions src/Core/Manager/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace Windwalker\Core\Manager;

use RuntimeException;
use Windwalker\Database\DatabaseAdapter;
use Windwalker\Database\DatabaseFactory;
use Windwalker\Database\Platform\AbstractPlatform;
use Windwalker\Database\Platform\MySQLPlatform;
use Windwalker\DI\Container;
use Windwalker\ORM\ORM;

/**
Expand All @@ -30,6 +30,17 @@ public function getConfigPrefix(): string
}

protected function getFactoryPath(string $name): string
{
$path = 'connections.' . $name . '.factory';

if (!$this->config->hasDeep($path)) {
$path = 'connections.' . $name;
}

return $path;
}

protected function getLegacyFactoryPath(string $name): string
{
return 'connections.' . $name;
}
Expand All @@ -49,11 +60,19 @@ public function create(?string $name = null, ...$args): object

$db->orm()->setAttributesResolver($this->container->getAttributesResolver());

if ($db->getPlatform()->getName() === AbstractPlatform::MYSQL) {
$platform = $db->getPlatform();

if ($platform instanceof MySQLPlatform && $platform->getName() === $platform::MYSQL) {
$options = $db->getOptions();

if ($options['strict'] ?? true) {
$this->strictMode($db, $options);
try {
if ($options['strict'] ?? true) {
$platform->enableStrictMode($options['modes'] ?? null);
} elseif ($options['modes'] ?? null) {
$platform->setModes($options['modes'] ?? []);
}
} catch (\RuntimeException $e) {
// No actions
}
}

Expand All @@ -70,21 +89,27 @@ public function getDatabaseFactory(): DatabaseFactory
return $this->container->resolve(DatabaseFactory::class);
}

protected function strictMode(DatabaseAdapter $db, array $options): void
public static function createAdapter(string $instanceName): \Closure
{
$modes = $options['modes'] ?? [
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
];

try {
$db->execute("SET @@SESSION.sql_mode = '" . implode(',', $modes) . "';");
} catch (RuntimeException $e) {
// If not success, hide error.
}
return function (Container $container) use ($instanceName) {
$factory = $container->newInstance(DatabaseFactory::class);
$connConfig = $container->getParam('database.connections.' . $instanceName);

$driverKey = 'database.connection.driver.' . $instanceName;

if ($container->has($driverKey)) {
$driver = $container->get($driverKey);

return $factory->create(
$driver,
$connConfig['options'] ?? [],
);
}

return $factory->create(
$connConfig['driver'],
$connConfig['options'] ?? [],
);
};
}
}
9 changes: 5 additions & 4 deletions src/Core/Provider/MonologProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,21 @@ public static function rotatingFileHandler(string $channel, ...$args): ObjectBui
public static function logger(
?string $name = null,
array $handlers = [],
array $processors = []
array $processors = [],
string $formatter = 'line_formatter'
): ObjectBuilderDefinition {
return create(
static function (Container $container) use ($processors, $handlers, $name) {
static function (Container $container) use ($formatter, $processors, $handlers, $name) {
$logger = new Logger(
$name,
array_map(
function ($handler) use ($container, $name) {
static function ($handler) use ($formatter, $container, $name) {
/** @var HandlerInterface $handler */
$handler = $container->resolve($handler, ['instanceName' => $name]);

return $handler->setFormatter(
$container->resolve(
$container->getParam('logs.factories.formatters.line_formatter')
$container->getParam('logs.factories.formatters.' . $formatter)
)
);
},
Expand Down
23 changes: 14 additions & 9 deletions src/Core/Service/LoggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(LoggerManager $manager)
* @return static
* @throws Exception
*/
public function emergency(string|array $channel, string|array $message, array $context = []): static
public function emergency(string|array $channel, mixed $message, array $context = []): static
{
$this->log($channel, LogLevel::EMERGENCY, $message, $context);

Expand All @@ -68,7 +68,7 @@ public function emergency(string|array $channel, string|array $message, array $c
* @return static
* @throws Exception
*/
public function alert(string|array $channel, string|array $message, array $context = []): static
public function alert(string|array $channel, mixed $message, array $context = []): static
{
$this->log($channel, LogLevel::ALERT, $message, $context);

Expand All @@ -87,7 +87,7 @@ public function alert(string|array $channel, string|array $message, array $conte
* @return static
* @throws Exception
*/
public function critical(string|array $channel, string|array $message, array $context = []): static
public function critical(string|array $channel, mixed $message, array $context = []): static
{
$this->log($channel, LogLevel::CRITICAL, $message, $context);

Expand All @@ -105,7 +105,7 @@ public function critical(string|array $channel, string|array $message, array $co
* @return static
* @throws Exception
*/
public function error(string|array $channel, string|array $message, array $context = []): static
public function error(string|array $channel, mixed $message, array $context = []): static
{
$this->log($channel, LogLevel::ERROR, $message, $context);

Expand All @@ -125,7 +125,7 @@ public function error(string|array $channel, string|array $message, array $conte
* @return static
* @throws Exception
*/
public function warning(string|array $channel, string|array $message, array $context = []): static
public function warning(string|array $channel, mixed $message, array $context = []): static
{
$this->log($channel, LogLevel::WARNING, $message, $context);

Expand All @@ -142,7 +142,7 @@ public function warning(string|array $channel, string|array $message, array $con
* @return static
* @throws Exception
*/
public function notice(string|array $channel, string|array $message, array $context = []): static
public function notice(string|array $channel, mixed $message, array $context = []): static
{
$this->log($channel, LogLevel::NOTICE, $message, $context);

Expand All @@ -161,7 +161,7 @@ public function notice(string|array $channel, string|array $message, array $cont
* @return static
* @throws Exception
*/
public function info(string|array $channel, string|array $message, array $context = []): static
public function info(string|array $channel, mixed $message, array $context = []): static
{
$this->log($channel, LogLevel::INFO, $message, $context);

Expand All @@ -178,7 +178,7 @@ public function info(string|array $channel, string|array $message, array $contex
* @return static
* @throws Exception
*/
public function debug(string|array $channel, string|array $message, array $context = []): static
public function debug(string|array $channel, mixed $message, array $context = []): static
{
$this->log($channel, LogLevel::DEBUG, $message, $context);

Expand All @@ -196,7 +196,7 @@ public function debug(string|array $channel, string|array $message, array $conte
* @return static
* @throws Exception
*/
public function log(string|array $channel, string|int $level, string|array $message, array $context = []): static
public function log(string|array $channel, string|int $level, mixed $message, array $context = []): static
{
if (is_array($channel)) {
foreach ($channel as $cat) {
Expand All @@ -223,4 +223,9 @@ public function getLogger(string $channel): LoggerInterface
{
return $this->manager->get($channel);
}

public static function parseTrace(string $trace): string
{
return ' ' . str_replace(WINDWALKER_ROOT, '{ROOT}', $trace);
}
}

0 comments on commit 2a2c7aa

Please sign in to comment.