Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dd523cb

Browse files
[Console] Add Lockable trait
1 parent e949d34 commit dd523cb

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Command;
13+
14+
use Symfony\Component\Console\Exception\LogicException;
15+
use Symfony\Component\Console\Exception\RuntimeException;
16+
use Symfony\Component\Filesystem\LockHandler;
17+
18+
/**
19+
* Basic lock feature for commands.
20+
*
21+
* @author Fabien Potencier <[email protected]>
22+
*/
23+
trait LockableTrait
24+
{
25+
protected $lockHandler;
26+
27+
/**
28+
* Locks a command.
29+
*
30+
* @return bool
31+
*/
32+
protected function lock($name = null, $blocking = false)
33+
{
34+
if (!class_exists(LockHandler::class)) {
35+
throw new RuntimeException('To enable the locking feature you must install the symfony/filesystem component.');
36+
}
37+
38+
if (null !== $this->lockHandler) {
39+
throw new LogicException('A lock is already in place.');
40+
}
41+
42+
$this->lockHandler = new LockHandler($name ?: $this->getName());
43+
44+
if (!$this->lockHandler->lock($blocking)) {
45+
$this->lockHandler = null;
46+
47+
return false;
48+
}
49+
50+
return true;
51+
}
52+
53+
/**
54+
* Releases the command lock if there is one.
55+
*/
56+
protected function release()
57+
{
58+
if ($this->lockHandler) {
59+
$this->lockHandler->release();
60+
$this->lockHandler = null;
61+
}
62+
}
63+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\Command;
13+
14+
use Symfony\Component\Console\Tester\CommandTester;
15+
use Symfony\Component\Filesystem\LockHandler;
16+
17+
class LockableTraitTest extends \PHPUnit_Framework_TestCase
18+
{
19+
protected static $fixturesPath;
20+
21+
public static function setUpBeforeClass()
22+
{
23+
self::$fixturesPath = __DIR__.'/../Fixtures/';
24+
require_once self::$fixturesPath.'/FooLockCommand.php';
25+
require_once self::$fixturesPath.'/FooLock2Command.php';
26+
}
27+
28+
public function testLockIsReleased()
29+
{
30+
$command = new \FooLockCommand();
31+
32+
$tester = new CommandTester($command);
33+
$this->assertSame(2, $tester->execute(array()));
34+
$this->assertSame(2, $tester->execute(array()));
35+
}
36+
37+
public function testLockReturnsFalseIfAlreadyLockedByAnotherCommand()
38+
{
39+
$command = new \FooLockCommand();
40+
41+
$lock = new LockHandler($command->getName());
42+
$lock->lock();
43+
44+
$tester = new CommandTester($command);
45+
$this->assertSame(1, $tester->execute(array()));
46+
47+
$lock->release();
48+
$this->assertSame(2, $tester->execute(array()));
49+
}
50+
51+
public function testMultipleLockCallsThrowLogicException()
52+
{
53+
$command = new \FooLock2Command();
54+
55+
$tester = new CommandTester($command);
56+
$this->assertSame(1, $tester->execute(array()));
57+
}
58+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Command\LockableTrait;
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class FooLock2Command extends Command
9+
{
10+
use LockableTrait;
11+
12+
protected function configure()
13+
{
14+
$this->setName('foo:lock2');
15+
}
16+
17+
protected function execute(InputInterface $input, OutputInterface $output)
18+
{
19+
try {
20+
$this->lock();
21+
$this->lock();
22+
} catch (LogicException $e) {
23+
return 1;
24+
}
25+
26+
return 2;
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Command\LockableTrait;
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class FooLockCommand extends Command
9+
{
10+
use LockableTrait;
11+
12+
protected function configure()
13+
{
14+
$this->setName('foo:lock');
15+
}
16+
17+
protected function execute(InputInterface $input, OutputInterface $output)
18+
{
19+
if (!$this->lock()) {
20+
return 1;
21+
}
22+
23+
$this->release();
24+
25+
return 2;
26+
}
27+
}

src/Symfony/Component/Console/composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
},
2222
"require-dev": {
2323
"symfony/event-dispatcher": "~2.8|~3.0",
24+
"symfony/filesystem": "~2.8|~3.0",
2425
"symfony/process": "~2.8|~3.0",
2526
"psr/log": "~1.0"
2627
},
2728
"suggest": {
2829
"symfony/event-dispatcher": "",
30+
"symfony/filesystem": "",
2931
"symfony/process": "",
3032
"psr/log": "For using the console logger"
3133
},

0 commit comments

Comments
 (0)