1. Go to this page and download the library: Download cmpayments/atreyu 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');
/* Start to develop here. Best regards https://php-download.com/ */
cmpayments / atreyu example snippets
$injector = new Atreyu\Injector;
$injector = new Atreyu\Injector;
$obj1 = new SomeNamespace\MyClass;
$obj2 = $injector->make('SomeNamespace\MyClass');
var_dump($obj2 instanceof SomeNamespace\MyClass); // true
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 Atreyu\Injector;
$myObj = $injector->make('MyClass');
var_dump($myObj->dep1 instanceof SomeDependency); // true
var_dump($myObj->dep2 instanceof AnotherDependency); // true
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 Atreyu\Injector;
$car = $injector->make('Car');
var_dump($car instanceof Car); // true
interface Engine {}
class V8 implements Engine {}
class Car {
private $engine;
public function __construct(Engine $engine) {
$this->engine = $engine;
}
}
class MyClass {
private $arg1;
private $arg2;
public function __construct(SomeConcreteClass $arg1, SomeInterface $arg2) {
$this->arg1 = $arg1;
$this->arg2 = $arg2;
}
}
$injector = new Atreyu\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 Atreyu\Injector;
$dependencyInstance = new SomeImplementation;
$injector->define('MyClass', [':dependency' => $dependencyInstance]);
$myObj = $injector->make('MyClass');
var_dump($myObj instanceof MyClass); // true
interface SomeInterface {}
class SomeImplementationClass implements SomeInterface {}
class MyClass {
private $dependency;
public function __construct(SomeInterface $dependency) {
$this->dependency = $dependency;
}
}
$injector = new Atreyu\Injector;
$myObj = $injector->make('MyClass', ['dependency' => 'SomeImplementation']);
var_dump($myObj instanceof MyClass); // true
interface Engine {}
class V8 implements Engine {}
class Car {
private $engine;
public function __construct(Engine $engine) {
$this->engine = $engine;
}
}
$injector = new Atreyu\Injector;
// Tell the Injector class to inject an instance of V8 any time
// it encounters an Engine type-hint
$injector->alias('Engine', 'V8');
$car = $injector->make('Car');
var_dump($car instanceof Car); // bool(true)
$myUniversalValue = 42;
class MyClass {
public $myValue;
public function __construct($myValue) {
$this->myValue = $myValue;
}
}
$injector = new Atreyu\Injector;
$injector->defineParam('myValue', $myUniversalValue);
$obj = $injector->make('MyClass');
var_dump($obj->myValue === 42); // bool(true)
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 Atreyu\Injector;
$injector->share($db);
$myController = $injector->make('MyController');
class Person {
public $name = 'John Snow';
}
$injector = new Atreyu\Injector;
$injector->share('Person');
$person = $injector->make('Person');
var_dump($person->name); // John Snow
$person->name = 'Arya Stark';
$anotherPerson = $injector->make('Person');
var_dump($anotherPerson->name); // Arya Stark
var_dump($person === $anotherPerson); // bool(true) because it's the same instance!
class MyComplexClass {
public $verification = false;
public function doSomethingAfterInstantiation() {
$this->verification = true;
}
}
$complexClassFactory = function() {
$obj = new MyComplexClass;
$obj->doSomethingAfterInstantiation();
return $obj;
};
$injector = new Atreyu\Injector;
$injector->delegate('MyComplexClass', $complexClassFactory);
$obj = $injector->make('MyComplexClass');
var_dump($obj->verification); // bool(true)
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;
}
}
// Works because MyFactory specifies a magic __invoke method
$injector->delegate('SomeClassWithDelegatedInstantiation', 'MyFactory');
$obj = $injector->make('SomeClassWithDelegatedInstantiation');
var_dump($obj->value); // int(1)
// This also works
$injector->delegate('SomeClassWithDelegatedInstantiation', 'MyFactory::factoryMethod');
$obj = $injector->make('SomeClassWithDelegatedInstantiation');
$obj = $injector->make('SomeClassWithDelegatedInstantiation');
var_dump($obj->value); // int(2)
class Dependency {}
class AnotherDependency {}
class Example {
function __construct(Dependency $dep){}
function myMethod(AnotherDependency $arg1, $arg2) {
return $arg2;
}
}
$injector = new Atreyu\Injector;
// outputs: int(42)
var_dump($injector->execute('Example::myMethod', $args = [':arg2' => 42]));
namespace Atreyu\Test;
use Atreyu\Injector;
interface DepInterface
{
}
class TestDependency
{
public $testProp = 'testVal';
}
class TestDependency2 extends TestDependency implements DepInterface
{
public $testProp = 'testVal2';
}
class TestMultiDepsNeeded
{
public function __construct(TestDependency $val1, DepInterface $val2)
{
$this->testDep = $val1;
$this->testDep2 = $val2;
}
}
class TestMultiDepsNeededExample2
{
/**
* TestMultiDepsNeededExample2 constructor.
*
* @param TestDependency $val1
* @param DepInterface $val2
*
* @throws \Exception
*/
public function __construct($val1, $val2)
{
if (!($val1 instanceof TestDependency)) {
throw new \Exception('param $val1 does not meet
// try instantiate class 'TestMultiDepsNeeded'
$injector = new Injector;
$injected = $injector->make('Atreyu\Test\TestMultiDepsNeeded', [new TestDependency2()]);
// fails ...
// try instantiate class 'TestMultiDepsNeeded'
$injector = new Injector;
$injected = $injector->make('Atreyu\Test\TestMultiDepsNeeded', [new TestDependency2()]);
// works now!
// try instantiate class 'TestMultiDepsNeededExample2'
$injector = new Injector;
$injected = $injector->make('Atreyu\Test\TestMultiDepsNeededExample2', [new TestDependency2()]);
// also works now!
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;
}
// more data mapper methods here ...
}
class PersonMapper {
private $pdo;
public function __construct(PDO $pdo) {
$this->pdo = $pdo;
}
// data mapper methods here
}
class SomeService {
private $houseMapper;
private $personMapper;
public function __construct(HouseMapper $hm, PersonMapper $pm) {
$this->houseMapper = $hm;
$this->personMapper = $pm;
}
public function doSomething() {
// do something with the mappers
}
}
$pdo = new PDO('sqlite:some_sqlite_file.db');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$injector = new Atreyu\Injector;
$injector->share($pdo);
$mapper = $injector->make('SomeService');
class WidgetController {
private $request;
private $mapper;
public function __construct(Request $request, WidgetDataMapper $mapper) {
$this->request = $request;
$this->mapper = $mapper;
}
public function get() {
// do something for HTTP GET requests
}
public function post() {
// do something for HTTP POST requests
}
}