1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Majora\Framework\Loader; |
4
|
|
|
|
5
|
|
|
use Majora\Framework\Repository\RepositoryInterface; |
6
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Base trait for loaders. |
10
|
|
|
* |
11
|
|
|
* @method retrieveById() |
12
|
|
|
*/ |
13
|
|
|
trait LoaderTrait |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var RepositoryInterface |
17
|
|
|
*/ |
18
|
|
|
protected $entityRepository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $entityProperties; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $entityClass; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $collectionClass; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var OptionsResolver |
37
|
|
|
*/ |
38
|
|
|
protected $filterResolver; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \Closure |
42
|
|
|
*/ |
43
|
|
|
private $loadingDelegate; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* setUp method. |
47
|
|
|
* |
48
|
|
|
* @param string $entityClass |
49
|
|
|
* @param array $entityProperties |
50
|
|
|
* @param string $collectionClass |
51
|
|
|
* @param RepositoryInterface $entityRepository |
52
|
|
|
*/ |
53
|
|
|
public function setUp( |
54
|
|
|
$entityClass, |
55
|
|
|
array $entityProperties, |
56
|
|
|
$collectionClass, |
57
|
|
|
RepositoryInterface $entityRepository = null |
58
|
|
|
) { |
59
|
|
|
@trigger_error(__METHOD__.'() is deprecated and will be removed in 2.0. Please use configureMetadata() instead.', E_USER_DEPRECATED); |
|
|
|
|
60
|
|
|
|
61
|
|
|
if ($entityRepository) { |
62
|
|
|
@trigger_error('Repository injection throught '.__METHOD__.'() is deprecated and will be removed in 2.0. Please inject it by constructor.', E_USER_DEPRECATED); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$this->entityRepository = $entityRepository; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->configureMetadata($entityClass, $entityProperties, $collectionClass); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @see LoaderInterface::configureMetadata() |
72
|
|
|
*/ |
73
|
|
|
public function configureMetadata($entityClass, array $entityProperties, $collectionClass) |
74
|
|
|
{ |
75
|
|
|
$this->entityClass = $entityClass; |
76
|
|
|
$this->entityProperties = $entityProperties; |
77
|
|
|
$this->collectionClass = $collectionClass; |
78
|
|
|
|
79
|
|
|
$this->filterResolver = new OptionsResolver(); |
80
|
|
|
foreach ($entityProperties as $propertyName) { |
81
|
|
|
$this->filterResolver->setDefined($propertyName); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns repository loading delegate. |
87
|
|
|
* |
88
|
|
|
* @return \Closure |
89
|
|
|
*/ |
90
|
|
|
public function getLoadingDelegate() |
91
|
|
|
{ |
92
|
|
|
return $this->loadingDelegate = $this->loadingDelegate ?: function ($id) { |
93
|
|
|
return $this->retrieveById($id); |
|
|
|
|
94
|
|
|
}; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: