private function lookupSchema($schema, string $schemaPath) {
        if ($schema instanceof Schema) {
            return [$schema, $schemaPath];
        } else {
            $lookup = $this->getRefLookup();
            $visited = [];

            // Resolve any references first.
            while (!empty($schema['$ref'])) {
                $schemaPath = $schema['$ref'];

                if (isset($visited[$schemaPath])) {
                    throw new RefNotFoundException("Cyclical reference cannot be resolved. ($schemaPath)", 508);
                }
                $visited[$schemaPath] = true;

                try {
                    $schema = call_user_func($lookup, $schemaPath);
                } catch (\Exception $ex) {
                    throw new RefNotFoundException($ex->getMessage(), $ex->getCode(), $ex);
                }
                if ($schema === null) {
                    throw new RefNotFoundException("Schema reference could not be found. ($schemaPath)");
                }
            }

            return [$schema, $schemaPath];
        }
    }