Added dependency injection engine
This commit is contained in:
parent
78f57e5fc4
commit
6265a09d39
9 changed files with 83 additions and 27 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
local.ini
|
||||
vendor
|
||||
composer.lock
|
5
composer.json
Normal file
5
composer.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"require": {
|
||||
"mnapoli/php-di": "~4.0"
|
||||
}
|
||||
}
|
3
config.ini
Normal file
3
config.ini
Normal file
|
@ -0,0 +1,3 @@
|
|||
databaseHost = localhost
|
||||
databasePort = 27017;
|
||||
databaseName = booru-dev
|
6
di.php
Normal file
6
di.php
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
return [
|
||||
\Szurubooru\Config::class => DI\object()->constructor([
|
||||
__DIR__ . DS . 'config.ini',
|
||||
__DIR__ . DS . 'local.ini']),
|
||||
];
|
|
@ -1,25 +1,13 @@
|
|||
<?php
|
||||
require_once('..' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'AutoLoader.php');
|
||||
$start = microtime(true);
|
||||
|
||||
function injectControllers($router)
|
||||
{
|
||||
\Szurubooru\Controllers\AuthController::register($router);
|
||||
}
|
||||
define('DS', DIRECTORY_SEPARATOR);
|
||||
require_once(__DIR__ . DS . '..' . DS . 'vendor' . DS . 'autoload.php');
|
||||
require_once(__DIR__ . DS . '..' . DS . 'src' . DS . 'AutoLoader.php');
|
||||
|
||||
$router = new \Szurubooru\Router;
|
||||
injectControllers($router);
|
||||
|
||||
try
|
||||
{
|
||||
$json = $router->handle($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$json = [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTrace(),
|
||||
];
|
||||
}
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($json);
|
||||
$builder = new \DI\ContainerBuilder();
|
||||
$builder->setDefinitionCache(new Doctrine\Common\Cache\ArrayCache());
|
||||
$builder->addDefinitions(__DIR__ . DS . '..' . DS . 'di.php');
|
||||
$container = $builder->build();
|
||||
$dispatcher = $container->get('Szurubooru\Dispatcher');
|
||||
$dispatcher->run();
|
||||
|
|
18
src/ControllerRepository.php
Normal file
18
src/ControllerRepository.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
namespace Szurubooru;
|
||||
|
||||
final class ControllerRepository
|
||||
{
|
||||
private $controllers = [];
|
||||
|
||||
public function __construct(
|
||||
\Szurubooru\Controllers\AuthController $auth)
|
||||
{
|
||||
$this->controllers = func_get_args();
|
||||
}
|
||||
|
||||
public function getControllers()
|
||||
{
|
||||
return $this->controllers;
|
||||
}
|
||||
}
|
|
@ -3,8 +3,5 @@ namespace Szurubooru\Controllers;
|
|||
|
||||
abstract class AbstractController
|
||||
{
|
||||
public static function register(\Szurubooru\Router $router)
|
||||
{
|
||||
return new static($router);
|
||||
}
|
||||
abstract function registerRoutes(\Szurubooru\Router $router);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ namespace Szurubooru\Controllers;
|
|||
|
||||
final class AuthController extends AbstractController
|
||||
{
|
||||
public function __construct(\Szurubooru\Router $router)
|
||||
public function registerRoutes(\Szurubooru\Router $router)
|
||||
{
|
||||
$router->post('/api/login', [$this, 'login']);
|
||||
$router->get('/api/login', [$this, 'login']);
|
||||
|
|
36
src/Dispatcher.php
Normal file
36
src/Dispatcher.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
namespace Szurubooru;
|
||||
|
||||
final class Dispatcher
|
||||
{
|
||||
private $router;
|
||||
|
||||
public function __construct(
|
||||
\Szurubooru\Router $router,
|
||||
\Szurubooru\ControllerRepository $controllerRepository)
|
||||
{
|
||||
$this->router = $router;
|
||||
foreach ($controllerRepository->getControllers() as $controller)
|
||||
$controller->registerRoutes($router);
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
global $start;
|
||||
try
|
||||
{
|
||||
$json = $this->router->handle($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$json = [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTrace(),
|
||||
];
|
||||
}
|
||||
$end = microtime(true);
|
||||
$json['__time'] = $end - $start;
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($json);
|
||||
}
|
||||
}
|
Reference in a new issue