Skip to content

Commit

Permalink
add no-debug route method
Browse files Browse the repository at this point in the history
  • Loading branch information
phphleb committed Dec 21, 2024
1 parent 8c65eaa commit f53f47b
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Constructor/Exceptions/Route/AsyncRouteException.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ abstract class AsyncRouteException extends \AsyncExitException implements CoreEx

final public const HL39_ERROR = 'HL39_ERROR';

final public const HL40_ERROR = 'HL40_ERROR';

private const ALL = [
self::HL00_ERROR => [
'en' => 'Sample error output number %value%',
Expand Down Expand Up @@ -255,6 +257,10 @@ abstract class AsyncRouteException extends \AsyncExitException implements CoreEx
'en' => 'Error in creating routes. The redirect() method status code can be in the range 300-308.',
'ru' => 'Ошибка составления маршрутов. Код статуса метода redirect() может быть в диапазоне 300-308.'
],
self::HL40_ERROR => [
'en' => 'Error in creating routes. Only one noDebug() method can be added to a route.',
'ru' => 'Ошибка составления маршрутов. К маршруту может быть добавлен только один метод noDebug().'
],
];

protected array $errorInfo = [];
Expand Down
2 changes: 1 addition & 1 deletion HlebBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function __construct(?string $publicPath = null, array $config = [], ?Log

// The current version of the framework.
// Текущая версия фреймворка.
\defined('HLEB_CORE_VERSION') or \define('HLEB_CORE_VERSION', '2.0.58');
\defined('HLEB_CORE_VERSION') or \define('HLEB_CORE_VERSION', '2.0.59');

$this->logger = $logger;

Expand Down
4 changes: 4 additions & 0 deletions Init/Connectors/HlebConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ final class HlebConnector
'Hleb\Main\Routes\Methods\Traits\InsertProtectTrait' => '/Main/Routes/Methods/Traits/InsertProtectTrait.php',
'Hleb\Main\Routes\Methods\Traits\InsertAfterTrait' => '/Main/Routes/Methods/Traits/InsertAfterTrait.php',
'Hleb\Main\Routes\Methods\Traits\StandardTrait' => '/Main/Routes/Methods/Traits/StandardTrait.php',
'Hleb\Main\Routes\Methods\Traits\Group\GroupNoDebugTrait' => '/Main/Routes/Methods/Traits/Group/GroupNoDebugTrait.php',
'Hleb\Main\Routes\Methods\Traits\InsertNoDebugTrait' => '/Main/Routes/Methods/Traits/InsertNoDebugTrait.php',
'Hleb\Route\Group\GroupNoDebug' => '/Route/Group/GroupNoDebug.php',
'Hleb\Route\NoDebug' => '/Route/NoDebug.php',
'Hleb\Main\Routes\BaseRoute' => '/Main/Routes/BaseRoute.php',
'Hleb\Main\Routes\Methods\BaseType' => '/Main/Routes/Methods/BaseType.php',
'Hleb\Main\Info\PathInfoDoc' => '/Main/Info/PathInfoDoc.php',
Expand Down
4 changes: 4 additions & 0 deletions Main/ProjectLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public static function init(): void
}
/** @see hl_check() - searchHeadMethod completed */

if ($routes->getIsNoDebug()) {
DynamicParams::setDynamicDebug(false);
}

