src/Controller/HomepageController.php line 42

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: antoinenedelec
  5.  * Date: 25/04/2018
  6.  * Time: 18:08
  7.  */
  8. namespace App\Controller;
  9. use App\Entity\Presta;
  10. use App\Kernel;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Lock\Factory;
  16. use Symfony\Component\Lock\LockFactory;
  17. use Symfony\Component\Lock\Store\SemaphoreStore;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. class HomepageController extends AbstractController
  21. {
  22.     /**
  23.      * @var Kernel
  24.      */
  25.     private $kernel;
  26.     private $factory;
  27.     private $lockName "!!!lock-dropbox-simultaneous-id-";
  28.     private $prestaLockName "!!!lock-dropbox-presta-";
  29.     private $fileName "PROCESS_WEB_APP.txt";
  30.     public function __construct(Kernel $kernel)
  31.     {
  32.         $this->kernel $kernel;
  33.     }
  34.     /**
  35.      * @Route("/", name="homepage")
  36.      */
  37.     public function homepage(TranslatorInterface $translator): Response
  38.     {
  39.         return $this->render('homepage/view.html.twig', [
  40.             'title' => $translator->trans('title.homepage'),
  41.         ]);
  42.     }
  43.     /**
  44.      * @Route("/errorlog", name="errorlog")
  45.      */
  46.     public function errorLogAction(LoggerInterface $loggerTranslatorInterface $translator): Response
  47.     {
  48.         $numberOfLog 50;
  49.         $fileName $this->kernel->getLogDir() . "/alert.log";
  50.         $logs = [];
  51.         try {
  52.             $handle fopen($fileName"r");
  53.             if ($handle) {
  54.                 while (($line fgets($handle)) !== false) {
  55.                     $logs[] = substr(str_replace("app.ALERT: """$line), 0, -6);
  56.                 }
  57.                 fclose($handle);
  58.             }
  59.         } catch (\Exception $e) {
  60.             $logger->error($e->getMessage());
  61.         }
  62.         return $this->render('settings/error_log.html.twig', [
  63.             'title' => $translator->trans('title.errorlog'),
  64.             'logs' => array_reverse(array_slice($logs, -$numberOfLog$numberOfLogtrue)),
  65.         ]);
  66.     }
  67.     /**
  68.      * @Route("/faq", name="faq")
  69.      */
  70.     public function faqAction(TranslatorInterface $translator): Response
  71.     {
  72.         return $this->render('faq/view.html.twig', [
  73.             'title' => $translator->trans('title.faq')
  74.         ]);
  75.     }
  76.     /**
  77.      * @Route("/information/{id}", name="information")
  78.      */
  79.     public function informationAction($idTranslatorInterface $translator): Response
  80.     {
  81.         return $this->render('common/information.html.twig', [
  82.             'title' => $translator->trans('title.information'),
  83.             'information' => $translator->trans('information.' $id)
  84.         ]);
  85.     }
  86.     /**
  87.      * @Route("/admin/process", name="admin.process")
  88.      */
  89.     public function userProcessAction(ManagerRegistry $doctrine): Response
  90.     {
  91.         // GET Number
  92.         $fileName $this->kernel->getLogDir() . "/" $this->fileName;
  93.         $last = -1;
  94.         try {
  95.             $handle fopen($fileName"r");
  96.             if ($handle) {
  97.                 while (($line fgets($handle)) !== false) {
  98.                     $last = (int)$line;
  99.                 }
  100.                 fclose($handle);
  101.             }
  102.         } catch (\Exception $e) {
  103.         }
  104.         $store = new SemaphoreStore();
  105.         $this->factory = new LockFactory($store);
  106.         $prestas $doctrine->getRepository(Presta::class)->findAllPrestaToSync();
  107.         $presta_locks = [];
  108.         foreach ($prestas as $presta) {
  109.             $presta_locks[$presta->getId()] = $this->testLock($this->prestaLockName $presta->getId());
  110.         }
  111.         $process_lock = [];
  112.         $process_lock[] = $this->testLock($this->lockName "0");
  113.         $process_lock[] = $this->testLock($this->lockName "1");
  114.         $dropbox_lock $this->testLock("lock_use_dropbox_" "0");
  115.         return $this->render('admin/process.html.twig', [
  116.             'title' => 'Touts les process',
  117.             'presta_locks' => $presta_locks,
  118.             'process_lock' => $process_lock,
  119.             'dropbox_lock' => $dropbox_lock,
  120.             'last' => $last,
  121.         ]);
  122.     }
  123.     private function testLock($lock_name): string
  124.     {
  125.         if ($this->factory instanceof Factory) {
  126.             $lock $this->factory->createLock($lock_name3600);
  127.         }
  128.         if ($lock->acquire()) {
  129.             $lock->release();
  130.             return $lock_name " libre";
  131.         } else {
  132.             return $lock_name " occupĂ©";
  133.         }
  134.     }
  135. }