Skip to content

Commit c338ff5

Browse files
committed
Initial Implementation
Signed-off-by: RJ Garcia <[email protected]>
0 parents  commit c338ff5

File tree

9 files changed

+370
-0
lines changed

9 files changed

+370
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
/playground.php
3+
/composer.lock

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
5+
and this project adheres to [Semantic Versioning](http://semver.org/).
6+
7+
## [Unreleased]
8+
### Added
9+
10+
- Initial Implementation
11+
- Added Documentation

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Bighead Technology 2017
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.PHONY: test
2+
3+
test:
4+
./vendor/bin/peridot test

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Auto Args
2+
3+
Auto Args provides a system for automatically resolving arguments for any callable. This can also be referred to as Auto wiring.
4+
5+
## Installation
6+
7+
Install with composer at `krak/auto-args`.
8+
9+
## Usage
10+
11+
```php
12+
<?php
13+
14+
use Krak\AutoArgs;
15+
16+
$args = new AutoArgs();
17+
18+
$context = [
19+
'vars' => ['a' => 1],
20+
'objects' => [new SplStack()],
21+
];
22+
23+
$func = function($a, SplDoublyLinkedList $stack, $b = 1) {
24+
assert($a == 1 && $b === 1);
25+
};
26+
27+
$args->invoke($func, $context);
28+
```
29+
30+
### Pimple Integration
31+
32+
```php
33+
<?php
34+
35+
use Krak\AutoArgs;
36+
37+
$args = new AutoArgs();
38+
39+
$context = [
40+
'pimple' => new \Pimple\Container([
41+
AutoArgs\AutoArgs::class => function() {
42+
return $this->aa;
43+
}
44+
])
45+
];
46+
47+
$func = function(\Pimple\Container $container, AutoArgs\AutoArgs $aa) {
48+
49+
};
50+
51+
$args->invoke($func, $context);
52+
```
53+
54+
## API
55+
56+
### Class AutoArgs
57+
58+
#### \_\_construct($resolve_arg = null)
59+
60+
Accepts an argument resolver which will accept Argument metadata and context and return the proper argument for it. If none is supplied, the default stack is created and composed instead.
61+
62+
#### mixed invoke(callable $callable, array $context)
63+
64+
Invokes a callable and resolves the arguments from the argument resolver and given context.
65+
66+
#### array resolveArguments(callable $callable, array $context)
67+
68+
Returns the array of resolved arguments for the given callable. An exception will be thrown if no argument was able to be resolved.
69+
70+
#### Krak\\Mw\\MwStack ::createStack()
71+
72+
Returns a configured instance of an mw stack.

composer.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "krak/auto-args",
3+
"description": "Auto Argument Resolver",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords": ["auto args", "auto wiring", "argument resolver"],
7+
"authors": [
8+
{
9+
"name": "RJ Garcia",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require-dev": {
14+
"peridot-php/peridot": "^1.18",
15+
"pimple/pimple": "^3.0",
16+
"symfony/var-dumper": "^3.2"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Krak\\AutoArgs\\": "src"
21+
},
22+
"files": ["src/resolve-argument.php"]
23+
},
24+
"require": {
25+
"krak/mw": "^0.4.0"
26+
}
27+
}

src/AutoArgs.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Krak\AutoArgs;
4+
5+
use Krak\Mw;
6+
7+
class AutoArgs
8+
{
9+
private $resolve_arg;
10+
11+
public function __construct(callable $resolve_arg = null) {
12+
$this->resolve_arg = $resolve_arg ?: self::createStack()->compose(function() {
13+
return [];
14+
});
15+
}
16+
17+
public function invoke(callable $callable, array $context) {
18+
$args = $this->resolveArguments($callable, $context);
19+
return call_user_func_array($callable, $args);
20+
}
21+
22+
/** resolves the arguments for the callable and returns the array of args */
23+
public function resolveArguments(callable $callable, array $context) {
24+
$resolve_arg = $this->resolve_arg;
25+
26+
$rf = $this->callableToReflectionFunctionAbstract($callable);
27+
28+
$args = [];
29+
foreach ($rf->getParameters() as $i => $arg_meta) {
30+
$arg = $resolve_arg($arg_meta, $context);
31+
if (!count($arg) && $arg_meta->isOptional()) {
32+
continue;
33+
} else if (!count($arg)) {
34+
throw new \RuntimeException(sprintf('Action argument %d is unable to be resolved.', $i));
35+
}
36+
37+
$args[] = $arg[0];
38+
}
39+
40+
return $args;
41+
}
42+
43+
public function callableToReflectionFunctionAbstract(callable $callable) {
44+
if (is_array($callable)) {
45+
return new \ReflectionMethod($callable[0], $callable[1]);
46+
} else if (is_object($callable) && method_exists($callable, '__invoke')) {
47+
return new \ReflectionMethod($callable, '__invoke');
48+
} else if (is_string($callable) && strpos($callable, '::') !== false) {
49+
list($class, $method) = explode('::', $callable);
50+
return new \ReflectionMethod($class, $method);
51+
} else {
52+
return new \ReflectionFunction($callable);
53+
}
54+
}
55+
56+
public static function createStack() {
57+
return mw\stack('Resolve Argument')
58+
->push(defaultValueResolveArgument(), -1, 'defaultValue')
59+
->push(varNameResolveArgument(), 0, 'varName')
60+
->push(subclassOfResolveArgument(), 0, 'subclassOf')
61+
->push(pimpleContainerResolveArgument(), 0, 'pimpleContainer');
62+
}
63+
}

