|
1
|
|
|
<?php namespace Limoncello\Flute\Package; |
|
2
|
|
|
|
|
3
|
|
|
use Generator; |
|
4
|
|
|
use Limoncello\Contracts\Settings\SettingsInterface; |
|
5
|
|
|
use Limoncello\Flute\Contracts\Schema\SchemaInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @package Limoncello\Flute |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class FluteSettings implements SettingsInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* By default it checks that all Schemes have unique resource types. That's a legit case |
|
14
|
|
|
* to have multiple Schemes for a same resource type however it's more likely that developer |
|
15
|
|
|
* just forgot to set a unique one. If you do need multiple Schemes for a resource feel free |
|
16
|
|
|
* to set it to `false`. |
|
17
|
|
|
* |
|
18
|
|
|
* @var bool |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $requireUniqueTypes = true; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract protected function getSchemesPath(): string; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $path |
|
29
|
|
|
* @param string $implementClassName |
|
30
|
|
|
* |
|
31
|
|
|
* @return Generator |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract protected function selectClasses(string $path, string $implementClassName): Generator; |
|
34
|
|
|
|
|
35
|
|
|
/** Config key */ |
|
36
|
|
|
const KEY_MODEL_TO_SCHEME_MAP = 0; |
|
37
|
|
|
|
|
38
|
|
|
/** Config key */ |
|
39
|
|
|
const KEY_RELATIONSHIP_PAGING_SIZE = self::KEY_MODEL_TO_SCHEME_MAP + 1; |
|
40
|
|
|
|
|
41
|
|
|
/** Config key */ |
|
42
|
|
|
const KEY_JSON_ENCODE_OPTIONS = self::KEY_RELATIONSHIP_PAGING_SIZE + 1; |
|
43
|
|
|
|
|
44
|
|
|
/** Config key */ |
|
45
|
|
|
const KEY_JSON_ENCODE_DEPTH = self::KEY_JSON_ENCODE_OPTIONS + 1; |
|
46
|
|
|
|
|
47
|
|
|
/** Config key */ |
|
48
|
|
|
const KEY_IS_SHOW_VERSION = self::KEY_JSON_ENCODE_DEPTH + 1; |
|
49
|
|
|
|
|
50
|
|
|
/** Config key */ |
|
51
|
|
|
const KEY_META = self::KEY_IS_SHOW_VERSION + 1; |
|
52
|
|
|
|
|
53
|
|
|
/** Config key */ |
|
54
|
|
|
const KEY_URI_PREFIX = self::KEY_META + 1; |
|
55
|
|
|
|
|
56
|
|
|
/** Config key */ |
|
57
|
|
|
const KEY_LAST = self::KEY_URI_PREFIX + 1; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
public function get(): array |
|
63
|
|
|
{ |
|
64
|
|
|
$jsonOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES; |
|
65
|
|
|
|
|
66
|
|
|
$map = []; |
|
67
|
|
|
$types = []; |
|
68
|
|
|
foreach ($this->selectClasses($this->getSchemesPath(), SchemaInterface::class) as $schemeClass) { |
|
69
|
|
|
assert( |
|
70
|
|
|
is_string($schemeClass) && |
|
71
|
|
|
class_exists($schemeClass) && |
|
72
|
|
|
array_key_exists(SchemaInterface::class, class_implements($schemeClass)) |
|
73
|
|
|
); |
|
74
|
|
|
/** @var SchemaInterface $schemeClass */ |
|
75
|
|
|
$modelClass = $schemeClass::MODEL; |
|
76
|
|
|
$resourceType = $schemeClass::TYPE; |
|
77
|
|
|
|
|
78
|
|
|
assert(is_string($modelClass) === true && empty($modelClass) === false); |
|
79
|
|
|
assert(is_string($resourceType) === true && empty($resourceType) === false); |
|
80
|
|
|
|
|
81
|
|
|
// By default it checks that all Schemes have unique resource types. That's a legit case |
|
82
|
|
|
// to have multiple Schemes for a same resource type however it's more likely that developer |
|
83
|
|
|
// just forgot to set a unique one. If you do need multiple Schemes for a resource feel free |
|
84
|
|
|
// to set to turn off this check. |
|
85
|
|
|
assert( |
|
86
|
|
|
$this->requireUniqueTypes === false || array_key_exists($resourceType, $types) === false, |
|
87
|
|
|
"Are you sure it's not an error to use resource type `$resourceType` more than once?" |
|
88
|
|
|
); |
|
89
|
|
|
$types[$resourceType] = true; |
|
90
|
|
|
|
|
91
|
|
|
$map[$modelClass] = $schemeClass; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return [ |
|
95
|
|
|
static::KEY_MODEL_TO_SCHEME_MAP => $map, |
|
96
|
|
|
static::KEY_RELATIONSHIP_PAGING_SIZE => 20, |
|
97
|
|
|
static::KEY_JSON_ENCODE_OPTIONS => $jsonOptions, |
|
98
|
|
|
static::KEY_JSON_ENCODE_DEPTH => 512, |
|
99
|
|
|
static::KEY_IS_SHOW_VERSION => false, |
|
100
|
|
|
static::KEY_META => null, |
|
101
|
|
|
static::KEY_URI_PREFIX => null, |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|