<?php
namespace App\V4\Dev\APIPlatform;
use ApiPlatform\Core\Bridge\Symfony\Bundle\DataProvider\TraceableChainCollectionDataProvider as ApiPlatformTraceableChainCollectionDataProvider;
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use ReflectionClass;
use Symfony\Component\Stopwatch\Stopwatch;
class TraceableContextAwareCollectionDataProvider implements ContextAwareCollectionDataProviderInterface
{
/**
* @var ApiPlatformTraceableChainCollectionDataProvider
*/
private $decorated;
/**
* @var Stopwatch
*/
private $stopwatch;
/**
* @param ApiPlatformTraceableChainCollectionDataProvider $decorated
* @param Stopwatch $stopwatch
*/
public function __construct(
ApiPlatformTraceableChainCollectionDataProvider $decorated,
Stopwatch $stopwatch
) {
$this->decorated = $decorated;
$this->stopwatch = $stopwatch;
}
/**
* @param string $resourceClass
* @param string|null $operationName
* @param array $context
*
* @return array|iterable
*
* @throws ResourceClassNotSupportedException
*/
public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
{
$stopwatchKey = sprintf('%s->%s_%s', (new ReflectionClass($this->decorated))->getShortName(), $resourceClass, $operationName);
$this->stopwatch->start($stopwatchKey, 'ContextAwareCollectionDataProvider');
$results = $this->decorated->getCollection($resourceClass, $operationName, $context);
$this->stopwatch->stop($stopwatchKey);
return $results;
}
}