Move docker services to a reusable trait.

This commit is contained in:
Travis Tomka
2017-03-30 19:55:06 -06:00
parent e3ace8a3ea
commit 4697d956fa
2 changed files with 56 additions and 44 deletions

53
src/DockerServicesTrait.php Executable file
View File

@@ -0,0 +1,53 @@
<?php
namespace Droath\RoboDockerCompose;
/**
* Define docker services trait.
*/
trait DockerServicesTrait
{
/**
* Docker compose services.
*
* @var array
*/
protected $services = [];
/**
* Add docker composer service.
*
* @param string $service
* The docker services.
*/
public function setService($service)
{
$this->services[] = $service;
return $this;
}
/**
* Add docker composer services.
*
* @param array $services
* An array of services.
*/
public function setServices(array $services)
{
foreach ($services as $service) {
$this->setService($service);
}
return $this;
}
/**
* {@inheritdoc}
*/
protected function getCommand()
{
// Append the services to the end of the command.
return parent::getCommand() . ' ' . implode(' ', $this->services);
}
}

47
src/Task/Up.php Normal file → Executable file
View File

@@ -2,6 +2,7 @@
namespace Droath\RoboDockerCompose\Task; namespace Droath\RoboDockerCompose\Task;
use Droath\RoboDockerCompose\DockerServicesTrait;
use Robo\Exception\TaskException; use Robo\Exception\TaskException;
/** /**
@@ -9,18 +10,13 @@ use Robo\Exception\TaskException;
*/ */
class Up extends Base class Up extends Base
{ {
use DockerServicesTrait;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $action = 'up'; protected $action = 'up';
/**
* Docker compose services.
*
* @var array
*/
protected $services = [];
/** /**
* Command detached mode. * Command detached mode.
* *
@@ -28,34 +24,6 @@ class Up extends Base
*/ */
protected $detachedMode = false; protected $detachedMode = false;
/**
* Add docker composer service.
*
* @param string $service
* The docker services.
*/
public function setService($service)
{
$this->services[] = $service;
return $this;
}
/**
* Add docker composer services.
*
* @param array $services
* An array of services.
*/
public function setServices(array $services)
{
foreach ($services as $service) {
$this->setService($service);
}
return $this;
}
/** /**
* Run containers in the background. * Run containers in the background.
*/ */
@@ -147,15 +115,6 @@ class Up extends Base
return $this; return $this;
} }
/**
* {@inheritdoc}
*/
protected function getCommand()
{
// Append the services to the end of the command.
return parent::getCommand() . ' ' . implode(' ', $this->services);
}
/** /**
* Set command detached mode. * Set command detached mode.
*/ */