Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
new event: ServiceWriterWritePreparedEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
svensp committed Oct 16, 2019
1 parent 20f74b7 commit 3dc1ca4
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php namespace Rancherize\Blueprint\Infrastructure\Service\Events;

use Rancherize\Blueprint\Infrastructure\Service\Service;
use Rancherize\Blueprint\Infrastructure\Service\ServiceYamlDefinition;
use Symfony\Component\EventDispatcher\Event;

/**
* Class ServiceWriterServicePreparedEvent
* @package Rancherize\Blueprint\Infrastructure\Service\Events
*/
class ServiceWriterWritePreparedEvent extends Event {

const NAME = 'service-writer.prepared-to-write';

/**
* @var Service
*/
private $service;

/**
* @var int
*/
private $fileVersion = 2;
/**
* @var ServiceYamlDefinition
*/
private $definition;

/**
*
* /**
* ServiceWriterServicePreparedEvent constructor.
* @param Service $service
* @param ServiceYamlDefinition $definition
*/
public function __construct( Service $service, ServiceYamlDefinition $definition ) {
$this->service = $service;
$this->definition = $definition;
}

/**
* @return Service
*/
public function getService() {
return $this->service;
}

/**
* @return int
*/
public function getFileVersion(): int
{
return $this->fileVersion;
}

/**
* @return ServiceYamlDefinition
*/
public function getDefinition(): ServiceYamlDefinition
{
return $this->definition;
}

}
15 changes: 13 additions & 2 deletions app/Blueprint/Infrastructure/Service/ServiceWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use Rancherize\Blueprint\Infrastructure\Dockerfile\Dockerfile;
use Rancherize\Blueprint\Infrastructure\Dockerfile\DockerfileWriter;
use Rancherize\Blueprint\Infrastructure\Service\Events\ServiceWriterServicePreparedEvent;
use Rancherize\Blueprint\Infrastructure\Service\Events\ServiceWriterWritePreparedEvent;
use Rancherize\Configuration\Exceptions\FileNotFoundException;
use Rancherize\File\FileLoader;
use Rancherize\File\FileWriter;
Expand Down Expand Up @@ -180,13 +181,23 @@ public function write(Service $service, FileWriter $fileWriter) {

$rancherContentPreparedEvent = new ServiceWriterServicePreparedEvent($service, $content, $volumeDefinitions, $rancherContent);
$this->event->dispatch(ServiceWriterServicePreparedEvent::NAME, $rancherContentPreparedEvent);

// might have been changed by the listeners
$rancherContent = $rancherContentPreparedEvent->getRancherContent();
$content = $rancherContentPreparedEvent->getDockerContent();
$volumeDefinitions = $rancherContentPreparedEvent->getVolumeDefinition();

$this->writeYaml($this->path . '/docker-compose.yml', $service, $fileWriter, $content, $volumeDefinitions);
$this->writeYaml($this->path . '/rancher-compose.yml', $service, $fileWriter, $rancherContent);

$definition = ServiceYamlDefinition::make($content, $rancherContent, $volumeDefinitions);
$this->event->dispatch(ServiceWriterWritePreparedEvent::NAME,
new ServiceWriterWritePreparedEvent(
$service,
$definition
)
);

$this->writeYaml($this->path . '/docker-compose.yml', $service, $fileWriter, $definition->dockerComposeEntry, $definition->volumeDefinition);
$this->writeYaml($this->path . '/rancher-compose.yml', $service, $fileWriter, $definition->rancherComposeEntry);
}

/**
Expand Down
39 changes: 39 additions & 0 deletions app/Blueprint/Infrastructure/Service/ServiceYamlDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php namespace Rancherize\Blueprint\Infrastructure\Service;

/**
* Class ServiceYamlDefinition
* @package Rancherize\Blueprint\Infrastructure\Service
*/
class ServiceYamlDefinition
{

/**
* @var array
*/
public $dockerComposeEntry = [];

/**
* @var array
*/
public $rancherComposeEntry = [];

/**
* @var array
*/
public $volumeDefinition = [];

/**
* @param $dockerComposeEntry
* @param $rancherComposeEntry
* @param $volumeDefinition
* @return ServiceYamlDefinition
*/
static function make($dockerComposeEntry, $rancherComposeEntry, $volumeDefinition) {
$definition = new self;
$definition->dockerComposeEntry = $dockerComposeEntry;
$definition->rancherComposeEntry = $rancherComposeEntry;
$definition->volumeDefinition = $volumeDefinition;
return $definition;
}

}

0 comments on commit 3dc1ca4

Please sign in to comment.