Skip to content

Instantly share code, notes, and snippets.

@stojg
Created April 5, 2017 00:27
Show Gist options
  • Save stojg/3c41324e784411a31f258921997b0b23 to your computer and use it in GitHub Desktop.
Save stojg/3c41324e784411a31f258921997b0b23 to your computer and use it in GitHub Desktop.
Playing around with features
<?php
// could be abstract class?
interface FeatureInterface {
function isSupported();
}
interface LetmeinInterface extends FeatureInterface {
function letmein($username, $password);
}
class CWPLetmein implements LetmeinInterface {
function _construct(\CWPEnvironment $environment) {
$this->env = $environment;
}
function isSupported() {
// CWPLetmeInConfig is a DataObject with a HasOne to CWPEnvironment that contains data
// that DNEnvironment shouldnt care about. It's a feature specific data stored somewhere ...
$config = CWPLetmeInConfig::get()->where('CWPEnvironmentID', $this->env->ID);
return $config->isEnabled;
}
function letmein($username, $password) {
$config = CWPLetmeInConfig::get()->where('CWPEnvironmentID', $this->env->ID);
$config->username = $username;
$config->password = $password;
$config->write();
$this->schedulePuppetRerun($config);
return true;
}
}
class RainforestLetmein implements LetmeinInterface {
function _construct(\RainforestEnvironment $environment) {
$this->env = $environment;
}
function isSupported() {
if (!$this->env->manifestAtLeat('4.1.3')) {
throw new BackendUnsupportedException('at least 4.1.3');
}
return true;
}
// Use $this->env to get base data, maybe this backend queries some other data?
function letmein($username, $password) {
return true;
}
}
// mysite specific environment
class RainforestEnvironment {
function supports($feature) {
if($this->features == null) {
$this->features = $this->initSupports();
}
if(isset($this->features[$feature])) {
// this might throw an exception, or highlight that it's supported, but not usable at the moment
if($this->features[$feature]->isSupported()) {
$this->features[$feature];
}
}
return false;
}
// could be setup somewhere else
function initSupports() {
$features = [
GenericFeatureInterface::class => new SomeGenericFeature($this),
DomainsSupportInterface::class => new RainforestDomains($this),
];
if($this->manifestAtLeast('4.1.3')) {
$features[LetmeinInterface::class] => new RainforestLetmein($this),
}
// db value, feature flag, bad example perhaps?
if($this->NewDeployModal) {
$features[DeployScreenInterface::class] = new DeployDispatcher($this);
} else {
$features[DeployScreenInterface::class] = new DNRoot($this); // eh, I hope you get the point
}
return $features;
}
}
// On UI dispatcher action:
try {
if ($feature = $env->supports(LetmeinInterface::class)) {
$feature->letmein($username, $password);
}
} catch (\BackendUnsupportedException $ex) {
// show warning for user? can we use something else than exceptions?
}
// UI / Rendering? where is that? I dunno..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment