formula refactoring

This commit is contained in:
Yuri Kuznetsov
2020-07-14 15:03:21 +03:00
parent c6293580d9
commit d9752ed970
7 changed files with 162 additions and 161 deletions

View File

@@ -41,7 +41,7 @@ class Evaluator
{
private $functionFactory = null;
private $formula;
private $processor;
private $parser;
@@ -49,12 +49,9 @@ class Evaluator
private $parsedHash;
public function __construct(
?InjectableFactory $injectableFactory = null, array $functionClassNameMap = [], array $parsedHash = []
) {
public function __construct(InjectableFactory $injectableFactory, array $functionClassNameMap = [], array $parsedHash = []) {
$this->attributeFetcher = new AttributeFetcher();
$this->functionFactory = new FunctionFactory($injectableFactory, $this->attributeFetcher, $functionClassNameMap);
$this->formula = new Formula($this->functionFactory);
$this->processor = new Processor($injectableFactory, $this->attributeFetcher, $functionClassNameMap);
$this->parser = new Parser();
$this->parsedHash = [];
}
@@ -68,7 +65,7 @@ class Evaluator
$item = $this->parsedHash[$expression];
}
$result = $this->formula->process($item, $entity, $variables);
$result = $this->processor->process($item, $entity, $variables);
$this->attributeFetcher->resetRuntimeCache();

View File

@@ -38,6 +38,8 @@ use StdClass;
class FunctionFactory
{
private $processor;
private $injectableFactory;
private $attributeFetcher;
@@ -45,8 +47,12 @@ class FunctionFactory
private $classNameMap;
public function __construct(
InjectableFactory $injectableFactory, AttributeFetcher $attributeFetcher, ?array $classNameMap = null
Processor $processor,
InjectableFactory $injectableFactory,
AttributeFetcher $attributeFetcher,
?array $classNameMap = null
) {
$this->processor = $processor;
$this->injectableFactory = $injectableFactory;
$this->attributeFetcher = $attributeFetcher;
$this->classNameMap = $classNameMap;
@@ -83,7 +89,7 @@ class FunctionFactory
}
$object = $this->injectableFactory->createWith($className, [
'itemFactory' => $this,
'processor' => $this->processor,
'entity' => $entity,
'variables' => $variables,
]);

View File

@@ -34,6 +34,7 @@ use Espo\Core\Interfaces\Injectable;
use Espo\ORM\Entity;
use Espo\Core\Formula\FunctionFactory;
use Espo\Core\Formula\Processor;
use StdClass;
@@ -71,9 +72,9 @@ abstract class Base extends FunctionBase implements Injectable
return $this->dependencyList;
}
public function __construct(FunctionFactory $itemFactory, ?Entity $entity = null, ?StdClass $variables = null)
public function __construct(Processor $processor, ?Entity $entity = null, ?StdClass $variables = null)
{
parent::__construct($itemFactory, $entity, $variables);
parent::__construct($processor, $entity, $variables);
$this->init();
}

View File

@@ -29,17 +29,17 @@
namespace Espo\Core\Formula\Functions;
use Espo\ORM\Entity;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\FunctionFactory;
use Espo\ORM\Entity;
use Espo\Core\Formula\Processor;
use StdClass;
abstract class FunctionBase
{
protected $itemFactory;
protected $processor;
private $entity;
@@ -58,22 +58,16 @@ abstract class FunctionBase
return $this->entity;
}
public function __construct(FunctionFactory $itemFactory, ?Entity $entity = null, ?StdClass $variables = null)
public function __construct(Processor $processor, ?Entity $entity = null, ?StdClass $variables = null)
{
$this->itemFactory = $itemFactory;
$this->processor = $processor;
$this->entity = $entity;
$this->variables = $variables;
}
protected function getFactory() : FunctionFactory
{
return $this->itemFactory;
}
protected function evaluate(StdClass $item)
{
$function = $this->getFactory()->create($item, $this->entity, $this->variables);
return $function->process($item);
return $this->processor->process($item, $this->entity, $this->variables);
}
public abstract function process(StdClass $item);

View File

@@ -39,7 +39,7 @@ use Espo\Core\ORM\Entity;
use StdClass;
/**
* The entry point for formula execution.
* An access point for the formula functionality.
*/
class Manager
{

View File

@@ -31,22 +31,27 @@ namespace Espo\Core\Formula;
use Espo\ORM\Entity;
use Espo\Core\InjectableFactory;
use StdClass;
class Formula
class Processor
{
private $functionFactory;
public function __construct(FunctionFactory $functionFactory)
public function __construct(InjectableFactory $injectableFactory, AttributeFetcher $attributeFetcher, ?array $functionClassNameMap = null)
{
$this->functionFactory = $functionFactory;
$this->functionFactory = new FunctionFactory($this, $injectableFactory, $attributeFetcher, $functionClassNameMap);
}
public function process(StdClass $item, ?Entity $entity = null, ?StdClass $variables = null)
{
if (is_null($variables)) {
$variables = (object)[];
$variables = (object) [];
}
return $this->functionFactory->create($item, $entity, $variables)->process($item);
$function = $this->functionFactory->create($item, $entity, $variables);
return $function->process($item);
}
}

File diff suppressed because it is too large Load Diff