Skip to content

Commit

Permalink
constants are PascalCase
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 28, 2023
1 parent 2c0820a commit 73391b9
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function run(): void
public function createInitialRequest(): Request
{
$params = $this->router->match($this->httpRequest);
$presenter = $params[UI\Presenter::PRESENTER_KEY] ?? null;
$presenter = $params[UI\Presenter::PresenterKey] ?? null;

if ($params === null) {
throw new BadRequestException('No route for HTTP request.');
Expand All @@ -122,7 +122,7 @@ public function createInitialRequest(): Request
throw new BadRequestException('Invalid request. Presenter is not achievable.');
}

unset($params[UI\Presenter::PRESENTER_KEY]);
unset($params[UI\Presenter::PresenterKey]);
return new Request(
$presenter,
$this->httpRequest->getMethod(),
Expand Down
8 changes: 4 additions & 4 deletions src/Application/LinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function link(string $dest, array $params = []): string

if (is_subclass_of($class, UI\Presenter::class)) {
if ($action === '') {
$action = UI\Presenter::DEFAULT_ACTION;
$action = UI\Presenter::DefaultAction;
}

if (
Expand All @@ -80,14 +80,14 @@ public function link(string $dest, array $params = []): string
}

if ($action !== '') {
$params[UI\Presenter::ACTION_KEY] = $action;
$params[UI\Presenter::ActionKey] = $action;
}

$params[UI\Presenter::PRESENTER_KEY] = $presenter;
$params[UI\Presenter::PresenterKey] = $presenter;

$url = $this->router->constructUrl($params, $this->refUrl);
if ($url === null) {
unset($params[UI\Presenter::ACTION_KEY], $params[UI\Presenter::PRESENTER_KEY]);
unset($params[UI\Presenter::ActionKey], $params[UI\Presenter::PresenterKey]);
$paramsDecoded = urldecode(http_build_query($params, '', ', '));
throw new UI\InvalidLinkException("No route for $dest($paramsDecoded)");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Routers/SimpleRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct($defaults = [], int $flags = 0)

$defaults = [
self::PresenterKey => $presenter,
'action' => $action === '' ? Application\UI\Presenter::DEFAULT_ACTION : $action,
'action' => $action === '' ? Application\UI\Presenter::DefaultAction : $action,
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Application/UI/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected function receiveHttpData(): ?array
protected function beforeRender()
{
parent::beforeRender();
$key = ($this->isMethod('post') ? '_' : '') . Presenter::SIGNAL_KEY;
$key = ($this->isMethod('post') ? '_' : '') . Presenter::SignalKey;
if (!isset($this[$key])) {
$do = $this->lookupPath(Presenter::class) . self::NAME_SEPARATOR . 'submit';
$this[$key] = (new Nette\Forms\Controls\HiddenField($do))->setOmitted();
Expand Down
66 changes: 38 additions & 28 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,28 @@ abstract class Presenter extends Control implements Application\IPresenter
{
/** bad link handling {@link Presenter::$invalidLinkMode} */
public const
INVALID_LINK_SILENT = 0b0000,
INVALID_LINK_WARNING = 0b0001,
INVALID_LINK_EXCEPTION = 0b0010,
INVALID_LINK_TEXTUAL = 0b0100;
InvalidLinkSilent = 0b0000,
InvalidLinkWarning = 0b0001,
InvalidLinkException = 0b0010,
InvalidLinkTextual = 0b0100;

/** @internal special parameter key */
public const
PRESENTER_KEY = 'presenter',
SIGNAL_KEY = 'do',
ACTION_KEY = 'action',
FLASH_KEY = '_fid',
DEFAULT_ACTION = 'default';
PresenterKey = 'presenter',
SignalKey = 'do',
ActionKey = 'action',
FlashKey = '_fid',
DefaultAction = 'default';

public const INVALID_LINK_SILENT = self::InvalidLinkSilent;
public const INVALID_LINK_WARNING = self::InvalidLinkWarning;
public const INVALID_LINK_EXCEPTION = self::InvalidLinkException;
public const INVALID_LINK_TEXTUAL = self::InvalidLinkTextual;
public const PRESENTER_KEY = self::PresenterKey;
public const SIGNAL_KEY = self::SignalKey;
public const ACTION_KEY = self::ActionKey;
public const FLASH_KEY = self::FlashKey;
public const DEFAULT_ACTION = self::DefaultAction;

/** @var int */
public $invalidLinkMode;
Expand Down Expand Up @@ -879,7 +889,7 @@ protected function createRequest(
// PROCESS ARGUMENTS
if (is_subclass_of($presenterClass, self::class)) {
if ($action === '') {
$action = self::DEFAULT_ACTION;
$action = self::DefaultAction;
}

$current = ($action === '*' || strcasecmp($action, (string) $this->action) === 0) && $presenterClass === static::class;
Expand Down Expand Up @@ -947,16 +957,16 @@ protected function createRequest(

// ADD ACTION & SIGNAL & FLASH
if ($action) {
$args[self::ACTION_KEY] = $action;
$args[self::ActionKey] = $action;
}

if (!empty($signal)) {
$args[self::SIGNAL_KEY] = $component->getParameterId($signal);
$current = $current && $args[self::SIGNAL_KEY] === $this->getParameter(self::SIGNAL_KEY);
$args[self::SignalKey] = $component->getParameterId($signal);
$current = $current && $args[self::SignalKey] === $this->getParameter(self::SignalKey);
}

if (($mode === 'redirect' || $mode === 'forward') && $this->hasFlashSession()) {
$args[self::FLASH_KEY] = $this->getFlashKey();
$args[self::FlashKey] = $this->getFlashKey();
}

$this->lastCreatedRequest = new Application\Request($presenter, Application\Request::FORWARD, $args);
Expand Down Expand Up @@ -1011,7 +1021,7 @@ protected function requestToUrl(Application\Request $request, ?bool $relative =
$url = $this->router->constructUrl($request->toArray(), $this->refUrlCache);
if ($url === null) {
$params = $request->getParameters();
unset($params[self::ACTION_KEY], $params[self::PRESENTER_KEY]);
unset($params[self::ActionKey], $params[self::PresenterKey]);
$params = urldecode(http_build_query($params, '', ', '));
throw new InvalidLinkException("No route for {$request->getPresenterName()}:{$request->getParameter('action')}($params)");
}
Expand Down Expand Up @@ -1104,13 +1114,13 @@ public static function argsToParams(
*/
protected function handleInvalidLink(InvalidLinkException $e): string
{
if ($this->invalidLinkMode & self::INVALID_LINK_EXCEPTION) {
if ($this->invalidLinkMode & self::InvalidLinkException) {
throw $e;
} elseif ($this->invalidLinkMode & self::INVALID_LINK_WARNING) {
} elseif ($this->invalidLinkMode & self::InvalidLinkWarning) {
trigger_error('Invalid link: ' . $e->getMessage(), E_USER_WARNING);
}

return $this->invalidLinkMode & self::INVALID_LINK_TEXTUAL
return $this->invalidLinkMode & self::InvalidLinkTextual
? '#error: ' . $e->getMessage()
: '#';
}
Expand Down Expand Up @@ -1149,7 +1159,7 @@ public function restoreRequest(string $key): void
$request = clone $session[$key][1];
unset($session[$key]);
$params = $request->getParameters();
$params[self::FLASH_KEY] = $this->getFlashKey();
$params[self::FlashKey] = $this->getFlashKey();
$request->setParameters($params);
if ($request->isMethod('POST')) {
$request->setFlag(Application\Request::RESTORED, true);
Expand Down Expand Up @@ -1284,12 +1294,12 @@ private function initGlobalParameters(): void
$selfParams = [];

$params = $this->request->getParameters();
if (($tmp = $this->request->getPost('_' . self::SIGNAL_KEY)) !== null) {
$params[self::SIGNAL_KEY] = $tmp;
if (($tmp = $this->request->getPost('_' . self::SignalKey)) !== null) {
$params[self::SignalKey] = $tmp;
} elseif ($this->isAjax()) {
$params += $this->request->getPost();
if (($tmp = $this->request->getPost(self::SIGNAL_KEY)) !== null) {
$params[self::SIGNAL_KEY] = $tmp;
if (($tmp = $this->request->getPost(self::SignalKey)) !== null) {
$params[self::SignalKey] = $tmp;
}
}

Expand All @@ -1304,7 +1314,7 @@ private function initGlobalParameters(): void
}

// init & validate $this->action & $this->view
$action = $selfParams[self::ACTION_KEY] ?? self::DEFAULT_ACTION;
$action = $selfParams[self::ActionKey] ?? self::DefaultAction;
if (!is_string($action) || !Nette\Utils\Strings::match($action, '#^[a-zA-Z0-9][a-zA-Z0-9_\x7f-\xff]*$#D')) {
$this->error('Action name is not valid.');
}
Expand All @@ -1313,8 +1323,8 @@ private function initGlobalParameters(): void

// init $this->signalReceiver and key 'signal' in appropriate params array
$this->signalReceiver = $this->getUniqueId();
if (isset($selfParams[self::SIGNAL_KEY])) {
$param = $selfParams[self::SIGNAL_KEY];
if (isset($selfParams[self::SignalKey])) {
$param = $selfParams[self::SignalKey];
if (!is_string($param)) {
$this->error('Signal name is not string.');
}
Expand Down Expand Up @@ -1354,7 +1364,7 @@ final public function popGlobalParameters(string $id): array

private function getFlashKey(): ?string
{
$flashKey = $this->getParameter(self::FLASH_KEY);
$flashKey = $this->getParameter(self::FlashKey);
return is_string($flashKey) && $flashKey !== ''
? $flashKey
: null;
Expand All @@ -1379,7 +1389,7 @@ public function getFlashSession(): Http\SessionSection
{
$flashKey = $this->getFlashKey();
if ($flashKey === null) {
$this->params[self::FLASH_KEY] = $flashKey = Nette\Utils\Random::generate(4);
$this->params[self::FlashKey] = $flashKey = Nette\Utils\Random::generate(4);
}

return $this->getSession('Nette.Application.Flash/' . $flashKey);
Expand Down
4 changes: 2 additions & 2 deletions src/Bridges/ApplicationDI/ApplicationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public function loadConfiguration()
$builder->addExcludedClasses([UI\Presenter::class]);

$this->invalidLinkMode = $this->debugMode
? UI\Presenter::INVALID_LINK_TEXTUAL | ($config->silentLinks ? 0 : UI\Presenter::INVALID_LINK_WARNING)
: UI\Presenter::INVALID_LINK_WARNING;
? UI\Presenter::InvalidLinkTextual | ($config->silentLinks ? 0 : UI\Presenter::InvalidLinkWarning)
: UI\Presenter::InvalidLinkWarning;

$builder->addDefinition($this->prefix('application'))
->setFactory(Nette\Application\Application::class)
Expand Down
8 changes: 4 additions & 4 deletions src/Bridges/ApplicationTracy/RoutingPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ private function findSource(): void
$rc = new \ReflectionClass($class);

if ($rc->isSubclassOf(Nette\Application\UI\Presenter::class)) {
if (isset($params[Presenter::SIGNAL_KEY])) {
$method = $class::formatSignalMethod($params[Presenter::SIGNAL_KEY]);
if (isset($params[Presenter::SignalKey])) {
$method = $class::formatSignalMethod($params[Presenter::SignalKey]);

} elseif (isset($params[Presenter::ACTION_KEY])) {
$action = $params[Presenter::ACTION_KEY];
} elseif (isset($params[Presenter::ActionKey])) {
$action = $params[Presenter::ActionKey];
$method = $class::formatActionMethod($action);
if (!$rc->hasMethod($method)) {
$method = $class::formatRenderMethod($action);
Expand Down
10 changes: 5 additions & 5 deletions src/Bridges/ApplicationTracy/templates/RoutingPanel.panel.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ use Tracy\Helpers;
<h1>
<?php if ($matched === null): ?>
no route
<?php elseif (isset($matched[Presenter::PRESENTER_KEY])): ?>
<?= Helpers::escapeHtml($matched[Presenter::PRESENTER_KEY] . ':' . (isset($matched[Presenter::ACTION_KEY]) ? $matched[Presenter::ACTION_KEY] : Presenter::DEFAULT_ACTION) . (isset($matched[Presenter::SIGNAL_KEY]) ? " {$matched[Presenter::SIGNAL_KEY]}!" : '')) ?>
<?php elseif (isset($matched[Presenter::PresenterKey])): ?>
<?= Helpers::escapeHtml($matched[Presenter::PresenterKey] . ':' . (isset($matched[Presenter::ActionKey]) ? $matched[Presenter::ActionKey] : Presenter::DefaultAction) . (isset($matched[Presenter::SignalKey]) ? " {$matched[Presenter::SignalKey]}!" : '')) ?>
<?php endif ?>
</h1>

Expand Down Expand Up @@ -99,9 +99,9 @@ use Tracy\Helpers;

<td><?php if ($router->params): ?><code>
<?php $params = $router->params; ?>
<?php if (isset($params[Presenter::PRESENTER_KEY])): ?>
<strong><?= Helpers::escapeHtml($params['presenter'] . ':' . (isset($params[Presenter::ACTION_KEY]) ? $params[Presenter::ACTION_KEY] : Presenter::DEFAULT_ACTION)) ?></strong><br />
<?php unset($params[Presenter::PRESENTER_KEY], $params[Presenter::ACTION_KEY]) ?>
<?php if (isset($params[Presenter::PresenterKey])): ?>
<strong><?= Helpers::escapeHtml($params['presenter'] . ':' . (isset($params[Presenter::ActionKey]) ? $params[Presenter::ActionKey] : Presenter::DefaultAction)) ?></strong><br />
<?php unset($params[Presenter::PresenterKey], $params[Presenter::ActionKey]) ?>
<?php endif ?>
<?php foreach ($params as $key => $value): ?>
<?= Helpers::escapeHtml($key), '&nbsp;=&nbsp;', is_string($value) ? Helpers::escapeHtml($value) . '<br />' : Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true]) ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ use Tracy\Helpers;
<svg viewBox="0 0 2048 2048">
<path fill="#d86b01" d="m1559.7 1024c0 17-6 32-19 45l-670 694.48c-13 13-28 19-45 19s-32-6-45-19-19-28-19-45v-306.48h-438.52c-17 0-32-6-45-19s-19-28-19-45v-642c0-17 6-32 19-45s28-19 45-19h438.52v-309.41c0-17 6-32 19-45s28-19 45-19 32 6 45 19l670 691.41c13 13 19 28 19 45z"/>
<path d="m1914.7 1505c0 79-31 147-87 204-56 56-124 85-203 85h-320c-9 0-16-3-22-9-14-23-21-90 3-110 5-4 12-6 21-6h320c44 0 82-16 113-47s47-69 47-113v-962c0-44-16-82-47-113s-69-47-113-47h-312c-11 0-21-3-30-9-15-25-21-90 3-110 5-4 12-6 21-6h320c79 0 147 28 204 85 56 56 82 124 82 204-9 272 9 649 0 954z" fill-opacity=".5" fill="#d86b01"/>
</svg><span class="tracy-label"><?php if ($matched === null): ?>no route<?php elseif (isset($matched[Presenter::PRESENTER_KEY])): echo Helpers::escapeHtml($matched[Presenter::PRESENTER_KEY] . ':' . (isset($matched[Presenter::ACTION_KEY]) ? $matched[Presenter::ACTION_KEY] : Presenter::DEFAULT_ACTION) . (isset($matched[Presenter::SIGNAL_KEY]) ? " {$matched[Presenter::SIGNAL_KEY]}!" : '')); endif ?></span>
</svg><span class="tracy-label"><?php if ($matched === null): ?>no route<?php elseif (isset($matched[Presenter::PresenterKey])): echo Helpers::escapeHtml($matched[Presenter::PresenterKey] . ':' . (isset($matched[Presenter::ActionKey]) ? $matched[Presenter::ActionKey] : Presenter::DefaultAction) . (isset($matched[Presenter::SignalKey]) ? " {$matched[Presenter::SignalKey]}!" : '')); endif ?></span>
</span>
8 changes: 4 additions & 4 deletions tests/Bridges.DI/ApplicationExtension.invalidLink.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test('', function () {

$container = new Container4;
Assert::same(
Presenter::INVALID_LINK_TEXTUAL,
Presenter::InvalidLinkTextual,
$container->getService('presenter')->invalidLinkMode
);
});
Expand All @@ -58,7 +58,7 @@ test('', function () {

$container = new Container5;
Assert::same(
Presenter::INVALID_LINK_WARNING | Presenter::INVALID_LINK_TEXTUAL,
Presenter::InvalidLinkWarning | Presenter::InvalidLinkTextual,
$container->getService('presenter')->invalidLinkMode
);
});
Expand All @@ -78,7 +78,7 @@ test('', function () {

$container = new Container6;
Assert::same(
Presenter::INVALID_LINK_WARNING,
Presenter::InvalidLinkWarning,
$container->getService('presenter')->invalidLinkMode
);
});
Expand All @@ -98,7 +98,7 @@ test('', function () {

$container = new Container7;
Assert::same(
Presenter::INVALID_LINK_WARNING,
Presenter::InvalidLinkWarning,
$container->getService('presenter')->invalidLinkMode
);
});
Loading

0 comments on commit 73391b9

Please sign in to comment.