public function initOauth2Server()
    {
        if ($this->serverInitialized) {
            return;
        }

        $this->serverInitialized = true;
        $this->modelMap = array_merge($this->defaultModelMap, $this->modelMap);
        $this->storageMap = array_merge($this->defaultStorageMap, $this->storageMap);
        foreach ($this->modelMap as $name => $definition) {
            Yii::$container->set(models::class . '\\' . $name, $definition);
        }

        foreach ($this->storageMap as $name => $definition) {
            Yii::$container->set($name, $definition);
        }

        $storages = [];
        foreach(array_keys($this->storageMap) as $name) {
            $storages[$name] = Yii::$container->get($name);
        }

        $grantTypes = [];
        foreach($this->grantTypes as $name => $options) {
            if(!isset($storages[$name]) || empty($options['class'])) {
                throw new InvalidConfigException(
                    'Invalid grant types configuration.'
                );
            }

            $class = $options['class'];
            unset($options['class']);

            $reflection = new ReflectionClass($class);
            $config = array_merge([0 => $storages[$name]], [$options]);

            $instance = $reflection->newInstanceArgs($config);
            $grantTypes[$name] = $instance;
        }

        $this->set('server', Yii::$container->get(Server::class, [
            $this,
            $storages,
            [
                'token_param_name' => $this->tokenParamName,
                'access_lifetime' => $this->tokenAccessLifetime,
            ],
            $grantTypes
        ]));
        $this->set('request', Request::createFromGlobals());
        $this->set('response', new Response());
    }