From 6101a09ced8299a91451762425f97a111c82e784 Mon Sep 17 00:00:00 2001 From: Sara McCutcheon Date: Wed, 30 Mar 2016 14:47:37 -0700 Subject: [PATCH] Streamlined the site-creation process --- CHANGELOG.md | 6 +++ php/Terminus/Caches/FileCache.php | 10 ++-- php/Terminus/Caches/SitesCache.php | 17 ++++--- php/Terminus/Commands/SitesCommand.php | 16 +++---- php/Terminus/Helpers/InputHelper.php | 29 ++++++++++-- php/Terminus/Models/Collections/Sites.php | 51 ++++++++++----------- tests/features/cli.feature | 2 +- tests/unit_tests/caches/test-file-cache.php | 2 +- 8 files changed, 78 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81d863a28..8fed150f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ All notable changes to this project starting with the 0.6.0 release will be docu - New parameter `--owner` added to `sites list` to filter the list just for the sites the current user owns. (#1003) - New option to filter for organization sites via `sites list --org=all`. (#1003) +### Changed +- `InputHelper#upstream()` now returns the UUID of the chosen upstream rather than an upstream object. (#1013) +- `InputHelper#upstream()` will not check upstream data if it has been given a UUID in the $args[$key]. (#1013) +- `InputHelper#orgId()` will not check organizational data if it has been given a UUID in the $args[$key]. (#1013) +- Running `Sites#addSiteToCache` while the cache is empty will no longer trigger a full cache sync. (#1013) + ### Fixed - Alternate command suggestion for `drush "sql-connect"` corrected to `site connection-info --field=mysql_command`. (#1005) diff --git a/php/Terminus/Caches/FileCache.php b/php/Terminus/Caches/FileCache.php index 1afe0812e..d0bddffe9 100755 --- a/php/Terminus/Caches/FileCache.php +++ b/php/Terminus/Caches/FileCache.php @@ -138,11 +138,11 @@ public function flush() { * [bool] ttl TTL for file read * @return bool|string The file contents or false */ - public function getData($key, array $options = array()) { - $defaults = array( + public function getData($key, array $options = []) { + $defaults = [ 'decode_array' => false, - 'ttl' => null - ); + 'ttl' => null, + ]; $options = array_merge($defaults, $options); try { @@ -151,7 +151,7 @@ public function getData($key, array $options = array()) { return false; } - $data = false; + $data = []; if ($contents) { $data = json_decode($contents, $options['decode_array']); } diff --git a/php/Terminus/Caches/SitesCache.php b/php/Terminus/Caches/SitesCache.php index e24fa65a0..138003204 100644 --- a/php/Terminus/Caches/SitesCache.php +++ b/php/Terminus/Caches/SitesCache.php @@ -72,15 +72,18 @@ public function __construct() { * @param array $memberships_data Memberships of use to add to cache * @return array */ - public function add(array $memberships_data = array()) { + public function add(array $memberships_data = []) { $cache = (array)$this->cache->getData( $this->cachekey, - array('decode_array' => true) + ['decode_array' => true,] ); + if (!$cache) { + $cache = []; + } //If a single site item is passed in, wrap it in an array if (isset($memberships_data['id'])) { - $memberships_data = array($memberships_data); + $memberships_data = [$memberships_data,]; } foreach ($memberships_data as $membership_data) { @@ -96,7 +99,7 @@ public function add(array $memberships_data = array()) { //Then add the membership $cache[$site_name] = array_merge( $cache[$site_name], - array('memberships' => array($membership_id => $membership)) + ['memberships' => [$membership_id => $membership,],] ); } @@ -328,7 +331,7 @@ private function fetchUserOrganizations() { * @return array */ private function getSiteData($response_data, $membership_data = array()) { - $site_data = array( + $site_data = [ 'id' => null, 'name' => null, 'label' => null, @@ -341,8 +344,8 @@ private function getSiteData($response_data, $membership_data = array()) { 'holder_type' => null, 'holder_id' => null, 'owner' => null, - 'membership' => array(), - ); + 'membership' => [], + ]; foreach ($site_data as $index => $value) { if (($value == null) && isset($response_data[$index])) { $site_data[$index] = $response_data[$index]; diff --git a/php/Terminus/Commands/SitesCommand.php b/php/Terminus/Commands/SitesCommand.php index 6c7ac5b0c..51534815b 100755 --- a/php/Terminus/Commands/SitesCommand.php +++ b/php/Terminus/Commands/SitesCommand.php @@ -119,13 +119,11 @@ public function aliases($args, $assoc_args) { * */ public function create($args, $assoc_args) { - $options = $this->getSiteCreateOptions($assoc_args); - $upstream = $this->input()->upstream(['args' => $assoc_args]); - $options['upstream_id'] = $upstream->get('id'); - $this->log()->info( - 'Creating new {upstream} installation ... ', - array('upstream' => $upstream->get('longname')) + $options = $this->getSiteCreateOptions($assoc_args); + $options['upstream_id'] = $this->input()->upstream( + ['args' => $assoc_args,] ); + $this->log()->info('Creating new site installation ... '); $workflow = $this->sites->addSite($options); $workflow->wait(); @@ -137,9 +135,9 @@ public function create($args, $assoc_args) { $this->helpers->launch->launchSelf( [ - 'command' => 'site', - 'args' => ['info'], - 'assoc_args' => ['site' => $options['name']] + 'command' => 'site', + 'args' => ['info',], + 'assoc_args' => ['site' => $options['name'],], ] ); diff --git a/php/Terminus/Helpers/InputHelper.php b/php/Terminus/Helpers/InputHelper.php index 549b8ea8b..9ae94223d 100755 --- a/php/Terminus/Helpers/InputHelper.php +++ b/php/Terminus/Helpers/InputHelper.php @@ -388,6 +388,9 @@ public function orgId(array $arg_options = []) { $arguments = $options['args']; $key = $options['key']; + if (isset($arguments[$key]) && $this->isValidUuid($arguments[$key])) { + return $arguments[$key]; + } $org_list = $this->orgList($options); if (isset($arguments[$key])) { if ($id = array_search($arguments[$key], $org_list)) { @@ -849,7 +852,7 @@ public function string(array $arg_options = []) { * array args Args to parse value from * string key Index to search for in args * bool exit If true, throw error when no value is found - * @return Upstream + * @return string * @throws TerminusException */ public function upstream(array $arg_options = []) { @@ -860,9 +863,12 @@ public function upstream(array $arg_options = []) { ]; $options = array_merge($default_options, $arg_options); - $upstreams = new Upstreams(); if (isset($options['args'][$options['key']])) { - $upstream = $upstreams->getByIdOrName($options['args'][$options['key']]); + if ($this->isValidUuid($options['args'][$options['key']])) { + return $options['args'][$options['key']]; + } + $upstreams = new Upstreams(); + $upstream = $upstreams->getByIdOrName($options['args'][$options['key']]); if ($upstream == null) { throw new TerminusException( 'Could not find upstream: {upstream}', @@ -877,7 +883,8 @@ public function upstream(array $arg_options = []) { ) ); } - return $upstream; + $upstream_id = $upstream->get('id'); + return $upstream_id; } /** @@ -948,6 +955,20 @@ function($workflow) use ($workflow_id) { } } + /** + * Ascertains whether the given string is properly formatted to be a UUID. + * + * @param string $uuid The string to evaluate + * @return bool + */ + private function isValidUuid($uuid) { + $regex = '~^\{?[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-'; + $regex .= '[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}\}?$~'; + preg_match($regex, $uuid, $matches); + $is_uuid = count($matches) === 1; + return $is_uuid; + } + /** * Returns an array listing organizaitions applicable to user * diff --git a/php/Terminus/Models/Collections/Sites.php b/php/Terminus/Models/Collections/Sites.php index d8c100357..1e92e8972 100644 --- a/php/Terminus/Models/Collections/Sites.php +++ b/php/Terminus/Models/Collections/Sites.php @@ -75,39 +75,34 @@ public function addSite($options = array()) { * @return Site The newly created site object */ public function addSiteToCache($site_id, $org_id = null) { - if (count($this->models) == 0) { - $this->rebuildCache(); - $site = $this->get($site_id); + $site = new Site( + (object)['id' => $site_id,], + ['collection' => $this,] + ); + $site->fetch(); + $cache_membership = $site->info(); + + if (!is_null($org_id)) { + $org = new Organization(null, ['id' => $org_id,]); + $cache_membership['membership'] = [ + 'id' => $org_id, + 'name' => $org->profile->name, + 'type' => 'organization', + ]; } else { - $site = new Site( - $this->objectify(array('id' => $site_id)), - array('collection' => $this) - ); - $site->fetch(); - $cache_membership = $site->info(); - - if (!is_null($org_id)) { - $org = new Organization(null, array('id' => $org_id)); - $cache_membership['membership'] = array( - 'id' => $org_id, - 'name' => $org->profile->name, - 'type' => 'organization' - ); - } else { - $user_id = Session::getValue('user_uuid'); - $cache_membership['membership'] = array( - 'id' => $user_id, - 'name' => 'Team', - 'type' => 'team' - ); - } - $this->sites_cache->add($cache_membership); + $user_id = Session::getValue('user_uuid'); + $cache_membership['membership'] = [ + 'id' => $user_id, + 'name' => 'Team', + 'type' => 'team', + ]; } + $this->sites_cache->add($cache_membership); return $site; } /** - * Removes site with given site ID from cache + * Removes site with given site ID from cache * * @param string $site_name Name of site to remove from cache * @return void @@ -130,7 +125,7 @@ public function fetch(array $options = array()) { $cache = $this->sites_cache->all(); } foreach ($cache as $name => $model) { - $this->add($this->objectify($model)); + $this->add((object)$model); } } return $this; diff --git a/tests/features/cli.feature b/tests/features/cli.feature index c8c0e248f..2a2c07cfa 100644 --- a/tests/features/cli.feature +++ b/tests/features/cli.feature @@ -53,7 +53,7 @@ Feature: CLI Commands And I run "terminus cli session-dump --format=json" Then I should get: """ - false + [] """ @vcr cli_session-dump diff --git a/tests/unit_tests/caches/test-file-cache.php b/tests/unit_tests/caches/test-file-cache.php index 7ae5e1e22..17d90d812 100644 --- a/tests/unit_tests/caches/test-file-cache.php +++ b/tests/unit_tests/caches/test-file-cache.php @@ -67,7 +67,7 @@ public function testGetData() { //Trying to get data when the file is not there $data = $this->file_cache->getData($this->test_file_name); - $this->assertFalse($data); + $this->assertInternalType('array', $data); } public function testGetRoot() {