PHP code example of desmart / laravel-padlock

1. Go to this page and download the library: Download desmart/laravel-padlock library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

desmart / laravel-padlock example snippets


class FooCommand extends \Illuminate\Console\Command
{
    protected $signature = 'foo:bar';

    protected $description = 'Foo command utilizing Padlock';

    /** @var PadlockHandler */
    private $padlockHandler;

    const PADLOCK_SCRIPT = 'FooCommand';

    /** 30 seconds padlock time to live - after that your padlock will be unlocked */
    const PADLOCK_TTL = 30;

    /**
     * FooCommand constructor.
     * @param \DeSmart\Padlock\PadlockHandler $padlockHandler
     */
    public function __construct(\DeSmart\Padlock\PadlockHandler $padlockHandler)
    {
        parent::__construct();

        $this->padlockHandler = $padlockHandler;
    }

    public function handle()
    {
        if (true === $this->padlockHandler->isLocked(self::PADLOCK_SCRIPT, self::PADLOCK_TTL)) {
            echo "Padlock exists, script locked." . PHP_EOL;

            return;
        }

        $this->padlockHandler->lock(self::PADLOCK_SCRIPT);
        
        // do your stuff
        
        $this->padlockHandler->unlock(self::PADLOCK_SCRIPT);
    }
}