Skip to content

Commit e838f31

Browse files
committed
Merge branch '23.x'
2 parents 2745939 + 122390d commit e838f31

6 files changed

Lines changed: 121 additions & 44 deletions

File tree

.phpstorm.meta.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/*
3+
* This file is part of One Project.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
namespace PHPSTORM_META;
9+
10+
registerArgumentsSet(
11+
'methods',
12+
\Framework\HTTP\Method::CONNECT,
13+
\Framework\HTTP\Method::DELETE,
14+
\Framework\HTTP\Method::GET,
15+
\Framework\HTTP\Method::HEAD,
16+
\Framework\HTTP\Method::OPTIONS,
17+
\Framework\HTTP\Method::PATCH,
18+
\Framework\HTTP\Method::POST,
19+
\Framework\HTTP\Method::PUT,
20+
\Framework\HTTP\Method::TRACE,
21+
'CONNECT',
22+
'DELETE',
23+
'GET',
24+
'HEAD',
25+
'OPTIONS',
26+
'PATCH',
27+
'POST',
28+
'PUT',
29+
'TRACE',
30+
);
31+
32+
expectedArguments(
33+
\Tests\One\TestCase::runOne(),
34+
1,
35+
argumentsSet('methods')
36+
);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
],
2525
"require": {
2626
"php": ">=8.1",
27-
"aplus/framework": "^22"
27+
"aplus/framework": "^23"
2828
},
2929
"require-dev": {
3030
"ext-xdebug": "*",

guide/index.rst

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Aplus Framework One Project.
99
- `Installation`_
1010
- `Structure`_
1111
- `Configuration`_
12-
- `Routing`_
1312
- `Running`_
1413
- `Deployment`_
1514
- `Conclusion`_
@@ -60,27 +59,30 @@ Configuration
6059
-------------
6160

6261
The application's configurations are set directly in the App class constructor,
63-
where, by default, only two services are set; the **exceptionHandler** and the
62+
where, by default, five services are set; the **exceptionHandler** and the
6463
**logger**. Which shows a beautiful page when exceptions are thrown and is also
6564
able to save logs in the ``storage/logs`` directory.
6665

67-
Routing
68-
-------
66+
Also, the **request** service is used to allow certain hosts and auto-redirect
67+
to URLs with HTTPS.
6968

70-
The routing is defined directly in the index.php file, as it is expected not to
71-
contain too many lines. By default, only the route with the root path of the URL
72-
is set, and also a customization in the Error 404 page. Both return a closure
73-
with an array to be JSON-encoded.
69+
The **response** service is defined so that the application works with the
70+
browser using cache through the
71+
`ETag <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag>`_ header.
7472

75-
Running
76-
-------
73+
And the routing is defined directly in the **router** config, as it is expected
74+
not to contain too many lines. By default, only the route with the root path of
75+
the URL is set, and also a customization in the Error 404 page. Both return a
76+
closure with an array to be JSON-encoded.
7777

78-
Finally, the application will respond to HTTP requests. As per the last line of
79-
the index.php file:
78+
Information on how to set up services can be found in the
79+
`MVC library documentation <https://docs.aplus-framework.com/guides/libraries/mvc/index.html#services>`_.
8080

81-
.. code-block:: php
81+
Running
82+
-------
8283

83-
$app->runHttp();
84+
Finally, the application will respond to HTTP requests through the ``runHttp``
85+
method.
8486

8587
If the application also responds from the command line, it is possible to use
8688
the ``run`` method and, if it is only for the command line; ``runCli``.

public/index.php

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
require __DIR__ . '/../vendor/autoload.php';
1212
}
1313

14-
use Framework\Debug\ExceptionHandler;
1514
use Framework\Log\LogLevel;
1615
use Framework\MVC\App;
1716
use Framework\Routing\RouteCollection;
17+
use Framework\Routing\Router;
1818

19-
$app = new App([
19+
define('ENVIRONMENT', $_SERVER['ENVIRONMENT'] ?? 'production');
20+
21+
(new App([
2022
'exceptionHandler' => [
2123
'default' => [
22-
'environment' => $_SERVER['ENVIRONMENT'] ?? ExceptionHandler::PRODUCTION,
24+
'environment' => ENVIRONMENT,
2325
'initialize' => true,
2426
'logger_instance' => 'default',
2527
],
@@ -30,15 +32,35 @@
3032
'level' => LogLevel::ERROR,
3133
],
3234
],
33-
]);
34-
App::router()->serve(null, static function (RouteCollection $routes) : void {
35-
$routes->get('/', static function () : array {
36-
return [
37-
'message' => 'I am the One! You found me.',
38-
];
39-
});
40-
$routes->notFound(static fn () : array => [
41-
'message' => 'Route not found.',
42-
]);
43-
});
44-
$app->runHttp();
35+
'request' => [
36+
'default' => [
37+
'allowed_hosts' => [
38+
'localhost:8080',
39+
],
40+
'force_https' => ENVIRONMENT !== 'development',
41+
],
42+
],
43+
'response' => [
44+
'default' => [
45+
'auto_etag' => true,
46+
],
47+
],
48+
'router' => [
49+
'default' => [
50+
'auto_methods' => true,
51+
'auto_options' => true,
52+
'callback' => static function (Router $router) : void {
53+
$router->serve(null, static function (RouteCollection $routes) : void {
54+
$routes->get('/', static function () : array {
55+
return [
56+
'message' => 'I am the One! You found me.',
57+
];
58+
});
59+
$routes->notFound(static fn () : array => [
60+
'message' => 'Route not found.',
61+
]);
62+
});
63+
},
64+
],
65+
],
66+
], ENVIRONMENT === 'development'))->runHttp();

tests/OneTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
namespace Tests\One;
99

10+
use Framework\HTTP\Status;
1011
use Framework\MVC\App;
1112

1213
/**
@@ -16,11 +17,9 @@
1617
*/
1718
final class OneTest extends TestCase
1819
{
19-
protected string $baseUrl = 'http://domain.tld/';
20-
2120
public function testConfigs() : void
2221
{
23-
$this->runOne($this->baseUrl);
22+
$this->runOne();
2423
$configs = App::config()->get('exceptionHandler');
2524
self::assertArrayHasKey('logger_instance', $configs);
2625
$configs = App::config()->get('logger');
@@ -30,15 +29,16 @@ public function testConfigs() : void
3029

3130
public function testIndex() : void
3231
{
33-
$response = $this->runOne($this->baseUrl);
34-
self::assertSame(200, $response['code']);
32+
$response = $this->runOne();
33+
self::assertSame(Status::OK, $response['code']);
34+
self::assertArrayHasKey('etag', $response['headers']);
3535
self::assertStringContainsString('One', $response['body']);
3636
}
3737

3838
public function testNotFound() : void
3939
{
40-
$response = $this->runOne($this->baseUrl . 'wakawaka');
41-
self::assertSame(404, $response['code']);
40+
$response = $this->runOne('/wakawaka');
41+
self::assertSame(Status::NOT_FOUND, $response['code']);
4242
self::assertStringContainsString('Route not found', $response['body']);
4343
}
4444
}

tests/TestCase.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,44 @@
88
namespace Tests\One;
99

1010
use Framework\HTTP\URL;
11+
use Framework\MVC\App;
1112
use JetBrains\PhpStorm\ArrayShape;
1213

1314
/**
1415
* Class TestCase.
1516
*/
1617
abstract class TestCase extends \PHPUnit\Framework\TestCase
1718
{
19+
protected string $baseUrl = 'https://localhost:8080/';
20+
1821
/**
1922
* Run the One file.
2023
*
21-
* @param string|URL $url
24+
* @param string|URL|null $url
2225
* @param string $method
2326
* @param array<string,string> $headers
2427
*
2528
* @return array<string,mixed>
2629
*/
2730
#[ArrayShape(['code' => 'int', 'headers' => 'array', 'body' => 'string'])]
28-
protected function runOne(URL | string $url, string $method = 'GET', array $headers = []) : array
29-
{
31+
protected function runOne(
32+
URL | string $url = null,
33+
string $method = 'GET',
34+
array $headers = []
35+
) : array {
36+
$this->baseUrl = \rtrim($this->baseUrl, '/') . '/';
37+
if ($url === null) {
38+
$url = $this->baseUrl;
39+
}
40+
if (\is_string($url) && \str_starts_with($url, '/')) {
41+
$url = $this->baseUrl . \ltrim($url, '/');
42+
}
3043
if ( ! $url instanceof URL) {
3144
$url = new URL($url);
3245
}
46+
if ($url->getScheme() === 'https') {
47+
$_SERVER['HTTPS'] = 'on';
48+
}
3349
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
3450
$_SERVER['REQUEST_METHOD'] = $method;
3551
$_SERVER['REQUEST_URI'] = $url->getPath();
@@ -41,11 +57,12 @@ protected function runOne(URL | string $url, string $method = 'GET', array $head
4157
}
4258
\ob_start();
4359
require __DIR__ . '/../public/index.php';
44-
$body = (string) \ob_get_clean();
60+
\ob_end_clean();
61+
$response = App::response();
4562
return [
46-
'code' => (int) \http_response_code(),
47-
'headers' => \headers_list(),
48-
'body' => $body,
63+
'code' => $response->getStatusCode(),
64+
'headers' => $response->getHeaders(),
65+
'body' => $response->getBody(),
4966
];
5067
}
5168
}

0 commit comments

Comments
 (0)