Skip to content

Commit 21ec0c6

Browse files
committed
Updated codebase to new dependencies and php 7.4
- Removed routing dependency - Added missing phpDoc comments - Added property and return typehints - Added strict dypes directive - Minor refactorings
2 parents 4786522 + 20b047a commit 21ec0c6

17 files changed

Lines changed: 270 additions & 244 deletions

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"require": {
88
"php": "^7.4",
99
"polymorphine/container": "^1.0",
10-
"polymorphine/routing": "0.1.*",
1110
"psr/http-message": "^1.0",
1211
"psr/http-server-handler": "^1.0",
1312
"psr/http-server-middleware": "^1.0"

cs-fixer.php.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of Polymorphine/App package.

src/AppHandler.php

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of Polymorphine/App package.
@@ -11,41 +11,40 @@
1111

1212
namespace Polymorphine\App;
1313

14-
use Polymorphine\Routing\Router;
15-
use Polymorphine\Container\ContainerSetup;
16-
use Polymorphine\Container\RecordSetup;
17-
use Polymorphine\Container\Exception;
14+
use Polymorphine\Container\Setup;
15+
use Polymorphine\Container\Setup\Build;
16+
use Polymorphine\Container\Setup\Entry;
17+
use Psr\Container\ContainerInterface;
1818
use Psr\Http\Server\MiddlewareInterface;
1919
use Psr\Http\Server\RequestHandlerInterface;
2020
use Psr\Http\Message\ServerRequestInterface;
2121
use Psr\Http\Message\ResponseInterface;
22-
use Psr\Container\ContainerInterface;
2322

2423

2524
abstract class AppHandler implements RequestHandlerInterface
2625
{
2726
public const ROUTER_ID = 'app.router';
2827
public const DEV_ENVIRONMENT = 'APP_DEV';
2928

30-
private $setup;
31-
private $container;
32-
private $middleware = [];
33-
private $processQueue = [];
29+
private Setup $setup;
30+
private ContainerInterface $container;
31+
32+
private array $middleware = [];
33+
private array $processQueue = [];
3434

3535
/**
36-
* @param ContainerSetup $setup
36+
* @param Build|null $build
3737
*/
38-
public function __construct(ContainerSetup $setup = null)
38+
public function __construct(Build $build = null)
3939
{
40-
$this->registerShutdown(getenv(static::DEV_ENVIRONMENT));
41-
$this->setup = $setup ?? new ContainerSetup();
42-
$this->container = $this->setup->container();
43-
$this->environmentSetup();
40+
$this->registerShutdown((bool) getenv(static::DEV_ENVIRONMENT));
41+
$this->setup = $this->environmentSetup($build ?? new Build());
4442
}
4543

4644
final public function handle(ServerRequestInterface $request): ResponseInterface
4745
{
48-
while ($middlewareId = array_shift($this->processQueue)) {
46+
$this->container ??= $this->setup->container();
47+
if ($middlewareId = array_shift($this->processQueue)) {
4948
return $this->process($this->container->get($middlewareId), $request);
5049
}
5150

@@ -54,31 +53,44 @@ final public function handle(ServerRequestInterface $request): ResponseInterface
5453
return $this->container->get(static::ROUTER_ID)->handle($request);
5554
}
5655

57-
final public function config(string $id): RecordSetup
56+
/**
57+
* @param string $id
58+
*
59+
* @return Setup\Entry
60+
*/
61+
final public function config(string $id): Entry
5862
{
59-
return $this->setup->entry($id);
63+
return $this->setup->set($id);
6064
}
6165

62-
final public function middleware(string $id): RecordSetup
66+
/**
67+
* @param string $id
68+
*
69+
* @return Entry
70+
*/
71+
final public function middleware(string $id): Entry
6372
{
6473
$this->middleware[] = $id;
6574
$this->processQueue[] = $id;
66-
return $this->setup->entry($id);
75+
return $this->setup->set($id);
6776
}
6877

69-
abstract protected function routing(ContainerInterface $c): Router;
78+
abstract protected function routing(ContainerInterface $c): RequestHandlerInterface;
7079

71-
protected function environmentSetup()
80+
protected function environmentSetup(Build $build): Setup
7281
{
73-
if ($this->setup->exists(static::ROUTER_ID)) {
82+
if ($build->has(static::ROUTER_ID)) {
7483
$message = 'Reserved router key `%s` used as container entry (rename entry or %s ROUTER_ID constant)';
7584
$override = static::ROUTER_ID === self::ROUTER_ID ? 'override' : 'change';
76-
throw new Exception\InvalidIdException(sprintf($message, static::ROUTER_ID, $override));
85+
throw new Setup\Exception\OverwriteRuleException(sprintf($message, static::ROUTER_ID, $override));
7786
}
7887

79-
$this->setup->entry(static::ROUTER_ID)->invoke(function () {
88+
$setup = new Setup($build);
89+
$setup->set(self::ROUTER_ID)->callback(function () {
8090
return $this->routing($this->container);
8191
});
92+
93+
return $setup;
8294
}
8395

8496
protected function registerShutdown(bool $devEnv)
@@ -94,7 +106,7 @@ protected function registerShutdown(bool $devEnv)
94106
});
95107
}
96108

97-
private function process(MiddlewareInterface $middleware, ServerRequestInterface $request)
109+
private function process(MiddlewareInterface $middleware, ServerRequestInterface $request): ResponseInterface
98110
{
99111
return $middleware->process($request, $this);
100112
}

src/ServerProcess.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of Polymorphine/App package.
@@ -20,25 +20,30 @@
2020

2121
final class ServerProcess
2222
{
23-
private $app;
24-
private $buffer;
23+
private RequestHandlerInterface $requestHandler;
24+
private int $outputBufferSize;
2525

2626
/**
2727
* When large handler's responses are not expected
2828
* buffer size parameter might be omitted.
2929
*
30-
* @param RequestHandlerInterface $app
30+
* @param RequestHandlerInterface $requestHandler
3131
* @param int $outputBufferSize (bytes)
3232
*/
33-
public function __construct(RequestHandlerInterface $app, int $outputBufferSize = 0)
33+
public function __construct(RequestHandlerInterface $requestHandler, int $outputBufferSize = 0)
3434
{
35-
$this->app = $app;
36-
$this->buffer = $outputBufferSize;
35+
$this->requestHandler = $requestHandler;
36+
$this->outputBufferSize = $outputBufferSize;
3737
}
3838

39+
/**
40+
* Emits response for given request.
41+
*
42+
* @param ServerRequestInterface $request
43+
*/
3944
public function execute(ServerRequestInterface $request): void
4045
{
41-
$this->emitResponse($this->app->handle($request));
46+
$this->emitResponse($this->requestHandler->handle($request));
4247
}
4348

4449
private function emitResponse(ResponseInterface $response)
@@ -87,12 +92,12 @@ private function emitBody(ResponseInterface $response)
8792
if ($body->isSeekable()) { $body->rewind(); }
8893

8994
while (!$body->eof()) {
90-
echo $body->read($this->buffer);
95+
echo $body->read($this->outputBufferSize);
9196
}
9297
}
9398

94-
private function chunksRequired(StreamInterface $body)
99+
private function chunksRequired(StreamInterface $body): bool
95100
{
96-
return $this->buffer && $body->isReadable() && $body->getSize() > $this->buffer;
101+
return $this->outputBufferSize && $body->isReadable() && $body->getSize() > $this->outputBufferSize;
97102
}
98103
}

tests/AppHandlerIntegrationTest.php

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of Polymorphine/App package.
@@ -13,15 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Polymorphine\App\AppHandler;
16-
use Polymorphine\App\Tests\Doubles\FakeMiddleware;
17-
use Polymorphine\App\Tests\Doubles\FakeUri;
18-
use Polymorphine\App\Tests\Fixtures\HeadersState;
19-
use Polymorphine\App\Tests\Fixtures\ShutdownState;
2016
use Polymorphine\Container;
21-
use Polymorphine\Container\Exception;
22-
use Psr\Http\Server\RequestHandlerInterface;
23-
use Psr\Http\Message\ResponseInterface;
24-
use Psr\Container\ContainerInterface;
2517

2618
require_once __DIR__ . '/Fixtures/shutdown-functions.php';
2719
require_once __DIR__ . '/Fixtures/header-functions.php';
@@ -32,26 +24,25 @@ class AppHandlerIntegrationTest extends TestCase
3224
public function testInstantiation()
3325
{
3426
$this->assertInstanceOf(AppHandler::class, $this->app());
35-
$this->assertInstanceOf(RequestHandlerInterface::class, $this->app());
3627
}
3728

38-
public function testConfig_ReturnsRegistryInput()
29+
public function testConfig_ReturnsSetupEntry()
3930
{
4031
$app = $this->app();
41-
$this->assertInstanceOf(Container\RecordSetup::class, $app->config('test'));
32+
$this->assertInstanceOf(Container\Setup\Entry::class, $app->config('test'));
4233
}
4334

4435
public function testRoutingContainerIntegration()
4536
{
46-
$app = $this->app(['test' => new Container\Record\ValueRecord('Hello World!')]);
37+
$app = $this->app(['test' => new Container\Records\Record\ValueRecord('Hello World!')]);
4738
$response = $app->handle(new Doubles\FakeServerRequest());
48-
$this->assertSame('//example.com/foo/bar: Hello World!', $response->body);
39+
$this->assertSame('//example.com/foo/bar: Hello World!', (string) $response->getBody());
4940
}
5041

5142
public function testRepeatedHandleCallsWithMiddlewareProcessing_ReturnsEqualResponse()
5243
{
5344
$app = $this->middlewareContextsApp();
54-
$request = new Doubles\FakeServerRequest('GET', FakeUri::fromString('/test'));
45+
$request = new Doubles\FakeServerRequest('GET', Doubles\FakeUri::fromString('/test'));
5546
$response = $app->handle($request);
5647

5748
$expectedBody = 'outerContext innerContext /test: MAIN innerContext outerContext';
@@ -61,8 +52,8 @@ public function testRepeatedHandleCallsWithMiddlewareProcessing_ReturnsEqualResp
6152

6253
public function testInstanceWithDefinedInternalContainerId_ThrowsException()
6354
{
64-
$this->expectException(Exception\InvalidIdException::class);
65-
$this->app([AppHandler::ROUTER_ID => new Container\Record\ValueRecord('Hello World!')]);
55+
$this->expectException(Container\Setup\Exception\OverwriteRuleException::class);
56+
$this->app([AppHandler::ROUTER_ID => new Container\Records\Record\ValueRecord('Hello World!')]);
6657
}
6758

6859
public function testFallbackNotFoundRoute()
@@ -72,47 +63,46 @@ public function testFallbackNotFoundRoute()
7263
$app->notFoundResponse = new Doubles\FakeResponse();
7364

7465
$response = $app->handle(new Doubles\FakeServerRequest());
75-
$this->assertInstanceOf(ResponseInterface::class, $response);
7666
$this->assertInstanceOf(Doubles\FakeResponse::class, $response);
7767
$this->assertSame($app->notFoundResponse, $response);
7868
}
7969

8070
public function testShutdownRegisteredOnProduction()
8171
{
82-
ShutdownState::reset();
83-
ShutdownState::$override = true;
72+
Fixtures\ShutdownState::reset();
73+
Fixtures\ShutdownState::$override = true;
8474
$this->assertFalse(getenv(AppHandler::DEV_ENVIRONMENT));
8575
$this->app();
86-
$this->assertTrue(is_callable($callback = ShutdownState::$callback));
76+
$this->assertTrue(is_callable($callback = Fixtures\ShutdownState::$callback));
8777
$callback();
88-
$this->assertSame(503, ShutdownState::$status);
89-
$this->assertSame([], HeadersState::$headers);
90-
$this->assertTrue(ShutdownState::$outputBufferCleared);
78+
$this->assertSame(503, Fixtures\ShutdownState::$status);
79+
$this->assertSame([], Fixtures\HeadersState::$headers);
80+
$this->assertTrue(Fixtures\ShutdownState::$outputBufferCleared);
9181
}
9282

9383
public function testShutdownNotRegisteredOnDevelopment()
9484
{
95-
ShutdownState::reset();
96-
ShutdownState::$override = true;
85+
Fixtures\ShutdownState::reset();
86+
Fixtures\ShutdownState::$override = true;
9787
putenv(AppHandler::DEV_ENVIRONMENT . '=1');
9888
$this->assertNotFalse(getenv(AppHandler::DEV_ENVIRONMENT));
9989
$this->app();
100-
$this->assertFalse(is_callable(ShutdownState::$callback));
90+
$this->assertFalse(is_callable(Fixtures\ShutdownState::$callback));
10191
}
10292

103-
private function app(array $records = [], bool $secure = false)
93+
private function app(array $records = [], bool $secure = false): Doubles\MockedAppHandler
10494
{
105-
$setup = $secure ? new Container\TrackingContainerSetup($records) : new Container\ContainerSetup($records);
95+
$setup = $secure ? new Container\Setup\Build\ValidatedBuild($records) : new Container\Setup\Build($records);
10696
return new Doubles\MockedAppHandler($setup);
10797
}
10898

109-
private function middlewareContextsApp()
99+
private function middlewareContextsApp(): Doubles\MockedAppHandler
110100
{
111101
$app = $this->app();
112-
$app->config('test')->set('MAIN');
113-
$app->middleware('one')->set(new FakeMiddleware('outerContext'));
114-
$app->middleware('two')->invoke(function (ContainerInterface $c) {
115-
return new FakeMiddleware($c->get('one')->inContext ? 'innerContext' : '--- error ---');
102+
$app->config('test')->value('MAIN');
103+
$app->middleware('one')->value(new Doubles\FakeMiddleware('outerContext'));
104+
$app->middleware('two')->callback(function ($c) {
105+
return new Doubles\FakeMiddleware($c->get('one')->inContext ? 'innerContext' : '--- error ---');
116106
});
117107

118108
return $app;

tests/Doubles/FakeMiddleware.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of Polymorphine/App package.
@@ -19,8 +19,8 @@
1919

2020
class FakeMiddleware implements MiddlewareInterface
2121
{
22-
public $bodyWrap;
23-
public $inContext = false;
22+
public string $bodyWrap;
23+
public bool $inContext = false;
2424

2525
public function __construct(string $bodyWrap = 'processed')
2626
{

tests/Doubles/FakeRequestHandler.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of Polymorphine/App package.
@@ -18,18 +18,18 @@
1818

1919
class FakeRequestHandler implements RequestHandlerInterface
2020
{
21-
private $response;
22-
private $sideEffect;
21+
private $handleRequest;
2322

24-
public function __construct(ResponseInterface $response, callable $sideEffect = null)
23+
/**
24+
* @param callable $handleRequest fn(ServerRequestInterface) => ResponseInterface
25+
*/
26+
public function __construct(callable $handleRequest)
2527
{
26-
$this->response = $response;
27-
$this->sideEffect = $sideEffect;
28+
$this->handleRequest = $handleRequest;
2829
}
2930

3031
public function handle(ServerRequestInterface $request): ResponseInterface
3132
{
32-
if ($this->sideEffect) { ($this->sideEffect)(); }
33-
return $this->response;
33+
return ($this->handleRequest)($request);
3434
}
3535
}

0 commit comments

Comments
 (0)