Skip to content

Refactor Application to return Response from processRequest #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ea75bb3
optimized global function calls
dg Jun 19, 2025
8e44912
github actions updated
dg Feb 3, 2025
a15b2bf
error 500: improved CSS style reset [Closes nette/tracy#581]
dg Nov 7, 2024
a4f2b39
Latte: Template is abstract
dg Nov 23, 2024
4fcf653
exception: use natural explanatory style
dg Dec 12, 2024
c4dc05c
RoutingPanel: convert templates to Latte-like syntax
dg Jan 12, 2025
257053d
uses nette/tester 2.5.2
dg Feb 1, 2025
cee6954
tests: improved descriptions
dg Feb 2, 2025
b01244b
UIExtension: added filter |absoluteUrl
dg Feb 18, 2025
580e8fc
opened 4.0-dev
dg Dec 14, 2023
38e0368
removed support for Latte 2
dg May 26, 2024
6512dd5
removed compatibility for old class names
dg Oct 15, 2021
9012b17
added type hints (BC break)
dg Feb 8, 2024
1f8b489
RouteList: array access is deprecated
dg Oct 15, 2021
e22102a
Presenter::handleInvalidLink() -> processInvalidLink() (BC break)
dg May 12, 2024
990189c
Revert "UI\PresenterComponent: removed references created by loadStat…
dg Oct 24, 2022
36a8e10
Component: method checkRequirements() is called for createComponent<N…
dg Apr 20, 2024
8f90019
Component: only UI components can be added to presenter/component (BC…
dg Apr 7, 2024
a4bc74f
deprecated magic properties except for $template & $payload (BC break)
dg Sep 24, 2021
b38ee7c
Component::link() & etc uses variadic parameter
dg Oct 19, 2021
369351f
Component::getParameter() $default is deprecated
dg Jun 21, 2022
d7a5642
LinkGenerator: query part in link is deprecated
dg Apr 19, 2024
84ec05f
Presenter: removed constructor (BC break!)
dg Dec 11, 2023
5c82f7d
@annotations are deprecated (BC break)
dg Apr 19, 2024
f6a7af4
PresenterFactoryCallback: refreshes page instead of creating presente…
dg Apr 28, 2024
7e3b662
LatteFactory: $control is passed to create() (BC break)
dg Jun 11, 2024
ee24f8e
PresenterFactory: default mapping is App\Presentation\*\**Presenter (…
dg Apr 14, 2024
0aa7f5e
Refactor Application to return Response from processRequest
bckp Jun 22, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/coding-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.3
coverage: none

- run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress
Expand All @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.3
coverage: none

- run: composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress
Expand Down
9 changes: 2 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ jobs:
- run: composer install --no-progress --prefer-dist
- run: vendor/bin/tester tests -s -C
- if: failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: output
name: output-${{ matrix.php }}
path: tests/**/output


Expand All @@ -40,11 +40,6 @@ jobs:

- run: composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable
- run: vendor/bin/tester tests -s -C
- if: failure()
uses: actions/upload-artifact@v3
with:
name: output
path: tests/**/output


code_coverage:
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": "8.1 - 8.4",
"nette/component-model": "^3.1",
"nette/http": "^3.3",
"nette/http": "^3.3.2",
"nette/routing": "^3.1",
"nette/utils": "^4.0"
},
Expand All @@ -26,12 +26,12 @@
"latte/latte": "Allows using Latte in templates"
},
"require-dev": {
"nette/tester": "^2.5",
"nette/tester": "^2.5.2",
"nette/di": "^3.2",
"nette/forms": "^3.2",
"nette/robot-loader": "^4.0",
"nette/security": "^3.2",
"latte/latte": "^2.10.2 || ^3.0.18",
"latte/latte": "^3.0.18",
"tracy/tracy": "^2.9",
"mockery/mockery": "^1.6@stable",
"phpstan/phpstan-nette": "^2.0@stable",
Expand All @@ -42,7 +42,7 @@
"nette/di": "<3.2",
"nette/forms": "<3.2",
"nette/schema": "<1.3",
"latte/latte": "<2.7.1 || >=3.0.0 <3.0.18 || >=3.1",
"latte/latte": "<3.0.18",
"tracy/tracy": "<2.9"
},
"autoload": {
Expand All @@ -58,7 +58,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.2-dev"
"dev-master": "4.0-dev"
}
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Nette Application MVC
=====================

[![Downloads this Month](https://img.shields.io/packagist/dm/nette/application.svg)](https://packagist.org/packages/nette/application)
[![Tests](https://github.com/nette/application/actions/workflows/tests.yml/badge.svg?branch=v3.2)](https://github.com/nette/application/actions)
[![Tests](https://github.com/nette/application/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/nette/application/actions)
[![Latest Stable Version](https://poser.pugx.org/nette/application/v/stable)](https://github.com/nette/application/releases)
[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/nette/application/blob/master/license.md)

Expand Down
9 changes: 5 additions & 4 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Nette;
use Nette\Routing\Router;
use Nette\Utils\Arrays;
use function count, is_string, str_starts_with, strcasecmp;


/**
Expand Down Expand Up @@ -73,15 +74,15 @@ public function run(): void
{
try {
Arrays::invoke($this->onStartup, $this);
$this->processRequest($this->createInitialRequest());
$this->processRequest($this->createInitialRequest())->send($this->httpRequest, $this->httpResponse);
Copy link
Preview

Copilot AI Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] For enhanced readability and debugging, consider storing the returned Response in a temporary variable before calling send().

Suggested change
$this->processRequest($this->createInitialRequest())->send($this->httpRequest, $this->httpResponse);
$response = $this->processRequest($this->createInitialRequest());
$response->send($this->httpRequest, $this->httpResponse);

Copilot uses AI. Check for mistakes.

Arrays::invoke($this->onShutdown, $this);

} catch (\Throwable $e) {
$this->sendHttpCode($e);
Arrays::invoke($this->onError, $this, $e);
if ($this->catchExceptions && ($req = $this->createErrorRequest($e))) {
try {
$this->processRequest($req);
$this->processRequest($req)->send($this->httpRequest, $this->httpResponse);
Copy link
Preview

Copilot AI Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] For consistency with the run() method, consider assigning the returned Response to a variable before invoking send(), which can help with error handling and debugging.

Suggested change
$this->processRequest($req)->send($this->httpRequest, $this->httpResponse);
$response = $this->processRequest($req);
$response->send($this->httpRequest, $this->httpResponse);

Copilot uses AI. Check for mistakes.

Arrays::invoke($this->onShutdown, $this, $e);
return;

Expand Down Expand Up @@ -120,7 +121,7 @@ public function createInitialRequest(): Request
}


public function processRequest(Request $request): void
public function processRequest(Request $request): Response
{
process:
if (count($this->requests) > $this->maxLoop) {
Expand Down Expand Up @@ -155,7 +156,7 @@ public function processRequest(Request $request): void
}

Arrays::invoke($this->onResponse, $this, $response);
$response->send($this->httpRequest, $this->httpResponse);
return $response;
}


Expand Down
1 change: 1 addition & 0 deletions src/Application/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Application;

use Nette;
use function class_parents, class_uses, strrpos, substr;


/**
Expand Down
2 changes: 2 additions & 0 deletions src/Application/LinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Nette\Http\UrlScript;
use Nette\Routing\Router;
use Nette\Utils\Reflection;
use function array_intersect_key, array_key_exists, http_build_query, is_string, is_subclass_of, parse_str, preg_match, rtrim, str_contains, str_ends_with, strcasecmp, strlen, strncmp, strtr, substr, trigger_error, urldecode;


/**
Expand Down Expand Up @@ -242,6 +243,7 @@ public static function parseDestination(string $destination): array
}

if (!empty($matches['query'])) {
trigger_error("Link format is obsolete, use arguments instead of query string in '$destination'.", E_USER_DEPRECATED);
parse_str(substr($matches['query'], 1), $args);
}

Expand Down
1 change: 1 addition & 0 deletions src/Application/MicroPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Nette\Application\Responses;
use Nette\Http;
use Nette\Routing\Router;
use function is_array, is_object, is_string;


/**
Expand All @@ -22,7 +23,7 @@
*/
final class MicroPresenter implements Application\IPresenter
{
private ?Application\Request $request;

Check failure on line 26 in src/Application/MicroPresenter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property NetteModule\MicroPresenter::$request (Nette\Application\Request|null) is never assigned null so it can be removed from the property type.


public function __construct(
Expand Down Expand Up @@ -91,7 +92,7 @@

if (is_array($response)) {
[$templateSource, $templateParams] = $response;
$response = $this->createTemplate()->setParameters($templateParams);

Check failure on line 95 in src/Application/MicroPresenter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Nette\Application\UI\Template::setParameters().
if (!$templateSource instanceof \SplFileInfo) {
$response->getLatte()->setLoader(new Latte\Loaders\StringLoader);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Application/PresenterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Application;

use Nette;
use function array_shift, class_exists, count, explode, is_array, is_string, preg_match, strtr;


/**
Expand All @@ -19,7 +20,7 @@ class PresenterFactory implements IPresenterFactory
{
/** @var array[] of module => splited mask */
private array $mapping = [
'*' => ['', '*Module\\', '*Presenter'],
'*' => ['App\Presentation\\', '*\\', '**Presenter'],
'Nette' => ['NetteModule\\', '*\\', '*Presenter'],
];

Expand Down
11 changes: 6 additions & 5 deletions src/Application/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
namespace Nette\Application;

use Nette;
use function func_num_args, strcasecmp;


/**
* Presenter request.
*
* @property string $presenterName
* @property array $parameters
* @property array $post
* @property array $files
* @property string|null $method
* @property-deprecated string $presenterName
* @property-deprecated array $parameters
* @property-deprecated array $post
* @property-deprecated array $files
* @property-deprecated string|null $method
*/
final class Request
{
Expand Down
1 change: 1 addition & 0 deletions src/Application/Responses/FileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Application\Responses;

use Nette;
use function strlen;


/**
Expand Down
1 change: 1 addition & 0 deletions src/Application/Routers/CliRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Application\Routers;

use Nette;
use function is_array;


/**
Expand Down
4 changes: 1 addition & 3 deletions src/Application/Routers/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Application\Routers;

use Nette;
use function is_string, lcfirst, preg_replace, rawurlencode, str_replace, strlen, strncmp, strrpos, strtolower, strtr, substr, ucwords;


/**
Expand Down Expand Up @@ -84,8 +85,8 @@
return null;
}

$presenter = $params[self::PresenterKey] ?? null;

Check failure on line 88 in src/Application/Routers/Route.php

View workflow job for this annotation

GitHub Actions / PHPStan

Offset 'presenter' on non-empty-array on left side of ?? always exists and is not nullable.
if (isset($this->getMetadata()[self::ModuleKey], $params[self::ModuleKey]) && is_string($presenter)) {

Check failure on line 89 in src/Application/Routers/Route.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to function is_string() with string will always evaluate to true.
$params[self::PresenterKey] = $params[self::ModuleKey] . ':' . $params[self::PresenterKey];
}

Expand Down Expand Up @@ -187,6 +188,3 @@
return $s;
}
}


interface_exists(Nette\Application\IRouter::class);
14 changes: 11 additions & 3 deletions src/Application/Routers/RouteList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

use JetBrains\PhpStorm\Language;
use Nette;
use function count, is_int, is_string, strlen, strncmp, substr;


/**
* The router broker.
*/
class RouteList extends Nette\Routing\RouteList implements Nette\Routing\Router, \ArrayAccess

Check failure on line 20 in src/Application/Routers/RouteList.php

View workflow job for this annotation

GitHub Actions / PHPStan

The @implements tag of class Nette\Application\Routers\RouteList describes IteratorAggregate but the class implements: Nette\Routing\Router, ArrayAccess
{
private const PresenterKey = 'presenter';

Expand Down Expand Up @@ -75,7 +76,7 @@

public function withModule(string $module): static
{
$router = new static;

Check failure on line 79 in src/Application/Routers/RouteList.php

View workflow job for this annotation

GitHub Actions / PHPStan

Unsafe usage of new static().
$router->module = $module . ':';
$router->parent = $this;
$this->add($router);
Expand All @@ -95,6 +96,13 @@
*/
public function offsetSet($index, $router): void
{
if ($router instanceof Route) {
trigger_error('Usage `$router[] = new Route(...)` is deprecated, use `$router->addRoute(...)`.', E_USER_DEPRECATED);
} else {
$class = getclass($router);

Check failure on line 102 in src/Application/Routers/RouteList.php

View workflow job for this annotation

GitHub Actions / PHPStan

Function getclass not found.
trigger_error("Usage `\$router[] = new $class` is deprecated, use `\$router->add(new $class)`.", E_USER_DEPRECATED);
}

if ($index === null) {
$this->add($router);
} else {
Expand All @@ -109,6 +117,7 @@
*/
public function offsetGet($index): Nette\Routing\Router
{
trigger_error('Usage `$route = $router[...]` is deprecated, use `$router->getRouters()`.', E_USER_DEPRECATED);
if (!$this->offsetExists($index)) {
throw new Nette\OutOfRangeException('Offset invalid or out of range');
}
Expand All @@ -122,7 +131,8 @@
*/
public function offsetExists($index): bool
{
trigger_error('Usage `isset($router[...])` is deprecated.', E_USER_DEPRECATED);
return is_int($index) && $index >= 0 && $index < count($this->getRouters());

Check failure on line 135 in src/Application/Routers/RouteList.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to function is_int() with int will always evaluate to true.
}


Expand All @@ -132,13 +142,11 @@
*/
public function offsetUnset($index): void
{
trigger_error('Usage `unset($router[$index])` is deprecated, use `$router->modify($index, null)`.', E_USER_DEPRECATED);
if (!$this->offsetExists($index)) {
throw new Nette\OutOfRangeException('Offset invalid or out of range');
}

$this->modify($index, null);
}
}


interface_exists(Nette\Application\IRouter::class);
4 changes: 1 addition & 3 deletions src/Application/Routers/SimpleRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Nette;
use Nette\Application;
use function is_string;


/**
Expand Down Expand Up @@ -38,6 +39,3 @@ public function __construct(array|string $defaults = [])
parent::__construct($defaults);
}
}


interface_exists(Nette\Application\IRouter::class);
1 change: 1 addition & 0 deletions src/Application/UI/AccessPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Nette;
use Nette\Application\Attributes;
use Nette\Utils\Reflection;
use function array_map, implode, in_array, str_starts_with, strtoupper;


/**
Expand Down
Loading
Loading