2017 © Pedro Peláez
 

library sugarloaf

Lightweight Dependency Injection Framework for PHP

image

sugarloaf/sugarloaf

Lightweight Dependency Injection Framework for PHP

  • Saturday, July 28, 2018
  • by tobias74
  • Repository
  • 1 Watchers
  • 0 Stars
  • 22 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 8 Versions
  • 22 % Grown

The README.md

sugarloaf

PHP Dependency Injection Framework, (*1)

SugarLoaf is a (very) lightweight DI-Container that supports Constructor-Injection as well as Setter-Injection. In case of Setter-Injection, it is also possible (though frowned uppon?) to configure cyclic dependencies., (*2)

The following example consists of the Classes "LogFile", "Logger" and "Application". The Logger uses a LogFile which has to be injected in the constructor, while the Application depends on two instances of said logger, injected by two setter-methods., (*3)


// Class without Dependencies class LogFile { public function append($msg){ error_log($msg); } } // Class that uses Constructor-Injection class Logger { public function __construct($logFile, $logPrefix){ $this->logFile = $logFile; $this->logPrefix = $logPrefix; } public function error($msg){ $this->logFile->append('Logging Error: '.$this->logPrefix.': '.$msg); } public function info($msg){ $this->logFile->append('Logging Info: '.$this->logPrefix.': '.$msg); } } //Class that uses Setter-Injection class Application { public function setLogger($logger){ $this->logger = $logger; } public function setAnotherLogger($logger){ $this->anotherLogger = $logger; } public function run(){ $this->logger->info('Starting Application...'); $this->anotherLogger->info('Same here...'); echo "done."; } } // Configuration of the DI-Container $dependencyManager = \SugarLoaf\DependencyManager::getInstance(); // the LogFile-Class does not need have Dependencies $dependencyManager->registerDependencyManagedService(new \SugarLoaf\Service\ManagedService('LogFile')); // the Logger-Class takes to arguments in its constructor $parameter = new \SugarLoaf\Parameter\ParameterArray(); $parameter->appendParameter(new \SugarLoaf\Parameter\ManagedParameter('LogFile')); $parameter->appendParameter(new \SugarLoaf\Parameter\UnmanagedParameter('My_Prefix')); $dependencyManager->registerDependencyManagedService(new \SugarLoaf\Service\ManagedSingleton('Logger','Logger',$parameter)); // the Logger-Class takes to arguments in its constructor $parameter = new \SugarLoaf\Parameter\ParameterArray(); $parameter->appendParameter(new \SugarLoaf\Parameter\ManagedParameter('LogFile')); $parameter->appendParameter(new \SugarLoaf\Parameter\UnmanagedParameter('Another_Prefix')); $dependencyManager->registerDependencyManagedService(new \SugarLoaf\Service\ManagedSingleton('LoggerTwo','Logger',$parameter)); $dependencyManager->registerDependencyManagedService(new \SugarLoaf\Service\ManagedService('MyApplication','Application')) ->addDependency('Logger', new \SugarLoaf\Component\ManagedDependency('Logger')) ->addDependency('AnotherLogger', new \SugarLoaf\Component\ManagedDependency('LoggerTwo')); $application = $dependencyManager->get('MyApplication'); $application->run();

Using Service-Providers

In case a service can be preconfigured to use different dependencies, but still needs to be injected with another instance only available at runtime, the class "ManagedDependencyProvider" implements a wrapper that can be called by its "provide"-Method. See examples in the git-repo for further information., (*4)

The Versions

28/07 2018

dev-master

9999999-dev

Lightweight Dependency Injection Framework for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

28/07 2018

v0.2.3

0.2.3.0

Lightweight Dependency Injection Framework for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

01/09 2017

v0.2.2

0.2.2.0

Lightweight Dependency Injection Framework for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

01/09 2017

v0.2.1

0.2.1.0

Lightweight Dependency Injection Framework for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

30/08 2017

v0.2.0

0.2.0.0

Lightweight Dependency Injection Framework for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

21/05 2015

v0.1.2

0.1.2.0

Lightweight Dependency Injection Framework for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

21/05 2015

0.1.1

0.1.1.0

Lightweight Dependency Injection Framework for PHP

  Sources   Download

The Requires

  • php >=5.3.0

 

21/05 2015

0.1

0.1.0.0

Lightweight Dependency Injection Framework for PHP

  Sources   Download

The Requires

  • php >=5.3.0