Skip to content

Commit

Permalink
Initial checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
joostfaassen committed Jul 10, 2016
0 parents commit f1f12b4
Show file tree
Hide file tree
Showing 18 changed files with 555 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Created by http://www.gitignore.io

### Eclipse ###
*.pydevproject
.metadata
.gradle
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath

# sbteclipse plugin
.target

# TeXlipse plugin
.texlipse


### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml


### vim ###
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
*.un~
Session.vim
.netrwhist
*~

### Composer ###
vendor/
composer.lock
19 changes: 19 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Injector
========

Simple constructor and method argument injector.

You can pass a context to the injector to resolve parameters into injectable values.

## Supported contexts:

* ArrayContext: Resolves by a simple associative array's key/value
* RequestContext: Uses a PSR7 ServerRequest to resolve values from request attributes
* InteropContainerContext: Add support for DI containers implementing `Interop\Container\ContainerInterface`
* PsrContainerContext: Add support for DI containers implementing `Psr\Container\ContainerInterface`
* MultiContext: Pass an array of one or more of the above contexts to resolve from multiple contexts.

It's simple to add your own contexts by implementing `InteroPhp\Injector\Context\ContextInterface`

## Examples and usage

Please check the included `example/` directory for usage examples.

## License

MIT (see [LICENSE.md](LICENSE.md))

## Brought to you by the LinkORB Engineering team

<img src="http://www.linkorb.com/d/meta/tier1/images/linkorbengineering-logo.png" width="200px" /><br />
Check out our other projects at [linkorb.com/engineering](http://www.linkorb.com/engineering).

Btw, we're hiring!
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "interophp/injector",
"description": "Inject dependencies into constructors and methods",
"homepage": "http://www.github.com/interophp/injector",
"keywords": ["php", "injector", "inject", "injection", "dependencies", "auto-resolve", "linkorb"],
"type": "library",
"authors": [
{
"name": "LinkORB Engineering",
"email": "engineering@linkorb.com"
}
],
"require": {
"php": ">=5.5.0"
},
"require-dev": {
"acclimate/container": "~1.0",
"container-interop/container-interop": "~1.0",
"psr/http-message": "~1.0",
"zendframework/zend-diactoros": "~1.0"
},
"autoload": {
"psr-4": {
"InteroPhp\\Injector\\": "src/"
}
},
"license": "MIT"
}
15 changes: 15 additions & 0 deletions example/example1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

// Require the Composer autoloader
require __DIR__ . '/../vendor/autoload.php';

use Acclimate\Container\ArrayContainer;

$data = [
'greeting' => 'Hello world'
];

// A simple in-memory container, implementing the standard ContainerInterface
$container = new ArrayContainer($data);

echo $container->get('greeting') . PHP_EOL;
51 changes: 51 additions & 0 deletions example/example2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace InteroPhp\Injector\Test;

// Require the Composer autoloader
require __DIR__ . '/../vendor/autoload.php';

use InteroPhp\Injector\Context\ArrayContext;
use InteroPhp\Injector\Context\RequestContext;
use InteroPhp\Injector\Context\MultiContext;
use InteroPhp\Injector\Injector;

class Greeter
{
private $greeting = 'Hello';

public function __construct($greeting)
{
$this->greeting = $greeting;
}

public function greet($test__name)
{
return $this->greeting . ', ' . $test__name;
}
}


$request = (new \Zend\Diactoros\ServerRequest())
->withUri(new \Zend\Diactoros\Uri('http://example.com'))
->withMethod('GET')
->withAttribute('greeting', 'Hi');

$requestContext = new RequestContext($request);

$data = [
'color' => 'red',
'test.name' => 'Joe',
'other' => 'stuff'
];

$arrayContext = new ArrayContext($data);

$multiContext = new MultiContext([$requestContext, $arrayContext]);

$injector = new Injector();

$greeter = $injector->instantiate(Greeter::class, $multiContext);
$res = $injector->callMethod($greeter, 'greet', $multiContext);

