diff --git a/src/StackFormation/PreProcessor/Stage/Tree/InjectFilecontent.php b/src/StackFormation/PreProcessor/Stage/Tree/InjectFilecontent.php new file mode 100644 index 0000000..c4a3191 --- /dev/null +++ b/src/StackFormation/PreProcessor/Stage/Tree/InjectFilecontent.php @@ -0,0 +1,107 @@ +parent(1); /* @var RootlineItem $parentRootlineItem */ + $parent = $parentRootlineItem->getValue(); /* @var RecursiveArrayObject $parentData */ + $lines = $this->renderFileContent(trim(end($matches)), $matches[1]); + + $parent->offsetUnset($fnBase64); + $parent[$fnBase64][$fnJoin] = ['', [implode('', $lines)]]; + + return true; + } + + if (preg_match('+Fn::FileContent(|Unpretty|TrimLines|Minify)$+', $path, $matches)) { + + $parentRootlineItem = $rootLineReferences->parent(1); /* @var RootlineItem $parentRootlineItem */ + $parent = $parentRootlineItem->getValue(); /* @var RecursiveArrayObject $parentData */ + $lines = $this->renderFileContent($value, $matches[1]); + + $parent->offsetUnset($matches[0]); + $parent[$fnJoin] = ['', [implode('', $lines)]]; + + return true; + } + + return false; + } + + /** + * @param string $file + * @param string $modus + * @return array + * @throws \Exception + */ + protected function renderFileContent($file, $modus) + { + $file = $this->basePath . '/' . $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; + } +}