Issues (1268)

bootstrap.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
use RssApp\Bootstrap;
6
use RssApp\Components\Registry;
7
use RssApp\Controller;
8
use RssApp\RequestBootstrap;
9
use Spiral\Debug;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Debug. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
11
define("BASEPATH", dirname(__FILE__));
12
define("DS", DIRECTORY_SEPARATOR);
13
define("APPLICATION_ENV", getenv("APPLICATION_ENV") ?? 'dev');
14
15
require_once BASEPATH.DS.'external'.DS.'autoload.php';
16
17
$dumper = new Debug\Dumper();
18
$dumper->setRenderer(Debug\Dumper::ERROR_LOG, new Debug\Renderer\ConsoleRenderer());
19
function dump($msg) {
20
    global $dumper;
21
    $dumper->dump($msg, Debug\Dumper::ERROR_LOG);
22
}
23
24
try {
25
    Bootstrap::initialize();
26
} catch (Exception $e) {
27
    dump("Problem with application bootstrap: ".$e->getMessage());
28
}
29
30
set_error_handler(['RssApp\Components\Error\Handler', 'handle']);
31
32
$relay = new Spiral\Goridge\StreamRelay(STDIN, STDOUT);
33
$psr7  = new Spiral\RoadRunner\PSR7Client(new Spiral\RoadRunner\Worker($relay));
34
while ($req = $psr7->acceptRequest()) {
35
    try {
36
        Registry::set('request', $req);
37
        RequestBootstrap::initialize();
38
39
        $resp = Controller::handle();
40
        $psr7->respond($resp);
41
    } catch (Throwable $e) {
42
        $psr7->getWorker()->error((string) $e);
43
    }
44
}
45