api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JoinLimitsMiddleware.php 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Middleware;
  3. use Psr\Http\Message\ResponseInterface;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Psr\Http\Server\RequestHandlerInterface;
  6. use Tqdev\PhpCrudApi\Column\ReflectionService;
  7. use Tqdev\PhpCrudApi\Controller\Responder;
  8. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  9. use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
  10. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  11. use Tqdev\PhpCrudApi\RequestUtils;
  12. class JoinLimitsMiddleware extends Middleware
  13. {
  14. private $reflection;
  15. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  16. {
  17. parent::__construct($router, $responder, $properties);
  18. $this->reflection = $reflection;
  19. }
  20. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  21. {
  22. $operation = RequestUtils::getOperation($request);
  23. $params = RequestUtils::getParams($request);
  24. if (in_array($operation, ['read', 'list']) && isset($params['join'])) {
  25. $maxDepth = (int) $this->getProperty('depth', '3');
  26. $maxTables = (int) $this->getProperty('tables', '10');
  27. $maxRecords = (int) $this->getProperty('records', '1000');
  28. $tableCount = 0;
  29. $joinPaths = array();
  30. for ($i = 0; $i < count($params['join']); $i++) {
  31. $joinPath = array();
  32. $tables = explode(',', $params['join'][$i]);
  33. for ($depth = 0; $depth < min($maxDepth, count($tables)); $depth++) {
  34. array_push($joinPath, $tables[$depth]);
  35. $tableCount += 1;
  36. if ($tableCount == $maxTables) {
  37. break;
  38. }
  39. }
  40. array_push($joinPaths, implode(',', $joinPath));
  41. if ($tableCount == $maxTables) {
  42. break;
  43. }
  44. }
  45. $params['join'] = $joinPaths;
  46. $request = RequestUtils::setParams($request, $params);
  47. VariableStore::set("joinLimits.maxRecords", $maxRecords);
  48. }
  49. return $next->handle($request);
  50. }
  51. }