Skip to content

Commit 98dc3e7

Browse files
committed
fix config inheritance, refactor and add env
1 parent a624e24 commit 98dc3e7

16 files changed

Lines changed: 149 additions & 15 deletions

src/AbstractKernel.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
namespace MaplePHP\Emitron;
1616

1717
use MaplePHP\Container\Reflection;
18+
use MaplePHP\Emitron\Contracts\AppInterface;
1819
use MaplePHP\Emitron\Contracts\DispatchConfigInterface;
1920
use MaplePHP\Emitron\Contracts\EmitterInterface;
2021
use MaplePHP\Emitron\Contracts\KernelInterface;
2122
use MaplePHP\Emitron\Emitters\CliEmitter;
2223
use MaplePHP\Emitron\Emitters\HttpEmitter;
24+
use MaplePHP\Http\Interfaces\PathInterface;
2325
use MaplePHP\Http\ResponseFactory;
2426
use MaplePHP\Http\Stream;
2527
use Psr\Container\ContainerInterface;
@@ -127,6 +129,7 @@ public function getDispatchConfig(): DispatchConfigInterface
127129
protected function initRequestHandler(
128130
ServerRequestInterface $request,
129131
StreamInterface $stream,
132+
PathInterface $path,
130133
RequestHandlerInterface $finalHandler,
131134
array $middlewares = []
132135
): ResponseInterface {
@@ -136,11 +139,22 @@ protected function initRequestHandler(
136139
"RequestInterface" => $request,
137140
"ServerRequestInterface" => $request,
138141
"StreamInterface" => $stream,
142+
"PathInterface" => $path
139143
]);
140144

141145
$middlewares = array_merge($this->userMiddlewares, $middlewares);
142146
$handler = new RequestHandler($middlewares, $finalHandler);
143-
$response = $handler->handle($request);
147+
$app = $this->container->has("app") ? $this->container->get("app") : null;
148+
149+
ob_start();
150+
$response = $handler->handle($request);
151+
$output = ob_get_clean();
152+
153+
if((string)$output !== "" && ($app instanceof AppInterface && !$app->isProd())) {
154+
throw new \RuntimeException(
155+
'Unexpected output detected during request dispatch. Controllers must write to the response body instead of using echo.'
156+
);
157+
}
144158

145159
return $response;
146160
}

src/Configs/ConfigPropsFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MaplePHP\Emitron\Configs;
46

57
use MaplePHP\Emitron\AbstractConfigProps;
@@ -13,19 +15,17 @@ class ConfigPropsFactory
1315
* @param array $props
1416
* @return ConfigPropsInterface
1517
*/
16-
public static function create(array $props): ConfigPropsInterface
18+
public static function create(array $props, ?string $configProps = null): ConfigPropsInterface
1719
{
18-
$override = '\\Configs\\ConfigProps';
20+
$override = ($configProps !== null) ? $configProps : '\\Configs\\ConfigProps';
1921
$default = \MaplePHP\Unitary\Config\ConfigProps::class;
20-
$name = class_exists($override) ? $override : $default;
22+
$name = (class_exists($override)) ? $override : $default;
2123
if (!is_subclass_of($name, ConfigPropsInterface::class)) {
2224
$name = $default;
2325
}
24-
2526
if (!class_exists($name)) {
2627
return self::resolver($props);
2728
}
28-
2929
return new $name($props);
3030
}
3131

src/Contracts/AppInterface.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MaplePHP\Emitron\Contracts;
6+
7+
use MaplePHP\Core\Support\Dir;
8+
9+
interface AppInterface
10+
{
11+
12+
/**
13+
* This is a single to set App globals
14+
*
15+
* @param Dir $dir
16+
* @param array $config
17+
* @return self
18+
*/
19+
public static function boot(Dir $dir, array $config = []): self;
20+
21+
/**
22+
* Get App singleton instance
23+
*
24+
* @return self
25+
*/
26+
public static function get(): self;
27+
28+
/**
29+
* Check if the environment is in prod
30+
*
31+
* @return bool
32+
*/
33+
public function isProd(): bool;
34+
35+
/**
36+
* Check if the environment is in stage
37+
*
38+
* @return bool
39+
*/
40+
public function isStage(): bool;
41+
42+
/**
43+
* Check if the environment is in test
44+
*
45+
* @return bool
46+
*/
47+
public function isTest(): bool;
48+
49+
/**
50+
* Check if the environment is in dev
51+
*
52+
* @return bool
53+
*/
54+
public function isDev(): bool;
55+
56+
/**
57+
* Get current Environment
58+
*
59+
* @return string
60+
*/
61+
public function env(): string;
62+
63+
/**
64+
* Get core/boot dir where code app boot originate
65+
*
66+
* @return string
67+
*/
68+
public function coreDir(): string;
69+
70+
/**
71+
* Get the app core Dir instance
72+
*
73+
* @return Dir
74+
*/
75+
public function dir(): Dir;
76+
77+
}

src/Contracts/ConfigPropsInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MaplePHP\Emitron\Contracts;
46

57
interface ConfigPropsInterface

src/Contracts/ControllerResponseInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MaplePHP\Emitron\Contracts;
46

57
use Psr\Http\Message\ResponseInterface;

src/Contracts/DispatchConfigInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MaplePHP\Emitron\Contracts;
46

57
use Exception;

src/Contracts/KernelInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MaplePHP\Emitron\Contracts;
46

57
use Psr\Http\Message\ServerRequestInterface;

src/Contracts/MiddlewareInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
36
namespace MaplePHP\Emitron\Contracts;
47

58
use Psr\Http\Message\ResponseInterface;

src/Contracts/RequestHandlerInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MaplePHP\Emitron\Contracts;
46

57
use Psr\Http\Message\ResponseInterface;

src/Contracts/RouterDispatchInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MaplePHP\Emitron\Contracts;
46

57
interface RouterDispatchInterface

0 commit comments

Comments
 (0)