Skip to content

Commit

Permalink
VhostTemplate - Perform a structured search to determine the template (
Browse files Browse the repository at this point in the history
…#21)

For example, suppose we've configured the `httpd` as `nginx`:
 * If the env has variable `NGINX_VHOST_TPL`, use that.
 * If the web-root or any parent has `.amp/nginx-vhost.php`, use that.
 * Otherwise, use the default template specified in `amp`.
  • Loading branch information
totten committed Aug 8, 2017
1 parent f1173c3 commit 97070b5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/defaults/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ services:
httpd.apache:
class: Amp\Httpd\VhostTemplate
calls:
- [setConfigKey, ['apache']]
- [setTemplateEngine, ['@template.engine']]
- [setDefaultTemplate, ['%apache_tpl%']]
- [setDir, ['%apache_dir%']]
Expand All @@ -148,6 +149,7 @@ services:
httpd.apache24:
class: Amp\Httpd\VhostTemplate
calls:
- [setConfigKey, ['apache24']]
- [setTemplateEngine, ['@template.engine']]
- [setDefaultTemplate, ['%apache24_tpl%']]
- [setDir, ['%apache_dir%']]
Expand All @@ -160,6 +162,7 @@ services:
httpd.nginx:
class: Amp\Httpd\VhostTemplate
calls:
- [setConfigKey, ['nginx']]
- [setTemplateEngine, ['@template.engine']]
- [setDefaultTemplate, ['%nginx_tpl%']]
- [setDir, ['%nginx_dir%']]
Expand Down
55 changes: 54 additions & 1 deletion src/Amp/Httpd/VhostTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class VhostTemplate implements HttpdInterface {
*/
private $templateEngine;

/**
* @var string, a symbolic name for the template to lookup
*
* Ex: `nginx`, `apache24`.
*/
private $configKey;

/**
* @var string
* Maybe empty, 'NONE', or a command.
Expand Down Expand Up @@ -74,7 +81,7 @@ public function createVhost($root, $url, $visibility) {
$parameters['include_vhost_file'] = '';
$parameters['log_dir'] = $this->getLogDir();
$parameters['visibility'] = $visibility;
$content = $this->getTemplateEngine()->render($this->getDefaultTemplate(), $parameters);
$content = $this->getTemplateEngine()->render($this->pickTemplate($root, $url), $parameters);
$this->fs->dumpFile($this->createFilePath($root, $url), $content);

$this->setupLogDir();
Expand All @@ -93,6 +100,38 @@ public function dropVhost($root, $url) {
$this->fs->remove($this->createFilePath($root, $url));
}

/**
* Pick the most salient template for this build.
*
* For example, suppose we're searching on key `nginx`:
* - If the env has variable NGINX_VHOST_TPL, use that.
* - If the web-root or any parent has ".amp/nginx-vhost.php", use that.
* - Otherwise, use the defaultTemplate.
*
* @param string $root local path to document root
* @param string $url preferred public URL
* @return string
*/
public function pickTemplate($root, $url) {
$configKey = $this->getConfigKey();
$envVar = strtoupper($configKey) . '_VHOST_TPL';
if (getenv($envVar)) {
return getenv($envVar);
}

do {
$dir = !isset($dir) ? $root : dirname($dir);
$dotFile = $dir . DIRECTORY_SEPARATOR . '.amp'
. DIRECTORY_SEPARATOR . $configKey . '-vhost.php';
echo "check [$dotFile]\n";
if (file_exists($dotFile)) {
return $dotFile;
}
} while ($dir && $dir !== dirname($dir));

return $this->getDefaultTemplate();
}

public function restart() {
if ($this->restartCommand && $this->restartCommand !== 'NONE') {
passthru($this->restartCommand, $result);
Expand All @@ -119,6 +158,20 @@ public function createFilePath($root, $url) {
return $this->getDir() . DIRECTORY_SEPARATOR . $parameters['host'] . '_' . $parameters['port'] . '.conf';
}

/**
* @return string
*/
public function getConfigKey() {
return $this->configKey;
}

/**
* @param string $configKey
*/
public function setConfigKey($configKey) {
$this->configKey = $configKey;
}

/**
* @param string $dir
*/
Expand Down

0 comments on commit 97070b5

Please sign in to comment.