| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace LeKoala\DebugBar\Extension; |
||||||
| 4 | |||||||
| 5 | use LeKoala\DebugBar\DebugBar; |
||||||
| 6 | use SilverStripe\Control\Controller; |
||||||
| 7 | use SilverStripe\Core\Extension; |
||||||
| 8 | use LeKoala\DebugBar\Collector\HeaderCollector; |
||||||
| 9 | use LeKoala\DebugBar\Collector\SilverStripeCollector; |
||||||
| 10 | |||||||
| 11 | /** |
||||||
| 12 | * A controller extension to log times and render the Debug Bar |
||||||
| 13 | */ |
||||||
| 14 | class ControllerExtension extends Extension |
||||||
| 15 | { |
||||||
| 16 | public function onBeforeInit() |
||||||
| 17 | { |
||||||
| 18 | DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
||||||
| 19 | // We must set the current controller when it's available and before it's pushed out of stack |
||||||
| 20 | /** @var \LeKoala\DebugBar\Collector\SilverStripeCollector $ssCollector */ |
||||||
| 21 | $ssCollector = $debugbar->getCollector('silverstripe'); |
||||||
| 22 | $ssCollector->setController(Controller::curr()); |
||||||
| 23 | |||||||
| 24 | /** @var DebugBar\DataCollector\TimeDataCollector $timeData */ |
||||||
| 25 | $timeData = $debugbar->getCollector('time'); |
||||||
| 26 | if (!$timeData) { |
||||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||||
| 27 | return; |
||||||
| 28 | } |
||||||
| 29 | if ($timeData->hasStartedMeasure('pre_request')) { |
||||||
| 30 | $timeData->stopMeasure("pre_request"); |
||||||
| 31 | } |
||||||
| 32 | $timeData->startMeasure("init", get_class($this->owner) . ' init'); |
||||||
| 33 | }); |
||||||
| 34 | } |
||||||
| 35 | |||||||
| 36 | public function onAfterInit() |
||||||
| 37 | { |
||||||
| 38 | // Avoid requirements being called to early due to RootURLController/ModelAsController |
||||||
| 39 | if ($this->owner instanceof \SilverStripe\CMS\Controllers\RootURLController) { |
||||||
|
0 ignored issues
–
show
The type
SilverStripe\CMS\Controllers\RootURLController was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 40 | return; |
||||||
| 41 | } |
||||||
| 42 | if ($this->owner instanceof \SilverStripe\CMS\Controllers\ModelAsController) { |
||||||
|
0 ignored issues
–
show
The type
SilverStripe\CMS\Controllers\ModelAsController was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 43 | return; |
||||||
| 44 | } |
||||||
| 45 | DebugBar::includeRequirements(); |
||||||
| 46 | |||||||
| 47 | DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
||||||
| 48 | // Add the headers Collector |
||||||
| 49 | if (!$debugbar->hasCollector('Headers') && DebugBar::config()->get('header_collector')) { |
||||||
| 50 | $debugbar->addCollector(new HeaderCollector($this->owner)); |
||||||
| 51 | } |
||||||
| 52 | /** @var DebugBar\DataCollector\TimeDataCollector $timeData */ |
||||||
| 53 | $timeData = $debugbar->getCollector('time'); |
||||||
| 54 | if (!$timeData) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 55 | return; |
||||||
| 56 | } |
||||||
| 57 | if ($timeData->hasStartedMeasure("cms_init")) { |
||||||
| 58 | $timeData->stopMeasure("cms_init"); |
||||||
| 59 | } |
||||||
| 60 | if ($timeData->hasStartedMeasure("init")) { |
||||||
| 61 | $timeData->stopMeasure("init"); |
||||||
| 62 | } |
||||||
| 63 | $timeData->startMeasure("handle", get_class($this->owner) . ' handle request'); |
||||||
| 64 | }); |
||||||
| 65 | } |
||||||
| 66 | |||||||
| 67 | /** |
||||||
| 68 | * @param HTTPRequest $request |
||||||
|
0 ignored issues
–
show
|
|||||||
| 69 | * @param string $action |
||||||
| 70 | */ |
||||||
| 71 | public function beforeCallActionHandler($request, $action) |
||||||
| 72 | { |
||||||
| 73 | // If we don't have an action, getViewer will be called immediatly |
||||||
| 74 | // If we have custom routes, request action is different than action |
||||||
| 75 | $allParams = $request->allParams(); |
||||||
| 76 | $requestAction = null; |
||||||
| 77 | if (!empty($allParams['Action'])) { |
||||||
| 78 | $requestAction = $allParams['Action']; |
||||||
| 79 | } |
||||||
| 80 | if (!$this->owner->hasMethod($action) || ($requestAction && $requestAction != $action)) { |
||||||
| 81 | self::clearBuffer(); |
||||||
| 82 | } |
||||||
| 83 | |||||||
| 84 | DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($action) { |
||||||
| 85 | /** @var DebugBar\DataCollector\TimeDataCollector $timeData */ |
||||||
| 86 | $timeData = $debugBar->getCollector('time'); |
||||||
| 87 | if (!$timeData) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 88 | return; |
||||||
| 89 | } |
||||||
| 90 | if ($timeData->hasStartedMeasure("handle")) { |
||||||
| 91 | $timeData->stopMeasure("handle"); |
||||||
| 92 | } |
||||||
| 93 | $timeData->startMeasure("action", get_class($this->owner) . " action $action"); |
||||||
| 94 | }); |
||||||
| 95 | } |
||||||
| 96 | |||||||
| 97 | /** |
||||||
| 98 | * @param HTTPRequest $request |
||||||
| 99 | * @param string $action |
||||||
| 100 | * @param mixed $result |
||||||
| 101 | */ |
||||||
| 102 | public function afterCallActionHandler($request, $action, $result) |
||||||
|
0 ignored issues
–
show
The parameter
$result is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$request is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 103 | { |
||||||
| 104 | self::clearBuffer(); |
||||||
| 105 | |||||||
| 106 | DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($action) { |
||||||
| 107 | /** @var DebugBar\DataCollector\TimeDataCollector $timeData */ |
||||||
| 108 | $timeData = $debugBar->getCollector('time'); |
||||||
| 109 | if (!$timeData) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 110 | return; |
||||||
| 111 | } |
||||||
| 112 | if ($timeData->hasStartedMeasure("action")) { |
||||||
| 113 | $timeData->stopMeasure("action"); |
||||||
| 114 | } |
||||||
| 115 | $timeData->startMeasure( |
||||||
| 116 | "after_action", |
||||||
| 117 | get_class($this->owner) . " after action $action" |
||||||
| 118 | ); |
||||||
| 119 | }); |
||||||
| 120 | } |
||||||
| 121 | |||||||
| 122 | protected static function clearBuffer() |
||||||
| 123 | { |
||||||
| 124 | if (!DebugBar::$bufferingEnabled) { |
||||||
| 125 | return; |
||||||
| 126 | } |
||||||
| 127 | $buffer = ob_get_clean(); |
||||||
| 128 | if (!empty($buffer)) { |
||||||
| 129 | unset($_REQUEST['debug_request']); // Disable further messages that we can't intercept |
||||||
| 130 | SilverStripeCollector::setDebugData($buffer); |
||||||
| 131 | } |
||||||
| 132 | } |
||||||
| 133 | } |
||||||
| 134 |