Added Application

main
Igor Scheller 7 years ago
parent 8c81adc8e8
commit 0ac9818764

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

@ -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);
}
}

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

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

Loading…
Cancel
Save