src/Service/ServiceController.php line 74

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Doctrine\Common\Annotations\AnnotationReader;
  4. use Exception;
  5. use Symfony\Bridge\Doctrine\RegistryInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\KernelInterface;
  10. /**
  11.  * ProtoCMS Services - accessible anywhere
  12.  * USAGE in Controller : Typehint ServiceController or use Dependency Injection
  13.  * $this->serviceController->testFunction();.
  14.  */
  15. class ServiceController extends Controller
  16. {
  17.     protected \Symfony\Component\HttpKernel\KernelInterface $kernel;
  18.     protected $container;
  19.     protected \Symfony\Bridge\Doctrine\RegistryInterface $doctrine;
  20.     protected $projectRoot;
  21.     public function __construct(KernelInterface $kernelRegistryInterface $doctrine$projectRoot)
  22.     {
  23.         $this->kernel $kernel;
  24.         $this->container $this->kernel->getContainer();
  25.         $this->doctrine $doctrine;
  26.         $this->projectRoot $projectRoot;
  27.     }
  28.     //TODO: redirect will not work in a service - needs a rethink
  29.     public function refreshPageifLocalChanged(Request $request)
  30.     {
  31.         if ($request->query->has('_locale') && $request->query->get('_locale') != $request->getLocale()) {
  32.             $request->setLocale($request->query->get('_locale'));
  33.             $request->getSession()->set('_locale'$request->query->get('_locale'));
  34.             return $this->redirect($request->getUri());
  35.         }
  36.     }
  37.     //helper function for getInheritedCMSPage() function
  38.     //get bundle namespace for current controller
  39.     public function getBundleNamespace(Request $request)
  40.     {
  41.         $controllerpath $request->attributes->get('_controller');
  42.         if (false !== strpos($controllerpath'::')) {
  43.             $params explode('::'$controllerpath);
  44.             return $params[0];
  45.             // $bundleStructure = explode('\\', $params[0]);
  46.             // return $bundleStructure[0].$bundleStructure[1];
  47.         }
  48.         throw new Exception('Controller path needs accounting for');
  49.         // $bundleStructure = explode(':', $controllerpath);
  50.         // return $bundleStructure[0];
  51.     }
  52.     //finds page entity containing the inherited_bundle_name field relating to the bundle found in the request
  53.     //used to link none CMS pages to a CMS page (inherits page data i.e. content, meta, ect...)
  54.     public function getInheritedCMSPage(Request $request)
  55.     {
  56.         $bundleName $this->getBundleNamespace($request);
  57.         return $this->doctrine->getRepository('App:Page')->getInheritedBundleName($bundleName);
  58.     }
  59.     //Fetch cms component vars for all controllers containing the @CmsComponent annotation
  60.     public function fetchCmsComponents()
  61.     {
  62.         $cmsComponentArray = [];
  63.         $annotationReader = new AnnotationReader();
  64.         $controllersDir $this->projectRoot.'/src/Controller';
  65.         $files scandir($controllersDir);
  66.         foreach ($files as $file) {
  67.             [$filename$ext] = explode('.'$file);
  68.             if ('php' != $ext) {
  69.                 continue;
  70.             }
  71.             $reflectedClass = new \ReflectionClass('App\\Controller\\'.$filename);
  72.             foreach ($reflectedClass->getMethods() as $reflectedMethod) {
  73.                 // the annotations
  74.                 $annotations $annotationReader->getMethodAnnotations($reflectedMethod);
  75.                 if (!empty($annotations)) {
  76.                     foreach ($annotations as $annotation) {
  77.                         $annotationName get_class($annotation);
  78.                         $pos stripos($annotationNamestrtolower('CmsComponent'));
  79.                         //var_dump($annotation);
  80.                         if (false !== $pos && true == $annotation->active) {
  81.                             $cmsComponentArray[] = [
  82.                                 'name' => $annotation->propertyName,
  83.                                 'slug' => $annotation->slug,
  84.                                 'route' => $annotation->routeName,
  85.                                 'slugEntity' => $annotation->slugEntity,
  86.                                 'bundle' => 'App',
  87.                                 'componentType' => $annotation->componentType,
  88.                             ];
  89.                         }
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.         return $cmsComponentArray;
  95.     }
  96.     public function getBundleNameFromEntity($entity$field null)
  97.     {
  98.         //get entity properties
  99.         $reflClass = new \ReflectionClass($entity);
  100.         $refMeth $reflClass->getMethods();
  101.         $refProp $reflClass->getProperties();
  102.         //get namespace
  103.         $pos strpos($refProp[0]->class'Bundle');
  104.         $segments explode('\\'$refProp[0]->class);
  105.         if (false !== $pos) {
  106.             $namespace $segments[0].$segments[1].':'.$segments[3];
  107.             $short $segments[3];
  108.         } else {
  109.             $namespace $segments[0].':'.$segments[2];
  110.             $short $segments[2];
  111.         }
  112.         //get field data
  113.         $nameMetadata null;
  114.         if (null != $field) {
  115.             $metadata $this->doctrine->getManager()->getClassMetadata($refProp[0]->class);
  116.             $nameMetadata $metadata->fieldMappings[$field];
  117.         }
  118.         //print_r($nameMetadata);
  119.         return ['full' => $namespace'short' => $short'fieldmeta' => $nameMetadata];
  120.     }
  121.     //Fetch cms bundles registered with the kernel
  122.     public function fetchProtoCmsBundles()
  123.     {
  124.         $bundles = [];
  125.         $controllersDir $this->projectRoot.'/src/Controller';
  126.         $files scandir($controllersDir);
  127.         foreach ($files as $file) {
  128.             if (false === strpos($file'Controller')) {
  129.                 continue;
  130.             }
  131.             [$filename$ext] = explode('.'$file);
  132.             $bundles[] = 'App\\Controller\\'.$filename;
  133.         }
  134.         return $bundles;
  135.     }
  136.     // helper function for component redirects
  137.     public function componentRedirect($url)
  138.     {
  139.         return new Response(json_encode(['componentRedirect' => $url], JSON_THROW_ON_ERROR));
  140.     }
  141. }