繁体   English   中英

使用 mezzio swoole/openswoole 定义全局常量

[英]Define global constant with mezzio swoole/openswoole

我试图设置一个全局常量,以后所有 php swoole 进程和线程都可以访问它,但它似乎不可见或其他东西

这是我的 index.php - 几乎是香草 mezzio - 我只看到APPROOT常量:

(function () {
    if (!defined('APPROOT')) {
        define('APPROOT', __DIR__);
    }

    /** @var \Psr\Container\ContainerInterface $container */
    $container = require 'config/container.php';

    /** @var \Mezzio\Application $app */
    $app     = $container->get(\Mezzio\Application::class);
    $factory = $container->get(\Mezzio\MiddlewareFactory::class);

    // Execute programmatic/declarative middleware pipeline and routing
    // configuration statements
    (require 'config/pipeline.php')($app, $factory, $container);
    (require 'config/routes.php')($app, $factory, $container);

    $app->run();
})();

但是当我尝试在其中一个中间件中引用/使用该常量时出现错误:

<?php

declare(strict_types=1);

namespace Application\Middleware;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class BootstrapMiddleware implements MiddlewareInterface
{
    public function __construct(private ResponseFactoryInterface $responseFactory)
    {
    }

    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {


        $this->setAssetsCompiledLoc();

        $response = $handler->handle($request);
        return $response;
    }

    private function setAssetsCompiledLoc()
    {
        if ( ! defined('ASSET_MAP')) {
            $manifestPath = \APPROOT . '/manifests/manifest-' . ENV . '.json';
            // ...blah...
        }
    }
}

我收到此错误:

错误未定义常量“APPROOT”

为什么? 那是因为 swoole 上下文之外的所有变量都被丢弃了,而我在 swoole 上下文之外设置了这个?

在查看 Mezzio\Swoole\Command\StartCommand 的源代码后,我发现我认为是我的 php swoole 应用程序的入口点的 index.php 实际上根本没有被使用。
因此常量确实是未定义的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM