Created
July 29, 2019 12:48
-
-
Save kobus1998/7865a14e692068283a4828c8391db4a2 to your computer and use it in GitHub Desktop.
simple service api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface ServiceProvider | |
{ | |
public function register($container); | |
} | |
class UserServiceProvider implements ServiceProvider | |
{ | |
public function register($container) | |
{ | |
$container->add(UserService::class, [Database::class, Logger::class]); | |
} | |
} | |
class Database {} | |
class Logger {} | |
class CommonServiceProvider implements ServiceProvider | |
{ | |
public function register($container) | |
{ | |
$container->add(Database::class); | |
$container->add(Logger::class); | |
} | |
} | |
class Container | |
{ | |
protected $items = []; | |
public function add($class, $params = []) | |
{ | |
$this->items[$class] = $params; | |
} | |
} | |
class Application | |
{ | |
protected $container; | |
public function __construct($container) | |
{ | |
$this->container = $container; | |
} | |
public function registerService($class) | |
{ | |
(new $class())->register($this->container); | |
} | |
public function addRoute(Route $route) | |
{ | |
$this->routes[] = $route; | |
} | |
public function route() | |
{ | |
foreach($this->routes as $route) | |
{ | |
if (!$route->matches()) { | |
continue; | |
} | |
return $route->exec($this->request, $this->response, $this->container); | |
} | |
} | |
} | |
$app = new Application(new Container()); | |
$app->registerService(UserServiceProvider::class); | |
$app->registerService(CommonServiceProvider::class); | |
$respone = $app->route(); | |
$response->serve(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment