You are here

class Engine in ThemeKey 8

Hierarchy

Expanded class hierarchy of Engine

1 string reference to 'Engine'
themekey.services.yml in ./themekey.services.yml
themekey.services.yml
1 service uses Engine
themekey.engine in ./themekey.services.yml
Drupal\themekey\Engine\Engine

File

src/Engine/Engine.php, line 20

Namespace

Drupal\themekey\Engine
View source
class Engine implements EngineInterface {
  use PropertyManagerTrait;
  use OperatorManagerTrait;
  use RuleChainManagerTrait;

  /**
   * The system theme config object.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;
  protected $routeMatch;

  /**
   * Constructs a DefaultNegotiator object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * @return \Drupal\Core\Routing\RouteMatchInterface
   */
  public function getRouteMatch() {
    return $this->routeMatch;
  }

  /**
   * @return \Drupal\Core\Config\ConfigFactoryInterface
   */
  public function getConfigFactory() {
    return $this->configFactory;
  }

  /**
   * {@inheritdoc}
   */
  public function determineTheme(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;

    #var_dump($route_match);
    $ruleChainManager = $this
      ->getRuleChainManager();
    $chain = $ruleChainManager
      ->getOptimizedChain();
    return $chain ? $this
      ->walkRuleChildren($chain) : NULL;
  }
  protected function walkRuleChildren($chain, $theme = NULL, $parent = 0) {
    $has_children = FALSE;
    foreach ($chain as $ruleId => $ruleMetaData) {
      if ($ruleMetaData['parent'] == $parent) {
        $has_children = TRUE;
        $rule = ThemeKeyRule::load($ruleId);
        if ($this
          ->matchCondition($rule)) {
          $theme = $this
            ->walkRuleChildren($chain, $rule
            ->theme(), $ruleId);
          if ($theme) {
            return $theme;
          }
        }
      }
    }

    // No children: return theme of parent.
    // Has children: all children did not match => return no theme.
    return $has_children ? NULL : $theme;
  }

  /**
   * Detects if a ThemeKey rule matches to the current
   * page request.
   *
   * @param object $rule
   *   ThemeKey rule as object:
   *   - property
   *   - operator
   *   - value
   *
   * @return bool
   */
  protected function matchCondition(ThemeKeyRuleInterface $rule) {
    $operator = $this
      ->getOperatorManager()
      ->createInstance($rule
      ->operator());
    $property = $this
      ->getPropertyManager()
      ->createInstance($rule
      ->property());
    $property
      ->setEngine($this);

    #drupal_set_message(print_r($property->getValues(), TRUE));
    $values = $property
      ->getValues();
    $key = $rule
      ->key();
    if (!is_null($key)) {
      if (isset($values[$key])) {
        return $operator
          ->evaluate($values[$key], $rule
          ->value());
      }
    }
    else {
      foreach ($values as $value) {
        if ($operator
          ->evaluate($value, $rule
          ->value())) {
          return TRUE;
        }
      }
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Engine::$configFactory protected property The system theme config object.
Engine::$routeMatch protected property
Engine::determineTheme public function Determine the active theme for the request. Overrides EngineInterface::determineTheme
Engine::getConfigFactory public function Overrides EngineInterface::getConfigFactory
Engine::getRouteMatch public function Overrides EngineInterface::getRouteMatch
Engine::matchCondition protected function Detects if a ThemeKey rule matches to the current page request.
Engine::walkRuleChildren protected function
Engine::__construct public function Constructs a DefaultNegotiator object.
OperatorManagerTrait::$operatorManager private property @var
OperatorManagerTrait::getOperatorManager protected function Gets the ThemeKey Operator manager.
OperatorManagerTrait::setOperatorManager public function Sets the ThemeKey Property manager to use.
PropertyManagerTrait::$propertyManager private property @var
PropertyManagerTrait::getPropertyManager protected function Gets the ThemeKey Property manager.
PropertyManagerTrait::setPropertyManager public function Sets the ThemeKey Property manager to use.
RuleChainManagerTrait::$ruleChainManager private property @var
RuleChainManagerTrait::getRuleChainManager protected function Gets the ThemeKey Rule Chain manager. 1
RuleChainManagerTrait::setRuleChainManager public function Sets the ThemeKey Rule Chain manager to use.