"Fossies" - the Fresh Open Source Software Archive

Member "contao-4.4.48/core-bundle/src/Command/AbstractLockedCommand.php" (2 Apr 2020, 1439 Bytes) of package /linux/www/contao-4.4.48.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractLockedCommand.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 4.8.8_vs_4.9.0.

    1 <?php
    2 
    3 /*
    4  * This file is part of Contao.
    5  *
    6  * (c) Leo Feyer
    7  *
    8  * @license LGPL-3.0-or-later
    9  */
   10 
   11 namespace Contao\CoreBundle\Command;
   12 
   13 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
   14 use Symfony\Component\Console\Input\InputInterface;
   15 use Symfony\Component\Console\Output\OutputInterface;
   16 use Symfony\Component\Filesystem\LockHandler;
   17 
   18 /**
   19  * Runs a command and locks it while its running.
   20  *
   21  * @author Leo Feyer <https://github.com/leofeyer>
   22  */
   23 abstract class AbstractLockedCommand extends ContainerAwareCommand
   24 {
   25     /**
   26      * {@inheritdoc}
   27      */
   28     final protected function execute(InputInterface $input, OutputInterface $output)
   29     {
   30         $lock = new LockHandler(
   31             $this->getName(),
   32             sys_get_temp_dir().'/'.md5($this->getContainer()->getParameter('kernel.project_dir'))
   33         );
   34 
   35         if (!$lock->lock()) {
   36             $output->writeln('The command is already running in another process.');
   37 
   38             return 1;
   39         }
   40 
   41         if (($errorCode = $this->executeLocked($input, $output)) > 0) {
   42             $lock->release();
   43 
   44             return $errorCode;
   45         }
   46 
   47         $lock->release();
   48 
   49         return 0;
   50     }
   51 
   52     /**
   53      * Executes the command.
   54      *
   55      * @param InputInterface  $input
   56      * @param OutputInterface $output
   57      *
   58      * @return int|null
   59      */
   60     abstract protected function executeLocked(InputInterface $input, OutputInterface $output);
   61 }