1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pinq\Providers; |
4
|
|
|
|
5
|
|
|
use Pinq\Expressions as O; |
6
|
|
|
use Pinq\Queries\Builders; |
7
|
|
|
use Pinq\Queries; |
8
|
|
|
use Pinq\Repository; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Base class for the repository provider. |
12
|
|
|
* |
13
|
|
|
* @author Elliot Levin <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class RepositoryProvider extends ProviderBase implements IRepositoryProvider |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Configuration\IRepositoryConfiguration |
19
|
|
|
*/ |
20
|
|
|
protected $configuration; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var IQueryProvider |
24
|
|
|
*/ |
25
|
|
|
protected $queryProvider; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Builders\IOperationQueryBuilder |
29
|
|
|
*/ |
30
|
|
|
protected $operationQueryBuilder; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
Queries\ISourceInfo $sourceInfo, |
34
|
|
|
IQueryProvider $queryProvider, |
35
|
|
|
Configuration\IRepositoryConfiguration $configuration = null |
36
|
|
|
) { |
37
|
|
|
parent::__construct($sourceInfo, $configuration ?: new Configuration\DefaultRepositoryConfiguration()); |
38
|
|
|
|
39
|
|
|
$this->queryProvider = $queryProvider; |
40
|
|
|
$this->queryResultCollection = $queryProvider->getQueryResultCollection(); |
41
|
|
|
$this->operationQueryBuilder = $this->configuration->getOperationQueryBuilder(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
final public function getQueryProvider() |
45
|
|
|
{ |
46
|
|
|
return $this->queryProvider; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
final public function getQueryResultCollection() |
50
|
|
|
{ |
51
|
|
|
return $this->queryResultCollection; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
final public function createQueryable(O\TraversalExpression $scopeExpression = null) |
55
|
|
|
{ |
56
|
|
|
return $this->queryProvider->createQueryable($scopeExpression); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function createRepository(O\TraversalExpression $scopeExpression = null) |
60
|
|
|
{ |
61
|
|
|
return new Repository($this, $this->sourceInfo, $scopeExpression, $this->scheme); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
final public function load(O\Expression $requestExpression) |
65
|
|
|
{ |
66
|
|
|
return $this->queryProvider->load($requestExpression); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function execute(O\Expression $operationExpression) |
70
|
|
|
{ |
71
|
|
|
$this->executeOperationExpression($operationExpression); |
72
|
|
|
if ($this->queryResultCollection !== null) { |
73
|
|
|
$this->queryResultCollection->clearResults(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
protected function executeOperationExpression(O\Expression $operationExpression) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$resolution = $this->operationQueryBuilder->resolveOperation($operationExpression); |
80
|
|
|
$queryHash = $resolution->getHash(); |
81
|
|
|
$query = $this->queryCache->tryGet($queryHash); |
82
|
|
|
|
83
|
|
|
if (!($query instanceof Queries\IOperationQuery)) { |
84
|
|
|
$query = $this->operationQueryBuilder->parseOperation($operationExpression); |
85
|
|
|
$this->queryCache->save($queryHash, $query); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$resolvedParameters = $query->getParameters()->resolve($resolution); |
89
|
|
|
|
90
|
|
|
$this->executeOperation($query, $resolvedParameters); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param \Pinq\Queries\IOperationQuery $operation |
95
|
|
|
* @param \Pinq\Queries\IResolvedParameterRegistry $resolvedParameters |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
abstract protected function executeOperation( |
100
|
|
|
Queries\IOperationQuery $operation, |
101
|
|
|
Queries\IResolvedParameterRegistry $resolvedParameters |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.