Added Application

This commit is contained in:
Igor Scheller 2017-09-19 14:50:20 +02:00
parent 8c81adc8e8
commit 0ac9818764
4 changed files with 60 additions and 27 deletions

View File

@ -1,13 +1,12 @@
<?php
use Engelsystem\Application;
use Engelsystem\Config\Config;
use Engelsystem\Container\Container;
use Engelsystem\Database\Db;
use Engelsystem\Exceptions\Handler as ExceptionHandler;
use Engelsystem\Http\Request;
use Engelsystem\Renderer\HtmlEngine;
use Engelsystem\Renderer\Renderer;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
/**
@ -18,18 +17,16 @@ require_once __DIR__ . '/autoload.php';
/**
* Initialize the container
* Initialize the application
*/
$container = Container::getInstance();
$container->instance('container', $container);
$container->instance(ContainerInterface::class, $container);
$app = Application::getInstance();
/**
* Load configuration
*/
$config = new Config();
$container->instance('config', $config);
$app->instance('config', $config);
$config->set(require __DIR__ . '/../config/config.default.php');
if (file_exists(__DIR__ . '/../config/config.php')) {
@ -48,7 +45,7 @@ date_default_timezone_set($config->get('timezone'));
* @var Request $request
*/
$request = Request::createFromGlobals();
$container->instance('request', $request);
$app->instance('request', $request);
/**
@ -64,7 +61,7 @@ if ($config->get('maintenance')) {
* Initialize renderer
*/
$renderer = new Renderer();
$container->instance('renderer', $renderer);
$app->instance('renderer', $renderer);
$renderer->addRenderer(new HtmlEngine());
@ -72,7 +69,7 @@ $renderer->addRenderer(new HtmlEngine());
* Register error handler
*/
$errorHandler = new ExceptionHandler();
$container->instance('error.handler', $errorHandler);
$app->instance('error.handler', $errorHandler);
if (config('environment') == 'development') {
$errorHandler->setEnvironment(ExceptionHandler::ENV_DEVELOPMENT);
ini_set('display_errors', true);
@ -184,7 +181,7 @@ foreach ($includeFiles as $file) {
* Init application
*/
$session = new Session();
$container->instance('session', $session);
$app->instance('session', $session);
$session->start();
$request->setSession($session);

25
src/Application.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace Engelsystem;
use Engelsystem\Container\Container;
use Psr\Container\ContainerInterface;
class Application extends Container
{
public function __construct()
{
$this->registerBaseBindings();
}
protected function registerBaseBindings()
{
self::setInstance($this);
Container::setInstance($this);
$this->instance('app', $this);
$this->instance('container', $this);
$this->instance(Container::class, $this);
$this->instance(Application::class, $this);
$this->instance(ContainerInterface::class, $this);
}
}

View File

@ -48,6 +48,17 @@ class Container implements ContainerInterface
* @param mixed $instance Entry
*/
public function instance($abstract, $instance)
{
$this->singleton($abstract, $instance);
}
/**
* Register a shared entry as singleton in the container
*
* @param string $abstract
* @param mixed $instance
*/
public function singleton($abstract, $instance)
{
$this->instances[$abstract] = $instance;
}
@ -68,10 +79,21 @@ class Container implements ContainerInterface
return isset($this->instances[$id]);
}
/**
* Resolve the requested object
*
* @param string $abstract
* @return mixed
*/
protected function resolve($abstract)
{
return $this->instances[$abstract];
}
/**
* Get the globally available instance of the container
*
* @return Container
* @return self
*/
public static function getInstance()
{
@ -91,15 +113,4 @@ class Container implements ContainerInterface
{
static::$instance = $container;
}
/**
* Resolve the requested object
*
* @param string $abstract
* @return mixed
*/
protected function resolve($abstract)
{
return $this->instances[$abstract];
}
}

View File

@ -1,15 +1,15 @@
<?php
// Some useful functions
use Engelsystem\Application;
use Engelsystem\Config\Config;
use Engelsystem\Container\Container;
use Engelsystem\Http\Request;
use Engelsystem\Renderer\Renderer;
use Engelsystem\Routing\UrlGenerator;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* Get the global container instance
* Get the global app instance
*
* @param string $id
* @return mixed
@ -17,10 +17,10 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface;
function app($id = null)
{
if (is_null($id)) {
return Container::getInstance();
return Application::getInstance();
}
return Container::getInstance()->get($id);
return Application::getInstance()->get($id);
}
/**