src/Controller/CacheController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\SiteSettings;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use App\Entity\CrmSmsTemplates;
  10. use App\Utils\Paginator;
  11. use Symfony\Component\DependencyInjection\ContainerInterface
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use App\Form\Filters\MemcacheDataFilter;
  14. #[Route('/settings/cache')]
  15. class CacheController extends AbstractController
  16. {
  17.     protected $container;
  18.     protected $tokenStorage;
  19.     protected $memcache;
  20.     
  21.     public function __construct(ContainerInterface $containerTokenStorageInterface $tokenStorage
  22.     {
  23.         $this->tokenStorage $tokenStorage;
  24.         $this->container $container;
  25.         $this->memcache = new \Memcached();
  26.         
  27.         $this->memcache->addServer($this->container->getParameter('memcache_host'),$this->container->getParameter('memcache_port'));
  28.     }
  29.     #[Route('/'defaults: ['page' => '1'], methods: ['GET'], name'manage_cache')]
  30.     #[Route('/page/{page<[1-9]\d*>}'methods: ['GET'], name'manage_cache_index_paginated')]
  31.     #[Cache(smaxage10)]
  32.     public function index(Request $request,int $page,EntityManagerInterface $entityManager): Response
  33.         {        
  34.             $params $request->query->all();
  35.             $filter = [];
  36.             $filterForm $this->createForm(MemcacheDataFilter::class);
  37.         
  38.             if(isset($params['key'])) {
  39.                 $filterForm['key']->setData($params['key']);
  40.                 $filter['key'] = $params['key'];
  41.             }
  42.             if(isset($params['crm']) && !empty($params['crm'])) {
  43.                 $userCrm =  $entityManager->getRepository(\App\Entity\UserCrms::class)->findOneBy(['crm'=>$params['crm']]);
  44.                 $filterForm['crm']->setData($params['crm']);
  45.                 if($userCrm){
  46.                     $filter['crm'] = '_'.$userCrm->getCrmId();
  47.                 }
  48.                 
  49.             }
  50.             if(isset($params['user_name']) && !empty($params['user_name'])) {
  51.                 $user =  $entityManager->getRepository(\App\Entity\User::class)->findOneBy(['name'=>$params['user_name']]);
  52.                 $filterForm['user_name']->setData($params['user_name']);
  53.                 if($user){
  54.                     $filter['user_name'] = '_'.$user->getUserId();
  55.                 }
  56.             }
  57.             if(isset($params['user_email']) && !empty($params['user_email'])) {
  58.                 $user =  $entityManager->getRepository(\App\Entity\User::class)->findOneBy(['email'=>$params['user_email']]);
  59.                 $filterForm['user_email']->setData($params['user_email']);
  60.                 if($user){
  61.                     $filter['user_email'] = '_'.$user->getUserId();
  62.                 }
  63.             }
  64.             if(isset($params['phone_number']) && !empty($params['phone_number'])) {
  65.                 $userProfiles =  $entityManager->getRepository(\App\Entity\UserProfiles::class)->findOneBy(['phone'=>$params['phone_number']]);
  66.                 $filterForm['phone_number']->setData($params['phone_number']);
  67.                 if($userProfiles){
  68.                     $filter['phone_number'] = '_'.$userProfiles->getUserId();
  69.                 }
  70.             }
  71.             
  72.             $cacheArray $this->memcache->getAllKeys();
  73.             $this->memcache->getDelayed($cacheArraytrue);
  74.         
  75.         /* echo '<pre>';
  76.         print_r($memcache->fetchAll());
  77.         exit; */
  78.         
  79.         $page = ($request->query->get('page'))?$request->query->get('page'):1;
  80.         $limit 10;
  81.         $offset $limit * ($page 1);
  82.         
  83.         $dbcacheArray = array();
  84.         foreach($cacheArray as $arrayKey => $arrayVal){
  85.             if($arrayVal == 'stored_keys'){
  86.                 continue;
  87.             }
  88.             $key_arr explode(".",$arrayVal);
  89.             //$data = array_slice($key_arr, 0, 3);
  90.             $data array_slice($key_arr03);
  91.             $dbcacheArray[implode("_",$data)] = implode(".",$data);
  92.         }
  93.         $filteredArray $dbcacheArray;
  94.         $totalCache array_filter($dbcacheArray);
  95.         if(isset($filter['key']) && !empty($filter['key'])){
  96.             // Filter the array by a specific string
  97.             $filterString $filter['key']; // Replace this with the string you want to filter by
  98.             $filteredArray array_filter($dbcacheArray, function($value) use ($filterString) {
  99.                 return strpos($value$filterString) !== false;
  100.             });
  101.         }
  102.         if(isset($filter['crm']) && !empty($filter['crm'])){
  103.             // Filter the array by a specific string
  104.             $filterString $filter['crm']; // Replace this with the string you want to filter by
  105.             $filteredArray array_filter($dbcacheArray, function($value) use ($filterString) {
  106.                 return strpos($value$filterString) !== false;
  107.             });
  108.         }
  109.         if(isset($filter['user_name']) && !empty($filter['user_name'])){
  110.             // Filter the array by a specific string
  111.             $filterString $filter['user_name']; // Replace this with the string you want to filter by
  112.             $filteredArray array_filter($dbcacheArray, function($value) use ($filterString) {
  113.                 return strpos($value$filterString) !== false;
  114.             });
  115.         }
  116.         if(isset($filter['user_email']) && !empty($filter['user_email'])){
  117.             // Filter the array by a specific string
  118.             $filterString $filter['user_email']; // Replace this with the string you want to filter by
  119.             $filteredArray array_filter($dbcacheArray, function($value) use ($filterString) {
  120.                 return strpos($value$filterString) !== false;
  121.             });
  122.         }
  123.         if(isset($filter['phone_number']) && !empty($filter['phone_number'])){
  124.             // Filter the array by a specific string
  125.             $filterString $filter['phone_number']; // Replace this with the string you want to filter by
  126.             $filteredArray array_filter($dbcacheArray, function($value) use ($filterString) {
  127.                 return strpos($value$filterString) !== false;
  128.             });
  129.         }
  130.         /* echo '<pre>';
  131.         print_r($dbcacheArray);
  132.         exit; */
  133.         $totalFilterCache array_filter($filteredArray);
  134.         
  135.         
  136.          return $this->render('cache/index.html.twig', [
  137.             'paginator' => $totalFilterCache,
  138.             'filter' => $filter,
  139.             'totalCache' => count($totalCache),
  140.             'totalFilterCache' => count($totalFilterCache),
  141.             'form' => $filterForm->createView(),
  142.          ]);
  143.     }
  144.     #[Route('/delete/{id}'name'cache_delete'methods: ['POST','GET'])]
  145.     public function delete($idRequest $requestEntityManagerInterface $entityManager): Response
  146.     {
  147.         $params $request->query->all();
  148.        $filter = [];
  149.         
  150.             if(isset($params['key'])) {
  151.                 $filter['key'] = $params['key'];
  152.             }
  153.             if(isset($params['crm']) && !empty($params['crm'])) {
  154.                 $userCrm =  $entityManager->getRepository(\App\Entity\UserCrms::class)->findOneBy(['crm'=>$params['crm']]);
  155.                 if($userCrm){
  156.                     $filter['crm'] = '_'.$userCrm->getCrmId();
  157.                 }
  158.                 
  159.             }
  160.             if(isset($params['user_name']) && !empty($params['user_name'])) {
  161.                 $user =  $entityManager->getRepository(\App\Entity\User::class)->findOneBy(['name'=>$params['user_name']]);
  162.                 if($user){
  163.                     $filter['user_name'] = '_'.$user->getUserId();
  164.                 }
  165.             }
  166.             if(isset($params['user_email']) && !empty($params['user_email'])) {
  167.                 $user =  $entityManager->getRepository(\App\Entity\User::class)->findOneBy(['email'=>$params['user_email']]);
  168.                 if($user){
  169.                     $filter['user_email'] = '_'.$user->getUserId();
  170.                 }
  171.             }
  172.             if(isset($params['phone_number']) && !empty($params['phone_number'])) {
  173.                 $userProfiles =  $entityManager->getRepository(\App\Entity\UserProfiles::class)->findOneBy(['phone'=>$params['phone_number']]);
  174.                 if($userProfiles){
  175.                     $filter['phone_number'] = '_'.$userProfiles->getUserId();
  176.                 }
  177.             }
  178.             
  179.         if ($request->getMethod() == 'POST') {
  180.             $cacheArray $this->memcache->getAllKeys();
  181.             if($id == 'all'){    
  182.                 
  183.                 foreach($cacheArray as $arrayKey => $arrayVal){
  184.                     $this->memcache->delete($arrayVal);
  185.                 }
  186.                 return $this->redirectToRoute('manage_cache', [], Response::HTTP_SEE_OTHER);
  187.             }else{
  188.                 if(count($filter) > 0){
  189.                     $dbcacheArray = array();
  190.                     foreach($cacheArray as $arrayKey => $arrayVal){
  191.                         
  192.                         $key_arr explode(".",$arrayVal);
  193.                         //$data = array_slice($key_arr, 0, 3);
  194.                         $data array_slice($key_arr03);
  195.                         $dbcacheArray[implode("_",$data)] = implode(".",$data);
  196.                     }
  197.                     $filteredArray $dbcacheArray;
  198.                     
  199.                     if(isset($filter['key']) && !empty($filter['key'])){
  200.                         // Filter the array by a specific string
  201.                         $filterString $filter['key']; // Replace this with the string you want to filter by
  202.                         $filteredArray array_filter($dbcacheArray, function($value) use ($filterString) {
  203.                             return strpos($value$filterString) !== false;
  204.                         });
  205.                     }
  206.                     if(isset($filter['crm']) && !empty($filter['crm'])){
  207.                         // Filter the array by a specific string
  208.                         $filterString $filter['crm']; // Replace this with the string you want to filter by
  209.                         $filteredArray array_filter($dbcacheArray, function($value) use ($filterString) {
  210.                             return strpos($value$filterString) !== false;
  211.                         });
  212.                     }
  213.                     if(isset($filter['user_name']) && !empty($filter['user_name'])){
  214.                         // Filter the array by a specific string
  215.                         $filterString $filter['user_name']; // Replace this with the string you want to filter by
  216.                         $filteredArray array_filter($dbcacheArray, function($value) use ($filterString) {
  217.                             return strpos($value$filterString) !== false;
  218.                         });
  219.                     }
  220.                     if(isset($filter['user_email']) && !empty($filter['user_email'])){
  221.                         // Filter the array by a specific string
  222.                         $filterString $filter['user_email']; // Replace this with the string you want to filter by
  223.                         $filteredArray array_filter($dbcacheArray, function($value) use ($filterString) {
  224.                             return strpos($value$filterString) !== false;
  225.                         });
  226.                     }
  227.                     if(isset($filter['phone_number']) && !empty($filter['phone_number'])){
  228.                         // Filter the array by a specific string
  229.                         $filterString $filter['phone_number']; // Replace this with the string you want to filter by
  230.                         $filteredArray array_filter($dbcacheArray, function($value) use ($filterString) {
  231.                             return strpos($value$filterString) !== false;
  232.                         });
  233.                     }
  234.                     foreach($filteredArray as $arrayKey => $arrayVal){
  235.                         $this->memcache->delete($arrayVal);
  236.                     }
  237.                     return $this->redirectToRoute('manage_cache', [], Response::HTTP_SEE_OTHER);
  238.                 }
  239.             }
  240.             
  241.             $this->addFlash('error''Delete successfully.');
  242.             if($request->request->get('redirect_page')){
  243.                 return $this->redirectToRoute('manage_cache_index_paginated', ['page'=> $request->request->get('redirect_page')], Response::HTTP_SEE_OTHER);
  244.             }
  245.         
  246.         
  247.         }
  248.         return $this->renderForm('cache/_delete_form.html.twig', [
  249.             'redirect_page' => $request->query->get('redirect_page'),
  250.             'id' => $id,
  251.         ]);
  252.     }
  253.   
  254. }