binding refactoring

This commit is contained in:
Yuri Kuznetsov
2021-04-07 15:34:42 +03:00
parent c6f95a9030
commit 1a04dec97b
11 changed files with 70 additions and 35 deletions

View File

@@ -46,7 +46,7 @@ class Binder
* @param $key An interface or interface with a parameter name (`Interface $name`).
* @param $implementationClassName An implementation class name.
*/
public function bindImplementation(string $key, string $implementationClassName) : self
public function bindImplementation(string $key, string $implementationClassName): self
{
if (!$key || $key[0] === '$') {
throw new LogicException("Can't binding a parameter name globally.");
@@ -66,7 +66,7 @@ class Binder
* @param $key An interface or interface with a parameter name (`Interface $name`).
* @param $serviceName A service name.
*/
public function bindService(string $key, string $serviceName) : self
public function bindService(string $key, string $serviceName): self
{
if (!$key || $key[0] === '$') {
throw new LogicException("Can't binding a parameter name globally.");
@@ -86,7 +86,7 @@ class Binder
* @param $key An interface or interface with a parameter name (`Interface $name`).
* @param $callback A callback that will resolve a dependency.
*/
public function bindCallback(string $key, callable $callback) : self
public function bindCallback(string $key, callable $callback): self
{
if (!$key || $key[0] === '$') {
throw new LogicException("Can't binding a parameter name globally.");
@@ -105,7 +105,7 @@ class Binder
*
* @param $className A context.
*/
public function for(string $className) : ContextualBinder
public function for(string $className): ContextualBinder
{
return new ContextualBinder($this->data, $className);
}

View File

@@ -51,7 +51,7 @@ class Binding
$this->value = $value;
}
public function getType() : int
public function getType(): int
{
return $this->type;
}
@@ -61,7 +61,7 @@ class Binding
return $this->value;
}
public static function createFromImplementationClassName(string $implementationClassName) : self
public static function createFromImplementationClassName(string $implementationClassName): self
{
if (!$implementationClassName) {
throw new LogicException("Bad binding.");
@@ -70,7 +70,7 @@ class Binding
return new self(self::IMPLEMENTATION_CLASS_NAME, $implementationClassName);
}
public static function createFromServiceName(string $serviceName) : self
public static function createFromServiceName(string $serviceName): self
{
if (!$serviceName) {
throw new LogicException("Bad binding.");
@@ -79,12 +79,12 @@ class Binding
return new self(self::CONTAINER_SERVICE, $serviceName);
}
public static function createFromValue($value) : self
public static function createFromValue($value): self
{
return new self(self::VALUE, $value);
}
public static function createFromCallback(callable $callback) : self
public static function createFromCallback(callable $callback): self
{
return new self(self::CALLBACK, $callback);
}

View File

@@ -35,14 +35,14 @@ use LogicException;
class BindingContainer
{
protected $data;
private $data;
public function __construct(BindingLoader $loader)
public function __construct(BindingData $data)
{
$this->data = $loader->load();
$this->data = $data;
}
public function has(?ReflectionClass $class, ReflectionParameter $param) : bool
public function has(?ReflectionClass $class, ReflectionParameter $param): bool
{
if ($this->getInternal($class, $param) === null) {
return false;
@@ -51,7 +51,7 @@ class BindingContainer
return true;
}
public function get(?ReflectionClass $class, ReflectionParameter $param) : Binding
public function get(?ReflectionClass $class, ReflectionParameter $param): Binding
{
if (!$this->has($class, $param)) {
throw new LogicException("BindingContainer: Can't get not existing binding.");
@@ -60,7 +60,7 @@ class BindingContainer
return $this->getInternal($class, $param);
}
protected function getInternal(?ReflectionClass $class, ReflectionParameter $param) : ?Binding
private function getInternal(?ReflectionClass $class, ReflectionParameter $param): ?Binding
{
$className = null;

View File

@@ -52,12 +52,12 @@ class BindingData
$this->context->$className->$key = $binding;
}
public function addGlobal(string $key, Binding $binding)
public function addGlobal(string $key, Binding $binding): void
{
$this->global->$key = $binding;
}
public function hasContext(string $className, string $key) : bool
public function hasContext(string $className, string $key): bool
{
if (!property_exists($this->context, $className)) {
return false;
@@ -70,7 +70,7 @@ class BindingData
return true;
}
public function getContext(string $className, string $key) : Binding
public function getContext(string $className, string $key): Binding
{
if (!$this->hasContext($className, $key)) {
throw new LogicException("No data.");
@@ -79,7 +79,7 @@ class BindingData
return $this->context->$className->$key;
}
public function hasGlobal(string $key) : bool
public function hasGlobal(string $key): bool
{
if (!property_exists($this->global, $key)) {
return false;
@@ -88,7 +88,7 @@ class BindingData
return true;
}
public function getGlobal(string $key) : Binding
public function getGlobal(string $key): Binding
{
if (!$this->hasGlobal($key)) {
throw new LogicException("No data.");
@@ -97,21 +97,21 @@ class BindingData
return $this->global->$key;
}
public function getGlobalKeyList() : array
public function getGlobalKeyList(): array
{
return array_keys(
get_object_vars($this->global)
);
}
public function getContextList() : array
public function getContextList(): array
{
return array_keys(
get_object_vars($this->context)
);
}
public function getContextKeyList(string $context) : array
public function getContextKeyList(string $context): array
{
return array_keys(
get_object_vars($this->context->$context ?? (object) [])

View File

@@ -31,5 +31,5 @@ namespace Espo\Core\Binding;
interface BindingLoader
{
public function load() : BindingData;
public function load(): BindingData;
}

View File

@@ -0,0 +1,35 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Binding;
interface BindingProcessor
{
public function process(Binder $binder): void;
}

View File

@@ -49,7 +49,7 @@ class ContextualBinder
* @param $key An interface or interface with a parameter name (`Interface $name`).
* @param $implementationClassName An implementation class name.
*/
public function bindImplementation(string $key, string $implementationClassName) : self
public function bindImplementation(string $key, string $implementationClassName): self
{
if (!$key || $key[0] === '$') {
throw new LogicException("Bad binding.");
@@ -70,7 +70,7 @@ class ContextualBinder
* @param $key An interface, parameter name (`$name`) or interface with a parameter name (`Interface $name`).
* @param $serviceName A service name.
*/
public function bindService(string $key, string $serviceName) : self
public function bindService(string $key, string $serviceName): self
{
if (!$key || $key[0] === '$') {
throw new LogicException("Bad binding.");
@@ -91,7 +91,7 @@ class ContextualBinder
* @param $key An interface, parameter name (`$name`) or interface with a parameter name (`Interface $name`).
* @param $value A value of any type.
*/
public function bindValue(string $key, $value) : self
public function bindValue(string $key, $value): self
{
$this->data->addContext(
$this->className,
@@ -108,7 +108,7 @@ class ContextualBinder
* @param $key An interface, parameter name (`$name`) or interface with a parameter name (`Interface $name`).
* @param $callback A callback that will resolve a dependency.
*/
public function bindCallback(string $key, callable $callback) : self
public function bindCallback(string $key, callable $callback): self
{
$this->data->addContext(
$this->className,

View File

@@ -29,9 +29,9 @@
namespace Espo\Core\Binding;
class DefaultBinding
class DefaultBinding implements BindingProcessor
{
public function process(Binder $binder)
public function process(Binder $binder): void
{
$binder->bindService(
'Espo\\Core\\InjectableFactory',

View File

@@ -42,7 +42,7 @@ class EspoBindingLoader implements BindingLoader
$this->moduleNameList = $module->getOrderedList();
}
public function load() : BindingData
public function load(): BindingData
{
$data = new BindingData();
@@ -59,7 +59,7 @@ class EspoBindingLoader implements BindingLoader
return $data;
}
private function loadModule(Binder $binder, string $moduleName)
private function loadModule(Binder $binder, string $moduleName): void
{
$className = 'Espo\\Modules\\' . $moduleName . '\\Binding';
@@ -70,7 +70,7 @@ class EspoBindingLoader implements BindingLoader
(new $className())->process($binder);
}
private function loadCustom(Binder $binder)
private function loadCustom(Binder $binder): void
{
$className = 'Espo\\Custom\\Binding';

View File

@@ -160,7 +160,7 @@ class ContainerBuilder
)
);
$bindingContainer = new BindingContainer($bindingLoader);
$bindingContainer = new BindingContainer($bindingLoader->load());
return new $this->containerClassName(
$this->containerConfigurationClassName, $this->loaderClassNames, $this->services, $bindingContainer

View File

@@ -116,7 +116,7 @@ class BindingContainerTest extends \PHPUnit\Framework\TestCase
protected function createContainer()
{
return new BindingContainer($this->loader);
return new BindingContainer($this->loader->load());
}
public function testHasTrue()