if ($block) {
if (self::searchDefaultHttpOptionsMethod($block)) {
return;
Expand Down
28 changes: 28 additions & 0 deletions Main/Routes/Methods/Traits/Group/GroupNoDebugTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Hleb\Main\Routes\Methods\Traits\Group;


use Hleb\Route\Group\GroupNoDebug;

trait GroupNoDebugTrait
{
/**
* Disables the debug panel output for group routes.
* Only relevant in DEBUG mode.
* The implication is that this is not a workaround, and under these conditions
* there is really no need to display a debug panel, such as a GET request
* to an API expecting a JSON response.
*
* Отключает вывод отладочной панели у маршрутов группы.
* Актуально только в DEBUG-режиме.
* Подразумевается, что это не временное решение, а в этих условиях действительно
* не нужно выводить панель отладки, например, GET-запрос к API, ожидающий в ответ JSON.
*/
public function noDebug(): GroupNoDebug
{
return new GroupNoDebug();
}
}
1 change: 1 addition & 0 deletions Main/Routes/Methods/Traits/Group/StandardGroupTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ trait StandardGroupTrait
use GroupBeforeTrait;
use GroupDomainTrait;
use GroupWhereTrait;
use GroupNoDebugTrait;
use GroupTrait;
}
27 changes: 27 additions & 0 deletions Main/Routes/Methods/Traits/InsertNoDebugTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Hleb\Main\Routes\Methods\Traits;

use Hleb\Route\NoDebug;

trait InsertNoDebugTrait
{
/**
* Applies the specified protection to the route.
* Only relevant in DEBUG mode.
* The implication is that this is not a workaround, and under these conditions
* there is really no need to display a debug panel, such as a GET request
* to an API expecting a JSON response.
*
* Отключает вывод отладочной панели у маршрута.
* Актуально только в DEBUG-режиме.
* Подразумевается, что это не временное решение, а в этих условиях действительно
* не нужно выводить панель отладки, например, GET-запрос к API, ожидающий в ответ JSON.
*/
public function noDebug(): NoDebug
{
return new NoDebug();
}
}
1 change: 1 addition & 0 deletions Main/Routes/Methods/Traits/StandardTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ trait StandardTrait
use InsertPageTrait;
use InsertPlainTrait;
use InsertRedirectTrait;
use InsertNoDebugTrait;

}
8 changes: 8 additions & 0 deletions Main/Routes/Prepare/Optimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ private function routesDataByMethod(): void
if ($action['method'] === StandardRoute::PROTECT_TYPE) {
$this->routesInfo['has_protect'] = 1;
}
if ($action['method'] === StandardRoute::NO_DEBUG_TYPE) {
$this->routesInfo['no_debug'] = 1;
}
if ($action['method'] === StandardRoute::PLAIN_TYPE) {
$this->routesInfo['has_plain'] = 1;
}
Expand Down Expand Up @@ -166,6 +169,7 @@ private function createAddress(array $route): string
* h - domain data.
* p - the route is protected.
* b - simple content.
* u - debug panel is disabled.
*
* Извлечение данных для идентификации запроса метода.
* a - полный адрес маршрута, собранный вместе с префиксами.
Expand All @@ -182,6 +186,7 @@ private function createAddress(array $route): string
* h - данные домена.
* p - маршрут защищён.
* b - простое содержимое.
* u - отключена отладочная панель.
*/
private function createRouteRequest(string $address, array $route, int $key): array
{
Expand All @@ -208,6 +213,9 @@ private function createRouteRequest(string $address, array $route, int $key): ar
$result['p'] = $action['data']['rules'];
}
}
if ($action['method'] === StandardRoute::NO_DEBUG_TYPE) {
$result['u'] = 1;
}
if ($action['method'] === StandardRoute::PLAIN_TYPE) {
// Counts only on the last assigned.
// Считается только по последнему назначенному.
Expand Down
7 changes: 7 additions & 0 deletions Main/Routes/Prepare/Verifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function isCheckedOrError(): true
$pageCount = 0;
$protectCount = 0;
$plainCount = 0;
$noDebugCount = 0;
foreach ($route['actions'] ?? [] as $action) {
$method = $action['method'];
if (\in_array($method, [
Expand All @@ -89,6 +90,9 @@ public function isCheckedOrError(): true
if ($method === StandardRoute::PROTECT_TYPE) {
$protectCount++;
}
if ($method === StandardRoute::NO_DEBUG_TYPE) {
$noDebugCount++;
}
if ($method === StandardRoute::PLAIN_TYPE && $action['data']['on']) {
$plainCount++;
}
Expand Down Expand Up @@ -158,6 +162,9 @@ public function isCheckedOrError(): true
if ($route['method'] === StandardRoute::DOMAIN_TYPE) {
$this->checkDomain($route);
}
if ($noDebugCount > 1) {
$this->error(AsyncRouteException::HL40_ERROR);
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions Main/Routes/Search/RouteFileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class RouteFileManager

protected ?bool $isPlain = null;

protected ?bool $isNoDebug = null;

protected static ?array $infoCache = null;

protected static bool|array $stubData = false;
Expand Down Expand Up @@ -122,6 +124,16 @@ public function getIsPlain(): null|bool
return $this->isPlain;
}

/**
* Returns the flag for forcing the debug panel to be disabled.
*
* Возвращает признак принудительного отключения отладочной панели.
*/
public function getIsNoDebug(): null|bool
{
return $this->isNoDebug;
}

/**
* Returns dynamic route data when matching parts in `/{param}/` as 'param' => `value`.
*
Expand Down Expand Up @@ -178,6 +190,8 @@ protected function searchBlock(): false|array
$index = $this->searchIndexPage((int)(self::$infoCache['index_page'] ?? 0), $request);
if ($index) {
$this->routeName = self::$infoCache['index_page_name'] ?? null;
$this->isNoDebug = self::$infoCache['no_debug'] ?? null;

return $index;
}
// Search for a list of routes.
Expand Down Expand Up @@ -481,6 +495,7 @@ private function createBlockDataNumber(SearchBlock $block): false|int
}
$this->protected = $block->protected();
$this->isPlain = $block->getIsPlain();
$this->isNoDebug = $block->getIsNoDebug();
$this->data = $block->getData();

return $blockNumber;
Expand Down
15 changes: 15 additions & 0 deletions Main/Routes/Search/SearchBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final class SearchBlock

private ?bool $isPlain = null;

private ?bool $isNoDebug = null;

private bool $isCompleteAddress = true;

public function __construct(
Expand Down Expand Up @@ -81,6 +83,16 @@ public function getIsPlain(): null|bool
return $this->isPlain;
}

/**
* Returns the flag for forcing the debug panel to be disabled.
*
* Возвращает признак принудительного отключения отладочной панели.
*/
public function getIsNoDebug(): null|bool
{
return $this->isNoDebug;
}

/**
* Returns an indication whether the found route has or does not have a trailing part.
*
Expand Down Expand Up @@ -278,6 +290,9 @@ private function setData(array $route, array $data = []): void
if (isset($route['b'])) {
$this->isPlain = (bool)$route['b'];
}
if (isset($route['u'])) {
$this->isNoDebug = (bool)$route['u'];
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions Main/Routes/StandardRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ abstract class StandardRoute

final public const REDIRECT_TYPE = 'redirect';

final public const NO_DEBUG_TYPE = 'noDebug';

private ?NameConverter $nameConverter = null;

/**
Expand Down
24 changes: 24 additions & 0 deletions Route/Group/GroupNoDebug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Hleb\Route\Group;

use Hleb\Main\Routes\Methods\Traits\Group\StandardGroupTrait;
use Hleb\Main\Routes\StandardRoute;

/**
* @internal
*/
final class GroupNoDebug extends StandardRoute
{
use StandardGroupTrait;

public function __construct()
{
$this->register([
'method' => self::NO_DEBUG_TYPE,
'from-group' => true,
]);
}
}
44 changes: 44 additions & 0 deletions Route/NoDebug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Hleb\Route;

use Hleb\Main\Routes\Methods\Traits\InsertAfterTrait;
use Hleb\Main\Routes\Methods\Traits\InsertBeforeTrait;
use Hleb\Main\Routes\Methods\Traits\InsertControllerTrait;
use Hleb\Main\Routes\Methods\Traits\InsertDomainTrait;
use Hleb\Main\Routes\Methods\Traits\InsertMiddlewareTrait;
use Hleb\Main\Routes\Methods\Traits\InsertModuleTrait;
use Hleb\Main\Routes\Methods\Traits\InsertNameTrait;
use Hleb\Main\Routes\Methods\Traits\InsertPageTrait;
use Hleb\Main\Routes\Methods\Traits\InsertProtectTrait;
use Hleb\Main\Routes\Methods\Traits\InsertRedirectTrait;
use Hleb\Main\Routes\Methods\Traits\InsertWhereTrait;
use Hleb\Main\Routes\StandardRoute;

/**
* @internal
*/
final class NoDebug extends StandardRoute
{
use InsertWhereTrait;
use InsertControllerTrait;
use InsertMiddlewareTrait;
use InsertAfterTrait;
use InsertBeforeTrait;
use InsertNameTrait;
use InsertProtectTrait;
use InsertModuleTrait;
use InsertDomainTrait;
use InsertPageTrait;
use InsertRedirectTrait;

public function __construct()
{
$this->register([
'method' => self::NO_DEBUG_TYPE,
'from-group' => false,
]);
}
}
1 change: 0 additions & 1 deletion Route/Protect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Hleb\Main\Routes\Methods\Traits\InsertMiddlewareTrait;
use Hleb\Main\Routes\Methods\Traits\InsertModuleTrait;
use Hleb\Main\Routes\Methods\Traits\InsertNameTrait;
use Hleb\Main\Routes\Methods\Traits\InsertPlainTrait;
use Hleb\Main\Routes\Methods\Traits\InsertPageTrait;
use Hleb\Main\Routes\Methods\Traits\InsertRedirectTrait;
use Hleb\Main\Routes\Methods\Traits\InsertWhereTrait;
Expand Down

0 comments on commit f53f47b

Please sign in to comment.