Yii2 Locker Extension
Extension includes following features:, (*1)
- Activate/Deactivate resource lock by unique identifier.
- Check and block request if exists active lock.
For license information check the LICENSE-file., (*2)
Installation
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
php composer.phar require --prefer-dist notamedia/yii2-locker
or add, (*5)
"notamedia/yii2-locker": "1.0.0"
to the require section of your composer.json., (*6)
Configuration
To use this extension, simply add the following code in your application configuration:, (*7)
return [
//....
'components' => [
//....
'lockManager' => [
'class' => LockManager::class,
'lockTime' => [
LockManager::DEFAULT_LOCK_TIME_KEY => 900,
]
],
//....
],
//....
];
- Note: you may set custom time for each resource, simple add to
lockTime
, key - resource class and
value - time in seconds
- Note: if your db driver non mysql, you need set custom
'initTimeExpressionValue' = '...'
and
'diffExpressionValue' = '...'
- Note: if you want to use custom lock class, you need set new
LockInterface::class
definitions like
'container' => [
'...',
'definitions' => [
'...',
LockInterface::class => Lock::class
'...'
],
'...'
]
connect following actions in your controllers and configure routing, (*8)
return [
//....
'lock' => [
'class' => LockAction::class,
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess']
],
'unlock' => [
'class' => UnlockAction::class,
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
],
//....
];
- Note: you may set
'lockManager' = '...'
attribute if your LockManager component has other key
attach behavior to check lock, or you can check by yourself, (*9)
return [
//....
[
'class' => LockControlFilter::class,
'only' => ['update']
],
//....
];
- Note: you may set
'lockManager' = '...'
attribute if your LockManager component has other key
connect and execute migration, example:, (*10)
For yii2 > 2.0.10, (*11)
'controllerMap' => [
//...
'migrate' => [
'class' => MigrateController::class,
'migrationNamespaces' => [
'notamedia\locker\migrations',
],
//...
],
//...
]
For yii2 < 2.0.10 create new migration and use extends, (*12)
class mxxxxxx_xxxxxx_create_table_lock extends m000000_000000_create_table_lock
Usage
Methods:
* LockAction
- activates lock and returns 204
status code if successful
* UnlockAction
- deactivates lock and returns 204
status code if successful, (*13)
Exceptions
-
LockAnotherUserException
- exception if lock set another user, status code 500
-
LockNotExistException
- exception if lock not exist, status code 500
-
LockNotExpiredException
- exception if lock actual and its time not expired, status code 500