Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
69 changes: 48 additions & 21 deletions src/AbstractKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MaplePHP\Core;

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

$this->config = $config->fetch();

$app = App::boot(new Dir($this->dir), $this->config);
$this->container = new Container();
$this->container->set("config", $this->config);
$this->container->set("app", $app);

Clock::setDefaultLocale($this->config['configs']['locale']);
Clock::setDefaultTimezone($this->config['configs']['timezone']);
if(App::get()->getApp('locale') !== null) {
Clock::setDefaultLocale(App::get()->getApp('locale'));
}
if(App::get()->getApp('timezone') !== null) {
Clock::setDefaultTimezone(App::get()->getApp('timezone'));
}
}

/**
Expand All @@ -62,39 +65,63 @@ public function __construct(string $dir)
*/
protected function load(ServerRequestInterface $request, ?DispatchConfigInterface $config = null): KernelInterface
{
$this->bootServiceProviders();

if (isset($this->config['providers']) && is_array($this->config['providers'])) {
$this->bootServiceProviders($this->config['providers']);
}

if (isset($this->config['services']['providers']) && is_array($this->config['services']['providers'])) {
$this->bootServiceProviders($this->config['services']['providers']);
}

if (isset($this->config['services']['bindings']) && is_array($this->config['services']['bindings'])) {
$this->bootBindings($this->config['services']['bindings']);
}

return new Kernel($this->container, $this->middlewares, $config);
}


public function bootBindings(array $bindings): void
{
Autowire::interfaceWiring($bindings);
}

/**
* Boot service providers
*
* @return void
*/
protected function bootServiceProviders()
protected function bootServiceProviders(array $providers)
{
if (isset($this->config['providers'])) {
$providers = [];

// We want to register first, that way the providers could talk to eachother
// through the container or event listners if you want.
foreach ($this->config['providers'] as $providerClass) {
$provider = new $providerClass();
if (!($provider instanceof ServiceProvider)) {
throw new \RuntimeException(
"$providerClass is not an instance of " . ServiceProvider::class . "!"
);
}
$provider->register($this->container);
$providers[] = $provider;
if ($providers !== []) {
$set = [];

// We want to register first, that way the providers could talk to each other
// through the container or event listeners if you want.
foreach ($providers as $providerClass) {
$this->registerProvider($providerClass, $set);
}

foreach ($providers as $provider) {
foreach ($set as $provider) {
$provider->boot();
}
}
}


private function registerProvider(string $providerClass, array &$providers): void
{
$provider = new $providerClass();
if (!($provider instanceof ServiceProvider)) {
throw new \RuntimeException(
"$providerClass is not an instance of " . ServiceProvider::class . "!"
);
}
$provider->register($this->container);
$providers[] = $provider;
}

/**
* @param Stream $stream
* @return $this
Expand Down
34 changes: 29 additions & 5 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ final class App implements AppInterface
private Dir $dir;
private string $coreDir;
private array $config;
private array $app;

private function __construct(Dir $dir, array $config = []) {
$this->dir = $dir;
$this->coreDir = __DIR__;
$this->config = $config;
// Renamed configs to app
$this->app = $this->config['app'] ?? ($this->config['configs'] ?? []);
}

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

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

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

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

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

/**
Expand Down Expand Up @@ -130,4 +133,25 @@ public function configs(): array
{
return $this->config;
}

/**
* Get the app core configs
*
* @return array
*/
public function app(): array
{
return $this->app;
}

/**
* Get the app core configs prop
*
* @param string $key
* @return mixed
*/
public function getApp(string $key): mixed
{
return $this->app[$key] ?? null;
}
}
2 changes: 1 addition & 1 deletion src/CliKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(string $dir)
{
parent::__construct($dir);
// Default config
Kernel::setRouterFilePath($dir . "/routers/console.php");
Kernel::setRouterFilePath(App::get()->dir()->routes() . "/console.php");
$this->stream = new Stream(Stream::STDERR);
}

Expand Down
4 changes: 2 additions & 2 deletions src/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ final class HttpKernel extends AbstractKernel implements KernelLoadInterface
public function __construct(string $dir)
{
parent::__construct($dir);
Kernel::setRouterFilePath($dir . "/routers/web.php");
Kernel::setRouterFilePath(App::get()->dir()->routes() . "/web.php");
$this->stream = new Stream(Stream::TEMP);
}

/**
* Initialize the HTTP kernel with default framework configuration.
* Initialize the HTTP kernel with the default framework configuration.
*
* This method loads HTTP-related configuration from `/configs/http.php`
* and registers globally configured middleware. It also attaches the
Expand Down
3 changes: 2 additions & 1 deletion src/Providers/DatabaseProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MaplePHP\Core\Providers;

use MaplePHP\Core\App;
use Psr\Container\ContainerInterface;
use Doctrine\DBAL\DriverManager;
use MaplePHP\Core\Support\ServiceProvider;
Expand All @@ -20,7 +21,7 @@ class DatabaseProvider extends ServiceProvider
public function register(ContainerInterface $container): void
{
$this->container = $container;
$config = $container->get('config');
$config = App::get()->configs();
$default = ($config['database']['default'] ?? null);
if (!empty($default)) {
$dbConfig = $this->resolveConnection($config);
Expand Down
27 changes: 27 additions & 0 deletions src/Render/Errors/TestPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace MaplePHP\Core\Render\Errors;

use MaplePHP\Core\App;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use MaplePHP\Core\Interfaces\ErrorPageInterface;

class TestPage implements ErrorPageInterface
{

public function render(
ResponseInterface $response,
ServerRequestInterface $request,
array $context = []
): string {
return "Test Page ";
}

public function test(): string {
return "Test Page 2";
}
}
5 changes: 1 addition & 4 deletions src/Render/Errors/TwigErrorPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ public function render(
'cache' => false,
]);

$configs = App::get()->configs();
$configs = isset($configs['configs']) ? $configs['configs'] : ['app_title' => "MaplePHP"];

$twig->addGlobal('app', $configs);
$twig->addGlobal('app', App::get()->app());

return $twig->render('error.twig', [
'code' => $response->getStatusCode(),
Expand Down
3 changes: 1 addition & 2 deletions src/Render/StaticRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ function welcome(): string
private function renderFile(string $fileName, array $data = [])
{
$response = $this->response;
$configs = App::get()->configs();
$configs = isset($configs['configs']) ? $configs['configs'] : ['app_title' => "MaplePHP"];
$app = App::get()->app();

extract($data);

Expand Down
8 changes: 4 additions & 4 deletions src/Render/Templates/welcome.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome MaplePHP</title>
<title>Welcome to MaplePHP</title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

Expand Down Expand Up @@ -301,9 +301,9 @@
</div>
<pre><span class="kw">class</span> <span class="fn">HelloController</span> <span class="kw">extends</span> <span class="fn">DefaultController</span>
{
<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>
<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>
{
<span class="kw">return</span> <span class="va">$twig</span>-><span class="fn">render</span>(<span class="st">'views/hello.twig'</span>, [
<span class="va">$twig</span>-><span class="fn">render</span>(<span class="st">'views/hello.twig'</span>, [
<span class="st">'title'</span> => <span class="st">'Hello'</span>,
<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>(),
]);
Expand All @@ -312,7 +312,7 @@
</div>

<div class="footer">
<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>
<p>MaplePHP &mdash; <a href="https://github.com/maplephp/maplephp" target="_blank" rel="noopener">Github</a> &mdash; PHP 8.2+ </p>
</div>

</div>
Expand Down
2 changes: 0 additions & 2 deletions src/Routing/DefaultCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ abstract class DefaultCommand
protected readonly ServerRequestInterface|RequestInterface $request;
protected readonly ContainerInterface $container;
protected Command $command;
protected array $config;
protected array $args;
protected ?ConfigPropsInterface $props = null;
protected string|bool $path;
Expand All @@ -40,7 +39,6 @@ public function __construct(ContainerInterface $container)
$this->props = $this->container->get("props");
$this->command = $this->container->get("command");
$this->request = $this->container->get("request");
$this->config = $this->container->get("config");
}

/**
Expand Down
16 changes: 7 additions & 9 deletions src/Routing/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,22 @@ abstract class DefaultController
protected readonly ServerRequestInterface|RequestInterface $request;
protected readonly ContainerInterface $container;
protected Command $command;
protected array $config;
protected array $args;
protected ?ConfigPropsInterface $props = null;
protected string|bool $path;

/**
* Set some data type safe object that comes from container and the dispatcher
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ErrorException
*/
/**
* Set some data type safe object that comes from container and the dispatcher
*
* @param ContainerInterface $container
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->args = $this->container->get("args");
$this->request = $this->container->get("request");
$this->config = $this->container->get("config");
}

}
8 changes: 7 additions & 1 deletion src/Support/Dir.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ public function resources(): string
*
* @return string
*/

public function routes(): string
{
return $this->dir . "/routes";
}

public function routers(): string
{
return $this->dir . "/routers";
return $this->routes();
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Support/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ class Twig
public function __construct(ContainerInterface $container, ResponseInterface $response)
{
$this->twig = $container->get(Environment::class);

$configs = App::get()->configs();
$configs = isset($configs['configs']) ? $configs['configs'] : ['app_title' => "MaplePHP"];

$this->twig->addGlobal('app', $configs);
$this->twig->addGlobal('app', App::get()->app());
$this->response = $response;
}

Expand Down