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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"maplephp/log": "^v2.0",
"nikic/fast-route": "^1.3",
"doctrine/dbal": "^4.4",
"doctrine/migrations": "^3.9"
"doctrine/migrations": "^3.9",
"twig/twig": "^3.24"
},
"require-dev": {
"maplephp/unitary": "^2.0"
Expand Down
216 changes: 107 additions & 109 deletions src/AbstractKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,49 @@
abstract class AbstractKernel
{

protected Stream $stream;
protected Stream $stream;
protected array $middlewares = [];
protected Env $env;
protected ContainerInterface $container;
protected Env $env;
protected ContainerInterface $container;
protected string $dir;
protected array $config;

public function __construct(string $dir)
{
if(!is_dir($dir)) {
throw new \RuntimeException("$dir is not a directory!");
}
public function __construct(string $dir)
{
if (!is_dir($dir)) {
throw new \RuntimeException("$dir is not a directory!");
}

$this->dir = realpath($dir);
$config = (new LoadConfigFiles())
->add("dir", $this->dir)
->loadEnvFile($this->dir . "/.env")
->loadFiles($this->dir . "/configs/");
$this->dir = realpath($dir);
$config = (new LoadConfigFiles())
->add("dir", $this->dir)
->loadEnvFile($this->dir . "/.env")
->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']);
}

/**
* Loader
*
* @param ServerRequestInterface $request
* @param DispatchConfigInterface|null $config
* @return KernelInterface
* @throws \Exception
*/
protected function load(ServerRequestInterface $request, ?DispatchConfigInterface $config = null): KernelInterface
{
$this->bootServiceProviders();
return new Kernel($this->container, $this->middlewares, $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']);
}

/**
* Loader
*
* @param ServerRequestInterface $request
* @param DispatchConfigInterface|null $config
* @return KernelInterface
* @throws \Exception
*/
protected function load(ServerRequestInterface $request, ?DispatchConfigInterface $config = null): KernelInterface
{
$this->bootServiceProviders();
return new Kernel($this->container, $this->middlewares, $config);
}

/**
* Boot service providers
Expand All @@ -75,14 +73,14 @@ protected function load(ServerRequestInterface $request, ?DispatchConfigInterfac
*/
protected function bootServiceProviders()
{
if(isset($this->config['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)) {
if (!($provider instanceof ServiceProvider)) {
throw new \RuntimeException(
"$providerClass is not an instance of " . ServiceProvider::class . "!"
);
Expand All @@ -108,76 +106,76 @@ public function withStream(Stream $stream): self
return $inst;
}

/**
* Clear the default middlewares, be careful with this
*
* @return $this
*/
public function clearDefaultMiddleware(): self
{
$inst = clone $this;
$inst->middlewares = [];
return $inst;
}

/**
* Add custom middlewares, follow PSR convention
*
* @param array $middleware
* @return $this
*/
public function withMiddleware(array $middleware): self
{
$inst = clone $this;
$inst->middlewares = array_merge($inst->middlewares, $middleware);
return $inst;
}

/**
* Change router file
*
* @param string $path
* @return $this
*/
public function withRouter(string $path): self
{
$inst = clone $this;
Kernel::setRouterFilePath($path);
return $inst;
}

/**
* Change the config file
*
* @param string $path
* @return $this
*/
public function withConfig(string $path): self
{
$inst = clone $this;
Kernel::setConfigFilePath($path);
return $inst;
}

/**
* Default error handler boot
* @param AbstractHandlerInterface $handler
* @return $this
*/
public function withErrorHandler(AbstractHandlerInterface $handler): self
{
$inst = clone $this;
$run = new Run($handler);
$run->severity()
->excludeSeverityLevels([E_USER_WARNING, E_NOTICE, E_USER_NOTICE, E_DEPRECATED, E_USER_DEPRECATED])
->redirectTo(function () {
// Let PHP’s default error handler process excluded severities
return false;
});
$run->setExitCode(1);
$run->load();
return $inst;
}
/**
* Clear the default middlewares, be careful with this
*
* @return $this
*/
public function clearDefaultMiddleware(): self
{
$inst = clone $this;
$inst->middlewares = [];
return $inst;
}

/**
* Add custom middlewares, follow PSR convention
*
* @param array $middleware
* @return $this
*/
public function withMiddleware(array $middleware): self
{
$inst = clone $this;
$inst->middlewares = array_merge($inst->middlewares, $middleware);
return $inst;
}

/**
* Change router file
*
* @param string $path
* @return $this
*/
public function withRouter(string $path): self
{
$inst = clone $this;
Kernel::setRouterFilePath($path);
return $inst;
}

/**
* Change the config file
*
* @param string $path
* @return $this
*/
public function withConfig(string $path): self
{
$inst = clone $this;
Kernel::setConfigFilePath($path);
return $inst;
}

/**
* Default error handler boot
* @param AbstractHandlerInterface $handler
* @return $this
*/
public function withErrorHandler(AbstractHandlerInterface $handler): self
{
$inst = clone $this;
$run = new Run($handler);
$run->severity()
->excludeSeverityLevels([E_USER_WARNING, E_NOTICE, E_USER_NOTICE, E_DEPRECATED, E_USER_DEPRECATED])
->redirectTo(function () {
// Let PHP’s default error handler process excluded severities
return false;
});
$run->setExitCode(1);
$run->load();
return $inst;
}

/**
* Helper method to load config file and return array
Expand Down
20 changes: 15 additions & 5 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function get(): self
*/
public function isProd(): bool
{
return $this->config['env'] === Environment::PROD->name();
return ($this->config['configs']['env'] ?? "development") === Environment::PROD->name();
}

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

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

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

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

/**
Expand All @@ -120,4 +120,14 @@ public function dir(): Dir
{
return $this->dir;
}

/**
* Get the app core configs
*
* @return array
*/
public function configs(): array
{
return $this->config;
}
}
2 changes: 1 addition & 1 deletion src/CliKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private function renderHelp(): void
$inst = $inst
->addOption(
"./maple $name",
$desc
(string)$desc
);
}
return $inst;
Expand Down
31 changes: 31 additions & 0 deletions src/Providers/TwigServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace MaplePHP\Core\Providers;

use MaplePHP\Core\App;
use MaplePHP\Core\Support\ServiceProvider;
use Psr\Container\ContainerInterface;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

class TwigServiceProvider extends ServiceProvider
{
public function register(ContainerInterface $container): void
{
$container->set(Environment::class, function () {
$loader = new FilesystemLoader(App::get()->dir()->resources());

$twig = new Environment($loader, [
'cache' => App::get()->isProd()
? App::get()->dir()->cache() . '/twig'
: false,
'debug' => App::get()->isDev(),
'auto_reload' => true,
]);

return $twig;
});
}
}
Loading