- First commit of this smallish kernel
This commit is contained in:
commit
8ab2d24073
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2020 Dave Mc Nicoll
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
"name": "mcnd/lean",
|
||||||
|
"description": "A more-than-micro framework for basic apps",
|
||||||
|
"type": "library",
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Dave Mc Nicoll",
|
||||||
|
"email": "mcndave@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"vlucas/phpdotenv": "^3.4@dev",
|
||||||
|
"php-di/php-di": "dev-master",
|
||||||
|
"league/route": "dev-master",
|
||||||
|
"laminas/laminas-diactoros ": "^2.4.1@dev",
|
||||||
|
"laminas/laminas-httphandlerrunner": "^1.2@dev"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Lean\\": "src/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,134 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lean;
|
||||||
|
|
||||||
|
use Dotenv\Dotenv;
|
||||||
|
|
||||||
|
use DI\ContainerBuilder,
|
||||||
|
DI\Container;
|
||||||
|
|
||||||
|
use League\Route\Strategy\ApplicationStrategy;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
|
use Zend\Diactoros\ServerRequestFactory,
|
||||||
|
Zend\HttpHandlerRunner\Emitter\EmitterInterface;
|
||||||
|
|
||||||
|
class Kernel {
|
||||||
|
|
||||||
|
protected Container $container;
|
||||||
|
|
||||||
|
protected string $locale;
|
||||||
|
|
||||||
|
public array $paths = [];
|
||||||
|
|
||||||
|
public int $errorReporting = E_ALL & ~E_USER_DEPRECATED & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE;
|
||||||
|
|
||||||
|
public string $definitionFilePath;
|
||||||
|
|
||||||
|
public string $errorLogPath;
|
||||||
|
|
||||||
|
public string $projectPath;
|
||||||
|
|
||||||
|
public string $routeDefinitionList = 'routes.list';
|
||||||
|
|
||||||
|
public function __construct(string $projectPath)
|
||||||
|
{
|
||||||
|
$this->projectPath = $projectPath;
|
||||||
|
|
||||||
|
$this->setEnvironment();
|
||||||
|
$this->initializeEngine();
|
||||||
|
$this->setupContainer();
|
||||||
|
$this->serviceContainer();
|
||||||
|
$this->handleRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function putenv($var, $value)
|
||||||
|
{
|
||||||
|
putenv("$var=$value");
|
||||||
|
$_ENV[$var] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setEnvironment() {
|
||||||
|
static::putenv('PROJECT_PATH', $this->projectPath);
|
||||||
|
|
||||||
|
// Environment vars (accessible from \DI\env(), getenv(), $_ENV and $_SERVER)
|
||||||
|
Dotenv::create(getenv("PROJECT_PATH"))->load();
|
||||||
|
|
||||||
|
// Paths and directories
|
||||||
|
foreach($this->paths as $name => $envkey) {
|
||||||
|
static::putenv($name, realpath(getenv("PROJECT_PATH") . DIRECTORY_SEPARATOR . getenv($envkey)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Override using headers
|
||||||
|
foreach(['APP_ENV', 'DEBUG', ] as $env) {
|
||||||
|
if ( null !== $value = $_SERVER["HTTP_$env"] ?? null ) {
|
||||||
|
static::putenv($env, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function initializeEngine() : self
|
||||||
|
{
|
||||||
|
# Defining default locale and timezone
|
||||||
|
date_default_timezone_set(getenv("DEFAULT_TIMEZONE"));
|
||||||
|
|
||||||
|
setlocale(LC_ALL, $this->locale = getenv("DEFAULT_LOCAL"));
|
||||||
|
setlocale(LC_TIME, getenv("DEFAULT_TIME"), getenv("DEFAULT_TIME_FALLBACK"));
|
||||||
|
|
||||||
|
ini_set("log_errors", "1");
|
||||||
|
ini_set("error_log", $this->errorLogPath);
|
||||||
|
ini_set('display_errors', getenv("DEBUG") ? 'on' : 'off');
|
||||||
|
|
||||||
|
error_reporting($this->errorReporting);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setupContainer() : self
|
||||||
|
{
|
||||||
|
$containerBuilder = new ContainerBuilder();
|
||||||
|
|
||||||
|
if (getenv("APP_ENV") === "prod") {
|
||||||
|
if (getenv("CACHE_PATH")) {
|
||||||
|
$containerBuilder->enableCompilation(getenv("CACHE_PATH") . "/di/");
|
||||||
|
$containerBuilder->writeProxiesToFile(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$containerBuilder->useAnnotations(false);
|
||||||
|
|
||||||
|
if ($this->definitionFilePath ?? false) {
|
||||||
|
$containerBuilder->addDefinitions(require($this->definitionFilePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->container = $containerBuilder->build();
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function serviceContainer() : self
|
||||||
|
{
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function handleRequest() : self
|
||||||
|
{
|
||||||
|
ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
|
||||||
|
|
||||||
|
// Router
|
||||||
|
$routeFactory = $this->container->get($this->routeDefinitionList);
|
||||||
|
|
||||||
|
$router = $routeFactory($this->container);
|
||||||
|
|
||||||
|
$router->setStrategy($this->container->get(ApplicationStrategy::class));
|
||||||
|
|
||||||
|
// Handle requests
|
||||||
|
$response = $router->dispatch($this->container->get(ServerRequestInterface::class));
|
||||||
|
|
||||||
|
// Reply to clients
|
||||||
|
$this->container->get(EmitterInterface::class)->emit($response);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in New Issue