src/resolve-argument.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Krak\AutoArgs;
4+
5+
use Krak\Mw,
6+
ReflectionParameter;
7+
8+
/** array resolveArgument(ReflectionParameter, array);
9+
array resolveArgumentMiddleware(ReflectParameter, array, callable $next)
10+
*/
11+
12+
function _isSubclassOf($parent, $child) {
13+
return $parent == $child || is_subclass_of($child, $parent);
14+
}
15+
16+
function hasKeyResolveArgument($key, $mw) {
17+
return mw\filter($mw, function(ReflectionParameter $arg_meta, array $context) use ($key) {
18+
return array_key_exists($key, $context);
19+
});
20+
}
21+
22+
/** Resolves an argument if the context contains a key that matches the var name */
23+
function varNameResolveArgument($key = 'vars') {
24+
return hasKeyResolveArgument($key, function(ReflectionParameter $arg_meta, array $context, $next) use ($key) {
25+
$vars = $context[$key];
26+
27+
if (array_key_exists($arg_meta->getName(), $vars)) {
28+
return [$vars[$arg_meta->getName()]];
29+
}
30+
31+
return $next($arg_meta, $context);
32+
});
33+
}
34+
35+
/** Resolves the argument if is a subclass of */
36+
function subclassOfResolveArgument($key = 'objects') {
37+
return hasKeyResolveArgument($key, function(ReflectionParameter $arg_meta, array $context, $next) use ($key) {
38+
if (!$arg_meta->getClass()) {
39+
return $next($arg_meta, $context);
40+
}
41+
42+
$class = $arg_meta->getClass();
43+
$objects = $context[$key];
44+
45+
foreach ($objects as $obj) {
46+
if (_isSubclassOf($arg_meta->getClass()->getName(), get_class($obj))) {
47+
return [$obj];
48+
}
49+
}
50+
51+
return $next($arg_meta, $context);
52+
});
53+
}
54+
55+
/** Resolves the argument as the given default value if supplied */
56+
function defaultValueResolveArgument() {
57+
return function(ReflectionParameter $arg_meta, array $context, $next) {
58+
if (!$arg_meta->isOptional()) {
59+
return $next($arg_meta, $context);
60+
}
61+
if (!$arg_meta->isDefaultValueAvailable()) {
62+
return [];
63+
}
64+
65+
return [$arg_meta->getDefaultValue()];
66+
};
67+
}
68+
69+
function pimpleContainerResolveArgument($key = 'pimple') {
70+
return hasKeyResolveArgument($key, function(ReflectionParameter $arg_meta, array $context, $next) use ($key) {
71+
if (!$arg_meta->getClass()) {
72+
return $next($arg_meta, $context);
73+
}
74+
75+
$class = $arg_meta->getClass();
76+
$container = $context[$key];
77+
78+
if (!$container instanceof \Pimple\Container) {
79+
throw new \LogicException('Expected Pimple\Container instance');
80+
}
81+
82+
if (isset($container[$class->getName()])) {
83+
return [$container[$class->getName()]];
84+
}
85+
if (_isSubclassOf(\Pimple\Container::class, $class->getName())) {
86+
return [$container];
87+
}
88+
89+
return $next($arg_meta, $context);
90+
});
91+
}

test/auto-args.spec.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
use Krak\AutoArgs;
4+
5+
class Invokeable {
6+
public function __invoke() {}
7+
}
8+
9+
class StaticMethodClass {
10+
public static function staticMethod() {}
11+
}
12+
13+
describe('Krak AutoArgs', function() {
14+
describe('AutoArgs', function() {
15+
beforeEach(function() {
16+
$this->aa = new AutoArgs\AutoArgs();
17+
});
18+
describe('->callableToReflectionFunctionAbstract()', function() {
19+
$params = [
20+
['converts a normal string callable', 'array_push'],
21+
['converts a Closure', function() {}],
22+
['converts an ($object,$method) tuple', [new SplStack, 'push']],
23+
['converts static method', 'StaticMethodClass::staticMethod'],
24+
['converts an invokeable object', new Invokeable()],
25+
];
26+
27+
foreach ($params as list($msg, $callable)) {
28+
it($msg, function() use ($callable) {
29+
$rf = $this->aa->callableToReflectionFunctionAbstract($callable);
30+
assert($rf instanceof ReflectionFunctionAbstract);
31+
});
32+
}
33+
});
34+
describe('->resolveArguments()', function() {
35+
it('returns an array of arguments from the argument resolver', function() {
36+
$aa = new AutoArgs\AutoArgs(function() {
37+
return [1];
38+
});
39+
40+
$func = function($a, $b) {
41+
assert($a === $b && $a === 1);
42+
};
43+
44+
$aa->resolveArguments($func, []);
45+
});
46+
it('throws an exception if no args where resolved', function() {
47+
$aa = new AutoArgs\AutoArgs(function() {
48+
return [];
49+
});
50+
51+
$func = function($a, $b) {};
52+
53+
try {
54+
$aa->resolveArguments($func, []);
55+
assert(false);
56+
} catch (RuntimeException $e) {
57+
assert(true);
58+
}
59+
});
60+
it('resolves arguments with context', function() {
61+
$context = [
62+
'vars' => ['a' => 1, 'stack' => 2],
63+
'objects' => [new SplStack()],
64+
'pimple' => new \Pimple\Container([
65+
AutoArgs\AutoArgs::class => function() {
66+
return $this->aa;
67+
}
68+
])
69+
];
70+
71+
$func = function($a, SplDoublyLinkedList $stack, \Pimple\Container $container, AutoArgs\AutoArgs $aa, $b = 1) {
72+
assert($a == 1 && $b === 1);
73+
};
74+
75+
$this->aa->invoke($func, $context);
76+
});
77+
});
78+
});
79+
});

0 commit comments

Comments
 (0)