Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Model/Collection for Bindings #303

Merged
merged 1 commit into from
Jul 30, 2015
Merged
Show file tree
Hide file tree
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
39 changes: 39 additions & 0 deletions php/Terminus/Collections/Bindings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Terminus\Collections;
use Terminus\Request;
use Terminus\Models\Binding;
use \Terminus_Command;

class Bindings {
private $environment;
private $models = array();

public function __construct($options = array()) {
$this->environment = $options['environment'];

return $this;
}

public function fetch() {
$results = Terminus_Command::request("sites", $this->environment->site->getId(), "bindings", "GET");

foreach (get_object_vars($results['data']) as $id => $binding_data) {
# Only include bindings for this environment
if ($binding_data->environment == $this->environment->id) {
$binding_data->id = $id;
$this->models[$id] = new Binding($binding_data, array('collection' => $this));
}
}

return $this;
}

public function get($id) {
return array_key_exists($id, $this->models) ? $this->models[$id] : null;
}

public function all() {
return array_values($this->models);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Terminus;
namespace Terminus\Collections;
use Terminus\Request;
use Terminus\Environment;
use \Terminus_Command;
Expand All @@ -23,7 +23,7 @@ public function fetch() {
$this->models[$id] = new Environment($this->site, $environment_data);
}

return $this->all();
return $this;
}

public function get($id) {
Expand Down
9 changes: 7 additions & 2 deletions php/Terminus/Environment.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php
namespace Terminus;
use \ReflectionClass, \Terminus\Request, \Terminus\EnvironmentWorkflow;
use \ReflectionClass;
use \Terminus\Request;
use \Terminus\EnvironmentWorkflow;
use \Terminus\Collections\Bindings;

class Environment {
public $id;
public $attributes;
public $bindings;

public $name = 'dev';
public $site = false;
Expand All @@ -19,7 +23,6 @@ class Environment {
public $target_ref;
public $watchers;
public $backups;
public $bindings;

public function __construct( Site $site, $data = null) {
$this->site = $site;
Expand All @@ -28,6 +31,8 @@ public function __construct( Site $site, $data = null) {
}
$this->attributes = $data;

$this->bindings = new Bindings(array('environment' => $this));

if (is_object($data)) {
// if we receive an environment object from the api hydrate the vars
$environment_properties = get_object_vars($data);
Expand Down
19 changes: 19 additions & 0 deletions php/Terminus/Models/Binding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Terminus\Models;
use \Terminus\Request;

class Binding {
public $id;
public $attributes;
public $collection;
public $environment;

public function __construct($attributes, $options = array()) {
$this->id = $attributes->id;
$this->attributes = $attributes;
$this->collection = $options['collection'];
$this->environment = $options['environment'];

return $this;
}
}
2 changes: 1 addition & 1 deletion php/Terminus/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Terminus\Deploy;
use \Terminus\SiteWorkflow;
use \Terminus_Command;
use Terminus\Environments;
use Terminus\Collections\Environments;

class Site {
public $id;
Expand Down