echo $res . PHP_EOL;
59 changes: 59 additions & 0 deletions example/example3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace InteroPhp\Injector\Test;

// Require the Composer autoloader
require __DIR__ . '/../vendor/autoload.php';

use InteroPhp\Injector\Context\ArrayContext;
use InteroPhp\Injector\Injector;

class Greeter
{
private $greeting = 'Hello';

public function __construct($greeting)
{
$this->greeting = $greeting;
}

public function greet($test__name)
{
return $this->greeting . ', ' . $test__name;
}
}

class LoudGreeter
{
private $greeter;

public function __construct(Greeter $greeter)
{
$this->greeter = $greeter;
}

public function greet($test__name)
{
return strtoupper($this->greeter->greet($test__name)) . "!!!";
}
}

$injector = new Injector();

$data = [
'color' => 'red',
'test.name' => 'Joe',
'other' => 'stuff',
'greeting' => 'Hi'
];
$arrayContext = new ArrayContext($data);

$greeter = $injector->instantiate(Greeter::class, $arrayContext);

$data['InteroPhp\Injector\Test\Greeter'] = $greeter;
$arrayContext = new ArrayContext($data);
$loudGreeter = $injector->instantiate(LoudGreeter::class, $arrayContext);

$res = $injector->callMethod($loudGreeter, 'greet', $arrayContext);

echo $res . PHP_EOL;
45 changes: 45 additions & 0 deletions example/example4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace InteroPhp\Injector\Test;

// Require the Composer autoloader
require __DIR__ . '/../vendor/autoload.php';

use InteroPhp\Injector\Context\InteropContainerContext;
use InteroPhp\Injector\Injector;
use Acclimate\Container\ArrayContainer;

class Greeter
{
private $greeting = 'Hello';

public function __construct($greeting)
{
$this->greeting = $greeting;
}

public function greet($test__name)
{
return $this->greeting . ', ' . $test__name;
}
}


$data = [
'greeting' => 'Aloha',
'color' => 'red',
'test.name' => 'Joe',
'other' => 'stuff'
];

// A simple in-memory container, implementing the standard ContainerInterface
$container = new ArrayContainer($data);

$containerContext = new InteropContainerContext($container);

$injector = new Injector();

$greeter = $injector->instantiate(Greeter::class, $containerContext);
$res = $injector->callMethod($greeter, 'greet', $containerContext);

echo $res . PHP_EOL;
28 changes: 28 additions & 0 deletions src/Context/ArrayContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace InteroPhp\Injector\Context;

use InteroPhp\Injector\Exception\KeyNotFoundException;

class ArrayContext implements ContextInterface
{
protected $data;

public function __construct($data)
{
$this->data = $data;
}

public function has($key)
{
return isset($this->data[$key]);
}

public function get($key)
{
if (!$this->has($key)) {
throw new KeyNotFoundException($key);
}
return $this->data[$key];
}
}
28 changes: 28 additions & 0 deletions src/Context/BaseContainerContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace InteroPhp\Injector\Context;

use InteroPhp\Injector\Exception\KeyNotFoundException;

abstract class BaseContainerContext implements ContextInterface
{
protected $container;

public function __construct($container)
{
$this->container = $container;
}

public function has($key)
{
return $this->container->has($key);
}

public function get($key)
{
if (!$this->has($key)) {
throw new KeyNotFoundException($key);
}
return $this->container->get($key);
}
}
9 changes: 9 additions & 0 deletions src/Context/ContextInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace InteroPhp\Injector\Context;

interface ContextInterface
{
public function has($key);
public function get($key);
}
14 changes: 14 additions & 0 deletions src/Context/InteropContainerContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace InteroPhp\Injector\Context;

use InteroPhp\Injector\Exception\KeyNotFoundException;
use Interop\Container\ContainerInterface;

class InteropContainerContext extends BaseContainerContext
{
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
}

0 comments on commit f1f12b4

Please sign in to comment.