This repository has been archived by the owner on Apr 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from oanhnn/feature/add-configure-recipe
Feature/add configure recipe
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 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
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. |
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,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'); |