<?php
class ServiceContainer
{
protected $services = [];
public function get($id, callable $creator)
{
// Нет в кэше? Берём из creator'а. todo: Сделать защиту от цикличных зависимостей
if (! isset($this->services[$id]))
$this->services[$id] = $creator();
// Возвращаем кэш
return $this->services[$id];
}
public function set($id, $service)
{
$this->services[$id] = $service;
}
}
class Framework
{
private static $services = null;
public static function getServices()
{
if (is_null(self::$services)) {
throw new \Exception('Не инициализировано');
}
return self::$services;
}
public static function init(ServiceContainer $services = null) // Можно в качестве аргумента передать нужную реализацию ServiceContainer
{
if ( $services )
self::$services = $services;
else
self::$services = new ServiceContainer();
}
public static function run()
{
// Маршрутизация => Контроллер => Выполнение Response
}
public static function getRouter()
{
return static::getServices()->get('framework.router', function() {
return "router";
});
}
}
class CMS extends Framework
{
private static $services = null;
public static function getServices()
{
if (is_null(self::$services)) {
throw new \Exception('Не инициализировано (говорит CMS)');
}
return self::$services;
}
public static function run()
{
// Маршрутизация => Встроенные контроллеры/Модули => Выполнение Response
}
public static function getModulesManager()
{
return static::getServices()->get('cms.modulesmanager', function() {
return "modulesManager";
});
}
}
// based on framework
Framework::init() && Framework::run();
// где-то в компоненте:
var_dump(Framework::getRouter());
// based on CMS
CMS::init() && CMS::run();
// где-то в модуле:
var_dump(CMS::getRouter());
var_dump(CMS::getModulesManager());
CMS::getServices()->set('framework.router', 'Ha, its not router!');
var_dump(CMS::getRouter());
string(6) "router"
Fatal error: Uncaught Exception: Не инициализировано (говорит CMS) in /in/5Bf0X:64
Stack trace:
#0 /in/5Bf0X(51): CMS::getServices()
#1 /in/5Bf0X(91): Framework::getRouter()
#2 {main}
thrown in /in/5Bf0X on line 64
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
string(6) "router"
Fatal error: Uncaught Exception: Не инициализировано (говорит CMS) in /in/5Bf0X:64
Stack trace:
#0 /in/5Bf0X(51): CMS::getServices()
#1 /in/5Bf0X(91): Framework::getRouter()
#2 {main}
thrown in /in/5Bf0X on line 64
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
string(6) "router"
Fatal error: Uncaught exception 'Exception' with message 'Не инициализировано (говорит CMS)' in /in/5Bf0X:64
Stack trace:
#0 /in/5Bf0X(51): CMS::getServices()
#1 /in/5Bf0X(91): Framework::getRouter()
#2 {main}
thrown in /in/5Bf0X on line 64
Process exited with code 255.