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
|
|
|
private $accessManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Reader |
27
|
|
|
*/ |
28
|
|
|
private $reader; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $aliases = []; |
34
|
|
|
|
35
|
|
|
private $accessible = []; |
|
|
|
|
36
|
|
|
|
37
|
3 |
|
public function __construct(AccessManager $accessManager, Reader $reader) |
38
|
|
|
{ |
39
|
3 |
|
$this->accessManager = $accessManager; |
40
|
3 |
|
$this->reader = $reader; |
41
|
3 |
|
} |
42
|
|
|
|
43
|
1 |
|
public function execute() |
44
|
|
|
{ |
45
|
|
|
/** @var \ReflectionClass $reflection */ |
46
|
1 |
|
$reflection = $this->getFromResponse('reflection'); |
47
|
|
|
/** @var QueryBuilder $queryBuilder */ |
48
|
1 |
|
$queryBuilder = $this->getFromResponse('query_builder'); |
49
|
1 |
|
$queryBuilder->from($reflection->getName(), 'e'); |
50
|
1 |
|
$queryBuilder->addSelect('e'); |
51
|
|
|
|
52
|
1 |
|
foreach ($reflection->getProperties() as $property) { |
53
|
1 |
|
$this->addJoinIfRelation($property, $queryBuilder); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return $this->createResponse(['query_builder' => $queryBuilder]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
1 |
|
public function requiresBefore() |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
1 |
|
GetQueryBuilderStep::class |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $property |
69
|
|
|
* @param $queryBuilder |
70
|
|
|
*/ |
71
|
1 |
|
protected function addJoinIfRelation(\ReflectionProperty $property, QueryBuilder $queryBuilder) |
72
|
|
|
{ |
73
|
1 |
|
if ($this->isRelation($property) && $this->accessManager->canAccessProperty($property)) { |
74
|
|
|
$alias = Inflector::tableize($property->getName()); |
75
|
|
|
$queryBuilder->leftJoin(sprintf('e.%s', $property->getName()), $alias); |
76
|
|
|
$queryBuilder->addSelect($alias); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
} |
80
|
|
|
|
81
|
1 |
|
protected function isRelation(\ReflectionProperty $property) |
82
|
|
|
{ |
83
|
1 |
|
return !empty($this->reader->getPropertyAnnotation($property, Relation::class)); |
84
|
|
|
} |
85
|
|
|
} |