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.
- Loading branch information
Julian Kleinhans
committed
Nov 23, 2016
1 parent
e450c27
commit 47ed3f2
Showing
8 changed files
with
150 additions
and
37 deletions.
There are no files selected for viewing
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
86 changes: 86 additions & 0 deletions
86
src/StackFormation/PreProcessor/Stage/Tree/InjectFilecontent.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,86 @@ | ||
<?php | ||
|
||
namespace StackFormation\PreProcessor\Stage\Tree; | ||
|
||
use StackFormation\PreProcessor\Stage\AbstractTreePreProcessorStage; | ||
use Symfony\Component\Filesystem\Exception\FileNotFoundException; | ||
|
||
class InjectFilecontent extends AbstractTreePreProcessorStage | ||
{ | ||
const MAX_JS_FILE_INCLUDE_SIZE = 4096; | ||
|
||
/** | ||
* @param array $tree | ||
*/ | ||
public function invoke(array &$tree) | ||
{ | ||
// Search in array key | ||
$this->treePreProcessor->searchTreeByExpression('/^Fn::FileContent(|Unpretty|TrimLines|Minify)$/', $tree, function (array &$tree, $key, $value, $matches) { | ||
unset($tree[$key]); | ||
$lines = $this->renderFileContent($value, $matches[1]); | ||
$tree['Fn::Join'] = [ '', [implode('', $lines)]]; | ||
}, true); | ||
|
||
// Search in values (content) | ||
$this->treePreProcessor->searchTreeByExpression('/Fn::FileContent(|Unpretty|TrimLines|Minify)(:)(.*)/', $tree, function (array &$tree, $key, $value, $matches) { | ||
unset($tree[$key]); | ||
$lines = $this->renderFileContent(trim(end($matches)), $matches[1]); | ||
$tree[$key] = ['Fn::Join' => [ '', [implode('', $lines)]]]; | ||
}); | ||
} | ||
|
||
/** | ||
* @param string $file | ||
* @param string $modus | ||
* @return array | ||
* @throws \Exception | ||
*/ | ||
protected function renderFileContent($file, $modus) | ||
{ | ||
$file = $this->template->getBasePath() . '/' . $file; | ||
if (!is_file($file)) { | ||
throw new FileNotFoundException("File '$file' not found"); | ||
} | ||
|
||
$ext = pathinfo($file, PATHINFO_EXTENSION); | ||
if ($modus == 'Minify' && $ext != 'js') { | ||
throw new \Exception('Fn::FileContentMinify is only supported for *.js files. (File: ' . $file . ')'); | ||
} | ||
|
||
$fileContent = file_get_contents($file); | ||
|
||
# TODO in own stage ? | ||
#$fileContent = $this->injectInclude($fileContent, dirname(realpath($file))); | ||
|
||
if ($ext === 'js') { | ||
if ($modus == 'Minify') { | ||
$fileContent = \JShrink\Minifier::minify($fileContent, ['flaggedComments' => false]); | ||
} | ||
|
||
$size = strlen($fileContent); | ||
if ($size > self::MAX_JS_FILE_INCLUDE_SIZE) { | ||
// this is assuming you are uploading an inline JS file to AWS Lambda | ||
throw new \Exception(sprintf("JS file is larger than %s bytes (actual size: %s bytes)", self::MAX_JS_FILE_INCLUDE_SIZE, $size)); | ||
} | ||
} | ||
|
||
// TODO: this isn't optimal. Why are we processing this here in between? | ||
#$fileContent = $this->base64encodedJson($fileContent); | ||
|
||
$lines = explode("\n", $fileContent); | ||
foreach ($lines as $lineKey => &$line) { | ||
if ($modus == 'TrimLines') { | ||
$line = trim($line); | ||
if (empty($line)) { | ||
unset($lines[$lineKey]); | ||
} | ||
} | ||
$line .= "\n"; | ||
} | ||
|
||
#$whitespace = trim($matches[1], "\n"); | ||
#$result = str_replace("\n", "\n" . $whitespace, $result); | ||
|
||
return $lines; | ||
} | ||
} |
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
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