Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb545b81

Browse files
[Console] Add lock feature
1 parent1298ce5 commitb545b81

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-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 <fabien@symfony.com>
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+
namespaceSymfony\Component\Console\Command;
13+
14+
useSymfony\Component\Console\Exception\LogicException;
15+
useSymfony\Component\Console\Exception\RuntimeException;
16+
useSymfony\Component\Filesystem\LockHandler;
17+
18+
/**
19+
* Basic lock feature for commands.
20+
*
21+
* @author Fabien Potencier <fabien@symfony.com>
22+
*/
23+
trait LockableTrait
24+
{
25+
protected$lock;
26+
27+
/**
28+
* Locks a command.
29+
*
30+
* @return bool
31+
*/
32+
protectedfunctionlock($name =null,$blocking =false)
33+
{
34+
if (!class_exists('Symfony\Component\Filesystem\LockHandler')) {
35+
thrownewRuntimeException('To enable the locking feature you must install the symfony/filesystem component');
36+
}
37+
38+
if ($this->lock !==null) {
39+
thrownewLogicException('A lock is already in place');
40+
}
41+
42+
$this->lock =newLockHandler($name ?:$this->getName());
43+
44+
if (!$this->lock->lock($blocking)) {
45+
$this->release();
46+
47+
returnfalse;
48+
}
49+
50+
returntrue;
51+
}
52+
53+
/**
54+
* Releases the command lock if there is one.
55+
*/
56+
protectedfunctionrelease()
57+
{
58+
if ($this->lock) {
59+
$this->lock->release();
60+
$this->lock =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 <fabien@symfony.com>
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+
namespaceSymfony\Component\Console\Tests\Command;
13+
14+
useSymfony\Component\Console\Tester\CommandTester;
15+
useSymfony\Component\Filesystem\LockHandler;
16+
17+
class LockableTraitTestextends \PHPUnit_Framework_TestCase
18+
{
19+
protectedstatic$fixturesPath;
20+
21+
publicstaticfunctionsetUpBeforeClass()
22+
{
23+
self::$fixturesPath =__DIR__.'/../Fixtures/';
24+
require_onceself::$fixturesPath.'/FooLockCommand.php';
25+
require_onceself::$fixturesPath.'/FooLock2Command.php';
26+
}
27+
28+
publicfunctiontestLockIsReleased()
29+
{
30+
$command =new \FooLockCommand();
31+
32+
$tester =newCommandTester($command);
33+
$this->assertSame(2,$tester->execute(array()));
34+
$this->assertSame(2,$tester->execute(array()));
35+
}
36+
37+
publicfunctiontestLockReturnsFalseIfAlreadyLockedByAnotherCommand()
38+
{
39+
$command =new \FooLockCommand();
40+
41+
$lock =newLockHandler($command->getName());
42+
$lock->lock();
43+
44+
$tester =newCommandTester($command);
45+
$this->assertSame(1,$tester->execute(array()));
46+
47+
$lock->release();
48+
$this->assertSame(2,$tester->execute(array()));
49+
}
50+
51+
publicfunctiontestMultipleLockCallsThrowsLogicException()
52+
{
53+
$command =new \FooLock2Command();
54+
55+
$tester =newCommandTester($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+
useSymfony\Component\Console\Command\Command;
4+
useSymfony\Component\Console\Command\LockableTrait;
5+
useSymfony\Component\Console\Input\InputInterface;
6+
useSymfony\Component\Console\Output\OutputInterface;
7+
8+
class FooLock2Commandextends Command
9+
{
10+
use LockableTrait;
11+
12+
protectedfunctionconfigure()
13+
{
14+
$this->setName('foo:lock2');
15+
}
16+
17+
protectedfunctionexecute(InputInterface$input,OutputInterface$output)
18+
{
19+
try {
20+
$this->lock();
21+
$this->lock();
22+
}catch (LogicException$e) {
23+
return1;
24+
}
25+
26+
return2;
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
useSymfony\Component\Console\Command\Command;
4+
useSymfony\Component\Console\Command\LockableTrait;
5+
useSymfony\Component\Console\Input\InputInterface;
6+
useSymfony\Component\Console\Output\OutputInterface;
7+
8+
class FooLockCommandextends Command
9+
{
10+
use LockableTrait;
11+
12+
protectedfunctionconfigure()
13+
{
14+
$this->setName('foo:lock');
15+
}
16+
17+
protectedfunctionexecute(InputInterface$input,OutputInterface$output)
18+
{
19+
if (!$this->lock()) {
20+
return1;
21+
}
22+
23+
$this->release();
24+
25+
return2;
26+
}
27+
}

‎src/Symfony/Component/Console/composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"require-dev": {
2323
"symfony/event-dispatcher":"~2.8|~3.0",
24+
"symfony/filesystem":"",
2425
"symfony/process":"~2.8|~3.0",
2526
"psr/log":"~1.0"
2627
},

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp