frame !== null) { throw new LogicException('COMPOSITION_NESTED_EXECUTION'); } self::validate($declaration); $verify(); $this->frame = [$declaration, $connection, $verify]; try { return $work(); } finally { $this->frame = null; } } public function allows(string $feature, object $connection): bool { if ($this->frame === null || $this->frame[1] !== $connection) { return false; } ($this->frame[2])(); // Re-read identity, context, permissions, pins and schema. return array_key_exists($feature, $this->frame[0]['features']); } public function component(string $feature, object $connection): array { if (! $this->allows($feature, $connection)) { throw new LogicException('COMPOSITION_CAPABILITY_ABSENT'); } return $this->frame[0]['features'][$feature]; } public function permits(string $operation, object $connection): bool { $feature = str_starts_with($operation, 'purchasing_conflicts.') ? 'conflict-release' : (str_starts_with($operation, 'purchasing_order_cancellations.') ? 'order-cancellation-cycles' : null); return $feature !== null && $this->allows($feature, $connection) && $this->frame[0]['operation'] === $operation; } private static function validate(array $declaration): void { $keys = array_keys($declaration); sort($keys); if ($keys !== ['actor', 'composition', 'contract', 'features', 'generation', 'operation', 'schema_sha256', 'scope', 'workflow_sha256'] || $declaration['contract'] !== self::CONTRACT || ! is_string($declaration['composition']) || ! preg_match('/^[a-f0-9]{12}$/D', $declaration['composition'])) { throw new LogicException('COMPOSITION_CONTRACT'); } if (! is_string($declaration['operation']) || ! preg_match('/^[a-z][a-z0-9_]*(?:\.[a-z0-9_]+)+$/D', $declaration['operation'])) { throw new LogicException('COMPOSITION_OPERATION'); } foreach (['generation', 'schema_sha256', 'workflow_sha256'] as $field) { if (! is_string($declaration[$field]) || ! preg_match('/^[a-f0-9]{64}$/D', $declaration[$field])) { throw new LogicException('COMPOSITION_HASH'); } } foreach (['scope' => ['tenant_id', 'company_id', 'unit_id'], 'actor' => ['host_id', 'native_id']] as $field => $expected) { if (! is_array($declaration[$field]) || count($declaration[$field]) !== count($expected) || array_diff($expected, array_keys($declaration[$field]))) { throw new LogicException('COMPOSITION_SCOPE'); } foreach ($declaration[$field] as $id) { if (! is_int($id) || $id < 1) { throw new LogicException('COMPOSITION_SCOPE'); } } } if (! is_array($declaration['features']) || array_diff(array_keys($declaration['features']), ['conflict-release', 'order-cancellation-cycles'])) { throw new LogicException('COMPOSITION_FEATURE'); } foreach ($declaration['features'] as $feature => $component) { if ($feature === 'conflict-release' && $component !== []) { if (! is_array($component) || array_keys($component) !== ['company_view', 'company_authorise'] || ! is_bool($component['company_view']) || ! is_bool($component['company_authorise'])) { throw new LogicException('COMPOSITION_FEATURE'); } } if ($feature === 'order-cancellation-cycles' && (! is_array($component) || array_keys($component) !== ['root', 'manifest_sha256'] || ! is_string($component['root']) || trim($component['root']) === '' || ! is_string($component['manifest_sha256']) || ! preg_match('/^[a-f0-9]{64}$/D', $component['manifest_sha256']))) { throw new LogicException('COMPOSITION_COMPONENT'); } } } }