diff --git a/README.md b/README.md index 7eee348..c048b7a 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ You need to create a `compile.json` file first (or look at the [example file](ex | generate | an array of files to generate. The index of the array will be passed in the generation process and is available in the \$nr variable | | placeholders | (object) - You can use placeholders which will be replaced with the values in this file. | | output_dir | Where should be the generated files be stored? eg. "../dist". If not specified, the source directory will be used as target directory. | -| zip | (object) settings for the ZIP file generation. Key "filename" is used for the output filename. Key "files" is an array of which files should be stored in the ZIP file. | +| zip | (object) settings for the ZIP file generation. Key "filename" is used for the output filename. Key "files" is an array of which additional files should be stored in the ZIP file. Local image assets will be automatically stored in the zip file| ## Parameters @@ -63,7 +63,7 @@ You want to extend it? project is open for pull requests :) Open Source License. Developed by MilMike // 29.11.2017 (https://www.milmike.com) -Last Update: 01.01.2018 (Happy New Year!) +Last Update: 02.08.2019 ## Further Reading diff --git a/composer.lock b/composer.lock index 78c0b71..5aecef5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,24 +4,24 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9cda4ec9d08ff2f72d723079952342bf", + "content-hash": "ab8c5aae9dc71eef3eeddbc6b1a1eee9", "packages": [ { "name": "league/climate", - "version": "3.4.1", + "version": "3.5.0", "source": { "type": "git", "url": "https://github.com/thephpleague/climate.git", - "reference": "d657a19837c1f79a891381fb128b755aa3386381" + "reference": "0d2fdbf8829f60f6ba6433df68d6f3fe1271b8e6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/climate/zipball/d657a19837c1f79a891381fb128b755aa3386381", - "reference": "d657a19837c1f79a891381fb128b755aa3386381", + "url": "https://api.github.com/repos/thephpleague/climate/zipball/0d2fdbf8829f60f6ba6433df68d6f3fe1271b8e6", + "reference": "0d2fdbf8829f60f6ba6433df68d6f3fe1271b8e6", "shasum": "" }, "require": { - "php": "^5.6|^7.0", + "php": "^7.1", "psr/log": "^1.0", "seld/cli-prompt": "^1.0" }, @@ -65,7 +65,7 @@ "php", "terminal" ], - "time": "2018-04-29T16:43:54+00:00" + "time": "2019-02-10T18:25:19+00:00" }, { "name": "pelago/emogrifier", @@ -238,16 +238,16 @@ }, { "name": "symfony/css-selector", - "version": "v4.2.1", + "version": "v4.3.3", "source": { "type": "git", "url": "https://github.com/symfony/css-selector.git", - "reference": "aa9fa526ba1b2ec087ffdfb32753803d999fcfcd" + "reference": "105c98bb0c5d8635bea056135304bd8edcc42b4d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/css-selector/zipball/aa9fa526ba1b2ec087ffdfb32753803d999fcfcd", - "reference": "aa9fa526ba1b2ec087ffdfb32753803d999fcfcd", + "url": "https://api.github.com/repos/symfony/css-selector/zipball/105c98bb0c5d8635bea056135304bd8edcc42b4d", + "reference": "105c98bb0c5d8635bea056135304bd8edcc42b4d", "shasum": "" }, "require": { @@ -256,7 +256,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "4.2-dev" + "dev-master": "4.3-dev" } }, "autoload": { @@ -272,14 +272,14 @@ "MIT" ], "authors": [ - { - "name": "Jean-François Simon", - "email": "jeanfrancois.simon@sensiolabs.com" - }, { "name": "Fabien Potencier", "email": "fabien@symfony.com" }, + { + "name": "Jean-François Simon", + "email": "jeanfrancois.simon@sensiolabs.com" + }, { "name": "Symfony Community", "homepage": "https://symfony.com/contributors" @@ -287,7 +287,7 @@ ], "description": "Symfony CssSelector Component", "homepage": "https://symfony.com", - "time": "2018-11-11T19:52:12+00:00" + "time": "2019-01-16T21:53:39+00:00" } ], "packages-dev": [], diff --git a/example/dist/compiled_templates.zip b/example/dist/compiled_templates.zip index c27f288..42c8d65 100644 Binary files a/example/dist/compiled_templates.zip and b/example/dist/compiled_templates.zip differ diff --git a/example/dist/img/htmlmailcompiler-logo.png b/example/dist/img/htmlmailcompiler-logo.png index 0e13866..d677369 100644 Binary files a/example/dist/img/htmlmailcompiler-logo.png and b/example/dist/img/htmlmailcompiler-logo.png differ diff --git a/example/dist/readme.txt b/example/dist/readme.txt new file mode 100644 index 0000000..23ed693 --- /dev/null +++ b/example/dist/readme.txt @@ -0,0 +1,10 @@ +This is a sample readme file. +This file will be added to the zip file because it was added in the compile.json file: + + + "zip": { + "filename": "compiled_templates.zip", + "files": [ + "readme.txt" + ] + } \ No newline at end of file diff --git a/example/dist/template_awesome.html b/example/dist/template_awesome.html index d93efe8..87215c6 100644 --- a/example/dist/template_awesome.html +++ b/example/dist/template_awesome.html @@ -1,21 +1,27 @@ - - - -Example - - + + + + + Example + + + + + -

I am a simple html email

+ + +

I am a simple html email

What can the compiler do for you?

+
diff --git a/example/dist/template_cool.html b/example/dist/template_cool.html index db839a9..b3ee269 100644 --- a/example/dist/template_cool.html +++ b/example/dist/template_cool.html @@ -1,21 +1,27 @@ - - - -Example - - + + + + + Example + + + + + -

I am a simple html email

+ + +

I am a simple html email

What can the compiler do for you?

+
diff --git a/example/dist/template_yeah.html b/example/dist/template_yeah.html index a038832..2feec3c 100644 --- a/example/dist/template_yeah.html +++ b/example/dist/template_yeah.html @@ -1,21 +1,27 @@ - - - -Example - - + + + + + Example + + + + + -

I am a simple html email

+ + +

I am a simple html email

What can the compiler do for you?

    -
  • +
  • You can work with CSS selectors and create your styles in a CSS file. The compiler will generate a html file with inline CSS.
  • @@ -32,6 +38,7 @@
+
diff --git a/example/src/compile.json b/example/src/compile.json index 80eefe6..ac68e5f 100644 --- a/example/src/compile.json +++ b/example/src/compile.json @@ -16,10 +16,7 @@ "zip": { "filename": "compiled_templates.zip", "files": [ - "template_cool.html", - "template_awesome.html", - "template_yeah.html", - "img/htmlmailcompiler-logo.png" + "readme.txt" ] } } \ No newline at end of file diff --git a/example/src/img/htmlmailcompiler-logo.png b/example/src/img/htmlmailcompiler-logo.png index 0e13866..d677369 100644 Binary files a/example/src/img/htmlmailcompiler-logo.png and b/example/src/img/htmlmailcompiler-logo.png differ diff --git a/example/src/readme.txt b/example/src/readme.txt new file mode 100644 index 0000000..23ed693 --- /dev/null +++ b/example/src/readme.txt @@ -0,0 +1,10 @@ +This is a sample readme file. +This file will be added to the zip file because it was added in the compile.json file: + + + "zip": { + "filename": "compiled_templates.zip", + "files": [ + "readme.txt" + ] + } \ No newline at end of file diff --git a/example/src/template.php b/example/src/template.php index 4d845d5..1711148 100644 --- a/example/src/template.php +++ b/example/src/template.php @@ -15,6 +15,7 @@ +

I am a simple html email

diff --git a/src/HtmlCompiler.php b/src/HtmlCompiler.php index d38c990..9df06af 100644 --- a/src/HtmlCompiler.php +++ b/src/HtmlCompiler.php @@ -15,6 +15,25 @@ class HtmlCompiler //For output we use climate lib: static $climate = null; + //extracted files from rendered source: + public static $additionalFiles = []; + + //selected path + static $path = null; + + //Dir for generated files + static $outputDir = null; + + //Set the working dir path: + private static function setPath($path = null){ + if($path === null){ + //restore path: + chdir(self::$path); + }else{ + chdir($path); + } + } + public static function run($path = '') { @@ -30,7 +49,8 @@ public static function run($path = '') } //Switch to the path - chdir($path); + self::$path = $path; + self::setPath(); //does the settings file exist? $settingsFilePath = self::$SettingsFile; @@ -46,8 +66,10 @@ public static function run($path = '') $css_file = $settings['css_file']; if (isset($settings['output_dir'])) { - $outputDir = $path . "/" . $settings['output_dir']; + $outputDir = $settings['output_dir']; + //die($outputDir); if (!file_exists($outputDir)) { + $d = realpath($outputDir); $created = mkdir($outputDir); if (!$created) { self::writeLog("Could not create output dir", "error"); @@ -58,9 +80,11 @@ public static function run($path = '') $outputDir = $path; } - if ($outputDir != $path) { + self::$outputDir = $outputDir; + + if (self::$outputDir != $path) { //copy static files to dist dir: - self::copyFilesToDist($settings['zip']['files'], $path, $outputDir); + self::copyFilesToDist($settings['zip']['files']); } //include own helper php file which will be always included and is available in your template . php file @@ -138,30 +162,45 @@ public static function minify($html) * Copy all files from Source to Dist * * @param $files - array with file names - * @param $sourceDir - copy from where? * @param $outDir - copy to where? */ - private static function copyFilesToDist($files, $sourceDir, $outDir) + private static function copyFilesToDist($files, $outDir = null) { + if($outDir === null){ + $outDir = self::$outputDir; + } + self::writeLog("Copying Source Files to $outDir"); foreach ($files as $file) { + + $outFile = $outDir . "/" . $file; + $sourceFile = $file; + if (strpos($file, '/') !== false) { + //copy from a subdirectory + $ex = explode("/", $file); $dir = $ex[0]; $newPath = $outDir . "/" . $dir; - $outFile = $outDir . "/" . $file; - $sourceFile = $sourceDir . "/" . $file; + if (!file_exists($newPath)) { mkdir($newPath); } + if (file_exists($sourceFile)) { copy($sourceFile, $outFile); } + }else{ + //copy from root of source path: + + if (file_exists($sourceFile)) { + copy($sourceFile, $outFile); + } } } @@ -225,6 +264,21 @@ public static function compile($params) $css_file_content .= file_get_contents($css_file) . "\n"; } + + //Extract local images to be put in zip file + $doc = new DOMDocument(); + @$doc->loadHTML($html_file_content); + $tags = $doc->getElementsByTagName('img'); + $images = []; + foreach ($tags as $tag) { + $img = $tag->getAttribute('src'); + //if(substr($img, start)) + if(!preg_match('/^http[s]?:/', $img)){ + self::$additionalFiles[] = $img; + } + } + + $emogrifier->setHtml($html_file_content); $emogrifier->setCss($css_file_content); @@ -304,6 +358,17 @@ private static function zip($settings, $path) $path = str_replace('\\', '/', $path); + if(!empty(self::$additionalFiles)){ + self::copyFilesToDist(self::$additionalFiles); + } + + + //Merge all files from json, extracted, generated + $files = []; + $settings['files'] = array_merge($settings['files'], self::$additionalFiles); + $settings['files'] = array_merge($settings['files'], self::$settings['generate']); + $settings['files'] = array_unique($settings['files']); + //generate file list: $files = array(); foreach ($settings['files'] as $file) { diff --git a/src/compile.php b/src/compile.php index 95e4d2e..73bca3c 100644 --- a/src/compile.php +++ b/src/compile.php @@ -10,7 +10,7 @@ error_reporting(E_ALL); ini_set('display_errors', 1); -$app_version = "1.0.2"; +$app_version = "1.1.0"; $autoload_file = __DIR__ . "/../vendor/autoload.php"; $autoload_file_global = __DIR__ . "/../../../autoload.php";