<?php require 'core/bootstrap.php';
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Core\DI; use Core\Starter; try { $di = new DI(); $providers = require __DIR__ . '/Provider/providerList.php'; foreach($providers as $item) { $provider = new $item($di); $provider->init(); } $starter = new Starter($di); $starter->run(); } catch(\ErrorException $e) { echo $e->getMessage(); } ?>
{ "name": "pimpops/php-di-container", "authors": [ { "name": "Andrii Pereverziev", "email": "pereverziev.andrii@gmail.com" } ], "require": {}, "autoload" : { "psr-4": { "Core\\": "core" } } }
<?php // autoload.php @generated by Composer require_once __DIR__ . '/composer/autoload_real.php'; return ComposerAutoloaderInit2b7621be73cd18ecc13077fabd56545f::getLoader();
<?php namespace Core; class Starter { private $di; public function __construct($di) { $this->di = $di; } public function run() { $contactBook = $this->di->get('contactBook'); $messenger = $this->di->get('messenger'); $list = $contactBook->getList(); foreach ($list as $contact) { echo( $messenger->sendMessage($contact['number']) . '<br>' ); } } } ?>
<?php namespace Core; class DI { private $container = []; public function set($key, $value) { $this->container[$key] = $value; } public function get($key) { return $this->has($key) ? $this->container[$key] : null; } public function has($key) { return isset($this->container[$key]); } } ?>
<?php return [ Core\Provider\ContactBook\Provider::class, Core\Provider\Messenger\Provider::class ];
<?php namespace Core\Provider; use Core\DI; abstract class AbstractProvider { protected $di; public function __construct(DI $di) { $this->di = $di; } abstract function init(); } ?>
<?php namespace Core\Provider\Messenger; use Core\Provider\AbstractProvider; use Core\Worker\Messenger\Messenger; class Provider extends AbstractProvider { public $workerName = 'messenger'; public function init() { $messenger = new Messenger(); $this->di->set($this->workerName, $messenger); } } ?>
<?php namespace Core\Provider\ContactBook; use Core\Provider\AbstractProvider; use Core\Worker\ContactBook\ContactBook; class Provider extends AbstractProvider { public $workerName = 'contactBook'; public function init() { $contactBook = new ContactBook(); $this->di->set($this->workerName, $contactBook); } } ?>
<?php namespace Core\Worker\Messenger; class Messenger { public function sendMessage($contact) { return "Message has been sent to number: " . $contact; } } ?>
<?php namespace Core\Worker\ContactBook; class ContactBook { private $contactList = [ ["name" => "Anna", "number" => "234234234"], ["name" => "Gleg", "number" => "234dfdf234234"] ]; public function getList() { return $this->contactList; } } ?>

pimpops

php-di-container

project
core
Provider
Messenger
Provider.php
ContactBook
Provider.php
AbstractProvider.php
providerList.php
Worker
Messenger
Messenger.php
ContactBook
ContactBook.php
bootstrap.php
DI.php
Starter.php
index.php
composer.json
php-di-container
php-di-container index.php composer.json autoload.php bootstrap.php Starter.php providerList.php AbstractProvider.php Provider.php Messenger.php ContactBook.php Provider.php DI.php core Worker Messenger ContactBook Provider Messenger ContactBook Vendor

Choose a file
here

index.html
test