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

Commit3f9fc8a

Browse files
[Console] Add 'auto lockable' behaviour
1 parent45b557a commit3f9fc8a

File tree

6 files changed

+177
-4
lines changed

6 files changed

+177
-4
lines changed

‎src/Symfony/Component/Console/Command/Command.php‎

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
useSymfony\Component\Console\Helper\HelperSet;
2222
useSymfony\Component\Console\Exception\InvalidArgumentException;
2323
useSymfony\Component\Console\Exception\LogicException;
24+
useSymfony\Component\Filesystem\LockHandler;
2425

2526
/**
2627
* Base class for all commands.
@@ -43,6 +44,8 @@ class Command
4344
private$synopsis =array();
4445
private$usages =array();
4546
private$helperSet;
47+
private$autoLock =false;
48+
private$lock;
4649

4750
/**
4851
* Constructor.
@@ -250,12 +253,22 @@ public function run(InputInterface $input, OutputInterface $output)
250253

251254
$input->validate();
252255

253-
if ($this->code) {
254-
$statusCode =call_user_func($this->code,$input,$output);
255-
}else {
256-
$statusCode =$this->execute($input,$output);
256+
try {
257+
if (!$this->lock($output)) {
258+
$statusCode =0;
259+
}elseif ($this->code) {
260+
$statusCode =call_user_func($this->code,$input,$output);
261+
}else {
262+
$statusCode =$this->execute($input,$output);
263+
}
264+
}catch (\Exception$e) {
265+
$this->release();
266+
267+
throw$e;
257268
}
258269

270+
$this->release();
271+
259272
returnis_numeric($statusCode) ? (int)$statusCode :0;
260273
}
261274

@@ -623,4 +636,60 @@ private function validateName($name)
623636
thrownewInvalidArgumentException(sprintf('Command name "%s" is invalid.',$name));
624637
}
625638
}
639+
640+
/**
641+
* Gets whether or not the auto lock option is enabled.
642+
*
643+
* @return bool
644+
*/
645+
publicfunctionisAutoLock()
646+
{
647+
return$this->autoLock;
648+
}
649+
650+
/**
651+
* Sets the auto lock option.
652+
*
653+
* @param bool $autoLock
654+
*
655+
* @return Command The current instance
656+
*/
657+
publicfunctionsetAutoLock($autoLock)
658+
{
659+
$this->autoLock = (boolean)$autoLock;
660+
}
661+
662+
/**
663+
* Puts a lock on the command.
664+
*
665+
* @param OutputInterface $output
666+
*
667+
* @return bool
668+
*/
669+
privatefunctionlock(OutputInterface$output)
670+
{
671+
if (!$this->isAutoLock()) {
672+
returntrue;
673+
}
674+
675+
$this->lock =newLockHandler($this->getName());
676+
677+
if (!$this->lock->lock()) {
678+
$output->writeln(sprintf('The command "%s" is already running in another process.',$this->getName()));
679+
680+
returnfalse;
681+
}
682+
683+
returntrue;
684+
}
685+
686+
/**
687+
* Releases the command lock if there is one.
688+
*/
689+
privatefunctionrelease()
690+
{
691+
if ($this->lock) {
692+
$this->lock->release();
693+
}
694+
}
626695
}

‎src/Symfony/Component/Console/Tests/ApplicationTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public static function setUpBeforeClass()
4343
require_onceself::$fixturesPath.'/Foo3Command.php';
4444
require_onceself::$fixturesPath.'/Foo4Command.php';
4545
require_onceself::$fixturesPath.'/Foo5Command.php';
46+
require_onceself::$fixturesPath.'/FooAutoLock1Command.php';
47+
require_onceself::$fixturesPath.'/FooAutoLock2Command.php';
4648
require_onceself::$fixturesPath.'/FoobarCommand.php';
4749
require_onceself::$fixturesPath.'/BarBucCommand.php';
4850
require_onceself::$fixturesPath.'/FooSubnamespaced1Command.php';

