app/ForkController.php line 73

Open in your IDE?
  1. <?php
  2. namespace ForkCMS\App;
  3. use Backend\Core\Engine\Ajax as BackendAjax;
  4. use Backend\Core\Engine\Backend;
  5. use Frontend\Core\Engine\Ajax as FrontendAjax;
  6. use Frontend\Core\Engine\Frontend;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8. use Backend\Init as BackendInit;
  9. use Frontend\Init as FrontendInit;
  10. use Common\Exception\RedirectException;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Twig_Error;
  13. /**
  14.  * Application routing
  15.  */
  16. class ForkController extends Controller
  17. {
  18.     const DEFAULT_APPLICATION 'Frontend';
  19.     /**
  20.      * Virtual folders mappings
  21.      *
  22.      * @var array
  23.      */
  24.     private static $routes = [
  25.         '' => self::DEFAULT_APPLICATION,
  26.         'private' => 'Backend',
  27.         'Backend' => 'Backend',
  28.         'backend' => 'Backend',
  29.     ];
  30.     /**
  31.      * Get the possible routes
  32.      */
  33.     public static function getRoutes(): array
  34.     {
  35.         return self::$routes;
  36.     }
  37.     /**
  38.      * Runs the backend
  39.      */
  40.     public function backendController(): Response
  41.     {
  42.         defined('APPLICATION') || define('APPLICATION''Backend');
  43.         defined('NAMED_APPLICATION') || define('NAMED_APPLICATION''private');
  44.         $applicationClass $this->initializeBackend('Backend');
  45.         $application = new $applicationClass($this->container->get('kernel'));
  46.         return $this->handleApplication($application);
  47.     }
  48.     /**
  49.      * Runs the backend ajax requests
  50.      */
  51.     public function backendAjaxController(): Response
  52.     {
  53.         defined('APPLICATION') || define('APPLICATION''Backend');
  54.         $applicationClass $this->initializeBackend('BackendAjax');
  55.         $application = new $applicationClass($this->container->get('kernel'));
  56.         return $this->handleApplication($application);
  57.     }
  58.     /**
  59.      * Runs the frontend requests
  60.      */
  61.     public function frontendController(): Response
  62.     {
  63.         defined('APPLICATION') || define('APPLICATION''Frontend');
  64.         $applicationClass $this->initializeFrontend('Frontend');
  65.         $application = new $applicationClass($this->container->get('kernel'));
  66.         return $this->handleApplication($application);
  67.     }
  68.     /**
  69.      * Runs the frontend ajax requests
  70.      */
  71.     public function frontendAjaxController(): Response
  72.     {
  73.         defined('APPLICATION') || define('APPLICATION''Frontend');
  74.         $applicationClass $this->initializeFrontend('FrontendAjax');
  75.         $application = new $applicationClass($this->container->get('kernel'));
  76.         return $this->handleApplication($application);
  77.     }
  78.     /**
  79.      * Runs an application and returns the Response
  80.      */
  81.     protected function handleApplication(ApplicationInterface $application): Response
  82.     {
  83.         $application->passContainerToModels();
  84.         try {
  85.             $application->initialize();
  86.             return $application->display();
  87.         } catch (RedirectException $ex) {
  88.             return $ex->getResponse();
  89.         } catch (Twig_Error $twigError) {
  90.             if ($twigError->getPrevious() instanceof RedirectException) {
  91.                 return $twigError->getPrevious()->getResponse();
  92.             }
  93.             throw $twigError;
  94.         }
  95.     }
  96.     /**
  97.      * @param string $app The name of the application to load (ex. BackendAjax)
  98.      *
  99.      * @return string The name of the application class we need to instantiate.
  100.      */
  101.     protected function initializeBackend(string $app): string
  102.     {
  103.         $init = new BackendInit($this->container->get('kernel'));
  104.         $init->initialize($app);
  105.         return $app === 'BackendAjax' BackendAjax::class : Backend::class;
  106.     }
  107.     /**
  108.      * @param string $app The name of the application to load (ex. frontend_ajax)
  109.      *
  110.      * @return string The name of the application class we need to instantiate.
  111.      */
  112.     protected function initializeFrontend(string $app): string
  113.     {
  114.         $init = new FrontendInit($this->container->get('kernel'));
  115.         $init->initialize($app);
  116.         return $app === 'FrontendAjax' FrontendAjax::class : Frontend::class;
  117.     }
  118. }