Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #20 from oanhnn/feature/add-configure-recipe
Browse files Browse the repository at this point in the history
Feature/add configure recipe
  • Loading branch information
gordalina committed Jun 29, 2015
2 parents a0f3511 + 1e176f0 commit f9c87d8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/configure.md
Original file line number Diff line number Diff line change
@@ -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 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

- `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.
74 changes: 74 additions & 0 deletions recipes/configure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* Make shared_dirs and configure files from templates
*/
task('deploy:configure', function () {

/**
* Paser value for template compiler
*
* @param array $matches
* @return string
*/
$paser = function($matches) {
if (isset($matches[1])) {
$value = env()->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("<info>✔</info> %s", $file->getRelativePathname()));
} else {
writeln(sprintf("<fg=red>✘</fg=red> %s", $file->getRelativePathname()));
}
}
})->desc('Make configure files for your stage');

0 comments on commit f9c87d8

Please sign in to comment.