This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#45 Implement YAML support and refactor PreProcessor
- Loading branch information
Julian Kleinhans
committed
Nov 23, 2016
1 parent
17c8313
commit b7e349a
Showing
10 changed files
with
288 additions
and
30 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/StackFormation/PreProcessor/Stage/AbstractStringPreProcessorStage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace StackFormation\PreProcessor\Stage; | ||
|
||
abstract class AbstractStringPreProcessorStage | ||
{ | ||
protected $stringPreProcessor; // reference to parent | ||
|
||
/** | ||
* @param \StackFormation\PreProcessor\StringPreProcessor $stringPreProcessor | ||
*/ | ||
public function __construct(\StackFormation\PreProcessor\StringPreProcessor $stringPreProcessor) { | ||
$this->stringPreProcessor = $stringPreProcessor; | ||
} | ||
|
||
/** | ||
* @param string $content | ||
* @throws \StackFormation\Exception\StringPreProcessorException | ||
*/ | ||
public function __invoke($content) | ||
{ | ||
try { | ||
return $this->invoke($content); | ||
} catch (\Exception $e) { | ||
throw new \StackFormation\Exception\StringPreProcessorException($content, $e); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $content | ||
*/ | ||
abstract function invoke($content); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/StackFormation/PreProcessor/Stage/AbstractTreePreProcessorStage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace StackFormation\PreProcessor\Stage; | ||
|
||
use StackFormation\Template; | ||
|
||
abstract class AbstractTreePreProcessorStage | ||
{ | ||
protected $treePreProcessor; // reference to parent | ||
|
||
/** | ||
* @param \StackFormation\PreProcessor\TreePreProcessor $treePreProcessor | ||
*/ | ||
public function __construct(\StackFormation\PreProcessor\TreePreProcessor $treePreProcessor) { | ||
$this->treePreProcessor = $treePreProcessor; | ||
} | ||
|
||
/** | ||
* @param Template $template | ||
* @throws \StackFormation\Exception\TreePreProcessorException | ||
*/ | ||
public function __invoke(Template $template) | ||
{ | ||
try { | ||
return $this->invoke($template); | ||
} catch (\Exception $e) { | ||
throw new \StackFormation\Exception\TreePreProcessorException($template, $e); | ||
} | ||
} | ||
|
||
/** | ||
* @param Template $template | ||
*/ | ||
abstract function invoke(Template $template); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/StackFormation/PreProcessor/Stage/String/StripComments.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace StackFormation\PreProcessor\Stage\String; | ||
|
||
use StackFormation\PreProcessor\Stage\AbstractStringPreProcessorStage; | ||
|
||
class StripComments extends AbstractStringPreProcessorStage { | ||
|
||
/** | ||
* @param string $content | ||
* @return string | ||
*/ | ||
public function invoke($content) | ||
{ | ||
// there's a problem with '"http://example.com"' being converted to '"http:' | ||
// $content = preg_replace('~//[^\r\n]*|/\*.*?\*/~s', '', $content); | ||
|
||
// there's a problem with "arn:aws:s3:::my-bucket/*" | ||
// $content = preg_replace('~/\*.*?\*/~s', '', $content); | ||
|
||
// quick workaround: don't allow quotes | ||
return preg_replace('~/\*[^"]*?\*/~s', '', $content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace StackFormation\PreProcessor\Stage\Tree; | ||
|
||
use StackFormation\PreProcessor\Stage\AbstractTreePreProcessorStage; | ||
use StackFormation\Template; | ||
|
||
class ExpandPort extends AbstractTreePreProcessorStage | ||
{ | ||
/** | ||
* @param Template $template | ||
*/ | ||
public function invoke(Template $template) | ||
{ | ||
$tree = $template->getTree(); | ||
$this->treePreProcessor->searchTreeByExpression('/^Port$/', $tree, function (&$tree, $key, $value) { | ||
unset($tree[$key]); | ||
$tree['FromPort'] = $value; | ||
$tree['ToPort'] = $value; | ||
}, 'key'); | ||
$template->setTree($tree); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace StackFormation\PreProcessor; | ||
|
||
use StackFormation\Helper\Pipeline; | ||
|
||
class StringPreProcessor { | ||
|
||
/** | ||
* @param string $content | ||
* @return string | ||
*/ | ||
public function process($content) | ||
{ | ||
$stageClasses = [ | ||
'\StackFormation\PreProcessor\Stage\String\StripComments', | ||
]; | ||
|
||
$pipeline = new Pipeline(); | ||
foreach ($stageClasses as $stageClass) { | ||
$pipeline->addStage(new $stageClass($this)); | ||
} | ||
|
||
return $pipeline->process($content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace StackFormation\PreProcessor; | ||
|
||
use StackFormation\Template; | ||
use StackFormation\Helper\Pipeline; | ||
|
||
class TreePreProcessor { | ||
|
||
/** | ||
* @param Template $template | ||
*/ | ||
public function process(Template $template) | ||
{ | ||
$stageClasses = [ | ||
'\StackFormation\PreProcessor\Stage\Tree\ExpandPort', | ||
|
||
# TODO, check also if we still need that | ||
#'\StackFormation\PreProcessor\Stage\Tree\ParseRefInDoubleQuotedStrings', | ||
#'\StackFormation\PreProcessor\Stage\Tree\InjectFilecontent', | ||
#'\StackFormation\PreProcessor\Stage\Tree\Base64encodedJson', | ||
#'\StackFormation\PreProcessor\Stage\Tree\Split', | ||
#'\StackFormation\PreProcessor\Stage\Tree\ReplaceFnGetAttr', | ||
#'\StackFormation\PreProcessor\Stage\Tree\ReplaceRef', | ||
#'\StackFormation\PreProcessor\Stage\Tree\ReplaceMarkers', | ||
]; | ||
|
||
$pipeline = new Pipeline(); | ||
foreach ($stageClasses as $stageClass) { | ||
$pipeline->addStage(new $stageClass($this)); | ||
} | ||
|
||
$pipeline->process($template); | ||
} | ||
|
||
/** | ||
* @param string $expression | ||
* @param array $tree | ||
* @param callable $callback | ||
* @param string $mode | ||
*/ | ||
public function searchTreeByExpression($expression, array &$tree, callable $callback, $mode = '') | ||
{ | ||
foreach ($tree as $key => &$leaf) { | ||
if (is_array($leaf)) { | ||
$this->searchTreeByExpression($expression, $leaf, $callback, $mode); | ||
continue; | ||
} | ||
|
||
$subject = ($mode == 'key' ? $key : $leaf); | ||
if (preg_match($expression, $subject)) { | ||
$callback($tree, $key, $leaf); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.