|
| 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 | +} |
0 commit comments