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 ;
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
{
private $workflows = array ();
2016-03-25 15:43:30 +00:00
/**
* @ param Workflow $workflow
* @ param string $classname
*/
public function add ( Workflow $workflow , $classname )
2013-12-01 09:16:07 +00:00
{
2016-03-25 15:43:30 +00:00
$this -> workflows [] = array ( $workflow , $classname );
2013-12-01 09:16:07 +00:00
}
2016-03-25 15:43:30 +00:00
public function get ( $subject , $workflowName = null )
2013-12-01 09:16:07 +00:00
{
2016-03-25 15:43:30 +00:00
$matched = null ;
foreach ( $this -> workflows as list ( $workflow , $classname )) {
if ( $this -> supports ( $workflow , $classname , $subject , $workflowName )) {
if ( $matched ) {
throw new InvalidArgumentException ( 'At least two workflows match this subject. Set a different name on each and use the second (name) argument of this method.' );
}
$matched = $workflow ;
}
}
if ( ! $matched ) {
throw new InvalidArgumentException ( sprintf ( 'Unable to find a workflow for class "%s".' , get_class ( $subject )));
}
return $matched ;
2013-12-01 09:16:07 +00:00
}
2016-03-25 15:43:30 +00:00
private function supports ( Workflow $workflow , $classname , $subject , $name )
2013-12-01 09:16:07 +00:00
{
2016-03-25 15:43:30 +00:00
if ( ! $subject instanceof $classname ) {
return false ;
}
if ( null === $name ) {
return true ;
2013-12-01 09:16:07 +00:00
}
2016-03-25 15:43:30 +00:00
return $name === $workflow -> getName ();
2013-12-01 09:16:07 +00:00
}
}