Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Commit

Permalink
#45 Add ExpandCslPort stage for tree preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Kleinhans committed Dec 13, 2016
1 parent 1a98961 commit 5b9b0d4
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/StackFormation/PreProcessor/Stage/Tree/ExpandCslPort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace StackFormation\PreProcessor\Stage\Tree;

use StackFormation\PreProcessor\Stage\AbstractTreePreProcessorStage;
use StackFormation\PreProcessor\Rootline;

class ExpandCslPort extends AbstractTreePreProcessorStage
{
/**
* This one converts "Port" into "FromPort" and "ToPort" and will resolve comma-separated lists
*
* @param string $path
* @param string $value
* @param Rootline $rootLineReferences
* @return bool
*/
function invoke($path, $value, Rootline $rootLineReferences) {
if (!preg_match('+/SecurityGroupIngress/.*/Port$+', $path)) {
return false; // indicate that nothing has been touched
}

$parentRootlineItem = $rootLineReferences->parent(1); /* @var $parentRootlineItem RootlineItem */
$parent = $parentRootlineItem->getValue(); /* @var $parent RecursiveArrayObject */

$grandParentRootlineItem = $rootLineReferences->parent(2); /* @var $grandParentRootlineItem RootlineItem */
$grandParent = $grandParentRootlineItem->getValue(); /* @var $grandParent RecursiveArrayObject */

// remove original item
$grandParent->offsetUnset($parentRootlineItem->getKey());

// add a new line for every csl item
foreach (explode(',', $value) as $port) {
$newItem = clone $parent;
$newItem->FromPort = $port;
$newItem->ToPort = $port;
$newItem->offsetUnset('Port');
$grandParent->append($newItem);
}

return true; // indicate that something has changed
}
}

0 comments on commit 5b9b0d4

Please sign in to comment.