at the end of the day, it was inevitable
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Processor;
|
||||
|
||||
use Common\Util\Processor\ExpressionLanguage\TestExpressionLanguage;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
||||
|
||||
/**
|
||||
* Class DataProcessor
|
||||
* Process data before sending to server.
|
||||
*
|
||||
* @package Common\Util\Processor
|
||||
*/
|
||||
class DataProcessor
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ExpressionLanguage
|
||||
*/
|
||||
private $language;
|
||||
|
||||
/**
|
||||
* DataProcessor constructor.
|
||||
*
|
||||
* @param ContainerInterface $container A ContainerInterface instance.
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->language = new TestExpressionLanguage($container);
|
||||
$this->registerFunctions();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $data Process data send to server and replace patterns.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function process($data)
|
||||
{
|
||||
if (is_array($data)) {
|
||||
// Recursively process arrays.
|
||||
return array_map(function ($data) {
|
||||
return $this->process($data);
|
||||
}, $data);
|
||||
} elseif (is_string($data)) {
|
||||
// Process string data.
|
||||
$replacer = function ($param) {
|
||||
// Sanitize params.
|
||||
$param = str_replace('\\"', '\'', trim(current($param), '#'));
|
||||
|
||||
return $this->language->evaluate($param);
|
||||
};
|
||||
|
||||
return preg_replace_callback("/#.+?#/", $replacer, $data);
|
||||
}
|
||||
|
||||
// Not change other variable types.
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $arguments Arguments specified by expression language.
|
||||
* @param mixed $time Pass to DateTime constructor.
|
||||
*
|
||||
* @return \DateTime
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function createDate($arguments, $time = 'now')
|
||||
{
|
||||
return new \DateTime($time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register custom expression language functions.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function registerFunctions()
|
||||
{
|
||||
$dummy = function () {
|
||||
// do nothing.
|
||||
};
|
||||
|
||||
$this->language->register('date', $dummy, [ $this, 'createDate' ]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Processor\ExpressionLanguage\Provider;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
|
||||
|
||||
/**
|
||||
* Class EntityGetterProvider
|
||||
* Register function like 'getUser' or 'getStoredQuery' for fetching single
|
||||
* entity from database.
|
||||
*
|
||||
* @package Common\Util\Processor\ExpressionLanguage\Provider
|
||||
*
|
||||
*/
|
||||
class EntityGetterProvider implements ExpressionFunctionProviderInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* EntityGetterProvider constructor.
|
||||
*
|
||||
* @param EntityManagerInterface $em A EntityManagerInterface instance.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ExpressionFunction[] An array of Function instances.
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
/**
|
||||
* Dummy compiler.
|
||||
* We use this expression function only in runtime and not compile its.
|
||||
*/
|
||||
$compiler = function () {
|
||||
};
|
||||
|
||||
$functions = [];
|
||||
/** @var ClassMetadataInfo[] $metadataList */
|
||||
$metadataList = $this->em->getMetadataFactory()->getAllMetadata();
|
||||
foreach ($metadataList as $metadata) {
|
||||
$name = $metadata->getName();
|
||||
$shortName = substr($name, strrpos($name, '\\') + 1);
|
||||
$fnName = 'get'. $shortName;
|
||||
|
||||
if (strpos($name, 'Entity\\'. $shortName) === false) {
|
||||
// Process only entities inside 'Entity' directory.
|
||||
continue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $arguments Arguments specified by expression language
|
||||
* @param array $criteria Search criteria.
|
||||
*
|
||||
* @return null|object Found entity or null.
|
||||
*/
|
||||
$evaluator = function ($arguments, array $criteria) use ($name) {
|
||||
return $this->em->getRepository($name)->findOneBy($criteria);
|
||||
};
|
||||
$functions[] = new ExpressionFunction($fnName, $compiler, $evaluator);
|
||||
}
|
||||
|
||||
return $functions;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Processor\ExpressionLanguage;
|
||||
|
||||
use Common\Util\Processor\ExpressionLanguage\Provider\EntityGetterProvider;
|
||||
use Doctrine\Bundle\DoctrineBundle\Registry;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
||||
|
||||
/**
|
||||
* Class TestExpressionLanguage
|
||||
* @package Common\Util\Processor\ExpressionLanguage
|
||||
*/
|
||||
class TestExpressionLanguage extends ExpressionLanguage
|
||||
{
|
||||
|
||||
/**
|
||||
* TestExpressionLanguage constructor.
|
||||
*
|
||||
* @param ContainerInterface $container A ContainerInterface instance.
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
/** @var Registry $doctrine */
|
||||
$doctrine = $container->get('doctrine');
|
||||
|
||||
$dummy = function () {
|
||||
// Dummy function for compiler argument of ExpressionFunction.
|
||||
};
|
||||
|
||||
parent::__construct(null, [
|
||||
new EntityGetterProvider($doctrine->getManager()),
|
||||
]);
|
||||
|
||||
// Add 'now' function which return current date.
|
||||
$this->addFunction(new ExpressionFunction('now', $dummy, function () {
|
||||
return date_create();
|
||||
}));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user