2021-05-10 00:23:30 +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 .
*/
2021-08-19 04:03:26 +00:00
namespace RectorPrefix20210819\Symfony\Component\Console\Command ;
2021-05-10 00:23:30 +00:00
2021-08-19 04:03:26 +00:00
use RectorPrefix20210819\Symfony\Component\Console\Exception\LogicException ;
use RectorPrefix20210819\Symfony\Component\Lock\Lock ;
use RectorPrefix20210819\Symfony\Component\Lock\LockFactory ;
use RectorPrefix20210819\Symfony\Component\Lock\Store\FlockStore ;
use RectorPrefix20210819\Symfony\Component\Lock\Store\SemaphoreStore ;
2021-05-10 00:23:30 +00:00
/**
* Basic lock feature for commands .
*
* @ author Geoffrey Brier < geoffrey . brier @ gmail . com >
*/
trait LockableTrait
{
/** @var Lock */
private $lock ;
/**
* Locks a command .
*/
private function lock ( string $name = null , bool $blocking = \false ) : bool
{
2021-08-19 04:03:26 +00:00
if ( ! \class_exists ( \RectorPrefix20210819\Symfony\Component\Lock\Store\SemaphoreStore :: class )) {
throw new \RectorPrefix20210819\Symfony\Component\Console\Exception\LogicException ( 'To enable the locking feature you must install the symfony/lock component.' );
2021-05-10 00:23:30 +00:00
}
if ( null !== $this -> lock ) {
2021-08-19 04:03:26 +00:00
throw new \RectorPrefix20210819\Symfony\Component\Console\Exception\LogicException ( 'A lock is already in place.' );
2021-05-10 00:23:30 +00:00
}
2021-08-19 04:03:26 +00:00
if ( \RectorPrefix20210819\Symfony\Component\Lock\Store\SemaphoreStore :: isSupported ()) {
$store = new \RectorPrefix20210819\Symfony\Component\Lock\Store\SemaphoreStore ();
2021-05-10 00:23:30 +00:00
} else {
2021-08-19 04:03:26 +00:00
$store = new \RectorPrefix20210819\Symfony\Component\Lock\Store\FlockStore ();
2021-05-10 00:23:30 +00:00
}
2021-08-19 04:03:26 +00:00
$this -> lock = ( new \RectorPrefix20210819\Symfony\Component\Lock\LockFactory ( $store )) -> createLock ( $name ? : $this -> getName ());
2021-05-10 00:23:30 +00:00
if ( ! $this -> lock -> acquire ( $blocking )) {
$this -> lock = null ;
return \false ;
}
return \true ;
}
/**
* Releases the command lock if there is one .
*/
private function release ()
{
if ( $this -> lock ) {
$this -> lock -> release ();
$this -> lock = null ;
}
}
}