<?php
namespace engine\service;
abstract class AbstractProvider {
/**
* [protected description]
* @var [type] \engine\DI\di;
*/
protected $di;
/**
* [__construct description]
* @param engineDIdi $di [description]
*/
public function __construct(\engine\DI\di $di){
$this->di = $di;
}
/**
* [abstract description]
* @var [type]
*/
abstract function init();
}
?>
<?php
namespace engine\service\router;
use engine\service\abstractprovider;
use engine\core\router\router;
Class provider extends abstractprovider
{
/**
* [public description]
* @var [type]
*/
public $serviceName = 'router';
/**
* [init description]
* @return [type] [description]
*/
public function init()
{
$router = new router('http://cmse.ru/');
$this->di->set($this->serviceName, $router);
}
}
?>
<?php
namespace engine;
class cms{
/**
*
* @var [type]
*/
private $di;
public $router;
/**
* [_construct description]
* @param [type] $di [description]
* @return [type] [description]
*/
public function _construct($di){
$this->di = $di;
$this->router = $this->di->get('router');
}
/**
* Run cms
*/
public function run(){
$this->router->add('home', '/', 'HomeController:index');
print_r($this->di);
}
}
?>
<?php
require_once __DIR__. '/../vendor/autoload.php';
use engine\cms;
use engine\DI\di;
try {
$di = new DI();
$services = require __DIR__ . '/config/service.php';
// init services
foreach ($services as $service) {
$provider = new $service($di);
$provider->init();
}
$cms = new cms($di);
$cms-> run();
} catch (\ErrorException $e){
echo $e -> getMessage();
}
?>
<?php
require_once 'engine/bootstrap.php';
?>
<?php
namespace engine\DI;
class DI {
/**
* @var array
*/
private $container = [];
/**
* @param $key
* @param $value
* @return $this
*/
public function set($key, $value){
$this->conteiner[$key] = $value;
return $this;
}
/**
* @param $key
* @return $mixed
*/
public function get($key)
{
return $this->has($key);
}
/**
* [has description]
* @param [type] $key [description]
* @return boolean [description]
*/
public function has($key){
return isset($this->$container[$key])? $this->$container[$key] : null;
}
}
?>
<?php
return [
engine\service\database\provider::class,
engine\service\router\provider::class,
];
?>