Skip to content

Commit f5a2a83

Browse files
committed
joinLimits
1 parent 0123135 commit f5a2a83

File tree

1 file changed

+55
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)