1
|
|
|
<?php namespace Neomerx\JsonApi\Schema; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015 [email protected] (www.neomerx.com) |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use \Closure; |
20
|
|
|
use \InvalidArgumentException; |
21
|
|
|
use \Psr\Log\LoggerAwareTrait; |
22
|
|
|
use \Psr\Log\LoggerAwareInterface; |
23
|
|
|
use \Neomerx\JsonApi\Factories\Exceptions; |
24
|
|
|
use \Neomerx\JsonApi\I18n\Translator as T; |
25
|
|
|
use \Neomerx\JsonApi\Contracts\Schema\ContainerInterface; |
26
|
|
|
use \Neomerx\JsonApi\Contracts\Schema\SchemaFactoryInterface; |
27
|
|
|
use \Neomerx\JsonApi\Contracts\Schema\SchemaProviderInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @package Neomerx\JsonApi |
31
|
|
|
*/ |
32
|
|
|
class Container implements ContainerInterface, LoggerAwareInterface |
33
|
|
|
{ |
34
|
|
|
use LoggerAwareTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $providerMapping = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $createdProviders = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $resourceType2Type = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var SchemaFactoryInterface |
53
|
|
|
*/ |
54
|
|
|
private $factory; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
const DOCTRINE_PROXY_CLASS_NAME = 'Doctrine\ORM\Proxy\Proxy'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param SchemaFactoryInterface $factory |
63
|
|
|
* @param array $schemas |
64
|
|
|
*/ |
65
|
74 |
|
public function __construct(SchemaFactoryInterface $factory, array $schemas = []) |
66
|
|
|
{ |
67
|
74 |
|
$this->factory = $factory; |
68
|
74 |
|
$this->registerArray($schemas); |
69
|
72 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Register provider for resource type. |
73
|
|
|
* |
74
|
|
|
* @param string $type |
75
|
|
|
* @param string|Closure $schema |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
64 |
|
public function register($type, $schema) |
80
|
|
|
{ |
81
|
|
|
// Type must be non-empty string |
82
|
64 |
|
$isOk = (is_string($type) === true && empty($type) === false); |
83
|
64 |
|
if ($isOk === false) { |
84
|
1 |
|
throw new InvalidArgumentException(T::t('Type must be non-empty string.')); |
85
|
|
|
} |
86
|
|
|
|
87
|
63 |
|
$isOk = ((is_string($schema) === true && empty($schema) === false) || $schema instanceof Closure); |
88
|
63 |
|
if ($isOk === false) { |
89
|
1 |
|
throw new InvalidArgumentException(T::t( |
90
|
1 |
|
'Schema for type \'%s\' must be non-empty string or Closure.', |
91
|
1 |
|
[$type] |
92
|
1 |
|
)); |
93
|
|
|
} |
94
|
|
|
|
95
|
62 |
View Code Duplication |
if (isset($this->providerMapping[$type]) === true) { |
|
|
|
|
96
|
1 |
|
throw new InvalidArgumentException(T::t( |
97
|
1 |
|
'Type should not be used more than once to register a schema (\'%s\').', |
98
|
1 |
|
[$type] |
99
|
2 |
|
)); |
100
|
|
|
} |
101
|
|
|
|
102
|
62 |
|
$this->providerMapping[$type] = $schema; |
103
|
62 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Register providers for resource types. |
107
|
|
|
* |
108
|
|
|
* @param array $schemas |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
74 |
|
public function registerArray(array $schemas) |
113
|
|
|
{ |
114
|
74 |
|
foreach ($schemas as $type => $schema) { |
115
|
64 |
|
$this->register($type, $schema); |
116
|
72 |
|
} |
117
|
72 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritdoc |
121
|
|
|
*/ |
122
|
53 |
|
public function getSchema($resource) |
123
|
|
|
{ |
124
|
53 |
|
$resourceType = $this->getResourceType($resource); |
125
|
|
|
|
126
|
53 |
|
return $this->getSchemaByType($resourceType); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritdoc |
131
|
|
|
*/ |
132
|
55 |
|
public function getSchemaByType($type) |
133
|
|
|
{ |
134
|
55 |
|
is_string($type) === true ?: Exceptions::throwInvalidArgument('type', $type); |
135
|
|
|
|
136
|
55 |
|
if (isset($this->createdProviders[$type])) { |
137
|
47 |
|
return $this->createdProviders[$type]; |
138
|
|
|
} |
139
|
|
|
|
140
|
55 |
View Code Duplication |
if (isset($this->providerMapping[$type]) === false) { |
|
|
|
|
141
|
2 |
|
throw new InvalidArgumentException(T::t('Schema is not registered for type \'%s\'.', [$type])); |
142
|
|
|
} |
143
|
|
|
|
144
|
55 |
|
$classNameOrClosure = $this->providerMapping[$type]; |
145
|
55 |
|
if ($classNameOrClosure instanceof Closure) { |
146
|
23 |
|
$this->createdProviders[$type] = ($schema = $classNameOrClosure($this->factory, $this)); |
147
|
23 |
|
} else { |
148
|
42 |
|
$this->createdProviders[$type] = ($schema = new $classNameOrClosure($this->factory, $this)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** @var SchemaProviderInterface $schema */ |
152
|
|
|
|
153
|
55 |
|
$this->resourceType2Type[$schema->getResourceType()] = $type; |
154
|
|
|
|
155
|
55 |
|
return $schema; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @inheritdoc |
160
|
|
|
*/ |
161
|
38 |
|
public function getSchemaByResourceType($resourceType) |
162
|
|
|
{ |
163
|
|
|
// Schema is not found among instantiated schemas for resource type $resourceType |
164
|
38 |
|
$isOk = (is_string($resourceType) === true && isset($this->resourceType2Type[$resourceType]) === true); |
165
|
|
|
|
166
|
|
|
// Schema might not be found if it hasn't been searched by type (not resource type) before. |
167
|
|
|
// We instantiate all schemas and then find one. |
168
|
38 |
|
if ($isOk === false) { |
169
|
1 |
|
foreach ($this->providerMapping as $type => $schema) { |
170
|
1 |
|
if (isset($this->createdProviders[$type]) === false) { |
171
|
|
|
// it will instantiate the schema |
172
|
1 |
|
$this->getSchemaByType($type); |
173
|
1 |
|
} |
174
|
1 |
|
} |
175
|
1 |
|
} |
176
|
|
|
|
177
|
|
|
// search one more time |
178
|
38 |
|
$isOk = (is_string($resourceType) === true && isset($this->resourceType2Type[$resourceType]) === true); |
179
|
|
|
|
180
|
38 |
|
if ($isOk === false) { |
181
|
1 |
|
throw new InvalidArgumentException(T::t( |
182
|
1 |
|
'Schema is not registered for resource type \'%s\'.', |
183
|
1 |
|
[$resourceType] |
184
|
1 |
|
)); |
185
|
|
|
} |
186
|
|
|
|
187
|
38 |
|
return $this->getSchemaByType($this->resourceType2Type[$resourceType]); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param object $resource |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
53 |
|
protected function getResourceType($resource) |
196
|
|
|
{ |
197
|
|
|
|
198
|
53 |
|
if(in_array(self::DOCTRINE_PROXY_CLASS_NAME, class_implements($resource))){ |
199
|
|
|
return get_parent_class($resource); |
200
|
|
|
} |
201
|
|
|
|
202
|
53 |
|
return get_class($resource); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
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.