src/Controller/Backend/BaseController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Backend;
  3. use Symfony\Contracts\Cache\CacheInterface;
  4. use FOS\RestBundle\Controller\AbstractFOSRestController;
  5. use App\Service\BaseService;
  6. class BaseController extends AbstractFOSRestController
  7. {
  8.     public $entityManager;
  9.     public $commonService;
  10.     public function __construct(
  11.         public BaseService $baseService,
  12.         public CacheInterface $appCache
  13.     ) {
  14.         $this->entityManager = $baseService->getEntityManager();
  15.         $this->commonService = $baseService->commonService;
  16.     }
  17.     public function getUser() {
  18.         return $this->baseService->getUser();
  19.     }
  20.     public function __get($propertyName) {
  21.         $entityName = $this->commonService->getClassName($this, true);
  22.         
  23.         if($propertyName == 'currentService') {
  24.             return $this
  25.                     ->baseService->serviceLocator
  26.                     ->get('App\Service\\' . ucfirst($entityName) . 'Service');
  27.         }
  28.         if($propertyName == 'currentRepo') {
  29.             return $this
  30.                     ->baseService->serviceLocator
  31.                     ->get('App\Service\\' . ucfirst($entityName) . 'Service')
  32.                     ->repository;
  33.         }
  34.         preg_match('/([a-zA-Z0-9]+)Service/i', $propertyName, $serviceMatches);
  35.         if(count($serviceMatches) > 0) {
  36.             return $this
  37.                     ->baseService->serviceLocator
  38.                     ->get('App\Service\\' . ucfirst($serviceMatches[1]) . 'Service');
  39.         }
  40.         
  41.         preg_match('/([a-zA-Z0-9]+)Repo/i', $propertyName, $repositoryMatches);
  42.         if(count($repositoryMatches) > 0) {
  43.             return $this
  44.                     ->baseService->serviceLocator
  45.                     ->get('App\Service\\' . ucfirst($repositoryMatches[1]) . 'Service')
  46.                     ->repository;
  47.         }
  48.         
  49.         $cacheKey = 'Const.' . $entityName . '.' . $propertyName;
  50.         $cachedConstantItem = $this->appCache->getItem($cacheKey);
  51.         
  52.         if($cachedConstantItem->isHit()) {
  53.             return $cachedConstantItem->get();
  54.         }
  55.         
  56.         $constant = @constant('App\Entity\\' . $entityName . '::' . $propertyName);
  57.         if(!is_null($constant)) {
  58.             $cachedConstantItem->set($constant);
  59.             $this->appCache->save($cachedConstantItem);
  60.             return $constant;
  61.         }
  62.         
  63.         $metas = $this->entityManager->getMetadataFactory()->getAllMetadata();
  64.         foreach ($metas as $meta) {
  65.             $classPath = $meta->getName();
  66.             $name = strtoupper($this->commonService->toSnakeCase(str_replace('App\Entity\\', '', $classPath)));
  67.             preg_match('/(' . $name . ')_(.+)/i', $propertyName, $matches);
  68.             if(count($matches) > 0) {
  69.                 $prop = $matches[2];
  70.                 $constant = @constant($classPath . '::' . $prop);
  71.                 if(!is_null($constant)) {
  72.                     $cachedConstantItem->set($constant);
  73.                     $this->appCache->save($cachedConstantItem);
  74.                     return $constant;
  75.                 }
  76.             }
  77.         }
  78.         
  79.         trigger_error('Could not found property "' . $propertyName . '" in ' . $this->commonService->getClassName($this), E_USER_ERROR);
  80.     }
  81. }