Skip to content

Commit

Permalink
Merge branch 'master' into 9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ivanov committed Mar 13, 2022
2 parents 2b7b45e + 3327b51 commit 382c756
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 84 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ExampleCommand extends Command
{
use WithoutOverlapping;
protected $mutexStrategy = 'mysql';
protected string $mutexStrategy = 'mysql';
// ...
}
Expand Down Expand Up @@ -125,7 +125,7 @@ class ExampleCommand extends Command
use WithoutOverlapping;

// In milliseconds
protected $mutexTimeout = 3000;
protected ?int $mutexTimeout = 3000;

// ...
}
Expand Down
32 changes: 8 additions & 24 deletions src/Mutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis as RedisFacade;
use NinjaMutex\Lock\FlockLock;
use NinjaMutex\Lock\LockAbstract;
use NinjaMutex\Lock\MemcachedLock;
use NinjaMutex\Lock\MySqlLock;
use NinjaMutex\Lock\PhpRedisLock;
Expand All @@ -19,33 +20,25 @@ class Mutex
{
/**
* The console command.
*
* @var \Illuminate\Console\Command
*/
private $command;
private Command $command;

/**
* The NinjaMutex.
*
* @var \NinjaMutex\Mutex
*/
private $ninjaMutex;
private NinjaMutex $ninjaMutex;

/**
* The NinjaMutex lock.
*
* @var \NinjaMutex\Lock\LockAbstract
*/
private $ninjaMutexLock;
private LockAbstract $ninjaMutexLock;

/**
* Create a new instance of the mutex.
*
* @param \Illuminate\Console\Command|\Illuminated\Console\WithoutOverlapping $command
* @return void
*/
public function __construct(Command $command)
{
/** @var WithoutOverlapping $command */
$this->command = $command;

$mutexName = $command->getMutexName();
Expand All @@ -55,10 +48,8 @@ public function __construct(Command $command)

/**
* Get the NinjaMutex lock.
*
* @return \NinjaMutex\Lock\LockAbstract
*/
public function getNinjaMutexLock()
public function getNinjaMutexLock(): LockAbstract
{
if (!empty($this->ninjaMutexLock)) {
return $this->ninjaMutexLock;
Expand Down Expand Up @@ -88,11 +79,8 @@ public function getNinjaMutexLock()

/**
* Get the redis lock.
*
* @param string $client
* @return \NinjaMutex\Lock\LockAbstract
*/
private function getRedisLock($client)
private function getRedisLock(string $client): LockAbstract
{
$redis = RedisFacade::connection()->client();

Expand All @@ -103,12 +91,8 @@ private function getRedisLock($client)

/**
* Forward method calls to NinjaMutex.
*
* @param string $method
* @param mixed $parameters
* @return mixed
*/
public function __call($method, $parameters)
public function __call(string $method, mixed $parameters): mixed
{
return call_user_func_array([$this->ninjaMutex, $method], $parameters);
}
Expand Down
41 changes: 9 additions & 32 deletions src/WithoutOverlapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ trait WithoutOverlapping
{
/**
* Overwrite the console command initialization.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
protected function initialize(InputInterface $input, OutputInterface $output)
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->initializeMutex();

Expand All @@ -23,10 +19,8 @@ protected function initialize(InputInterface $input, OutputInterface $output)

/**
* Initialize the mutex.
*
* @return void
*/
protected function initializeMutex()
protected function initializeMutex(): void
{
$mutex = new Mutex($this);

Expand All @@ -42,10 +36,8 @@ protected function initializeMutex()
* Get the mutex strategy.
*
* Currently supported: "file", "mysql", "redis" and "memcached".
*
* @return string
*/
public function getMutexStrategy()
public function getMutexStrategy(): string
{
return property_exists($this, 'mutexStrategy')
? $this->mutexStrategy
Expand All @@ -56,11 +48,8 @@ public function getMutexStrategy()
* Set the mutex strategy.
*
* Currently supported: "file", "mysql", "redis" and "memcached".
*
* @param string $strategy
* @return void
*/
public function setMutexStrategy($strategy)
public function setMutexStrategy(string $strategy): void
{
$this->mutexStrategy = $strategy;
}
Expand All @@ -72,10 +61,8 @@ public function setMutexStrategy($strategy)
* `0` - check without waiting;
* `{milliseconds}` - check, and wait for a maximum of milliseconds specified;
* `null` - wait, till running command finish its execution;
*
* @return int|null
*/
public function getMutexTimeout()
public function getMutexTimeout(): int|null
{
return property_exists($this, 'mutexTimeout')
? $this->mutexTimeout
Expand All @@ -89,21 +76,16 @@ public function getMutexTimeout()
* `0` - check without waiting;
* `{milliseconds}` - check, and wait for a maximum of milliseconds specified;
* `null` - wait, till running command finish its execution;
*
* @param int|null $timeout
* @return void
*/
public function setMutexTimeout($timeout)
public function setMutexTimeout(int|null $timeout): void
{
$this->mutexTimeout = $timeout;
}

/**
* Get the mutex name.
*
* @return string
*/
public function getMutexName()
public function getMutexName(): string
{
$name = $this->getName();
$argumentsHash = md5(json_encode($this->argument()));
Expand All @@ -113,10 +95,8 @@ public function getMutexName()

/**
* Get the mutex file storage path.
*
* @return string
*/
public function getMutexFileStorage()
public function getMutexFileStorage(): string
{
return storage_path('app');
}
Expand All @@ -125,11 +105,8 @@ public function getMutexFileStorage()
* Release the mutex lock.
*
* Called automatically, because it's registered as a shutdown function.
*
* @param \Illuminated\Console\Mutex $mutex
* @return void
*/
public function releaseMutexLock(Mutex $mutex)
public function releaseMutexLock(Mutex $mutex): void
{
$mutex->releaseLock();
}
Expand Down
8 changes: 3 additions & 5 deletions tests/MutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Illuminated\Console\Tests;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis as RedisFacade;
use Illuminated\Console\Mutex;
use Illuminated\Console\Tests\App\Console\Commands\GenericCommand;
use Mockery\Mock;
use NinjaMutex\Lock\FlockLock;
use NinjaMutex\Lock\MemcachedLock;
use NinjaMutex\Lock\MySqlLock;
Expand All @@ -18,15 +20,11 @@ class MutexTest extends TestCase
{
/**
* The console command mock.
*
* @var \Mockery\Mock|\Illuminate\Console\Command
*/
private $command;
private Mock|Command $command;

/**
* Setup the test environment.
*
* @return void
*/
protected function setUp(): void
{
Expand Down
4 changes: 1 addition & 3 deletions tests/fixture/app/Console/Commands/GenericCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ class GenericCommand extends Command

/**
* Handle the command.
*
* @return void
*/
public function handle()
public function handle(): void
{
$this->info('Done!');
}
Expand Down
8 changes: 2 additions & 6 deletions tests/fixture/app/Console/Commands/MysqlStrategyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ class MysqlStrategyCommand extends Command

/**
* The mutex strategy.
*
* @var string
*/
protected $mutexStrategy = 'mysql';
protected string $mutexStrategy = 'mysql';

/**
* Handle the command.
*
* @return void
*/
public function handle()
public function handle(): void
{
$this->info('Done!');
}
Expand Down
8 changes: 2 additions & 6 deletions tests/fixture/app/Console/Commands/NullTimeoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ class NullTimeoutCommand extends Command

/**
* The mutex timeout.
*
* @var int|null
*/
protected $mutexTimeout;
protected ?int $mutexTimeout = null;

/**
* Handle the command.
*
* @return void
*/
public function handle()
public function handle(): void
{
$this->info('Done!');
}
Expand Down
8 changes: 2 additions & 6 deletions tests/fixture/app/Console/Commands/TimeoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ class TimeoutCommand extends Command

/**
* The mutex timeout.
*
* @var int|null
*/
protected $mutexTimeout = 3000;
protected ?int $mutexTimeout = 3000;

/**
* Handle the command.
*
* @return void
*/
public function handle()
public function handle(): void
{
$this->info('Done!');
}
Expand Down

0 comments on commit 382c756

Please sign in to comment.