protected function instantiateService(string $id, array $arguments)
    {
        if (isset($this->instances[$id])) {
            return $this->instances[$id];
        }

        if (isset($this->factories[$id])) {
            return ($this->factories[$id])($arguments);
        }

        if (isset($this->callableDefinitions[$id])) {
            $this->factories[$id] = $this->createServiceFactoryFromCallable($this->callableDefinitions[$id]);

            return $this->invokeFactory($id, $arguments);
        }

        $class = $this->classDefinitions[$id] ?? $id;
        if ($class !== $id) {
            // Recursive call allows to bind contract to contract.
            return $this->saveShared($id, $this->instantiateService($class, $arguments));
        }
        if (!class_exists($class)) {
            throw new NotFoundException($id, $this->resolving);
        }
        $this->factories[$id] = $this->createServiceFactoryFromClass($class);

        return $this->invokeFactory($id, $arguments);
    }