app/KernelLoader.php line 37

Open in your IDE?
  1. <?php
  2. namespace ForkCMS\App;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Symfony\Component\HttpKernel\KernelInterface;
  5. use Backend\Core\Engine\Model as BackendModel;
  6. use Frontend\Core\Engine\Model as FrontendModel;
  7. /**
  8.  * This class is used in several Fork applications to bubble down the AppKernel/Kernel object.
  9.  */
  10. class KernelLoader
  11. {
  12.     /**
  13.      * @var KernelInterface
  14.      */
  15.     protected $kernel;
  16.     /**
  17.      * @param KernelInterface $kernel
  18.      */
  19.     public function __construct(KernelInterface $kernel)
  20.     {
  21.         $this->setKernel($kernel);
  22.     }
  23.     /**
  24.      * Gets a service by id.
  25.      *
  26.      * @param string $reference The service id
  27.      *
  28.      * @return mixed The service
  29.      */
  30.     public function get(string $reference)
  31.     {
  32.         return $this->getKernel()->getContainer()->get($reference);
  33.     }
  34.     /**
  35.      * @return ContainerInterface
  36.      */
  37.     public function getContainer(): ContainerInterface
  38.     {
  39.         return $this->getKernel()->getContainer();
  40.     }
  41.     /**
  42.      * @return KernelInterface
  43.      */
  44.     public function getKernel(): KernelInterface
  45.     {
  46.         return $this->kernel;
  47.     }
  48.     /**
  49.      * Returns true if the service id is defined.
  50.      *
  51.      * @param string $reference The service id
  52.      *
  53.      * @return bool true if the service id is defined, false otherwise
  54.      */
  55.     public function has(string $reference): bool
  56.     {
  57.         return $this->getKernel()->getContainer()->has($reference);
  58.     }
  59.     /**
  60.      * This is fairly dirty, but so is having static method classes for models.
  61.      * Consider this a temporary solution until we have genuine models available.
  62.      */
  63.     public function passContainerToModels(): void
  64.     {
  65.         FrontendModel::setContainer($this->getKernel()->getContainer());
  66.         BackendModel::setContainer($this->getKernel()->getContainer());
  67.     }
  68.     /**
  69.      * @param KernelInterface $kernel
  70.      */
  71.     public function setKernel(KernelInterface $kernel null)
  72.     {
  73.         $this->kernel $kernel;
  74.     }
  75. }