Skip to content

Commit

Permalink
Add Applicaion class
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed Dec 26, 2020
1 parent 6f3113d commit 0d5e516
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 52 deletions.
8 changes: 3 additions & 5 deletions external/import.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use XHGui\Saver\SaverInterface;
use XHGui\ServiceContainer;
use XHGui\Application;

require __DIR__ . '/../vendor/autoload.php';

Expand All @@ -21,9 +20,8 @@
throw new RuntimeException('Can\'t open ' . $file);
}

$container = ServiceContainer::instance();
/** @var SaverInterface $saver */
$saver = $container['saver'];
$app = new Application();
$saver = $app->getSaver();

while (!feof($fp)) {
$line = fgets($fp);
Expand Down
48 changes: 48 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace XHGui;

use Pimple\Container;
use Slim\Slim as App;
use XHGui\Saver\SaverInterface;

class Application extends Container
{
/** @var bool */
private $booted = false;

public function __construct()
{
parent::__construct();
$this->register(new ServiceProvider\ServiceProvider());
$this->register(new ServiceProvider\ConfigProvider());
$this->register(new ServiceProvider\PdoStorageProvider());
$this->register(new ServiceProvider\MongoStorageProvider());
$this->register(new ServiceProvider\SlimProvider());
}

public function run(): void
{
$this->boot()->getSlim()->run();
}

public function boot(): self
{
if (!$this->booted) {
$this->register(new ServiceProvider\RouteProvider());
$this->booted = true;
}

return $this;
}

public function getSlim(): App
{
return $this['app'];
}

public function getSaver(): SaverInterface
{
return $this['saver'];
}
}
39 changes: 0 additions & 39 deletions src/ServiceContainer.php

This file was deleted.

6 changes: 3 additions & 3 deletions tests/LazyContainerProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
use MongoDB;
use Slim\Slim as App;
use Slim\View;
use XHGui\Application;
use XHGui\Controller;
use XHGui\Saver\SaverInterface;
use XHGui\Searcher\MongoSearcher;
use XHGui\Searcher\SearcherInterface;
use XHGui\ServiceContainer;
use XHGui\Twig\TwigExtension;

trait LazyContainerProperties
{
use LazyPropertiesTrait;

/** @var ServiceContainer */
/** @var Application */
protected $di;
/** @var Controller\ImportController */
protected $import;
Expand Down Expand Up @@ -59,7 +59,7 @@ protected function setupProperties(): void

protected function getDi()
{
$di = new ServiceContainer();
$di = new Application();
$config = $di['config'];

// Use a test databases
Expand Down
7 changes: 2 additions & 5 deletions webroot/index.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<?php

use Slim\Slim as App;
use XHGui\ServiceContainer;
use XHGui\Application;

require dirname(__DIR__) . '/vendor/autoload.php';

$di = ServiceContainer::instance();
/** @var App $app */
$app = $di['app'];
$app = new Application();
$app->run();

0 comments on commit 0d5e516

Please sign in to comment.