Skip to content

Commit

Permalink
Fs/http sniffers (#186)
Browse files Browse the repository at this point in the history
* Add FS sniffing

* Apply fixes from StyleCI

* [ci-review] Apply changes from Rector action.

* Fix return type

* Add http(s) streams wrapper

* Add test for FS stream

* Fix types and stream wrapper logic

* Refactor tests

* Add a test for http stream

* Rename collector

* Add saving response for http client collector

* Rewind bodies for request collector

* Fix broken class names

* Rewrite collecting events

* Apply fixes from StyleCI

* Fix psalm

* Add composer-require-checker.json

* Add TODO

* Move comments to property description

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
Co-authored-by: rector-bot <[email protected]>
  • Loading branch information
3 people authored Feb 13, 2023
1 parent dc6b167 commit f9a215b
Show file tree
Hide file tree
Showing 30 changed files with 1,298 additions and 122 deletions.
5 changes: 5 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"symbol-whitelist": [
"opcache_invalidate"
]
}
25 changes: 20 additions & 5 deletions config/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

declare(strict_types=1);

use Composer\Autoload\ClassLoader;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Yiisoft\Aliases\Aliases;
use Yiisoft\VarDumper\ClosureExporter;
use Yiisoft\VarDumper\UseStatementParser;
use Yiisoft\Yii\Debug\Collector\ContainerProxyConfig;
use Yiisoft\Yii\Debug\Collector\FilesystemStreamCollector;
use Yiisoft\Yii\Debug\Collector\ServiceCollector;
use Yiisoft\Yii\Debug\DebuggerIdGenerator;
use Yiisoft\Yii\Debug\Collector\ContainerProxyConfig;
use Yiisoft\Yii\Debug\Storage\FileStorage;
use Yiisoft\Yii\Debug\Storage\StorageInterface;

Expand All @@ -23,13 +27,13 @@
$excludedClasses = $params['dumper.excludedClasses'];
$fileStorage = new FileStorage($params['path'], $debuggerIdGenerator, $aliases, $excludedClasses);
if (isset($params['historySize'])) {
$fileStorage->setHistorySize((int)$params['historySize']);
$fileStorage->setHistorySize((int) $params['historySize']);
}
return $fileStorage;
},
];

if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) {
if (!(bool) ($params['yiisoft/yii-debug']['enabled'] ?? false)) {
return $common;
}

Expand All @@ -38,8 +42,8 @@
$params = $params['yiisoft/yii-debug'];
$collector = $container->get(ServiceCollector::class);
$dispatcher = $container->get(EventDispatcherInterface::class);
$debuggerEnabled = (bool)($params['enabled'] ?? false);
$trackedServices = (array)($params['trackedServices'] ?? []);
$debuggerEnabled = (bool) ($params['enabled'] ?? false);
$trackedServices = (array) ($params['trackedServices'] ?? []);
$path = $container->get(Aliases::class)->get('@runtime/cache/container-proxy');
$logLevel = $params['logLevel'] ?? 0;
return new ContainerProxyConfig(
Expand All @@ -51,4 +55,15 @@
$logLevel
);
},
FilesystemStreamCollector::class => [
'__construct()' => [
'ignoredPathPatterns' => [],
'ignoredClasses' => [
ClosureExporter::class,
UseStatementParser::class,
FileStorage::class,
ClassLoader::class,
],
],
],
], $common);
4 changes: 4 additions & 0 deletions config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
use Yiisoft\Yii\Debug\Collector\ContainerInterfaceProxy;
use Yiisoft\Yii\Debug\Collector\EventCollector;
use Yiisoft\Yii\Debug\Collector\EventDispatcherInterfaceProxy;
use Yiisoft\Yii\Debug\Collector\FilesystemStreamCollector;
use Yiisoft\Yii\Debug\Collector\HttpClientCollector;
use Yiisoft\Yii\Debug\Collector\HttpClientInterfaceProxy;
use Yiisoft\Yii\Debug\Collector\HttpStreamCollector;
use Yiisoft\Yii\Debug\Collector\LogCollector;
use Yiisoft\Yii\Debug\Collector\LoggerInterfaceProxy;
use Yiisoft\Yii\Debug\Collector\MiddlewareCollector;
Expand Down Expand Up @@ -52,6 +54,8 @@
ValidatorCollector::class,
QueueCollector::class,
HttpClientCollector::class,
FilesystemStreamCollector::class,
HttpStreamCollector::class,
],
'collectors.web' => [
WebAppInfoCollector::class,
Expand Down
81 changes: 81 additions & 0 deletions src/Collector/FilesystemStreamCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Collector;

final class FilesystemStreamCollector implements CollectorInterface, IndexCollectorInterface
{
use CollectorTrait;

public function __construct(
/**
* Collection of regexps to ignore files sources to sniff.
* Examples:
* - '/' . preg_quote('yii-debug/src/Dumper', '/') . '/'
* - '/ClosureExporter/'
*/
private array $ignoredPathPatterns = [],
private array $ignoredClasses = [],
) {
}

/**
* @var array[]
*/
private array $operations = [];

public function getCollected(): array
{
return array_map('array_values', $this->operations);
}

public function startup(): void
{
$this->isActive = true;
FilesystemStreamProxy::register();
FilesystemStreamProxy::$collector = $this;
FilesystemStreamProxy::$ignoredPathPatterns = $this->ignoredPathPatterns;
FilesystemStreamProxy::$ignoredClasses = $this->ignoredClasses;
}

public function shutdown(): void
{
FilesystemStreamProxy::unregister();
FilesystemStreamProxy::$collector = null;
FilesystemStreamProxy::$ignoredPathPatterns = [];
FilesystemStreamProxy::$ignoredClasses = [];

$this->reset();
$this->isActive = false;
}

public function collect(string $operation, string $path, array $args): void
{
if (!$this->isActive()) {
return;
}

$this->operations[$operation][] = [
'path' => $path,
'args' => $args,
];
}

public function getIndexData(): array
{
return [
'fs_stream' => array_merge(
...array_map(
fn (string $operation) => [$operation => count($this->operations[$operation])],
array_keys($this->operations)
)
),
];
}

private function reset(): void
{
$this->operations = [];
}
}
Loading

0 comments on commit f9a215b

Please sign in to comment.