Skip to content
This repository was archived by the owner on Apr 5, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nbproject
126 changes: 126 additions & 0 deletions core/src/main/php/ioc/App.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/* This class is part of the XP framework
*
* $Id$
*/

uses(
'ioc.Injector',
'ioc.module.ArgumentsBindingModule',
'ioc.module.BindingModule',
'lang.IllegalArgumentException'
);

/**
* Class for starting the application by configuring the IoC container.
*/
class App extends Object {

/**
* configures the application using the given binding modules and returns
* injector so that the bootstrap file can request an instance of the entry
* class
*
* @return ioc.Injector
*/
public static function createInjector() {
return self::createInjectorWithBindings(self::extractArgs(func_get_args()));
}

/**
* extracts arguments
*
* If arguments has only one value and this is an array this will be returned,
* else all arguments will be returned.
*
* @param string[] $args
* @return string[]|ioc.module.BindingModule[]
*/
protected static function extractArgs($args) {
if (count($args) === 1 && is_array($args[0])) {
return $args[0];
}

return $args;
}

/**
* creates an object via injection
*
* If the class to create an instance of contains a static __bindings() method
* this method will be used to configure the ioc bindings before using the ioc
* container to create the instance.
*
* @param string $className full qualified class name of class to create an instance of
* @param string[] $argv optional list of arguments
* @return object
*/
public static function createInstance($className, $argv = NULL) {
return self::createInjectorWithBindings(self::getBindingsForClass($className, $argv))
->getInstance($className);
}

/**
* creates list of bindings from given class
*
* @param string $className full qualified class name of class to retrieve bindings for
* @param string[] $argv optional list of arguments
* @return string[]|ioc.module.BindingModule[]
*/
public static function getBindingsForClass($className, $argv = NULL) {
$bindings = array();
$class = XPClass::forName($className);
if (method_exists($class->getSimpleName(), '__bindings')) {
$bindings = call_user_func(array($class->getSimpleName(), '__bindings'));
}

if (NULL !== $argv) {
$bindings[] = new ArgumentsBindingModule($argv);
}

return $bindings;
}

/**
* configures the application using the given binding modules and returns
* injector so that the bootstrap file can request an instance of the entry
* class
*
* @param string[]|ioc.module.BindingModule[] $bindingModules
* @return ioc.Injector
*/
public static function createInjectorWithBindings($bindingModules) {
return self::createBinderWithBindings($bindingModules)->getInjector();
}

/**
* configures the application using the given binding modules and returns
* binder so that the bootstrap file can request an instance of the entry
* class
*
* @param string[]|ioc.module.BindingModule[] $bindingModules
* @return ioc.Binder
* @throws lang.IllegalArgumentException
*/
public static function createBinderWithBindings($bindingModules)
{
$binder = new Binder();
foreach ($bindingModules as $bindingModule) {
if (is_string($bindingModule)) {
$bindingModule = XPClass::forName($bindingModule)->newInstance();
}

if (!($bindingModule instanceof BindingModule)) {
throw new IllegalArgumentException('Given module class ' . get_class($bindingModule) . ' is not an instance of ioc.module.BindingModule');
}

$bindingModule->configure($binder);
}

// make injector itself available for injection
$binder->bind('ioc.Injector')
->toInstance($binder->getInjector());
return $binder;
}
}
?>
68 changes: 68 additions & 0 deletions core/src/main/php/ioc/Binder.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/* This class is part of the XP framework
*
* $Id: Binder.class.php 2991 2011-02-12 23:35:48Z mikey $
*/

uses('ioc.Injector', 'ioc.BindingScope');

/**
* Entry class to ease the adding of bindings.
*/
class Binder extends Object {
protected $injector;

/**
* Create a new binder
*
* @param ioc.Injector $injector optional
*/
public function __construct(Injector $injector = NULL) {
if (NULL === $injector) {
$this->injector = new Injector();
} else {
$this->injector = $injector;
}
}

/**
* sets session scope
*
* @param ioc.BindingScope $sessionScope
* @return ioc.Binder
*/
public function setSessionScope(BindingScope $sessionScope) {
$this->injector->setSessionScope($sessionScope);
return $this;
}

/**
* Bind a new interface to a class
*
* @param string $interface
* @return ioc.ClassBinding
*/
public function bind($interface) {
return $this->injector->bind($interface);
}

/**
* Bind a new constant with given name
*
* @param string $name
* @return ioc.ConstantBinding
*/
public function bindNamedConstant($name) {
return $this->injector->bindNamedConstant($name);
}

/**
* Get an injector for this binder
*
* @return ioc.Injector
*/
public function getInjector() {
return $this->injector;
}
}
?>
28 changes: 28 additions & 0 deletions core/src/main/php/ioc/Binding.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/* This class is part of the XP framework
*
* $Id: Binding.class.php 2991 2011-02-12 23:35:48Z mikey $
*/

/**
* Binds an interface to an implementation.
*/
interface Binding {

/**
* returns the created instance
*
* @param string $type
* @param string $name
* @return mixed
*/
public function getInstance($type, $name);

/**
* creates a unique key for this binding
*
* @return string
*/
public function getKey();
}
?>
13 changes: 13 additions & 0 deletions core/src/main/php/ioc/BindingException.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/* This class is part of the XP framework
*
* $Id: BindingException.class.php 2991 2011-02-12 23:35:48Z mikey $
*/

/**
* Indicates errors during the binding process.
*/
class BindingException extends XPException {

}
?>
24 changes: 24 additions & 0 deletions core/src/main/php/ioc/BindingScope.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/* This class is part of the XP framework
*
* $Id: BindingScope.class.php 2991 2011-02-12 23:35:48Z mikey $
*/

uses('ioc.InjectionProvider');

/**
* Interface for for different scopes of a binding.
*/
interface BindingScope {

/**
* returns the requested instance from the scope
*
* @param lang.XPClass $type type of the object
* @param lang.XPClass $impl concrete implementation
* @param ioc.InjectionProvider $provider
* @return object
*/
public function getInstance(XPClass $type, XPClass $impl, InjectionProvider $provider);
}
?>
72 changes: 72 additions & 0 deletions core/src/main/php/ioc/BindingScopes.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/* This class is part of the XP framework
*
* $Id: BindingScopes.class.php 2991 2011-02-12 23:35:48Z mikey $
*/

uses(
'ioc.SessionBindingScope',
'ioc.SingletonBindingScope',
'lang.RuntimeError'
);

/**
* Access to all built-in scopes.
*/
class BindingScopes extends Object {
protected
$singletonScope,
$sessionScope;

/**
* constructor
*
* @param ioc.BindingScope $singletonScope optional
* @param ioc.BindingScope $sessionScope optional
*/
public function __construct(BindingScope $singletonScope = NULL, BindingScope $sessionScope = NULL) {
if (NULL === $singletonScope) {
$this->singletonScope = new SingletonBindingScope();
} else {
$this->singletonScope = $singletonScope;
}

if (NULL !== $sessionScope) {
$this->sessionScope = $sessionScope;
}
}

/**
* returns scope for singleton objects
*
* @return ioc.BindingScope
*/
public function getSingletonScope() {
return $this->singletonScope;
}

/**
* sets session to be used with the session scope
*
* @param ioc.BindingScope $sessionScope
* @return ioc.BindingScope
*/
public function setSessionScope(BindingScope $sessionScope) {
$this->sessionScope = $sessionScope;
return $this;
}

/**
* returns scope for session resources
*
* @return ioc.BindingScope
*/
public function getSessionScope() {
if (NULL === $this->sessionScope) {
throw new RuntimeError('No session binding scope available.');
}

return $this->sessionScope;
}
}
?>
Loading