diff --git a/composer.json b/composer.json index a98fa2fbe..8537a5c3d 100644 --- a/composer.json +++ b/composer.json @@ -62,7 +62,6 @@ "yiisoft/error-handler": "^3.0", "yiisoft/event-dispatcher": "^1.0", "yiisoft/log": "^2.0", - "yiisoft/middleware-dispatcher": "^3.0|^4.0|^5.0", "yiisoft/yii-console": "^2.0", "yiisoft/yii-http": "^1.0" }, diff --git a/config/events-web.php b/config/events-web.php index 80950d0db..fb8c94ebe 100644 --- a/config/events-web.php +++ b/config/events-web.php @@ -3,13 +3,10 @@ declare(strict_types=1); use Yiisoft\ErrorHandler\Event\ApplicationError; -use Yiisoft\Middleware\Dispatcher\Event\AfterMiddleware; -use Yiisoft\Middleware\Dispatcher\Event\BeforeMiddleware; use Yiisoft\Profiler\ProfilerInterface; -use Yiisoft\Yii\Debug\Collector\Web\MiddlewareCollector; +use Yiisoft\Yii\Debug\Collector\ExceptionCollector; use Yiisoft\Yii\Debug\Collector\Web\RequestCollector; use Yiisoft\Yii\Debug\Collector\Web\WebAppInfoCollector; -use Yiisoft\Yii\Debug\Collector\ExceptionCollector; use Yiisoft\Yii\Debug\Debugger; use Yiisoft\Yii\Http\Event\AfterEmit; use Yiisoft\Yii\Http\Event\AfterRequest; @@ -17,7 +14,7 @@ use Yiisoft\Yii\Http\Event\ApplicationStartup; use Yiisoft\Yii\Http\Event\BeforeRequest; -if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) { +if (!(bool) ($params['yiisoft/yii-debug']['enabled'] ?? false)) { return []; } @@ -42,12 +39,6 @@ [WebAppInfoCollector::class, 'collect'], [Debugger::class, 'shutdown'], ], - BeforeMiddleware::class => [ - [MiddlewareCollector::class, 'collect'], - ], - AfterMiddleware::class => [ - [MiddlewareCollector::class, 'collect'], - ], ApplicationError::class => [ [ExceptionCollector::class, 'collect'], ], diff --git a/config/params.php b/config/params.php index 57d383650..2c02981ac 100644 --- a/config/params.php +++ b/config/params.php @@ -22,7 +22,6 @@ use Yiisoft\Yii\Debug\Collector\Stream\HttpStreamCollector; use Yiisoft\Yii\Debug\Collector\TimelineCollector; use Yiisoft\Yii\Debug\Collector\VarDumperCollector; -use Yiisoft\Yii\Debug\Collector\Web\MiddlewareCollector; use Yiisoft\Yii\Debug\Collector\Web\RequestCollector; use Yiisoft\Yii\Debug\Collector\Web\WebAppInfoCollector; use Yiisoft\Yii\Debug\Command\DebugContainerCommand; @@ -50,7 +49,6 @@ 'collectors.web' => [ WebAppInfoCollector::class, RequestCollector::class, - MiddlewareCollector::class, ], 'collectors.console' => [ ConsoleAppInfoCollector::class, diff --git a/src/Collector/Web/MiddlewareCollector.php b/src/Collector/Web/MiddlewareCollector.php deleted file mode 100644 index 784aa0370..000000000 --- a/src/Collector/Web/MiddlewareCollector.php +++ /dev/null @@ -1,114 +0,0 @@ -isActive()) { - return []; - } - $beforeStack = $this->beforeStack; - $afterStack = $this->afterStack; - $beforeAction = array_pop($beforeStack); - $afterAction = array_shift($afterStack); - $actionHandler = []; - - if ($beforeAction !== null && $afterAction !== null) { - $actionHandler = $this->getActionHandler($beforeAction, $afterAction); - } - - return [ - 'beforeStack' => $beforeStack, - 'actionHandler' => $actionHandler, - 'afterStack' => $afterStack, - ]; - } - - public function collect(BeforeMiddleware|AfterMiddleware $event): void - { - if (!$this->isActive()) { - return; - } - - if ( - method_exists($event->getMiddleware(), '__debugInfo') - && (new ReflectionClass($event->getMiddleware()))->isAnonymous() - ) { - $callback = $event->getMiddleware()->__debugInfo()['callback']; - if (is_array($callback)) { - $name = implode('::', $callback); - } else { - $name = 'object(Closure)#' . spl_object_id($callback); - } - } else { - $name = $event->getMiddleware()::class; - } - if ($event instanceof BeforeMiddleware) { - $this->beforeStack[] = [ - 'name' => $name, - 'time' => microtime(true), - 'memory' => memory_get_usage(), - 'request' => $event->getRequest(), - ]; - } else { - $this->afterStack[] = [ - 'name' => $name, - 'time' => microtime(true), - 'memory' => memory_get_usage(), - 'response' => $event->getResponse(), - ]; - } - $this->timelineCollector->collect($this, spl_object_id($event)); - } - - private function reset(): void - { - $this->beforeStack = []; - $this->afterStack = []; - } - - public function getSummary(): array - { - if (!$this->isActive()) { - return []; - } - return [ - 'middleware' => [ - 'total' => ($total = count($this->beforeStack)) > 0 ? $total - 1 : 0, // Remove action handler - ], - ]; - } - - private function getActionHandler(array $beforeAction, array $afterAction): array - { - return [ - 'name' => $beforeAction['name'], - 'startTime' => $beforeAction['time'], - 'request' => $beforeAction['request'], - 'response' => $afterAction['response'], - 'endTime' => $afterAction['time'], - 'memory' => $afterAction['memory'], - ]; - } -} diff --git a/tests/Unit/Collector/MiddlewareCollectorTest.php b/tests/Unit/Collector/MiddlewareCollectorTest.php deleted file mode 100644 index 81fd4efe6..000000000 --- a/tests/Unit/Collector/MiddlewareCollectorTest.php +++ /dev/null @@ -1,57 +0,0 @@ -collect(new BeforeMiddleware($this->createCallableMiddleware(static fn () => 1), new ServerRequest('GET', '/test'))); - $collector->collect(new BeforeMiddleware($this->createCallableMiddleware([DummyMiddleware::class, 'process']), new ServerRequest('GET', '/test'))); - $collector->collect(new BeforeMiddleware(new DummyMiddleware(), new ServerRequest('GET', '/test'))); - $collector->collect(new AfterMiddleware(new DummyMiddleware(), new Response(200))); - $collector->collect(new AfterMiddleware($this->createCallableMiddleware(static fn () => 1), new Response(200))); - $collector->collect(new AfterMiddleware($this->createCallableMiddleware([DummyMiddleware::class, 'process']), new Response(200))); - } - - protected function getCollector(): CollectorInterface - { - return new MiddlewareCollector(new TimelineCollector()); - } - - protected function checkCollectedData(array $data): void - { - parent::checkCollectedData($data); - - $this->assertNotEmpty($data['beforeStack']); - $this->assertNotEmpty($data['afterStack']); - $this->assertNotEmpty($data['actionHandler']); - $this->assertEquals(DummyMiddleware::class, $data['actionHandler']['name']); - $this->assertEquals('GET', $data['actionHandler']['request']->getMethod()); - } - - private function createCallableMiddleware(callable|array $callable): MiddlewareInterface - { - $factory = new MiddlewareFactory(new Container(ContainerConfig::create())); - return $factory->create($callable); - } -} diff --git a/tests/Unit/Support/DummyMiddleware.php b/tests/Unit/Support/DummyMiddleware.php deleted file mode 100644 index 505d8d10b..000000000 --- a/tests/Unit/Support/DummyMiddleware.php +++ /dev/null @@ -1,18 +0,0 @@ -handle($request); - } -}