vendor/symfony/symfony/src/Symfony/Component/Workflow/Registry.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.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. namespace Symfony\Component\Workflow;
  11. use Symfony\Component\Workflow\Exception\InvalidArgumentException;
  12. use Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface;
  13. use Symfony\Component\Workflow\SupportStrategy\WorkflowSupportStrategyInterface;
  14. /**
  15.  * @author Fabien Potencier <fabien@symfony.com>
  16.  * @author GrĂ©goire Pineau <lyrixx@lyrixx.info>
  17.  */
  18. class Registry
  19. {
  20.     private $workflows = [];
  21.     /**
  22.      * @param SupportStrategyInterface $supportStrategy
  23.      *
  24.      * @deprecated since Symfony 4.1, use addWorkflow() instead
  25.      */
  26.     public function add(Workflow $workflow$supportStrategy)
  27.     {
  28.         @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1. Use addWorkflow() instead.'__METHOD__), E_USER_DEPRECATED);
  29.         $this->workflows[] = [$workflow$supportStrategy];
  30.     }
  31.     public function addWorkflow(WorkflowInterface $workflowWorkflowSupportStrategyInterface $supportStrategy)
  32.     {
  33.         $this->workflows[] = [$workflow$supportStrategy];
  34.     }
  35.     /**
  36.      * @param object      $subject
  37.      * @param string|null $workflowName
  38.      *
  39.      * @return Workflow
  40.      */
  41.     public function get($subject$workflowName null)
  42.     {
  43.         $matched = [];
  44.         foreach ($this->workflows as list($workflow$supportStrategy)) {
  45.             if ($this->supports($workflow$supportStrategy$subject$workflowName)) {
  46.                 $matched[] = $workflow;
  47.             }
  48.         }
  49.         if (!$matched) {
  50.             throw new InvalidArgumentException(sprintf('Unable to find a workflow for class "%s".', \get_class($subject)));
  51.         }
  52.         if (<= \count($matched)) {
  53.             $names array_map(static function (WorkflowInterface $workflow): string {
  54.                 return $workflow->getName();
  55.             }, $matched);
  56.             throw new InvalidArgumentException(sprintf('Too many workflows (%s) match this subject (%s); set a different name on each and use the second (name) argument of this method.'implode(', '$names), \get_class($subject)));
  57.         }
  58.         return $matched[0];
  59.     }
  60.     /**
  61.      * @param object $subject
  62.      *
  63.      * @return Workflow[]
  64.      */
  65.     public function all($subject): array
  66.     {
  67.         $matched = [];
  68.         foreach ($this->workflows as list($workflow$supportStrategy)) {
  69.             if ($supportStrategy->supports($workflow$subject)) {
  70.                 $matched[] = $workflow;
  71.             }
  72.         }
  73.         return $matched;
  74.     }
  75.     /**
  76.      * @param WorkflowSupportStrategyInterface $supportStrategy
  77.      * @param object                           $subject
  78.      */
  79.     private function supports(WorkflowInterface $workflow$supportStrategy$subject, ?string $workflowName): bool
  80.     {
  81.         if (null !== $workflowName && $workflowName !== $workflow->getName()) {
  82.             return false;
  83.         }
  84.         return $supportStrategy->supports($workflow$subject);
  85.     }
  86. }