Skip to content

Commit 9ff2742

Browse files
committed
wip
1 parent 801d653 commit 9ff2742

File tree

2 files changed

+68
-87
lines changed

2 files changed

+68
-87
lines changed

src/Meta/RuntimeResolver.php

Lines changed: 38 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@
4545
use ReflectionProperty;
4646
use Reflector;
4747
use function array_key_exists;
48+
use function array_key_last;
4849
use function array_merge;
4950
use function assert;
50-
use function count;
5151
use function get_class;
5252
use function is_a;
5353
use function is_int;
5454
use function is_string;
55-
use function PHPUnit\Framework\assertNull;
5655
use function sprintf;
5756
use const PHP_VERSION_ID;
5857

@@ -315,33 +314,26 @@ private function resolveFieldsMeta(ReflectionClass $rootClass, CompileMeta $meta
315314
$docs = [];
316315
$modifiers = [];
317316
$rules = [];
318-
$default = DefaultValueMeta::fromNothing();
319-
$phpPropertyMeta = null;
320-
$reflector = null;
321-
foreach ($scopedProperty as $scope => $scopeConfig) {
322-
//TODO - mergování - variance, invariance
323-
$repeatable = $scopeConfig['repeatable'];
324317

325-
/*
326-
if ($repeatable === RepeatableBehavior::noRepeat()) {
327-
//TODO - kontrolovat, že je definice jenom jedna v téhle iteraci
328-
//TODO - kontrolovat, že ještě nastavená není z předchozí iterace
329-
$grouped[$propertyName][$scope][] = $definition;
330-
} elseif ($repeatable === RepeatableBehavior::merge()) {
331-
//TODO - mergnout
332-
$grouped[$propertyName][$scope][] = $definition;
333-
} elseif ($repeatable === RepeatableBehavior::override()) {
334-
//TODO - přepsat
335-
$grouped[$propertyName][$scope][] = $definition;
336-
}
337-
*/
318+
$lastPropertyConfig = $scopedProperty[array_key_last($scopedProperty)];
319+
//TODO - z těch ostatních se mi hodí jen source, runtime resolver nepotřebuju
320+
$lastPropertyStructure = $lastPropertyConfig['propertyStructure'];
321+
$lastContextReflector = $lastPropertyStructure->getContextReflector();
322+
323+
//TODO - získat 0-1, resolvnout, odstranit ze seznamu
324+
$default = $this->getDefaultValue(
325+
$lastPropertyStructure,
326+
$lastPropertyConfig['scopedDefinitions'][DefaultValueModifier::class] ?? [],
327+
);
328+
$phpPropertyMeta = PhpPropertyMeta::from($lastContextReflector);
329+
330+
$context = new MetaFieldContext($this->loader, $this, $default);
338331

339-
foreach ($scopeConfig['definitionsByClass'] as $className => $classScopeConfig) {
340-
/** @var PropertyStructure $propertyStructure */
341-
$propertyStructure = $classScopeConfig['propertyStructure'];
342-
$reflector = $propertyStructure->getContextReflector();
343-
$phpPropertyMeta = PhpPropertyMeta::from($propertyStructure->getContextReflector());
344-
foreach ($classScopeConfig['definitions'] as $definition) {
332+
foreach ($scopedProperty as $propertyClassName => $propertyConfig) {
333+
foreach ($propertyConfig['scopedDefinitions'] as $scope => $definitions) {
334+
foreach ($definitions as $definition) {
335+
//TODO - indexovat pomocí scope, ať se pak mohou mergnout
336+
//TODO - resolve by měl být možný už tady
345337
if ($definition instanceof CallbackDefinition) {
346338
$callbacks[] = $definition;
347339
} elseif ($definition instanceof DocDefinition) {
@@ -354,18 +346,27 @@ private function resolveFieldsMeta(ReflectionClass $rootClass, CompileMeta $meta
354346
$this->throwUnsupportedDefinitionType($definition);
355347
}
356348
}
357-
358-
//TODO - tohle je blbě, pro teď good enough
359-
$default = $this->getDefaultValue($propertyStructure, $modifiers);
360349
}
361350
}
362351

363-
$context = new MetaFieldContext($this->loader, $this, $default);
352+
/*
353+
if ($repeatable === RepeatableBehavior::noRepeat()) {
354+
//TODO - kontrolovat, že je definice jenom jedna v téhle iteraci
355+
//TODO - kontrolovat, že ještě nastavená není z předchozí iterace
356+
$grouped[$propertyName][$scope][] = $definition;
357+
} elseif ($repeatable === RepeatableBehavior::merge()) {
358+
//TODO - mergnout
359+
$grouped[$propertyName][$scope][] = $definition;
360+
} elseif ($repeatable === RepeatableBehavior::override()) {
361+
//TODO - přepsat
362+
$grouped[$propertyName][$scope][] = $definition;
363+
}
364+
*/
364365

365366
//TODO - předávat definice nestrukturovaně? jen seskupené podle scope
366367
$field = new FieldRuntimeMeta(
367368
//TODO - resolve volat při zpracování definic per-class
368-
$this->resolveCallbacksMeta($callbacks, $context, $reflector),
369+
$this->resolveCallbacksMeta($callbacks, $context, $lastContextReflector),
369370
$this->resolveDocsMeta($docs, $context),
370371
$this->resolveFieldModifiersMeta($modifiers, $context),
371372
$this->resolveRuleMeta($rules[0], $context),
@@ -485,8 +486,7 @@ private function propertyNameToFieldName(FieldRuntimeMeta $fieldMeta)
485486
*/
486487
private function resolveFieldMeta(
487488
ReflectionClass $rootClass,
488-
FieldCompileMeta $fieldMeta,
489-
RuleDefinition $ruleDefinition
489+
FieldCompileMeta $fieldMeta
490490
): FieldRuntimeMeta
491491
{
492492
$fieldStructure = $fieldMeta->getPropertyStructure();
@@ -510,31 +510,6 @@ private function resolveFieldMeta(
510510
if (!$classReflector->isSubclassOf(MappedObject::class)) {
511511
$this->throwFieldMetaOutsideOfMappedObject($rootClass, $classReflector, $fieldStructure->getSource());
512512
}
513-
514-
$callbacks = $docs = $modifiers = [];
515-
foreach ($fieldMeta->getDefinitions() as $definition) {
516-
if ($definition instanceof CallbackDefinition) {
517-
$callbacks[] = $definition;
518-
} elseif ($definition instanceof DocDefinition) {
519-
$docs[] = $definition;
520-
} elseif ($definition instanceof ModifierDefinition) {
521-
$modifiers[] = $definition;
522-
} elseif (!$definition instanceof RuleDefinition) {
523-
$this->throwUnsupportedDefinitionType($definition);
524-
}
525-
}
526-
527-
$defaultValue = $this->getDefaultValue($fieldMeta->getPropertyStructure(), $modifiers);
528-
$context = new MetaFieldContext($this->loader, $this, $defaultValue);
529-
530-
return new FieldRuntimeMeta(
531-
$this->resolveCallbacksMeta($callbacks, $context, $reflector),
532-
$this->resolveDocsMeta($docs, $context),
533-
$this->resolveFieldModifiersMeta($modifiers, $context),
534-
$this->resolveRuleMeta($ruleDefinition, $context),
535-
$defaultValue,
536-
PhpPropertyMeta::from($reflector),
537-
);
538513
}
539514

540515
/**
@@ -707,14 +682,12 @@ public function resolveRuleMeta(RuleDefinition $definition, MetaFieldContext $co
707682
}
708683

709684
/**
710-
* @param list<ModifierDefinition> $modifiers
685+
* @param list<MetaDefinition> $defaultModifiers
711686
*/
712-
private function getDefaultValue(PropertyStructure $propertyStructure, array $modifiers): DefaultValueMeta
687+
private function getDefaultValue(PropertyStructure $propertyStructure, array $defaultModifiers): DefaultValueMeta
713688
{
714-
foreach ($modifiers as $modifier) {
715-
if ($modifier->getHandler() === DefaultValueModifier::class) {
716-
return DefaultValueMeta::fromValue($modifier->getArgs()[DefaultValueModifier::Value]);
717-
}
689+
foreach ($defaultModifiers as $modifier) {
690+
return DefaultValueMeta::fromValue($modifier->getArgs()[DefaultValueModifier::Value]);
718691
}
719692

720693
$property = $propertyStructure->getContextReflector();

src/Meta/Scope/ScopeResolver.php

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,31 @@ public function setScopeConfig(string $scope, ScopeConfig $config): void
2929
$this->scopeConfigs[$scope] = $config;
3030
}
3131

32+
public function getScopeConfig(string $scope): ScopeConfig
33+
{
34+
$config = $this->scopeConfigs[$scope] ?? null;
35+
if ($config === null) {
36+
throw new Exception('f - config not available');
37+
}
38+
39+
return $config;
40+
}
41+
3242
//TODO
3343
// - třídy
3444
//TODO - strukturu do objektů
45+
3546
/**
36-
* @return array<string, array<class-string, array{
37-
* repeatable: RepeatableBehavior,
38-
* definitionsByClass: non-empty-array<class-string, array{
39-
* propertyStructure: PropertyStructure,
40-
* definitions: non-empty-list<MetaDefinition>,
41-
* }>
47+
* @return array<string, non-empty-array<class-string, array{
48+
* propertyStructure: PropertyStructure,
49+
* scopedDefinitions: array<class-string, list<MetaDefinition>>,
4250
* }>>
4351
*/
4452
public function resolveProperties(CompileMeta $meta): array
4553
{
4654
$target = Target::targetProperty();
4755
$scopedProperties = [];
56+
$scopesByProperty = [];
4857
foreach ($meta->getGroupedProperties() as $propertyName => $groupedProperty) {
4958
if ($groupedProperty === []) {
5059
continue;
@@ -55,12 +64,15 @@ public function resolveProperties(CompileMeta $meta): array
5564
foreach ($groupedProperty as $property) {
5665
$propertyStructure = $property->getPropertyStructure();
5766
$className = $propertyStructure->getSource()->getClass()->getReflector()->getName();
67+
$scopedDefinitions = [];
5868

5969
foreach ($property->getDefinitions() as $definition) {
6070
$this->checkScopeHandlerCompatibility($definition);
6171
$scope = $definition->getScope();
6272
$config = $this->getConfig($definition);
6373

74+
$scopesByProperty[$propertyName][$scope] = true;
75+
6476
$hierarchy = $config->hierarchyPosition;
6577
//TODO - dovolit první třídu, nejen trait? co když bude property definovaná skrze interface?
6678
if ($hierarchy === HierarchyPosition::firstType() && $property !== $firstGroupedProperty) {
@@ -74,21 +86,19 @@ public function resolveProperties(CompileMeta $meta): array
7486
throw new Exception('a - ' . $propertyStructure->getSource()->toString());
7587
}
7688

77-
if (!isset($scopedProperty[$scope])) {
78-
$scopedProperty[$scope] = [
79-
'repeatable' => $config->repeatableBehavior,
80-
'definitionsByClass' => [],
81-
];
82-
}
83-
84-
//TODO - tady by se mohl kontrolovat alespoň unique v rámci jedné třídy
85-
// - možná by se to mohlo oddělit od logiky RepeatableBehavior? nebo nějak odlišit v ní
86-
// - rule musí být unique
87-
$scopedProperty[$scope]['definitionsByClass'][$className]['propertyStructure'] = $propertyStructure;
88-
$scopedProperty[$scope]['definitionsByClass'][$className]['definitions'][] = $definition;
89+
$scopedDefinitions[$scope][] = $definition;
8990
}
91+
92+
//TODO - tady by se mohl kontrolovat alespoň unique v rámci jedné třídy
93+
// - možná by se to mohlo oddělit od logiky RepeatableBehavior? nebo nějak odlišit v ní
94+
// - rule musí být unique
95+
$scopedProperty[$className] = [
96+
'propertyStructure' => $propertyStructure,
97+
'scopedDefinitions' => $scopedDefinitions,
98+
];
9099
}
91100

101+
//TODO - předat dál repeatable pro každý scope
92102
$scopedProperties[$propertyName] = $scopedProperty;
93103
}
94104

@@ -97,10 +107,8 @@ public function resolveProperties(CompileMeta $meta): array
97107
continue;
98108
}
99109

100-
foreach ($scopedProperties as $propertyName => $scopedProperty) {
101-
//TODO - tahle podmínka bude blbě
102-
$definitions = $scopedProperty[$scope] ?? null;
103-
if ($definitions === null) {
110+
foreach ($scopesByProperty as $propertyName => $scopes) {
111+
if (!isset($scopes[$scope])) {
104112
//TODO - definice je vyžadovaná, ale není v property s definicemi dostupná
105113
throw new Exception('b - required ' . $propertyName);
106114
}

0 commit comments

Comments
 (0)