|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the ekino Drupal Debug project. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) ekino |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Ekino\Drupal\Debug\Action\DisplayDumpLocation; |
|
15
|
|
|
|
|
16
|
|
|
use Ekino\Drupal\Debug\Action\EventSubscriberActionInterface; |
|
17
|
|
|
use Ekino\Drupal\Debug\Kernel\Event\DebugKernelEvents; |
|
18
|
|
|
use Ekino\Drupal\Debug\Action\DisplayDumpLocation\SourceContextProvider as BackPortedSourceContextProvider; |
|
19
|
|
|
use Symfony\Component\VarDumper\Cloner\VarCloner; |
|
20
|
|
|
use Symfony\Component\VarDumper\Dumper\CliDumper; |
|
21
|
|
|
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider; |
|
|
|
|
|
|
22
|
|
|
use Symfony\Component\VarDumper\Dumper\HtmlDumper; |
|
23
|
|
|
use Symfony\Component\VarDumper\VarDumper; |
|
24
|
|
|
|
|
25
|
|
|
class DisplayDumpLocationAction implements EventSubscriberActionInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public static function getSubscribedEvents(): array |
|
31
|
|
|
{ |
|
32
|
|
|
return array( |
|
33
|
1 |
|
DebugKernelEvents::ON_KERNEL_INSTANTIATION => 'process', |
|
34
|
|
|
); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function process(): void |
|
38
|
|
|
{ |
|
39
|
1 |
|
$cloner = new VarCloner(); |
|
40
|
1 |
|
$dumper = \in_array(\PHP_SAPI, array('cli', 'phpdbg'), true) ? new CliDumper() : new HtmlDumper(); |
|
41
|
1 |
|
$sourceContextProvider = $this->getSourceContextProvider(); |
|
42
|
|
|
|
|
43
|
|
|
VarDumper::setHandler(function ($var) use ($cloner, $dumper, $sourceContextProvider ): void { |
|
44
|
|
|
(function () use ($sourceContextProvider) : void { |
|
45
|
|
|
list('name' => $name, 'file' => $file, 'line' => $line) = $sourceContextProvider->getContext(); |
|
46
|
|
|
|
|
47
|
|
|
$attr = array(); |
|
48
|
|
|
if ($this instanceof HtmlDumper) { |
|
|
|
|
|
|
49
|
|
|
if (\is_string($file)) { |
|
50
|
|
|
$attr = array( |
|
51
|
|
|
'file' => $file, |
|
52
|
|
|
'line' => $line, |
|
53
|
|
|
'title' => $file, |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
} else { |
|
57
|
|
|
$name = $file; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->line = \sprintf('%s on line %s:', $this->style('meta', $name, $attr), $this->style('meta', $line)); |
|
|
|
|
|
|
61
|
|
|
$this->dumpLine(0); |
|
|
|
|
|
|
62
|
|
|
})->bindTo($dumper, $dumper)(); |
|
63
|
|
|
|
|
64
|
|
|
$dumper->dump($cloner->cloneVar($var)); |
|
65
|
1 |
|
}); |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get the Source Context Provider. |
|
70
|
|
|
* It will return an instance of the SourceContextProvider if existing. |
|
71
|
|
|
* Otherwise, it will return an instance of |
|
72
|
|
|
* the BackPortedSourceContextProvider. |
|
73
|
|
|
*/ |
|
74
|
2 |
|
private function getSourceContextProvider() |
|
75
|
|
|
{ |
|
76
|
2 |
|
if (!\class_exists(SourceContextProvider::class)) { |
|
77
|
|
|
return new BackPortedSourceContextProvider(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
return new SourceContextProvider(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: