Skip to content

Commit

Permalink
Доработки
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Aug 24, 2021
1 parent 4c9a1c3 commit e208497
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 4 deletions.
85 changes: 85 additions & 0 deletions src/DataCollector/Decorators/DataCollectorDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Prokl\WebProfilierBundle\DataCollector\Decorators;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

/**
* Class DataCollectorDecorator
*
* @since 24.08.2021
*/
class DataCollectorDecorator extends DataCollector
{
/**
* @var DataCollector $dataCollector Декорируемый data collector.
*/
private $dataCollector;

/**
* @param DataCollector $dataCollector Декорируемый data collector.
*/
public function __construct(DataCollector $dataCollector)
{
$this->dataCollector = $dataCollector;
}

/**
* Класс декорируемого data collector.
*
* @return string
*/
public function getClass() : string
{
return get_class($this->dataCollector);
}

/**
* Оригинальный data collector.
*
* @return DataCollector
*/
public function getDataCollector(): DataCollector
{
return $this->dataCollector;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->dataCollector->getName();
}

public function lateCollect()
{
return $this->dataCollector->lateCollect();
}

public function reset()
{
return $this->dataCollector->reset();
}

/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
$result = $this->dataCollector->collect($request, $response, $exception);

// Внутренние нужды. Для сообщения между разными модулями в Битриксе
// запускается кастомное событие OnAfterDataCollectorDone
if (defined('B_PROLOG_INCLUDED') && B_PROLOG_INCLUDED===true) {
$events = GetModuleEvents('', 'OnAfterDataCollectorDone', true);
foreach ($events as $event) {
ExecuteModuleEventEx($event, ['dataCollector' => $this]);
}
}

return $result;
}
}
7 changes: 4 additions & 3 deletions src/Events/DataCollectingEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ public function handle(Response $sfResponse = null, Request $sfRequest = null):
return;
}

$json = $this->profileExtractor->extract($profile);

$result[$url] = $json;

if (!$isSymfonyRoute) {
$this->profiler->saveProfile($profile);
}

$json = $this->profileExtractor->extract($profile);

$result[$url] = $json;
$this->dataFileHandler->write($result);
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Extractor/ProfileExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ public function extract(Profile $profile) : array
$externalCollectors = $externalCollectorsBag->all();

foreach ($externalCollectors as $externalCollector) {
$profile->addCollector($externalCollector);
$collectorForAdd = $externalCollector;
// Декоратор
if (method_exists($externalCollector, 'getDataCollector')) {
$collectorForAdd = $externalCollector->getDataCollector();
}
$profile->addCollector($collectorForAdd);
}

foreach ($profile->getCollectors() as $collector) {
Expand Down
7 changes: 7 additions & 0 deletions src/Transformers/EventDataCollectorTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Prokl\WebProfilierBundle\Transformers;

use Prokl\WebProfilierBundle\DataCollector\Decorators\DataCollectorDecorator;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\EventDataCollector;

Expand All @@ -26,6 +27,12 @@ public function getTemplate() : string
*/
public static function support(DataCollector $dataCollector) : bool
{
if (is_a($dataCollector, DataCollectorDecorator::class)) {
$class = $dataCollector->getClass();

return $class === EventDataCollector::class;
}

return is_a($dataCollector, EventDataCollector::class);
}
}

0 comments on commit e208497

Please sign in to comment.