vendor/contao-bootstrap/core/src/Environment.php line 163

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao Bootstrap
  4.  *
  5.  * @package    contao-bootstrap
  6.  * @subpackage Core
  7.  * @author     David Molineus <david.molineus@netzmacht.de>
  8.  * @copyright  2017 netzmacht David Molineus. All rights reserved.
  9.  * @license    LGPL-3.0 https://github.com/contao-bootstrap/core
  10.  * @filesource
  11.  */
  12. namespace ContaoBootstrap\Core;
  13. use Contao\LayoutModel;
  14. use ContaoBootstrap\Core\Config\ArrayConfig;
  15. use ContaoBootstrap\Core\Environment\Context;
  16. use ContaoBootstrap\Core\Exception\LeavingContextFailed;
  17. use ContaoBootstrap\Core\Message\Event\ContextEntered;
  18. use ContaoBootstrap\Core\Message\Command\BuildContextConfig;
  19. use Symfony\Component\EventDispatcher\EventDispatcherInterface as MessageBus;
  20. /**
  21.  * Class Environment contain all things being provided in the bootstrap environment.
  22.  *
  23.  * @package ContaoBootstrap\Core
  24.  */
  25. final class Environment
  26. {
  27.     /**
  28.      * Bootstrap enabled state.
  29.      *
  30.      * @var bool
  31.      */
  32.     protected $enabled false;
  33.     /**
  34.      * Bootstrap config.
  35.      *
  36.      * @var Config
  37.      */
  38.     protected $config;
  39.     /**
  40.      * Layout model of current page.
  41.      *
  42.      * @var \LayoutModel
  43.      */
  44.     private $layout;
  45.     /**
  46.      * Current context.
  47.      *
  48.      * @var Context
  49.      */
  50.     private $context;
  51.     /**
  52.      * List of contexts.
  53.      *
  54.      * @var Context[]
  55.      */
  56.     private $contextStack;
  57.     /**
  58.      * MessageBus.
  59.      *
  60.      * @var MessageBus
  61.      */
  62.     private $messageBus;
  63.     /**
  64.      * Construct.
  65.      *
  66.      * @param MessageBus $messageBus Message bus.
  67.      */
  68.     public function __construct(MessageBus $messageBus)
  69.     {
  70.         $this->messageBus $messageBus;
  71.         $this->config     = new ArrayConfig();
  72.     }
  73.     /**
  74.      * Get bootstrap config.
  75.      *
  76.      * @return Config
  77.      */
  78.     public function getConfig(): Config
  79.     {
  80.         return $this->config;
  81.     }
  82.     /**
  83.      * Enter a context.
  84.      *
  85.      * @param Context $context Context.
  86.      *
  87.      * @return void
  88.      */
  89.     public function enterContext(Context $context): void
  90.     {
  91.         // Already in the context.
  92.         if ($this->context && $this->context->match($context)) {
  93.             return;
  94.         }
  95.         $this->switchContext($contexttrue);
  96.     }
  97.     /**
  98.      * Leave current context and enter the context which was used before.
  99.      *
  100.      * @param Context|null $currentContext Optional expected current context. Won't do anything if context not match.
  101.      *
  102.      * @return void
  103.      * @throws LeavingContextFailed When context stack is empty.
  104.      */
  105.     public function leaveContext(?Context $currentContext null): void
  106.     {
  107.         if (!$this->context) {
  108.             throw LeavingContextFailed::noContext();
  109.         }
  110.         // Not in expected context. Just quit.
  111.         if ($currentContext && !$currentContext->match($this->context)) {
  112.             return;
  113.         }
  114.         // Get last know context which was used before current context.
  115.         $context array_pop($this->contextStack);
  116.         if ($context === null) {
  117.             throw LeavingContextFailed::inContext($this->context);
  118.         }
  119.         $this->switchContext($context);
  120.     }
  121.     /**
  122.      * Switch to another context.
  123.      *
  124.      * @param Context $context            New context.
  125.      * @param bool    $keepCurrentInStack If true current context is added to the stack.
  126.      *
  127.      * @return void
  128.      */
  129.     private function switchContext(Context $contextbool $keepCurrentInStack false): void
  130.     {
  131.         $command = new BuildContextConfig($this$context$this->config);
  132.         $this->messageBus->dispatch($command::NAME$command);
  133.         if ($command->getConfig()) {
  134.             $this->config $command->getConfig();
  135.         }
  136.         if ($keepCurrentInStack && $this->context) {
  137.             $this->contextStack[] = $this->context;
  138.         }
  139.         $this->context $context;
  140.         $event = new ContextEntered($this$context);
  141.         $this->messageBus->dispatch($event::NAME$event);
  142.     }
  143.     /**
  144.      * Consider if bootstrap theme is enabled.
  145.      *
  146.      * @return bool
  147.      */
  148.     public function isEnabled(): bool
  149.     {
  150.         return $this->enabled;
  151.     }
  152.     /**
  153.      * Enable current bootstrap theme.
  154.      *
  155.      * @param bool $enabled Enabled state.
  156.      *
  157.      * @return $this
  158.      */
  159.     public function setEnabled(bool $enabled): self
  160.     {
  161.         $this->enabled $enabled;
  162.         return $this;
  163.     }
  164.     /**
  165.      * Set the layout.
  166.      *
  167.      * @param LayoutModel $layout Page layout.
  168.      *
  169.      * @return $this
  170.      */
  171.     public function setLayout(LayoutModel $layout): self
  172.     {
  173.         $this->layout $layout;
  174.         return $this;
  175.     }
  176.     /**
  177.      * Get the page layout.
  178.      *
  179.      * @return LayoutModel|null
  180.      */
  181.     public function getLayout()
  182.     {
  183.         return $this->layout;
  184.     }
  185. }