"Fossies" - the Fresh Open Source Software Archive

Member "contao-4.11.5/core-bundle/src/Command/AbstractLockedCommand.php" (23 Jun 2021, 2602 Bytes) of package /linux/www/contao-4.11.5.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.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 /*
    6  * This file is part of Contao.
    7  *
    8  * (c) Leo Feyer
    9  *
   10  * @license LGPL-3.0-or-later
   11  */
   12 
   13 namespace Contao\CoreBundle\Command;
   14 
   15 use Symfony\Component\Console\Command\Command;
   16 use Symfony\Component\Console\Input\InputInterface;
   17 use Symfony\Component\Console\Output\OutputInterface;
   18 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
   19 use Symfony\Component\DependencyInjection\ContainerInterface;
   20 use Symfony\Component\Lock\LockFactory;
   21 use Symfony\Component\Lock\Store\FlockStore;
   22 use Webmozart\PathUtil\Path;
   23 
   24 /**
   25  * @deprecated Deprecated since Contao 4.7, to be removed in Contao 5.0; use
   26  *             the Symfony Lock component instead
   27  */
   28 abstract class AbstractLockedCommand extends Command implements ContainerAwareInterface
   29 {
   30     /**
   31      * @var ContainerInterface|null
   32      */
   33     private $container;
   34 
   35     public function setContainer(ContainerInterface $container = null): void
   36     {
   37         $this->container = $container;
   38     }
   39 
   40     protected function getContainer(): ContainerInterface
   41     {
   42         if (null === $this->container) {
   43             throw new \LogicException('The container needs to be set before it can be retrieved.');
   44         }
   45 
   46         return $this->container;
   47     }
   48 
   49     final protected function execute(InputInterface $input, OutputInterface $output): int
   50     {
   51         trigger_deprecation('contao/core-bundle', '4.7', 'Using the "AbstractLockedCommand" has been deprecated and will no longer work in Contao 5.0. Use the Symfony Lock component instead.');
   52 
   53         $store = new FlockStore($this->getTempDir());
   54         $factory = new LockFactory($store);
   55         $lock = $factory->createLock($this->getName());
   56 
   57         if (!$lock->acquire()) {
   58             $output->writeln('The command is already running in another process.');
   59 
   60             return 1;
   61         }
   62 
   63         $errorCode = $this->executeLocked($input, $output);
   64         $lock->release();
   65 
   66         if ($errorCode > 0) {
   67             return $errorCode;
   68         }
   69 
   70         return 0;
   71     }
   72 
   73     /**
   74      * @return int
   75      */
   76     abstract protected function executeLocked(InputInterface $input, OutputInterface $output);
   77 
   78     /**
   79      * Creates an installation specific folder in the temporary directory and returns its path.
   80      */
   81     private function getTempDir(): string
   82     {
   83         $container = $this->getContainer();
   84         $tmpDir = Path::join(sys_get_temp_dir(), md5($container->getParameter('kernel.project_dir')));
   85 
   86         if (!is_dir($tmpDir)) {
   87             $container->get('filesystem')->mkdir($tmpDir);
   88         }
   89 
   90         return $tmpDir;
   91     }
   92 }