From b5d713d19e0f431bf827fa8f22714d9555ac59e7 Mon Sep 17 00:00:00 2001 From: Mike Van Winkle Date: Fri, 6 Mar 2015 21:45:17 +0000 Subject: [PATCH] Refactor clone to use workflows. resolves #173 --- php/Terminus/Site.php | 20 +++++++++----------- php/commands/site.php | 26 +++++++++++++------------- php/terminus.php | 2 +- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/php/Terminus/Site.php b/php/Terminus/Site.php index 795747c6a..b074415ef 100755 --- a/php/Terminus/Site.php +++ b/php/Terminus/Site.php @@ -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( @@ -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; } /** diff --git a/php/commands/site.php b/php/commands/site.php index a30b728e0..c560b6f8e 100755 --- a/php/commands/site.php +++ b/php/commands/site.php @@ -13,6 +13,7 @@ use \Terminus\Helpers\Input; use \Terminus\Deploy; use \Terminus\SiteWorkflow; +use \Terminus\EnvironmentWorkflow; class Site_Command extends Terminus_Command { @@ -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=]`", $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; } @@ -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 * diff --git a/php/terminus.php b/php/terminus.php index b6ac37912..7616cdc2a 100755 --- a/php/terminus.php +++ b/php/terminus.php @@ -1,7 +1,7 @@