You are here

SettingsFactory.php in Menu Bean 7

Settings Plugin Factor

Namespace

Drupal\menu_bean

File

lib/Drupal/menu_bean/SettingsFactory.php
View source
<?php

/**
 * @file
 * Settings Plugin Factor
 */
namespace Drupal\menu_bean;

use Drupal\menu_bean\Filter\SettingsFilterInterface;
use Drupal\menu_bean\Form\SettingsFormInterface;
use Drupal\menu_bean\Setting\SettingInterface;

/**
 * Class SettingsFactor
 * @singleton
 */
class SettingsFactory {

  /**
   * Get the Settings Form Class
   *
   * @param $key
   * @param $info
   * @throws MenuBeanException
   * @return SettingInterface
   */
  public static function getSettingInstance($key, $info) {
    return self::getInstance($key, menu_bean_get_settings_class($info), 'Drupal\\menu_bean\\Form\\SettingInterface');
  }

  /**
   * @param $key
   * @param $class
   * @param $interface
   * @return SettingsFilterInterface|SettingInterface
   * @throws MenuBeanException
   */
  protected static function getInstance($key, $class, $interface) {
    static $instances = array();
    if (!isset($instances[$key])) {
      $ref_class = new \ReflectionClass($class);
      if (in_array($interface, $ref_class
        ->getInterfaceNames())) {
        throw new MenuBeanException("{$interface} does not match " . $class);
      }
      $instances[$key] = new $class();
    }
    return $instances[$key];
  }

}

Classes

Namesort descending Description
SettingsFactory Class SettingsFactor @singleton