Skip to content

Commit

Permalink
Refactor clone to use workflows. resolves #173
Browse files Browse the repository at this point in the history
  • Loading branch information
mikevanwinkle committed Mar 6, 2015
1 parent f2369e4 commit b5d713d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
20 changes: 9 additions & 11 deletions php/Terminus/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,10 @@ public function bindings($type=null) {
* Return all environments for a site
*/
public function environments() {
$cache = \Terminus::get_cache();
if (empty($this->environments)) {
if (!$environments = $cache->get_data("environments:{$this->id}")) {
$results = \Terminus_Command::request("sites", $this->getId(), "environments", "GET");
$environments = $results['data'];
$cache->put_data("environments:{$this->id}",$environments);
}
$this->environments = $environments;
}

$results = \Terminus_Command::request("sites", $this->getId(), "environments", "GET");
$environments = $results['data'];
$this->environments = $environments;

// instantiate local objects
foreach ( $this->environments as $name => $env) {
$this->environments->$name = EnvironmentFactory::load($this, $name, array(
Expand All @@ -92,8 +86,12 @@ public function environment($environment) {
} else {
// load the environments
$this->environments();
if (array_key_exists($environment,$this->environments)) {
return $this->environments->$environment;
} else {
throw new \Terminus\Iterators\Exception("No such environment $environment.");
}
}
return $this->environments->$environment;
}

/**
Expand Down
26 changes: 13 additions & 13 deletions php/commands/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use \Terminus\Helpers\Input;
use \Terminus\Deploy;
use \Terminus\SiteWorkflow;
use \Terminus\EnvironmentWorkflow;

class Site_Command extends Terminus_Command {

Expand Down Expand Up @@ -563,20 +564,25 @@ public function clone_env($args, $assoc_args) {

$confirm = sprintf("Are you sure?\n\tClone from %s to %s\n\tInclude: %s\n", strtoupper($from_env), strtoupper($to_env), $append);
\Terminus::confirm($confirm);

if ( !$this->envExists($site_id, $to_env) ) {
\Terminus::error("The %s environment has not been created yet. run `terminus site create-env [--site=<env>]`", $to_env);
}
$to_env = $site->environment($to_env);
$from_env = $site->environment($from_env);

if ($db) {
print "Cloning database ... ";
$this->cloneObject( $to_env, $from_env, $site_id, 'database');
print "Cloning database ... ";
$workflow = new EnvironmentWorkflow('clone_database','sites', $to_env);
$workflow->setParams(array('from_environment' => $from_env->name));
$workflow->setMethod('POST');
$workflow->start()->wait();;
}

if ($files) {
print "Cloning files ... ";
$this->cloneObject( $to_env, $from_env, $site_id, 'files');
$workflow = new EnvironmentWorkflow('clone_files','sites', $to_env);
$workflow->setParams(array('from_environment' => $from_env->name));
$workflow->setMethod('POST');
$workflow->start()->wait();
}

\Terminus::success("Clone complete!");
return true;
}
Expand Down Expand Up @@ -741,12 +747,6 @@ function environments($args, $assoc_args) {
return $data;
}

private function envExists($site_id, $env) {
$response = \Terminus_Command::request('sites', $site_id, 'code-tips', 'GET');
$envs = (array) $response['data'];
return array_key_exists($env, $envs);
}

/**
* Hostname operations
*
Expand Down
2 changes: 1 addition & 1 deletion php/terminus.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
// Can be used by plugins/themes to check if Terminus is running or not
define( 'Terminus', true );
define( 'TERMINUS_VERSION', '0.5.4');
define( 'TERMINUS_VERSION', '0.5.5');
$source = 'unknown';
if ('cli' === PHP_SAPI && isset($argv)) {
$source = explode('/',$argv[0]);
Expand Down

0 comments on commit b5d713d

Please sign in to comment.