‎src/Symfony/Component/Console/Tests/Command/CommandTest.php‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
useSymfony\Component\Console\Output\OutputInterface;
2323
useSymfony\Component\Console\Output\NullOutput;
2424
useSymfony\Component\Console\Tester\CommandTester;
25+
useSymfony\Component\Filesystem\LockHandler;
2526

2627
class CommandTestextends \PHPUnit_Framework_TestCase
2728
{
@@ -338,6 +339,65 @@ public function callableMethodCommand(InputInterface $input, OutputInterface $ou
338339
{
339340
$output->writeln('from the code...');
340341
}
342+
343+
publicfunctiontestIsAutoLock()
344+
{
345+
$command =new \Foo1Command();
346+
$this->assertFalse($command->isAutoLock());
347+
348+
$command =new \FooAutoLock1Command();
349+
$this->assertTrue($command->isAutoLock());
350+
}
351+
352+
publicfunctiontestSetAutoLock()
353+
{
354+
$command =new \Foo1Command();
355+
$command->setAutoLock(true);
356+
357+
$this->assertTrue($command->isAutoLock());
358+
359+
$command->setAutoLock(false);
360+
361+
$this->assertFalse($command->isAutoLock());
362+
}
363+
364+
publicfunctiontestCommandLock()
365+
{
366+
$command1 =new \FooAutoLock1Command();
367+
368+
$lock =newLockHandler($command1->getName());
369+
$lock->lock();
370+
371+
$tester =newCommandTester($command1);
372+
$tester->execute(array());
373+
$this->assertEquals('The command "foo:autolock1" is already running in another process.'.PHP_EOL,$tester->getDisplay());
374+
375+
$lock->release();
376+
}
377+
378+
publicfunctiontestCommandLockIsReleased()
379+
{
380+
$command =new \FooAutoLock1Command();
381+
$tester =newCommandTester($command);
382+
$tester->execute(array());
383+
$tester->execute(array());
384+
$this->assertEquals('',$tester->getDisplay());
385+
}
386+
387+
publicfunctiontestCommandLockIsReleasedAfterException()
388+
{
389+
$command =new \FooAutoLock2Command();
390+
$tester =newCommandTester($command);
391+
try {
392+
$tester->execute(array());
393+
}catch (\Exception$e) {
394+
}
395+
try {
396+
$tester->execute(array());
397+
}catch (\Exception$e) {
398+
}
399+
$this->assertEquals('',$tester->getDisplay());
400+
}
341401
}
342402

343403
// In order to get an unbound closure, we should create it outside a class
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
useSymfony\Component\Console\Command\Command;
4+
useSymfony\Component\Console\Input\InputInterface;
5+
useSymfony\Component\Console\Output\OutputInterface;
6+
7+
class FooAutoLock1Commandextends Command
8+
{
9+
protectedfunctionconfigure()
10+
{
11+
$this
12+
->setName('foo:autolock1')
13+
->setAutoLock(true)
14+
;
15+
}
16+
17+
protectedfunctionexecute(InputInterface$input,OutputInterface$output)
18+
{
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
useSymfony\Component\Console\Command\Command;
4+
useSymfony\Component\Console\Input\InputInterface;
5+
useSymfony\Component\Console\Output\OutputInterface;
6+
7+
class FooAutoLock2Commandextends Command
8+
{
9+
protectedfunctionconfigure()
10+
{
11+
$this
12+
->setName('foo:autolock2')
13+
->setAutoLock(true)
14+
;
15+
}
16+
17+
protectedfunctionexecute(InputInterface$input,OutputInterface$output)
18+
{
19+
thrownew \Exception();
20+
}
21+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"suggest": {
2828
"symfony/event-dispatcher":"",
2929
"symfony/process":"",
30+
"symfony/filesystem":"",
3031
"psr/log":"For using the console logger"
3132
},
3233
"autoload": {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp