Skip to content

Commit 9c54984

Browse files
authored
Merge pull request #3 from maplephp/develop
Develop
2 parents 837950d + b05159a commit 9c54984

13 files changed

Lines changed: 130 additions & 57 deletions

File tree

src/AbstractKernel.php

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MaplePHP\Core;
66

7+
use MaplePHP\Container\Autowire;
78
use MaplePHP\Core\Configs\LoadConfigFiles;
89
use MaplePHP\Core\Support\ServiceProvider;
910
use MaplePHP\DTO\Format\Clock;
@@ -42,14 +43,16 @@ public function __construct(string $dir)
4243
->loadFiles($this->dir . "/configs/");
4344

4445
$this->config = $config->fetch();
45-
4646
$app = App::boot(new Dir($this->dir), $this->config);
4747
$this->container = new Container();
48-
$this->container->set("config", $this->config);
4948
$this->container->set("app", $app);
5049

51-
Clock::setDefaultLocale($this->config['configs']['locale']);
52-
Clock::setDefaultTimezone($this->config['configs']['timezone']);
50+
if(App::get()->getApp('locale') !== null) {
51+
Clock::setDefaultLocale(App::get()->getApp('locale'));
52+
}
53+
if(App::get()->getApp('timezone') !== null) {
54+
Clock::setDefaultTimezone(App::get()->getApp('timezone'));
55+
}
5356
}
5457

5558
/**
@@ -62,39 +65,63 @@ public function __construct(string $dir)
6265
*/
6366
protected function load(ServerRequestInterface $request, ?DispatchConfigInterface $config = null): KernelInterface
6467
{
65-
$this->bootServiceProviders();
68+
69+
if (isset($this->config['providers']) && is_array($this->config['providers'])) {
70+
$this->bootServiceProviders($this->config['providers']);
71+
}
72+
73+
if (isset($this->config['services']['providers']) && is_array($this->config['services']['providers'])) {
74+
$this->bootServiceProviders($this->config['services']['providers']);
75+
}
76+
77+
if (isset($this->config['services']['bindings']) && is_array($this->config['services']['bindings'])) {
78+
$this->bootBindings($this->config['services']['bindings']);
79+
}
80+
6681
return new Kernel($this->container, $this->middlewares, $config);
6782
}
6883

84+
85+
public function bootBindings(array $bindings): void
86+
{
87+
Autowire::interfaceWiring($bindings);
88+
}
89+
6990
/**
7091
* Boot service providers
7192
*
7293
* @return void
7394
*/
74-
protected function bootServiceProviders()
95+
protected function bootServiceProviders(array $providers)
7596
{
76-
if (isset($this->config['providers'])) {
77-
$providers = [];
78-
79-
// We want to register first, that way the providers could talk to eachother
80-
// through the container or event listners if you want.
81-
foreach ($this->config['providers'] as $providerClass) {
82-
$provider = new $providerClass();
83-
if (!($provider instanceof ServiceProvider)) {
84-
throw new \RuntimeException(
85-
"$providerClass is not an instance of " . ServiceProvider::class . "!"
86-
);
87-
}
88-
$provider->register($this->container);
89-
$providers[] = $provider;
97+
if ($providers !== []) {
98+
$set = [];
99+
100+
// We want to register first, that way the providers could talk to each other
101+
// through the container or event listeners if you want.
102+
foreach ($providers as $providerClass) {
103+
$this->registerProvider($providerClass, $set);
90104
}
91105

92-
foreach ($providers as $provider) {
106+
foreach ($set as $provider) {
93107
$provider->boot();
94108
}
95109
}
96110
}
97111

112+
113+
private function registerProvider(string $providerClass, array &$providers): void
114+
{
115+
$provider = new $providerClass();
116+
if (!($provider instanceof ServiceProvider)) {
117+
throw new \RuntimeException(
118+
"$providerClass is not an instance of " . ServiceProvider::class . "!"
119+
);
120+
}
121+
$provider->register($this->container);
122+
$providers[] = $provider;
123+
}
124+
98125
/**
99126
* @param Stream $stream
100127
* @return $this

src/App.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ final class App implements AppInterface
1414
private Dir $dir;
1515
private string $coreDir;
1616
private array $config;
17+
private array $app;
1718

1819
private function __construct(Dir $dir, array $config = []) {
1920
$this->dir = $dir;
2021
$this->coreDir = __DIR__;
2122
$this->config = $config;
23+
// Renamed configs to app
24+
$this->app = $this->config['app'] ?? ($this->config['configs'] ?? []);
2225
}
2326

2427
/**
@@ -58,7 +61,7 @@ public static function get(): self
5861
*/
5962
public function isProd(): bool
6063
{
61-
return ($this->config['configs']['env'] ?? "development") === Environment::PROD->name();
64+
return ($this->app['env'] ?? "development") === Environment::PROD->name();
6265
}
6366

6467
/**
@@ -68,7 +71,7 @@ public function isProd(): bool
6871
*/
6972
public function isStage(): bool
7073
{
71-
return ($this->config['configs']['env'] ?? "development") === Environment::STAGE->name();
74+
return ($this->app['env'] ?? "development") === Environment::STAGE->name();
7275
}
7376

7477
/**
@@ -78,7 +81,7 @@ public function isStage(): bool
7881
*/
7982
public function isTest(): bool
8083
{
81-
return ($this->config['configs']['env'] ?? "development") === Environment::TEST->name();
84+
return ($this->app['env'] ?? "development") === Environment::TEST->name();
8285
}
8386

8487
/**
@@ -88,7 +91,7 @@ public function isTest(): bool
8891
*/
8992
public function isDev(): bool
9093
{
91-
return ($this->config['configs']['env'] ?? "development") === Environment::DEV->name();
94+
return ($this->app['env'] ?? "development") === Environment::DEV->name();
9295
}
9396

9497
/**
@@ -98,7 +101,7 @@ public function isDev(): bool
98101
*/
99102
public function env(): string
100103
{
101-
return ($this->config['configs']['env'] ?? "development") ?? Environment::PROD->name();
104+
return ($this->app['env'] ?? "development") ?? Environment::PROD->name();
102105
}
103106

104107
/**
@@ -130,4 +133,25 @@ public function configs(): array
130133
{
131134
return $this->config;
132135
}
136+
137+
/**
138+
* Get the app core configs
139+
*
140+
* @return array
141+
*/
142+
public function app(): array
143+
{
144+
return $this->app;
145+
}
146+
147+
/**
148+
* Get the app core configs prop
149+
*
150+
* @param string $key
151+
* @return mixed
152+
*/
153+
public function getApp(string $key): mixed
154+
{
155+
return $this->app[$key] ?? null;
156+
}
133157
}

src/CliKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(string $dir)
4343
{
4444
parent::__construct($dir);
4545
// Default config
46-
Kernel::setRouterFilePath($dir . "/routers/console.php");
46+
Kernel::setRouterFilePath(App::get()->dir()->routes() . "/console.php");
4747
$this->stream = new Stream(Stream::STDERR);
4848
}
4949

src/HttpKernel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ final class HttpKernel extends AbstractKernel implements KernelLoadInterface
2323
public function __construct(string $dir)
2424
{
2525
parent::__construct($dir);
26-
Kernel::setRouterFilePath($dir . "/routers/web.php");
26+
Kernel::setRouterFilePath(App::get()->dir()->routes() . "/web.php");
2727
$this->stream = new Stream(Stream::TEMP);
2828
}
2929

3030
/**
31-
* Initialize the HTTP kernel with default framework configuration.
31+
* Initialize the HTTP kernel with the default framework configuration.
3232
*
3333
* This method loads HTTP-related configuration from `/configs/http.php`
3434
* and registers globally configured middleware. It also attaches the

src/Providers/DatabaseProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MaplePHP\Core\Providers;
66

7+
use MaplePHP\Core\App;
78
use Psr\Container\ContainerInterface;
89
use Doctrine\DBAL\DriverManager;
910
use MaplePHP\Core\Support\ServiceProvider;
@@ -20,7 +21,7 @@ class DatabaseProvider extends ServiceProvider
2021
public function register(ContainerInterface $container): void
2122
{
2223
$this->container = $container;
23-
$config = $container->get('config');
24+
$config = App::get()->configs();
2425
$default = ($config['database']['default'] ?? null);
2526
if (!empty($default)) {
2627
$dbConfig = $this->resolveConnection($config);

src/Render/Errors/TestPage.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MaplePHP\Core\Render\Errors;
6+
7+
use MaplePHP\Core\App;
8+
use Psr\Container\ContainerInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\ServerRequestInterface;
11+
use MaplePHP\Core\Interfaces\ErrorPageInterface;
12+
13+
class TestPage implements ErrorPageInterface
14+
{
15+
16+
public function render(
17+
ResponseInterface $response,
18+
ServerRequestInterface $request,
19+
array $context = []
20+
): string {
21+
return "Test Page ";
22+
}
23+
24+
public function test(): string {
25+
return "Test Page 2";
26+
}
27+
}

src/Render/Errors/TwigErrorPage.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ public function render(
2626
'cache' => false,
2727
]);
2828

29-
$configs = App::get()->configs();
30-
$configs = isset($configs['configs']) ? $configs['configs'] : ['app_title' => "MaplePHP"];
31-
32-
$twig->addGlobal('app', $configs);
29+
$twig->addGlobal('app', App::get()->app());
3330

3431
return $twig->render('error.twig', [
3532
'code' => $response->getStatusCode(),

src/Render/StaticRenderer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ function welcome(): string
2626
private function renderFile(string $fileName, array $data = [])
2727
{
2828
$response = $this->response;
29-
$configs = App::get()->configs();
30-
$configs = isset($configs['configs']) ? $configs['configs'] : ['app_title' => "MaplePHP"];
29+
$app = App::get()->app();
3130

3231
extract($data);
3332

src/Render/Templates/welcome.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<title>Welcome MaplePHP</title>
6+
<title>Welcome to MaplePHP</title>
77
<style>
88
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
99

@@ -301,9 +301,9 @@
301301
</div>
302302
<pre><span class="kw">class</span> <span class="fn">HelloController</span> <span class="kw">extends</span> <span class="fn">DefaultController</span>
303303
{
304-
<span class="kw">public function</span> <span class="fn">show</span>(<span class="fn">Twig</span> <span class="va">$twig</span>, <span class="fn">PathInterface</span> <span class="va">$path</span>): <span class="fn">ResponseInterface</span>
304+
<span class="kw">public function</span> <span class="fn">show</span>(<span class="fn">Twig</span> <span class="va">$twig</span>, <span class="fn">PathInterface</span> <span class="va">$path</span>): <span class="fn">void</span>
305305
{
306-
<span class="kw">return</span> <span class="va">$twig</span>-><span class="fn">render</span>(<span class="st">'views/hello.twig'</span>, [
306+
<span class="va">$twig</span>-><span class="fn">render</span>(<span class="st">'views/hello.twig'</span>, [
307307
<span class="st">'title'</span> => <span class="st">'Hello'</span>,
308308
<span class="st">'name'</span> => <span class="va">$path</span>-><span class="fn">select</span>(<span class="st">'name'</span>)-><span class="fn">last</span>(),
309309
]);
@@ -312,7 +312,7 @@
312312
</div>
313313

314314
<div class="footer">
315-
<p>MaplePHP &mdash; <a href="https://github.com/maplephp/maplephp" target="_blank" rel="noopener">github.com/maplephp</a> &mdash; PHP 8.2+ &mdash; Apache 2.0</p>
315+
<p>MaplePHP &mdash; <a href="https://github.com/maplephp/maplephp" target="_blank" rel="noopener">Github</a> &mdash; PHP 8.2+ </p>
316316
</div>
317317

318318
</div>

src/Routing/DefaultCommand.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ abstract class DefaultCommand
2121
protected readonly ServerRequestInterface|RequestInterface $request;
2222
protected readonly ContainerInterface $container;
2323
protected Command $command;
24-
protected array $config;
2524
protected array $args;
2625
protected ?ConfigPropsInterface $props = null;
2726
protected string|bool $path;
@@ -40,7 +39,6 @@ public function __construct(ContainerInterface $container)
4039
$this->props = $this->container->get("props");
4140
$this->command = $this->container->get("command");
4241
$this->request = $this->container->get("request");
43-
$this->config = $this->container->get("config");
4442
}
4543

4644
/**

0 commit comments

Comments
 (0)