Giter Club home page Giter Club logo

sugarloaf's Introduction

sugarloaf

PHP Dependency Injection Framework

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.

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.

// 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.

sugarloaf's People

Contributors

tobias74 avatar

Watchers

 avatar  avatar

sugarloaf's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.