Skip to content

Commit

Permalink
feat(proxy): allow setting a proxy in setup
Browse files Browse the repository at this point in the history
#3
The proxy wraps the entity instance
  • Loading branch information
rahuljayaraman committed Aug 4, 2017
1 parent a2ffb53 commit e8cb678
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/FieldResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,34 @@
namespace RahulJayaraman\DoctrineGraphQL;

class FieldResolver {
private $originalInstance;
private $instance;
private $resolver;

public function __construct($instance, \ReflectionMethod $resolver = null)
public function __construct(
$instance,
\ReflectionMethod $resolver = null,
Callable $proxy
)
{
$this->instance = $instance;
$this->originalInstance = $instance;
$this->resolver = $resolver;
$this->instance = $proxy($instance);
}

public function resolve($key, $args = [])
{
if (isset($this->resolver)) {
return $this->resolver->invokeArgs($this->instance,
array_values($args));
$methodName = $this->resolver->getName();
return call_user_func_array(array($this->instance, $methodName), $args);
}
return $this->getDefaultGraphQLResolver($key);
}

private function getDefaultGraphQLResolver($key)
{
$fn = "get". $this->camelCase($key);
if (method_exists($this->instance, $fn)) {
if (method_exists($this->originalInstance, $fn)) {
return $this->instance->$fn();
}
return null;
Expand Down
20 changes: 17 additions & 3 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ class Mapper {
*/
private static $lookUp;

/**
* It wraps the instance & returns a new instance
* Could be used for logging/authorization etc.
*
* @var callable
*/
private static $proxy;

/**
* className
* package\className of doctrine entity to be mapped
Expand Down Expand Up @@ -70,12 +78,17 @@ public static function setup(
EntityManager $em,
Callable $register,
Callable $lookUp,
Callable $proxy = null,
$cachedAnnotationReader = null
)
{
$identity = function ($arg) {
return $arg;
};
self::$em = $em;
self::$register = $register;
self::$lookUp = $lookUp;
self::$proxy = isset($proxy) ? $proxy : $identity;
self::$cachedAnnotationReader = $cachedAnnotationReader;
self::setupAnnotations();
}
Expand Down Expand Up @@ -526,9 +539,10 @@ private function buildField($key,
$description = '')
{
//Eval is used to resolve custom scalar types like date
$resolveFactory = function ($key, $eval) use ($resolver) {
return function ($val, $args) use ($key, $eval, $resolver) {
$fieldResolver = new FieldResolver($val, $resolver);
$proxy = self::$proxy;
$resolveFactory = function ($key, $eval) use ($resolver, $proxy) {
return function ($val, $args) use ($key, $eval, $resolver, $proxy) {
$fieldResolver = new FieldResolver($val, $resolver, $proxy);
$result = $fieldResolver->resolve($key, $args);
if (!$result instanceof PersistentCollection) {
return $eval($result);
Expand Down

0 comments on commit e8cb678

Please sign in to comment.