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

Streamlined the site-creation process #1013

Merged
merged 1 commit into from
Mar 30, 2016
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions php/Terminus/Caches/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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']);
}
Expand Down
17 changes: 10 additions & 7 deletions php/Terminus/Caches/SitesCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,],]
);
}

Expand Down Expand Up @@ -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,
Expand All @@ -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];
Expand Down
16 changes: 7 additions & 9 deletions php/Terminus/Commands/SitesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'],],
]
);

Expand Down
29 changes: 25 additions & 4 deletions php/Terminus/Helpers/InputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 = []) {
Expand All @@ -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}',
Expand All @@ -877,7 +883,8 @@ public function upstream(array $arg_options = []) {
)
);
}
return $upstream;
$upstream_id = $upstream->get('id');
return $upstream_id;
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
51 changes: 23 additions & 28 deletions php/Terminus/Models/Collections/Sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/features/cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/caches/test-file-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down