im trying to set a global constant that would be accessible to all php swoole processes and threads later, but it seems to be not visible or something
Here's my index.php - almost vanilla mezzio - I only see APPROOT constant:
(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();
})();
But then I get an error when I try to reference/use that constant in one of the middlewares:
<?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...
}
}
}
I get this error:
Error Undefined constant "APPROOT"
Why? Is that because all variable outside of the swoole context are discarded and I set this outside of swoole context?
Upon reviewing source code for Mezzio\Swoole\Command\StartCommand I've discovered that index.php that I thought was an entry point to my php swoole application is actually not being used at all.
thus constants were indeed undefined.