PHP code example of rdlowrey / auryn
1. Go to this page and download the library: Download rdlowrey/auryn library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
rdlowrey / auryn example snippets
$injector = new Auryn\Injector;
$injector = new Auryn\Injector;
$obj1 = new SomeNamespace\MyClass;
$obj2 = $injector->make('SomeNamespace\MyClass' );
var_dump($obj2 instanceof SomeNamespace\MyClass);
class SomeDependency {}
class AnotherDependency {}
class MyClass {
public $dep1;
public $dep2;
public function __construct (SomeDependency $dep1, AnotherDependency $dep2) {
$this ->dep1 = $dep1;
$this ->dep2 = $dep2;
}
}
$injector = new Auryn\Injector;
$myObj = $injector->make('MyClass' );
var_dump($myObj->dep1 instanceof SomeDependency);
var_dump($myObj->dep2 instanceof AnotherDependency);
class Car {
private $engine;
public function __construct (Engine $engine) {
$this ->engine = $engine;
}
}
class Engine {
private $sparkPlug;
private $piston;
public function __construct (SparkPlug $sparkPlug, Piston $piston) {
$this ->sparkPlug = $sparkPlug;
$this ->piston = $piston;
}
}
$injector = new Auryn\Injector;
$car = $injector->make('Car' );
var_dump($car instanceof Car);
interface Engine {}
class V8 implements Engine {}
class Car {
private $engine;
public function __construct (Engine $engine) {
$this ->engine = $engine;
}
}
$injector = new Auryn\Injector;
$injector->define('Car' , ['engine' => 'V8' ]);
$car = $injector->make('Car' );
var_dump($car instanceof Car);
class MyClass {
private $arg1;
private $arg2;
public function __construct (SomeConcreteClass $arg1, SomeInterface $arg2) {
$this ->arg1 = $arg1;
$this ->arg2 = $arg2;
}
}
$injector = new Auryn\Injector;
$injector->define('MyClass' , ['arg2' => 'SomeImplementationClass' ]);
$myObj = $injector->make('MyClass' );
interface SomeInterface {}
class SomeImplementation implements SomeInterface {}
class MyClass {
private $dependency;
public function __construct (SomeInterface $dependency) {
$this ->dependency = $dependency;
}
}
$injector = new Auryn\Injector;
$dependencyInstance = new SomeImplementation;
$injector->define('MyClass' , [':dependency' => $dependencyInstance]);
$myObj = $injector->make('MyClass' );
var_dump($myObj instanceof MyClass);
interface SomeInterface {}
class SomeImplementationClass implements SomeInterface {}
class MyClass {
private $dependency;
public function __construct (SomeInterface $dependency) {
$this ->dependency = $dependency;
}
}
$injector = new Auryn\Injector;
$myObj = $injector->make('MyClass' , ['dependency' => 'SomeImplementationClass' ]);
var_dump($myObj instanceof MyClass);
interface Engine {}
class V8 implements Engine {}
class Car {
private $engine;
public function __construct (Engine $engine) {
$this ->engine = $engine;
}
}
$injector = new Auryn\Injector;
$injector->alias('Engine' , 'V8' );
$car = $injector->make('Car' );
var_dump($car instanceof Car);
$injector = new Auryn\Injector;
$injector->share('PDO' );
$injector->define('PDO' , [
':dsn' => 'mysql:dbname=testdb;host=127.0.0.1' ,
':username' => 'dbuser' ,
':passwd' => 'dbpass'
]);
$db = $injector->make('PDO' );
$myUniversalValue = 42 ;
class MyClass {
public $myValue;
public function __construct ($myValue) {
$this ->myValue = $myValue;
}
}
$injector = new Auryn\Injector;
$injector->defineParam('myValue' , $myUniversalValue);
$obj = $injector->make('MyClass' );
var_dump($obj->myValue === 42 );
class DataMapper {
private $pdo;
public function __construct (PDO $pdo) {
$this ->pdo = $pdo;
}
}
class MyController {
private $mapper;
public function __construct (DataMapper $mapper) {
$this ->mapper = $mapper;
}
}
$db = new PDO('mysql:host=localhost;dbname=mydb' , 'user' , 'pass' );
$injector = new Auryn\Injector;
$injector->share($db);
$myController = $injector->make('MyController' );
class Person {
public $name = 'John Snow' ;
}
$injector = new Auryn\Injector;
$injector->share('Person' );
$person = $injector->make('Person' );
var_dump($person->name);
$person->name = 'Arya Stark' ;
$anotherPerson = $injector->make('Person' );
var_dump($anotherPerson->name);
var_dump($person === $anotherPerson);
class MyComplexClass {
public $verification = false ;
public function doSomethingAfterInstantiation () {
$this ->verification = true ;
}
}
$complexClassFactory = function () {
$obj = new MyComplexClass;
$obj->doSomethingAfterInstantiation();
return $obj;
};
$injector = new Auryn\Injector;
$injector->delegate('MyComplexClass' , $complexClassFactory);
$obj = $injector->make('MyComplexClass' );
var_dump($obj->verification);
class SomeClassWithDelegatedInstantiation {
public $value = 0 ;
}
class SomeFactoryDependency {}
class MyFactory {
private $dependency;
function __construct (SomeFactoryDependency $dep) {
$this ->dependency = $dep;
}
function __invoke () {
$obj = new SomeClassWithDelegatedInstantiation;
$obj->value = 1 ;
return $obj;
}
function factoryMethod () {
$obj = new SomeClassWithDelegatedInstantiation;
$obj->value = 2 ;
return $obj;
}
}
$injector->delegate('SomeClassWithDelegatedInstantiation' , 'MyFactory' );
$obj = $injector->make('SomeClassWithDelegatedInstantiation' );
var_dump($obj->value);
$injector->delegate('SomeClassWithDelegatedInstantiation' , 'MyFactory::factoryMethod' );
$obj = $injector->make('SomeClassWithDelegatedInstantiation' );
$obj = $injector->make('SomeClassWithDelegatedInstantiation' );
var_dump($obj->value);
class MyClass {
public $myProperty = 0 ;
}
$injector->prepare('MyClass' , function ($myObj, $injector) {
$myObj->myProperty = 42 ;
});
$myObj = $injector->make('MyClass' );
var_dump($myObj->myProperty);
$injector = new Auryn\Injector;
$injector->execute(function () {});
$injector->execute([$objectInstance, 'methodName' ]);
$injector->execute('globalFunctionName' );
$injector->execute('MyStaticClass::myStaticMethod' );
$injector->execute(['MyStaticClass' , 'myStaticMethod' ]);
$injector->execute(['MyChildStaticClass' , 'parent::myStaticMethod' ]);
$injector->execute('ClassThatHasMagicInvoke' );
$injector->execute($instanceOfClassThatHasMagicInvoke);
$injector->execute('MyClass::myInstanceMethod' );
class Dependency {}
class AnotherDependency {}
class Example {
function __construct (Dependency $dep) {}
function myMethod (AnotherDependency $arg1, $arg2) {
return $arg2;
}
}
$injector = new Auryn\Injector;
var_dump($injector->execute('Example::myMethod' , $args = [':arg2' => 42 ]));
class HouseMapper {
private $pdo;
public function __construct (PDO $pdo) {
$this ->pdo = $pdo;
}
public function find ($houseId) {
$query = 'SELECT * FROM houses WHERE houseId = :houseId' ;
$stmt = $this ->pdo->prepare($query);
$stmt->bindValue(':houseId' , $houseId);
$stmt->setFetchMode(PDO::FETCH_CLASS, 'Model\\Entities\\House' );
$stmt->execute();
$house = $stmt->fetch(PDO::FETCH_CLASS);
if (false === $house) {
throw new RecordNotFoundException(
'No houses exist for the specified ID'
);
}
return $house;
}
}
class PersonMapper {
private $pdo;
public function __construct (PDO $pdo) {
$this ->pdo = $pdo;
}
}
class SomeService {
private $houseMapper;
private $personMapper;
public function __construct (HouseMapper $hm, PersonMapper $pm) {
$this ->houseMapper = $hm;
$this ->personMapper = $pm;
}
public function doSomething () {
}
}
$pdo = new PDO('sqlite:some_sqlite_file.db' );
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$injector = new Auryn\Injector;
$injector->share($pdo);
$mapper = $injector->make('SomeService' );
$injector->define('PDO' , [
':dsn' => 'sqlite:some_sqlite_file.db'
]);
$injector->share('PDO' );
$service = $injector->make('SomeService' );
define('CONTROLLER_ROUTES' , '/hard/path/to/routes.xml' );
$routeLoader = new RouteLoader();
$routes = $routeLoader->loadFromXml(CONTROLLER_ROUTES);
$router = new Router($routes);
$requestDetector = new RequestDetector();
$request = $requestDetector->detectFromSuperglobal($_SERVER);
$requestUri = $request->getUri();
$requestMethod = strtolower($request->getMethod());
$injector = new Auryn\Injector;
$injector->share($request);
try {
if (!$controllerClass = $router->route($requestUri, $requestMethod)) {
throw new NoRouteMatchException();
}
$controller = $injector->make($controllerClass);
$callableController = array ($controller, $requestMethod);
if (!is_callable($callableController)) {
throw new MethodNotAllowedException();
} else {
$callableController();
}
} catch (NoRouteMatchException $e) {
} catch (MethodNotAllowedException $e) {
} catch (Exception $e) {
}
class WidgetController {
private $request;
private $mapper;
public function __construct (Request $request, WidgetDataMapper $mapper) {
$this ->request = $request;
$this ->mapper = $mapper;
}
public function get () {
}
public function post () {
}
}