bacart/symfony-common

Symfony common utils

v0.2.6 2020-03-18 22:28 UTC

README

PHP from Packagist Latest Stable Version Total Downloads License

Allows to create lockable commands in Symfony projects. Also includes aware interfaces and traits.

Installation

Using command line:

Run the following command and you will get the latest version by Packagist.

composer require bacart/symfony-common
Using composer.json

To use the newest (maybe unstable) version add following into your composer.json:

{
    "require": {
        "bacart/symfony-common": "dev-master"
    }
}

Usage example

Use AbstractLockableCommand as parent class of your command.

use Bacart\SymfonyCommon\Command\AbstractLockableCommand;

class SomeCommand extends AbstractLockableCommand
{
    ...
}

From now on your command will be able to run only once a time. You can also override a getLockTtl() method to set a lock lifetime in seconds (defaults to 60). If your command execution is too complex and may take more than lock lifetime you can extend it with EventDispatcher:

use Bacart\SymfonyCommon\Aware\Interfaces\EventDispatcherAwareInterface;
use Bacart\SymfonyCommon\Aware\Traits\EventDispatcherAwareTrait;
use Bacart\SymfonyCommon\Command\AbstractLockableCommand;

class SomeService implements EventDispatcherAwareInterface
{
    use EventDispatcherAwareTrait;
    
    public function someLongRunningMethod()
    {
        ...
        
        // extend a command lock TTL
        $this->dispatcher->dispatch(LockableCommandInterface::LOCKABLE_COMMAND_REFRESH_EVENT_NAME);
        
        ...
    }
}

All the examples assume that you are using an autowiring.

License

This project is released under the MIT license.

About

Project development is led by the Bacart team.