src/Hitso/Bundle/CommonBundle/Monitor/VisitManager.php line 136

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\CommonBundle\Monitor;
  4. use DirectoryIterator;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Filesystem\Filesystem;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class VisitManager implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var string
  12.      */
  13.     protected $statPath;
  14.     /**
  15.      * @var bool
  16.      */
  17.     protected $wasClean true;
  18.     /**
  19.      * @var bool
  20.      */
  21.     protected $registered false;
  22.     /**
  23.      * @var bool
  24.      */
  25.     protected $debug;
  26.     public function __construct($statPath$lockPath$debug)
  27.     {
  28.         $this->statPath $statPath;
  29.         $this->debug    $debug;
  30.         if (!is_dir($statPath)) {
  31.             $fs = new Filesystem();
  32.             $fs->mkdir($statPath);
  33.         }
  34.     }
  35.     public function logVisit($clean)
  36.     {
  37.         $statFile $this->statPath '/' strtotime('midnight');
  38.         if (file_exists($statFile)) {
  39.             $stats unserialize(file_get_contents($statFile));
  40.         } else {
  41.             $stats = [];
  42.         }
  43.         $hour = (int) strftime('%H');
  44.         if (!isset($stats[$hour])) {
  45.             $stats[$hour] = [00];
  46.         }
  47.         $stats[$hour][0]++;
  48.         if (!$clean) {
  49.             $stats[$hour][1]++;
  50.         }
  51.         file_put_contents($statFileserialize($stats));
  52.     }
  53.     public function readLogs()
  54.     {
  55.         $day  strtotime('midnight');
  56.         $hour = (int) strftime('%H');
  57.         $results = [];
  58.         $files = new DirectoryIterator($this->statPath);
  59.         foreach ($files as $file) {
  60.             if ($file->isFile() && is_numeric($file->getFilename()) && $file->getFilename() <= $day) {
  61.                 $fday = (int) $file->getFilename();
  62.                 $data = @unserialize(file_get_contents($file->getPathname()));
  63.                 if (!is_array($data)) {
  64.                     continue;
  65.                 }
  66.                 foreach ($data as $fhour => $row) {
  67.                     if ($fday == $day && $fhour >= $hour) {
  68.                         break;
  69.                     }
  70.                     $stamp           $fday + ($fhour 3600);
  71.                     $results[$stamp] = $row;
  72.                 }
  73.             }
  74.         }
  75.         return $results;
  76.     }
  77.     public function clearLogs()
  78.     {
  79.         $day  strtotime('midnight');
  80.         $hour = (int) strftime('%H');
  81.         $files = new DirectoryIterator($this->statPath);
  82.         foreach ($files as $file) {
  83.             if ($file->isFile() && is_numeric($file->getFilename()) && $file->getFilename() <= $day) {
  84.                 $fday $file->getFilename();
  85.                 if ($fday == $day) {
  86.                     $data = @unserialize(file_get_contents($file->getPathname()));
  87.                     if (!is_array($data)) {
  88.                         @unlink($file->getPathname());
  89.                     } else {
  90.                         $newData = [];
  91.                         foreach ($data as $fhour => $row) {
  92.                             if ($fhour >= $hour) {
  93.                                 $newData[$fhour] = $row;
  94.                             }
  95.                         }
  96.                         file_put_contents($file->getPathname(), serialize($newData));
  97.                     }
  98.                 } else {
  99.                     @unlink($file->getPathname());
  100.                 }
  101.             }
  102.         }
  103.     }
  104.     public function onRequest()
  105.     {
  106.         if (!$this->registered && !$this->debug) {
  107.             register_shutdown_function(
  108.                 function () {
  109.                     $this->logVisit($this->wasClean);
  110.                 }
  111.             );
  112.         }
  113.         $this->registered true;
  114.     }
  115.     public function onException()
  116.     {
  117.         $this->wasClean false;
  118.     }
  119.     public static function getSubscribedEvents()
  120.     {
  121.         return [
  122.             KernelEvents::REQUEST   => ['onRequest'512],
  123.             KernelEvents::EXCEPTION => ['onException'512],
  124.         ];
  125.     }
  126. }