} } class TestClass implements Dez\DependencyInjection\InjectableInterface { public function __construct() { $params = func_get_args(); if (count($params) > 0) { foreach ($params as $name => $value) { $this->{$name} = $value; } } } protected $di; public function setDi(Dez\DependencyInjection\ContainerInterface $dependencyInjector) { $this->di = $dependencyInjector; return $this; } public function getDi() { return $this->di; } } $di = new Dez\DependencyInjection\Container(); $di->set('test', function ($param1, $param2) { return new TestClass($param1, $param2); }); $di->set('test2', '\\TestClass2'); // dump result die(print_r([$di->get('test', ['param1' => '123qwe', 'param2' => '321312']), $di->get('test2', ['param1' => '123qwe', 'param2' => '321312']), $di], true));
<?php error_reporting(1); ini_set('display_errors', 1); include_once '../vendor/autoload.php'; $container = new \Dez\DependencyInjection\Container(); $container->set('response', new \Dez\Http\Response()); $container->set('cookies', new \Dez\Http\Cookies()); /** @var $cookies \Dez\Http\CookiesInterface */ $cookies = $container->get('cookies'); /** @var $response \Dez\Http\Response */ $response = $container->get('response'); $cookies->set('test', rand(), time() * 100); $response->setHeader('Test', '123qwe'); $response->setContent(['data' => [1, 2, 3]]); $response->setStatusCode(418); $response->setBodyFormat(\Dez\Http\Response::RESPONSE_API_JSON); $response->send(); $response->resetHeaders(); $response->setBodyFormat(\Dez\Http\Response::RESPONSE_HTML); $response->setContent('<h1>Hello world!</h1>'); $response->send(); (new \Dez\Http\Response('<h2>Page not found!</h2>', 401))->sendHeaders()->sendContent(); // for for response add manually Di Container $cookies->set('randomCookie_' . rand(), '1', time() + 365 * 86400); (new \Dez\Http\Response('<h3>Full response!</h3>', 401))->setDi($container)->send();
<?php error_reporting(1); ini_set('display_errors', 1); include_once '../vendor/autoload.php'; $container = new \Dez\DependencyInjection\Container(); $container->set('response', new \Dez\Http\Response()); $container->set('cookies', new \Dez\Http\Cookies()); /** @var $response \Dez\Http\Response */ $response = $container->get('response'); class CustomFormatter extends \Dez\Http\Response\Format { public function process() { $this->response->setContentTypePlain(); $this->response->setContent("before" . json_encode(['content' => $this->response->getContent()], JSON_PRETTY_PRINT) . "after"); } } $response->setBodyFormat(\Dez\Http\Response::RESPONSE_CUSTOM, CustomFormatter::class); $response->setContent('<h1>Hello world!</h1>'); $response->send();