<?php

namespace Application\Service\Extension;


use Application\Common\Container;
use Application\Mvc\Controller\Plugins\LocalService;
use Laminas\Mvc\Application;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Swoole\Coroutine;

/**
 *
 */
class Laminas
{
    /**
     * @var Application|null
     */
    public static ?Application $app = null;

    /**
     * @var Container
     */
    public static ?LocalService $serviceManager = null;

    /**
     * @var string
     */
    public static string $token = '';

    /**
     * @var array
     */
    public static array $coroutineToken = [];

    /**
     * 设置协程token
     * @param string $token
     */
    public static function setToken(string $token)
    {
        self::$coroutineToken[Coroutine::getCid()] = $token;
    }

    /**
     * 获取协程token
     * @return mixed
     */
    public static function getToken()
    {
        return self::$coroutineToken[Coroutine::getCid()];
    }

    /**
     * @param Application $app
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function initialize(Application $app)
    {
        self::$app = $app;
        self::$serviceManager = $app->getServiceManager()->get('ControllerPluginManager')->get('LocalService');
    }
}