From d46280e710c4618949aaba26cb574bd8ff88e081 Mon Sep 17 00:00:00 2001 From: oanhnn Date: Mon, 29 Jun 2015 09:42:50 +0700 Subject: [PATCH 1/2] add configure recipe for create configure files from template files --- docs/configure.md | 28 ++++++++++++++++ recipes/configure.php | 74 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 docs/configure.md create mode 100644 recipes/configure.php diff --git a/docs/configure.md b/docs/configure.md new file mode 100644 index 0000000..61953b0 --- /dev/null +++ b/docs/configure.md @@ -0,0 +1,28 @@ +# Configure recipe + +### Installing + +```php +// deploy.php + +require 'vendor/deployphp/recipes/recipes/configure.php'; +``` + +### Configuration options + +Make `shared` directory and put configure template files to it. +Template file extension is `.tpl` and it is removed on filename of compiled filed. +> Note: All configure files will be created with the same structure with template files. +> Eg: +> `shared/config/app.php.tpl` -> `shared/config/app.php` +> `shared/server/apache.conf.tpl` -> `shared/server/apache.conf` + +### Tasks + +- `deploy:configure` compile configure files and upload to servers + +### Suggested Usage + +Since you should only once time create configure files before deployment, +the `deploy:configure` task should be executed right only once time at the first. +If environment variable is changed, you can use this task to re-create configure files. diff --git a/recipes/configure.php b/recipes/configure.php new file mode 100644 index 0000000..038d5bc --- /dev/null +++ b/recipes/configure.php @@ -0,0 +1,74 @@ +get($matches[1]); + if (is_null($value) || is_bool($value) || is_array($value)) { + $value = var_export($value, true); + } + } else { + $value = $matches[0]; + } + + return $value; + }; + + /** + * Template compiler + * + * @param string $contents + * @return string + */ + $compiler = function ($contents) use ($paser) { + $contents = preg_replace_callback('/\{\{\s*([\w\.]+)\s*\}\}/', $paser, $contents); + + return $contents; + }; + + $finder = new \Symfony\Component\Finder\Finder(); + $iterator = $finder + ->files() + ->name('*.tpl') + ->in(__DIR__ . '/shared'); + + $tmpDir = sys_get_temp_dir(); + + /* @var $file \Symfony\Component\Finder\SplFileInfo */ + foreach ($iterator as $file) { + $success = false; + // Make tmp file + $tmpFile = tempnam($tmpDir, 'tmp'); + if (!empty($tmpFile)) { + try { + $contents = $compiler($file->getContents()); + $target = preg_replace('/\.tpl$/', '', $file->getRelativePathname()); + // Put contents and upload tmp file to server + if (file_put_contents($tmpFile, $contents) > 0) { + run('mkdir -p {{deploy_path}}/shared/' . dirname($target)); + upload($tmpFile, 'shared/' . $target); + $success = true; + } + } catch (\Exception $e) { + $success = false; + } + // Delete tmp file + unlink($tmpFile); + } + if ($success) { + writeln(sprintf(" %s", $file->getRelativePathname())); + } else { + writeln(sprintf(" %s", $file->getRelativePathname())); + } + } +})->desc('Make configure files for your stage'); From 1e176f0ee8bc1c18d99452e6053caca94796e949 Mon Sep 17 00:00:00 2001 From: Oanh Nguyen Ngoc Date: Mon, 29 Jun 2015 09:46:10 +0700 Subject: [PATCH 2/2] Update configure.md --- docs/configure.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/configure.md b/docs/configure.md index 61953b0..3e19cd5 100644 --- a/docs/configure.md +++ b/docs/configure.md @@ -10,12 +10,12 @@ require 'vendor/deployphp/recipes/recipes/configure.php'; ### Configuration options -Make `shared` directory and put configure template files to it. -Template file extension is `.tpl` and it is removed on filename of compiled filed. -> Note: All configure files will be created with the same structure with template files. -> Eg: -> `shared/config/app.php.tpl` -> `shared/config/app.php` -> `shared/server/apache.conf.tpl` -> `shared/server/apache.conf` +Make `shared` directory and put configure template files to it. +Template file extension is `.tpl` and it is removed on filename of compiled files. +> Note: All configure files will be created with the same structure with template files. +> Eg: +> `shared/config/app.php.tpl` -> `shared/config/app.php` +> `shared/server/apache.conf.tpl` -> `shared/server/apache.conf` ### Tasks