Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created July 29, 2019 12:48
Show Gist options
  • Save kobus1998/7865a14e692068283a4828c8391db4a2 to your computer and use it in GitHub Desktop.
Save kobus1998/7865a14e692068283a4828c8391db4a2 to your computer and use it in GitHub Desktop.
simple service api
<?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