Skip to content
This repository has been archived by the owner on Dec 18, 2019. It is now read-only.

Use late static binding so Feature can be extended with overrides. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class Feature {
* our API with non-static methods of the same names and arguments.
*/
public static function getInstance() {
if (!isset(self::$instance)) {
self::$instance = new Feature_Instance();
if (!isset(static::$instance)) {
static::$instance = new Feature_Instance();
}
return self::$instance;
return static::$instance;
}

/**
Expand All @@ -52,7 +52,8 @@ public static function getInstance() {
* @return bool
*/
public static function isEnabled ($name) {
return self::fromConfig($name)->isEnabled();

return static::fromConfig($name)->isEnabled();
}

/**
Expand All @@ -70,7 +71,7 @@ public static function isEnabled ($name) {
* @return bool
*/
public static function isEnabledFor($name, $user) {
return self::fromConfig($name)->isEnabledFor($user);
return static::fromConfig($name)->isEnabledFor($user);
}

/**
Expand All @@ -87,7 +88,7 @@ public static function isEnabledFor($name, $user) {
* @return bool
*/
public static function isEnabledBucketingBy($name, $string) {
return self::fromConfig($name)->isEnabledBucketingBy($string);
return static::fromConfig($name)->isEnabledBucketingBy($string);
}

/**
Expand All @@ -106,7 +107,7 @@ public static function isEnabledBucketingBy($name, $string) {
* @param string $name the config key for the feature.
*/
public static function variant($name) {
return self::fromConfig($name)->variant();
return static::fromConfig($name)->variant();
}

/**
Expand Down Expand Up @@ -134,7 +135,7 @@ public static function variant($name) {
* and hashed to get the bucketing.
*/
public static function variantFor($name, $user) {
return self::fromConfig($name)->variantFor($user);
return static::fromConfig($name)->variantFor($user);
}

/**
Expand All @@ -160,14 +161,14 @@ public static function variantFor($name, $user) {
* @param string $bucketingID A string to use as the bucketing ID.
*/
public static function variantBucketingBy($name, $bucketingID) {
return self::fromConfig($name)->variantBucketingBy($bucketingID);
return static::fromConfig($name)->variantBucketingBy($bucketingID);
}

/*
* Description of the feature.
*/
public static function description ($name) {
return self::fromConfig($name)->description();
return static::fromConfig($name)->description();
}

/**
Expand All @@ -179,7 +180,7 @@ public static function description ($name) {
* @return mixed
*/
public static function data($name, $default = array()) {
return self::world()->configValue("$name.data", $default);
return static::world()->configValue("$name.data", $default);
}

/**
Expand All @@ -191,8 +192,8 @@ public static function data($name, $default = array()) {
* @return mixed
*/
public static function variantData($name, $default = array()) {
$data = self::data($name);
$variant = self::variant($name);
$data = static::data($name);
$variant = static::variant($name);
return isset($data[$variant]) ? $data[$variant] : $default;
}

Expand All @@ -207,12 +208,13 @@ public static function variantData($name, $default = array()) {
* @return Feature_Config
*/
private static function fromConfig($name) {
if (array_key_exists($name, self::$configCache)) {
return self::$configCache[$name];
if (array_key_exists($name, static::$configCache)) {
return static::$configCache[$name];
} else {
$world = self::world();
$world = static::world();
drupal_set_message(get_class($world));
$stanza = $world->configValue($name);
return self::$configCache[$name] = new Feature_Config($name, $stanza, $world);
return static::$configCache[$name] = new Feature_Config($name, $stanza, $world);
}
}

Expand All @@ -223,7 +225,7 @@ private static function fromConfig($name) {
* have those changes be reflected in feature checks.)
*/
public static function clearCacheForTests() {
self::$configCache = array();
static::$configCache = array();
}


Expand All @@ -234,17 +236,17 @@ public static function clearCacheForTests() {
* what variants and why during the course of handling a request.
*/
public static function selections () {
return self::world()->selections();
return static::world()->selections();
}

/**
* This API always uses the default World. Feature_Config takes
* the world as an argument in order to ease unit testing.
*/
private static function world () {
if (!isset(self::$defaultWorld)) {
self::$defaultWorld = new Feature_World(new Feature_Logger());
if (!isset(static::$defaultWorld)) {
static::$defaultWorld = new Feature_World(new Feature_Logger());
}
return self::$defaultWorld;
return static::$defaultWorld;
}
}