-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathDirect.php
More file actions
133 lines (112 loc) · 4.27 KB
/
Copy pathDirect.php
File metadata and controls
133 lines (112 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Xfrocks\Api\Controller;
use XF\Mvc\ParameterBag;
use Xfrocks\Api\Transform\TransformContext;
use Xfrocks\Api\Transformer;
class Direct extends AbstractController
{
public function actionGetIndex(ParameterBag $params)
{
$paramType = $params->type;
$params = $this->params()
->define('where', 'array');
$types = explode('/', $paramType);
$type0 = array_shift($types);
$subTypes = $types;
unset($types);
$rootFinder = $this->finder($type0);
$where = $params['where'];
$input = $this->request->getInput();
foreach ($input as $inputKey => $inputValue) {
if (empty($inputValue)) {
continue;
}
if (isset($params[$inputKey])) {
continue;
}
$where[] = [$inputKey, '=', $inputValue];
}
foreach ($where as $whereOne) {
if (!is_array($whereOne)) {
continue;
}
try {
call_user_func_array([$rootFinder, 'where'], array_values($whereOne));
} catch (\InvalidArgumentException $e) {
// bad condition, ignore
}
}
$transformer = $this->transformFinderLazily($rootFinder);
$rootContext = $params->getTransformContext();
if (count($subTypes) > 0) {
$rootContext->onTransformFinderCallbacks[] = function ($context, $finder) use (
$rootContext,
$rootFinder,
$subTypes
) {
if ($context !== $rootContext || $finder !== $rootFinder) {
return;
}
/** @var TransformContext $context */
/** @var \XF\Mvc\Entity\Finder $finder */
/** @var Transformer $transformer */
$transformer = $this->app->container('api.transformer');
$subContext = $context;
$subFinder = $finder;
foreach ($subTypes as $subType) {
try {
$subFinder->with($subType);
} catch (\LogicException $e) {
// bad relation key, return asap
return;
}
$entityStructure = $subFinder->getStructure();
$relationConfig = $entityStructure->relations[$subType];
$subHandler = $transformer->handler($relationConfig['entity']);
$em = $this->app->em();
$parentFinder = $subFinder;
$subFinder = $em->getFinder($relationConfig['entity'], false);
$subFinder->setParentFinder($parentFinder, $subType);
$subContext = $subContext->getSubContext(null, $subHandler);
$subHandler->onTransformFinder($subContext, $subFinder);
}
};
$transformer->addCallbackFinderPostFetch(function ($entities) use ($subTypes) {
$subEntities = [];
/** @var \XF\Mvc\Entity\Entity $entity */
foreach ($entities as $entity) {
if (!$this->canView($entity)) {
continue;
}
foreach ($subTypes as $subType) {
try {
$entity = $entity->getRelation($subType);
} catch (\InvalidArgumentException $e) {
// bad relation key, return asap
return $subEntities;
}
if (!$this->canView($entity)) {
continue;
}
}
$subEntities[] = $entity;
}
return $subEntities;
});
}
$data = [$paramType => $transformer];
return $this->api($data);
}
/**
* @param \XF\Mvc\Entity\Entity $entity
* @return bool|mixed
*/
protected function canView(\XF\Mvc\Entity\Entity $entity)
{
$callable = [$entity, 'canView'];
if (!is_callable($callable)) {
return false;
}
return call_user_func($callable);
}
}