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

cycle/orm collector #203

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"yiisoft/yii-queue": "3.0.x-dev"
},
"require-dev": {
"cycle/orm": "^2.2",
"maglnet/composer-require-checker": "^4.2",
"nyholm/psr7": "^1.3",
"phpunit/phpunit": "^9.4",
Expand Down
50 changes: 50 additions & 0 deletions src/Collector/Database/CycleCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Collector\Database;

use Yiisoft\Yii\Debug\Collector\CollectorInterface;
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
use Yiisoft\Yii\Debug\Collector\IndexCollectorInterface;

final class CycleCollector implements CollectorInterface, IndexCollectorInterface
{
use CollectorTrait;

private array $queries = [];

public function getCollected(): array
{
return $this->queries;
}

public function collect(...$args): void
{
$this->queries[] = $args;
}

private function collectQuery(string $sql, array $params, string $line): void
{
$this->queries[] = [
'rawSql' => $sql,
'params' => $params,
'line' => $line,
'time' => microtime(true),
];
}

public function getIndexData(): array
{
return [
'cycle' => [
'total' => count($this->queries),
],
];
}

private function reset(): void
{
$this->queries = [];
}
}
114 changes: 114 additions & 0 deletions src/Collector/Database/CycleORMInterfaceProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Collector\Database;

use Cycle\ORM\FactoryInterface;
use Cycle\ORM\Heap\HeapInterface;
use Cycle\ORM\Heap\Node;
use Cycle\ORM\MapperInterface;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\RelationMap;
use Cycle\ORM\RepositoryInterface;
use Cycle\ORM\Select\SourceInterface;
use Cycle\ORM\Transaction\CommandGeneratorInterface;

final class CycleORMInterfaceProxy implements ORMInterface
{
public function __construct(
private ORMInterface $orm,
private CycleCollector $collector
) {
}

public function get(string $role, array $scope, bool $load = true): ?object
{
$this->collector->collect('get', $role, $scope, $load);
return $this->orm->get($role, $scope, $load);
}

public function getIndexes(string $entity): array
{
$this->collector->collect('getIndexes', $entity);
return $this->orm->getIndexes($entity);
}

public function resolveRole(object|string $entity): string
{
$this->collector->collect('resolveRole', $entity);
return $this->orm->resolveRole($entity);
}

public function make(string $role, array $data = [], int $status = Node::NEW, bool $typecast = false): object
{
$this->collector->collect('make', $role, $data, $status, $typecast);
return $this->orm->make($role, $data, $status, $typecast);
}

public function getFactory(): FactoryInterface
{
$this->collector->collect('getFactory');
return $this->orm->getFactory();
}

public function getCommandGenerator(): CommandGeneratorInterface
{
$this->collector->collect('getCommandGenerator');
return $this->orm->getCommandGenerator();
}

public function getService(string $class): object
{
$this->collector->collect('getService', $class);
return $this->orm->getService($class);
}

public function getSchema(): \Cycle\ORM\SchemaInterface
{
$this->collector->collect('getSchema');
return $this->orm->getSchema();
}

public function getHeap(): HeapInterface
{
$this->collector->collect('getHeap');
return $this->orm->getHeap();
}

public function with(
?\Cycle\ORM\SchemaInterface $schema = null,
?FactoryInterface $factory = null,
?HeapInterface $heap = null
): ORMInterface {
$this->collector->collect('with', $schema, $factory, $heap);
return new self($this->orm->with($schema, $factory, $heap), $this->collector);
}

public function getMapper(object|string $entity): MapperInterface
{
$this->collector->collect('getMapper', $entity);
return $this->orm->getMapper($entity);
}

public function getRepository(object|string $entity): RepositoryInterface
{
// var_dump($this->orm);
// exit();
// throw new \InvalidArgumentException();
$this->collector->collect('getRepository', $entity);
return $this->orm->getRepository($entity);
}

public function getRelationMap(string $entity): RelationMap
{
$this->collector->collect('getRelationMap', $entity);
return $this->orm->getRelationMap($entity);
}

public function getSource(string $entity): SourceInterface
{
$this->collector->collect('getSource', $entity);
return $this->orm->getSource($entity);
}
}
1 change: 1 addition & 0 deletions src/Collector/Web/AuthenticationMethodInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Auth\AuthenticationMethodInterface;
use Yiisoft\Auth\IdentityInterface;
use Yiisoft\Yii\Debug\Collector\IdentityCollector;

final class AuthenticationMethodInterfaceProxy implements AuthenticationMethodInterface
{
Expand Down