vendor/api-platform/core/src/Core/Bridge/Symfony/Bundle/DataPersister/TraceableChainDataPersister.php line 45

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DataPersister;
  12. use ApiPlatform\Core\DataPersister\ChainDataPersister;
  13. use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
  14. use ApiPlatform\Core\DataPersister\DataPersisterInterface;
  15. use ApiPlatform\Core\DataPersister\ResumableDataPersisterInterface;
  16. /**
  17.  * @author Anthony GRASSIOT <antograssiot@free.fr>
  18.  */
  19. final class TraceableChainDataPersister implements ContextAwareDataPersisterInterface
  20. {
  21.     private $persisters = [];
  22.     private $persistersResponse = [];
  23.     private $decorated;
  24.     public function __construct(DataPersisterInterface $dataPersister)
  25.     {
  26.         if ($dataPersister instanceof ChainDataPersister) {
  27.             $this->decorated $dataPersister;
  28.             $this->persisters $dataPersister->persisters;
  29.         }
  30.     }
  31.     public function getPersistersResponse(): array
  32.     {
  33.         return $this->persistersResponse;
  34.     }
  35.     public function supports($data, array $context = []): bool
  36.     {
  37.         return $this->decorated->supports($data$context);
  38.     }
  39.     public function persist($data, array $context = [])
  40.     {
  41.         $this->tracePersisters($data$context);
  42.         return $this->decorated->persist($data$context);
  43.     }
  44.     public function remove($data, array $context = [])
  45.     {
  46.         $this->tracePersisters($data$context);
  47.         return $this->decorated->remove($data$context);
  48.     }
  49.     private function tracePersisters($data, array $context = [])
  50.     {
  51.         $found false;
  52.         foreach ($this->persisters as $persister) {
  53.             if (
  54.                 ($this->persistersResponse[\get_class($persister)] = $found false $persister->supports($data$context))
  55.                 && !($persister instanceof ResumableDataPersisterInterface && $persister->resumable()) && !$found
  56.             ) {
  57.                 $found true;
  58.             }
  59.         }
  60.     }
  61. }