Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fs/http sniffers #186

Merged
merged 24 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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