<commit_before><?php declare(strict_types=1);

namespace hollodotme\Readis\Application\Configs;

use hollodotme\Readis\Application\Web\Server\Read\AjaxKeyDetailsRequestHandler;
use hollodotme\Readis\Application\Web\Server\Read\AjaxSearchKeysRequestHandler;
use hollodotme\Readis\Application\Web\Server\Read\ServerDetailsRequestHandler;
use hollodotme\Readis\Application\Web\Server\Read\ServerSelectionRequestHandler;
use hollodotme\Readis\Traits\EnvInjecting;
use IceHawk\IceHawk\Defaults\Traits\DefaultCookieProviding;
use IceHawk\IceHawk\Defaults\Traits\DefaultEventSubscribing;
use IceHawk\IceHawk\Defaults\Traits\DefaultFinalReadResponding;
use IceHawk\IceHawk\Defaults\Traits\DefaultFinalWriteResponding;
use IceHawk\IceHawk\Defaults\Traits\DefaultRequestBypassing;
use IceHawk\IceHawk\Defaults\Traits\DefaultRequestInfoProviding;
use IceHawk\IceHawk\Defaults\Traits\DefaultWriteRouting;
use IceHawk\IceHawk\Interfaces\ConfiguresIceHawk;
use IceHawk\IceHawk\Routing\Patterns\NamedRegExp;
use IceHawk\IceHawk\Routing\ReadRoute;
use function preg_quote;

final class IceHawkConfig implements ConfiguresIceHawk
{
	use EnvInjecting;
	use DefaultCookieProviding;
	use DefaultEventSubscribing;
	use DefaultFinalReadResponding;
	use DefaultFinalWriteResponding;
	use DefaultRequestBypassing;
	use DefaultRequestInfoProviding;
	use DefaultWriteRouting;

	/** @var array */
	private $readRoutes;

	public function getReadRoutes()
	{
		$this->buildReadRoutes();

		foreach ( $this->readRoutes as $pattern => $handlerClass )
		{
			yield new ReadRoute(
				new NamedRegExp( $pattern, 'i' ),
				new $handlerClass( $this->getEnv() )
			);
		}
	}

	private function buildReadRoutes() : void
	{
		if ( null !== $this->readRoutes )
		{
			return;
		}

		$baseUrl       = $this->getEnv()->getAppConfig()->getBaseUrl();
		$quotedBaseUrl = preg_quote( rtrim( $baseUrl, '/' ), '!' );

		$this->readRoutes = [
			'^' . $quotedBaseUrl . '/?$'                                                                                            => ServerSelectionRequestHandler::class,
			'^' . $quotedBaseUrl . '/server/(?:(?<serverKey>\d+))(?:/database/(?<database>\d+))?/?$'                                => ServerDetailsRequestHandler::class,
			'^' . $quotedBaseUrl . '/server/(?<serverKey>\d+)/database/(?<database>\d+)/keys/?$'                                    => AjaxSearchKeysRequestHandler::class,
			'^' . $quotedBaseUrl . '/server/(?<serverKey>\d+)/database/(?<database>\d+)/keys/(?<keyName>.+)/hash/(?<hashKey>.+)/?$' => AjaxKeyDetailsRequestHandler::class,
			'^' . $quotedBaseUrl . '/server/(?<serverKey>\d+)/database/(?<database>\d+)/keys/(?<keyName>.+)/?$'                     => AjaxKeyDetailsRequestHandler::class,
		];
	}
}<commit_msg>Add route to server stats<commit_after><?php declare(strict_types=1);

namespace hollodotme\Readis\Application\Configs;

use hollodotme\Readis\Application\Web\Server\Read\AjaxKeyDetailsRequestHandler;
use hollodotme\Readis\Application\Web\Server\Read\AjaxSearchKeysRequestHandler;
use hollodotme\Readis\Application\Web\Server\Read\ServerDetailsRequestHandler;
use hollodotme\Readis\Application\Web\Server\Read\ServerSelectionRequestHandler;
use hollodotme\Readis\Application\Web\Server\Read\ServerStatsRequestHandler;
use hollodotme\Readis\Traits\EnvInjecting;
use IceHawk\IceHawk\Defaults\Traits\DefaultCookieProviding;
use IceHawk\IceHawk\Defaults\Traits\DefaultEventSubscribing;
use IceHawk\IceHawk\Defaults\Traits\DefaultFinalReadResponding;
use IceHawk\IceHawk\Defaults\Traits\DefaultFinalWriteResponding;
use IceHawk\IceHawk\Defaults\Traits\DefaultRequestBypassing;
use IceHawk\IceHawk\Defaults\Traits\DefaultRequestInfoProviding;
use IceHawk\IceHawk\Defaults\Traits\DefaultWriteRouting;
use IceHawk\IceHawk\Interfaces\ConfiguresIceHawk;
use IceHawk\IceHawk\Routing\Patterns\NamedRegExp;
use IceHawk\IceHawk\Routing\ReadRoute;
use function preg_quote;

final class IceHawkConfig implements ConfiguresIceHawk
{
	use EnvInjecting;
	use DefaultCookieProviding;
	use DefaultEventSubscribing;
	use DefaultFinalReadResponding;
	use DefaultFinalWriteResponding;
	use DefaultRequestBypassing;
	use DefaultRequestInfoProviding;
	use DefaultWriteRouting;

	/** @var array */
	private $readRoutes;

	public function getReadRoutes()
	{
		$this->buildReadRoutes();

		foreach ( $this->readRoutes as $pattern => $handlerClass )
		{
			yield new ReadRoute(
				new NamedRegExp( $pattern, 'i' ),
				new $handlerClass( $this->getEnv() )
			);
		}
	}

	private function buildReadRoutes() : void
	{
		if ( null !== $this->readRoutes )
		{
			return;
		}

		$baseUrl       = $this->getEnv()->getAppConfig()->getBaseUrl();
		$quotedBaseUrl = preg_quote( rtrim( $baseUrl, '/' ), '!' );

		$this->readRoutes = [
			'^' . $quotedBaseUrl . '/?$'                                                                                            => ServerSelectionRequestHandler::class,
			'^' . $quotedBaseUrl . '/server/(?<serverKey>\d+)/stats/?$'                                                             => ServerStatsRequestHandler::class,
			'^' . $quotedBaseUrl . '/server/(?<serverKey>\d+)(?:/database/(?<database>\d+))?/?$'                                    => ServerDetailsRequestHandler::class,
			'^' . $quotedBaseUrl . '/server/(?<serverKey>\d+)/database/(?<database>\d+)/keys/?$'                                    => AjaxSearchKeysRequestHandler::class,
			'^' . $quotedBaseUrl . '/server/(?<serverKey>\d+)/database/(?<database>\d+)/keys/(?<keyName>.+)/hash/(?<hashKey>.+)/?$' => AjaxKeyDetailsRequestHandler::class,
			'^' . $quotedBaseUrl . '/server/(?<serverKey>\d+)/database/(?<database>\d+)/keys/(?<keyName>.+)/?$'                     => AjaxKeyDetailsRequestHandler::class,
		];
	}
}
{'fraction_non_alphanumeric': 0.14340745928909124, 'fraction_numerical': 0.0003502013657853266, 'mean_word_length': 3.7759197324414715, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6601511', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1810, 'n_words': 229}