zenstruck /
browser
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Zenstruck\Browser; |
||
| 4 | |||
| 5 | use Symfony\Component\BrowserKit\HttpBrowser as SymfonyHttpBrowser; |
||
| 6 | use Symfony\Component\HttpKernel\Profiler\Profile; |
||
| 7 | use Symfony\Component\HttpKernel\Profiler\Profiler; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @author Kevin Bond <[email protected]> |
||
| 11 | * |
||
| 12 | * @extends BrowserKitBrowser<SymfonyHttpBrowser> |
||
| 13 | */ |
||
| 14 | class HttpBrowser extends BrowserKitBrowser |
||
| 15 | { |
||
| 16 | private ?Profiler $profiler = null; |
||
| 17 | |||
| 18 | final public function __construct(SymfonyHttpBrowser $inner) |
||
| 19 | { |
||
| 20 | parent::__construct($inner); |
||
| 21 | } |
||
| 22 | |||
| 23 | final public function setProfiler(Profiler $profiler): self |
||
| 24 | { |
||
| 25 | $this->profiler = $profiler; |
||
| 26 | |||
| 27 | return $this; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Profile collection must be enabled globally for this feature. |
||
| 32 | */ |
||
| 33 | final public function profile(): Profile |
||
| 34 | { |
||
| 35 | if (!$this->profiler) { |
||
| 36 | throw new \RuntimeException('The profiler has not been set. Is profiling enabled?'); |
||
| 37 | } |
||
| 38 | |||
| 39 | if (!$token = $this->inner()->getInternalResponse()->getHeader('x-debug-token')) { |
||
| 40 | throw new \RuntimeException('Profiling is not enabled for this request. You must enable profile collection globally when using the HttpBrowser.'); |
||
| 41 | } |
||
| 42 | |||
| 43 | if (!$profile = $this->profiler->loadProfile($token)) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 44 | throw new \RuntimeException('Could not find profile for this request.'); |
||
| 45 | } |
||
| 46 | |||
| 47 | return $profile; |
||
| 48 | } |
||
| 49 | } |
||
| 50 |