Skip to content

Commit

Permalink
Adding a makeCallable() method to help make any string callable.
Browse files Browse the repository at this point in the history
  • Loading branch information
brentscheffler committed Oct 13, 2020
1 parent 345aa7d commit 3417c41
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Resolve.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,41 @@ public function getCallableArguments(callable $callable, array $parameters = [])
);
}

/**
* Try to make something callable.
*
* Supports:
* - Full\Qualified\Namespace\Classname@Method
* - Full\Qualified\Namespace\Classname (if class has __invoke() method.)
*
* @param string|callable $callable
* @return callable
*/
public function makeCallable($callable): callable
{
if( \is_callable($callable) ){
return $callable;
}

if( \preg_match("/^(.+)@(.+)$/", $callable, $match) ){
return [
$this->make($match[1]),
$match[2]
];
}

if( \is_string($callable) &&
\class_exists($callable) ){
$invokable = $this->make($callable);

if( \is_callable($invokable) ){
return $invokable;
}
}

throw new CallableResolutionException("Resolve does not have support for this type of callable.");
}

/**
* Resolve parameters.
*
Expand Down

0 comments on commit 3417c41

Please sign in to comment.