1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kami\ApiCoreBundle\RequestProcessor\Step\Common; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Annotations\Reader; |
7
|
|
|
use Doctrine\Common\Util\Inflector; |
8
|
|
|
use Doctrine\ORM\Mapping\ManyToMany; |
9
|
|
|
use Doctrine\ORM\Mapping\ManyToOne; |
10
|
|
|
use Doctrine\ORM\Mapping\OneToMany; |
11
|
|
|
use Doctrine\ORM\Mapping\OneToOne; |
12
|
|
|
use Doctrine\ORM\QueryBuilder; |
13
|
|
|
use Kami\ApiCoreBundle\Annotation\Relation; |
14
|
|
|
use Kami\ApiCoreBundle\RequestProcessor\ProcessingException; |
15
|
|
|
use Kami\ApiCoreBundle\RequestProcessor\Step\AbstractStep; |
16
|
|
|
use Kami\ApiCoreBundle\Security\AccessManager; |
17
|
|
|
|
18
|
|
|
class BuildSelectQueryStep extends AbstractStep |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var AccessManager |
22
|
|
|
*/ |
23
|
|
|
protected $accessManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Reader |
27
|
|
|
*/ |
28
|
|
|
protected $reader; |
29
|
|
|
|
30
|
|
|
|
31
|
3 |
|
public function __construct(AccessManager $accessManager, Reader $reader) |
32
|
|
|
{ |
33
|
3 |
|
$this->accessManager = $accessManager; |
34
|
3 |
|
$this->reader = $reader; |
35
|
3 |
|
} |
36
|
|
|
|
37
|
1 |
|
public function execute() |
38
|
|
|
{ |
39
|
|
|
/** @var \ReflectionClass $reflection */ |
40
|
1 |
|
$reflection = $this->getFromResponse('reflection'); |
41
|
|
|
/** @var QueryBuilder $queryBuilder */ |
42
|
1 |
|
$queryBuilder = $this->getFromResponse('query_builder'); |
43
|
1 |
|
$queryBuilder->from($reflection->getName(), 'e'); |
44
|
1 |
|
$queryBuilder->addSelect('e'); |
45
|
|
|
|
46
|
1 |
|
foreach ($reflection->getProperties() as $property) { |
47
|
1 |
|
$this->addJoinIfRelation($property, $queryBuilder); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
return $this->createResponse(['query_builder' => $queryBuilder]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
1 |
|
public function requiresBefore() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
1 |
|
GetQueryBuilderStep::class |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $property |
63
|
|
|
* @param $queryBuilder |
64
|
|
|
*/ |
65
|
1 |
|
protected function addJoinIfRelation(\ReflectionProperty $property, QueryBuilder $queryBuilder) |
66
|
|
|
{ |
67
|
1 |
|
if ($this->isRelation($property) && $this->accessManager->canAccessProperty($property)) { |
68
|
|
|
$alias = Inflector::tableize($property->getName()); |
69
|
|
|
$queryBuilder->leftJoin(sprintf('e.%s', $property->getName()), $alias); |
70
|
|
|
$queryBuilder->addSelect($alias); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
} |
74
|
|
|
|
75
|
1 |
|
protected function isRelation(\ReflectionProperty $property) |
76
|
|
|
{ |
77
|
1 |
|
return !empty($this->reader->getPropertyAnnotation($property, Relation::class)); |
78
|
|
|
} |
79
|
|
|
} |