2013-12-01 09:16:07 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Workflow ;
2016-03-25 15:43:30 +00:00
use Symfony\Component\Workflow\Exception\InvalidArgumentException ;
2017-10-30 06:24:47 +00:00
use Symfony\Component\Workflow\SupportStrategy\WorkflowSupportStrategyInterface ;
2016-03-25 15:43:30 +00:00
2013-12-01 09:16:07 +00:00
/**
* @ author Fabien Potencier < fabien @ symfony . com >
2016-03-25 15:43:30 +00:00
* @ author Grégoire Pineau < lyrixx @ lyrixx . info >
2013-12-01 09:16:07 +00:00
*/
class Registry
{
2019-01-16 18:24:45 +00:00
private $workflows = [];
2013-12-01 09:16:07 +00:00
2017-10-30 06:24:47 +00:00
public function addWorkflow ( WorkflowInterface $workflow , WorkflowSupportStrategyInterface $supportStrategy )
{
2019-01-16 18:24:45 +00:00
$this -> workflows [] = [ $workflow , $supportStrategy ];
2013-12-01 09:16:07 +00:00
}
2016-12-06 13:57:19 +00:00
/**
* @ return Workflow
*/
2019-06-27 15:12:25 +01:00
public function get ( object $subject , string $workflowName = null )
2013-12-01 09:16:07 +00:00
{
2019-09-11 09:54:49 +01:00
$matched = [];
2016-03-25 15:43:30 +00:00
2016-12-04 22:08:15 +00:00
foreach ( $this -> workflows as list ( $workflow , $supportStrategy )) {
if ( $this -> supports ( $workflow , $supportStrategy , $subject , $workflowName )) {
2019-09-11 09:54:49 +01:00
$matched [] = $workflow ;
2016-03-25 15:43:30 +00:00
}
}
if ( ! $matched ) {
2018-07-26 09:45:46 +01:00
throw new InvalidArgumentException ( sprintf ( 'Unable to find a workflow for class "%s".' , \get_class ( $subject )));
2016-03-25 15:43:30 +00:00
}
2019-09-11 09:54:49 +01:00
if ( 2 <= \count ( $matched )) {
$names = array_map ( static function ( WorkflowInterface $workflow ) : string {
return $workflow -> getName ();
}, $matched );
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 )));
}
return $matched [ 0 ];
2013-12-01 09:16:07 +00:00
}
2018-03-23 14:04:31 +00:00
/**
* @ return Workflow []
*/
2019-06-27 15:12:25 +01:00
public function all ( object $subject ) : array
2018-03-23 14:04:31 +00:00
{
2019-01-16 18:24:45 +00:00
$matched = [];
2018-03-23 14:04:31 +00:00
foreach ( $this -> workflows as list ( $workflow , $supportStrategy )) {
if ( $supportStrategy -> supports ( $workflow , $subject )) {
$matched [] = $workflow ;
}
}
return $matched ;
}
2019-08-14 00:26:41 +01:00
private function supports ( WorkflowInterface $workflow , WorkflowSupportStrategyInterface $supportStrategy , object $subject , ? string $workflowName ) : bool
2013-12-01 09:16:07 +00:00
{
2017-01-18 14:08:35 +00:00
if ( null !== $workflowName && $workflowName !== $workflow -> getName ()) {
2016-03-25 15:43:30 +00:00
return false ;
}
2017-01-18 14:08:35 +00:00
return $supportStrategy -> supports ( $workflow , $subject );
2013-12-01 09:16:07 +00:00
}
}