Skip to content

Commit

Permalink
refs #17 [Bind] Change key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Apr 8, 2021
1 parent 69682be commit f3f6ea0
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions src/Binding/BindingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,42 @@
class BindingServiceProvider
{
use HandlesRequestParameters;

/**
* The IoC container instance.
*
* @var Container
*/
protected Container $container;

/**
* The registered parameter binders.
*
* @var array
*/
protected array $binders = [];

/**
* The registered binding targets.
*
* @var array
*/
protected array $scopes = [];

/**
* The registered procedure method parameters.
*
* @var array
*/
protected array $procedureMethodParams = [];

/**
* The registered request parameters.
*
* @var array
*/
protected array $requestParameters = [];

/**
* Create a new instance.
*
Expand All @@ -63,7 +63,7 @@ public function __construct(Container $container = null)
{
$this->container = $container ?: new Container;
}

/**
* Register a model binder for a request parameter.
*
Expand Down Expand Up @@ -102,7 +102,7 @@ public function model(
$procedureMethodParam
);
}

/**
* Register a custom binder for a request parameter.
*
Expand Down Expand Up @@ -147,29 +147,25 @@ public function bind(
$this->procedureMethodParams[$key] = $procedureMethodParam;
$this->requestParameters[$key] = $requestParam;
}

/**
* Makes a key to be used with the arrays containing the bindings and related configuration.
*
* @param string|array $requestParam The parameter in the RPC request to bind for.
* @param string|callable|string[]|callable[] $scope See the `$bind` parameter of {@see bind()}.
* @param string $procedureMethodParam The parameter of the Procedure method to bind
* @param string|null $procedureMethod The parameter of the Procedure method to bind
* for.
*
* @return string
* @throws \JsonException
*/
private function makeKey($requestParam, $scope, $procedureMethodParam)
private function makeKey($requestParam, $scope, ?string $procedureMethod): string
{
if (is_array($requestParam) || is_object($requestParam)) {
$requestParam = json_encode($requestParam, JSON_THROW_ON_ERROR);
}
if (is_array($scope) || is_object($scope)) {
$scope = json_encode($scope, JSON_THROW_ON_ERROR);
}
return sha1($requestParam . $scope . $procedureMethodParam);
$json = json_encode([$requestParam, $scope, $procedureMethod], JSON_THROW_ON_ERROR);

return sha1($json);
}

/**
* Resolves the bound instance for a Procedure method parameter.
*
Expand Down Expand Up @@ -197,7 +193,7 @@ public function resolveInstance($requestParameters, $targetParam, $targetCallabl
throw new BindingResolutionException('Failed to perform binding resolution.', -32003, $e);
}
}

/**
* Finds the key used with the arrays for a specific Procedure method parameter.
*
Expand All @@ -214,7 +210,7 @@ public function findKey($targetParam, $targetCallable = '')
if ($boundProcedureMethodParam !== $targetParam) {
continue;
}

$maybeBoundScope = $this->scopes[$key];
if (!is_array($maybeBoundScope)) {
$maybeBoundScope = [$maybeBoundScope];
Expand All @@ -227,7 +223,7 @@ public function findKey($targetParam, $targetCallable = '')
}
return false;
}

/**
* Checks if a binding target scope contains an other, typically a specific method.
*
Expand Down Expand Up @@ -256,10 +252,10 @@ protected static function doesCallableContain($container, $contained)
$container = Str::parseCallback($container);
return $container === $contained;
}

$container = static::preparescopeForComparision($container);
$contained = static::preparescopeForComparision($contained);

if (false===$container || false===$contained) {
return false;
}
Expand All @@ -273,7 +269,7 @@ protected static function doesCallableContain($container, $contained)
}
return true;
}

/**
* Turns callable arrays and callable strings into arrays for comparison.
*
Expand All @@ -299,7 +295,7 @@ private static function preparescopeForComparision($scope)
$scope = preg_split('/[@\\\]/', $scope);
return $scope;
}

/**
* Call the binding callback for the given key.
*
Expand Down

1 comment on commit f3f6ea0

@BenceSzalai
Copy link

@BenceSzalai BenceSzalai commented on f3f6ea0 Apr 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it's my bad, that I've used $procedureMethodParam in makeKey while expecting it to be set. But the solution is not to allow null, but to move this part:

        if (is_null($procedureMethodParam)) {
            $procedureMethodParam = is_array($requestParam) ? end($requestParam) : $requestParam;
            $procedureMethodParam = explode(':', $procedureMethodParam)[0];
        }

to the top of the bind() method.

EDIT: well, in fact it makes no difference, this way it is the same too...

Please sign in to comment.