src/Eccube/Controller/Admin/Setting/System/LogController.php line 32

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Controller\Admin\Setting\System;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Event\EccubeEvents;
  15. use Eccube\Event\EventArgs;
  16. use Eccube\Form\Type\Admin\LogType;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  19. use Symfony\Component\HttpFoundation\Request;
  20. class LogController extends AbstractController
  21. {
  22.     /**
  23.      * @Route("/%eccube_admin_route%/setting/system/log", name="admin_setting_system_log")
  24.      * @Template("@admin/Setting/System/log.twig")
  25.      *
  26.      * @return array
  27.      */
  28.     public function index(Request $request)
  29.     {
  30.         $formData = [];
  31.         // default
  32.         $formData['files'] = 'site_'.date('Y-m-d').'.log';
  33.         $formData['line_max'] = '50';
  34.         $builder $this->formFactory
  35.             ->createBuilder(LogType::class);
  36.         $event = new EventArgs(
  37.             [
  38.                 'builder' => $builder,
  39.                 'data' => $formData,
  40.             ],
  41.             $request
  42.         );
  43.         $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_LOG_INDEX_INITIALIZE$event);
  44.         $formData $event->getArgument('data');
  45.         $form $builder->getForm();
  46.         if ('POST' === $request->getMethod()) {
  47.             $form->handleRequest($request);
  48.             if ($form->isValid()) {
  49.                 $formData $form->getData();
  50.             }
  51.             $event = new EventArgs(
  52.                 [
  53.                     'form' => $form,
  54.                 ],
  55.                 $request
  56.             );
  57.             $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_LOG_INDEX_COMPLETE$event);
  58.         }
  59.         $logDir $this->getParameter('kernel.logs_dir').DIRECTORY_SEPARATOR.$this->getParameter('kernel.environment');
  60.         $logFile $logDir.'/'.$formData['files'];
  61.         return [
  62.             'form' => $form->createView(),
  63.             'log' => $this->parseLogFile($logFile$formData),
  64.         ];
  65.     }
  66.     /**
  67.      * parse log file
  68.      *
  69.      * @param string $logFile
  70.      * @param $formData
  71.      *
  72.      * @return array
  73.      */
  74.     private function parseLogFile($logFile$formData)
  75.     {
  76.         $log = [];
  77.         if (!file_exists($logFile)) {
  78.             return $log;
  79.         }
  80.         foreach (array_reverse(file($logFileFILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES)) as $line) {
  81.             // 上限に達した場合、処理を抜ける
  82.             if (count($log) >= $formData['line_max']) {
  83.                 break;
  84.             }
  85.             $log[] = $line;
  86.         }
  87.         return $log;
  88.     }
  89. }