Skip to content

Commit

Permalink
Merge ee2410f into 8cf68ee
Browse files Browse the repository at this point in the history
  • Loading branch information
reneroboter committed Feb 22, 2019
2 parents 8cf68ee + ee2410f commit d7ed9ea
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,9 @@
## master
[v6.4.3...master](https://github.com/deployphp/deployer/compare/v6.4.3...master)

### Added
- Add support for + in --roles option #1610

### Changed
- Add lock and unlock task to flow_framework receipe

Expand Down
7 changes: 5 additions & 2 deletions src/Host/HostSelector.php
Expand Up @@ -8,6 +8,7 @@
namespace Deployer\Host;

use Deployer\Exception\Exception;
use function Deployer\Support\array_has;

class HostSelector
{
Expand Down Expand Up @@ -101,8 +102,10 @@ public function getByRoles($roles)

$hosts = [];
foreach ($this->hosts as $host) {
foreach ($host->get('roles', []) as $role) {
if (in_array($role, $roles, true)) {
$rolesByHost = $host->get('roles', []);

foreach ($roles as $role) {
if (array_has(\explode('+', $role), $rolesByHost)) {
$hosts[$host->getHostname()] = $host;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Support/helpers.php
Expand Up @@ -93,3 +93,15 @@ function ($key, $value) {
$array
));
}

/**
* Take array of needles and check if it's in haystack.
*
* @param $needles
* @param array $haystack
* @return bool
*/
function array_has(array $needles, array $haystack)
{
return empty(array_diff($needles, $haystack));
}
25 changes: 25 additions & 0 deletions test/src/Host/HostSelectorTest.php
Expand Up @@ -145,4 +145,29 @@ public function testReturnHostsArrayUsingGetByRoles()

$this->assertNotEmpty($hostSelector->getByRoles($roles));
}

public function testReturnHostsByMultipleRolesUsingGetByRoles()
{
$roles = 'role1+role2';
$roles2 = 'role1+role2,role3';
$roles3 = 'role2,role1,role3+role2';

$host = new Host('server');
$host->roles('role1', 'role2');
$host2 = new Host('server2');
$host2->roles('role1');
$host3 = new Host('server3');
$host3->roles('role2', 'role3');

$hostCollection = new HostCollection();
$hostCollection->set('server', $host);
$hostCollection->set('server2', $host2);
$hostCollection->set('server3', $host3);

$hostSelector = new HostSelector($hostCollection);

$this->assertEquals(['server' => $host], $hostSelector->getByRoles($roles));
$this->assertEquals(['server' => $host, 'server3' => $host3], $hostSelector->getByRoles($roles2));
$this->assertEquals(['server' => $host, 'server2' => $host2, 'server3' => $host3], $hostSelector->getByRoles($roles3));
}
}

0 comments on commit d7ed9ea

Please sign in to comment.