Skip to content

Commit

Permalink
Fix SetEnvParametersTask throwing an exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
bestit-el-bardan committed Feb 28, 2018
1 parent 5392755 commit 7844e51
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
47 changes: 33 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,44 @@ magephp:
hosts:
- production_server1
pre-deploy:
- misc/set-env-parameters: { file: 'configs/config_prod.php' }
# Prefix
- misc/set-env-parameters: { file: 'configs/config_prod.php', prefix: 'ENV_' }
on-deploy:
- deploy/release/prepare # Skips default prepare task which is not needed.
- prepare/sw-structure: { timeout: 500 } # Creates new release directory and copies all content of current into the created directory.
- fs/link: { from: '../../media', to: 'media' } # Creates a new symlink.
- fs/link: { from: '../../files', to: 'files' } # Creates a new symlink.
- deploy: { from: 'configs/config_prod.php', to: './config.php' } # Pushes config file to server(s).
- deploy: { from: 'scripts/remote/', to: './scripts/', strict: true, timeout: 500 } # Pushes scripts files to server(s).
- deploy: { from: 'libraries/', to: './engine/Library/', strict: false } # Pushes library (engine/Library) to server(s).
# Skips default prepare task which is not needed.
- deploy/release/prepare

# Creates new release directory and copies all content of current into the created directory.
- prepare/sw-structure: { timeout: 500 }

# Creates a new symlink.
- fs/link: { from: '../../media', to: 'media' }
- fs/link: { from: '../../files', to: 'files' }

# Pushes file(s) to server(s).
# If strict is true, it will actually do a sync (i.e. delete files that do not exist locally anymore).
# Otherwise it would just upload&overwrite without touching files that do not exist locally anymore.
- deploy: { from: 'configs/config_prod.php', to: './config.php' }
- deploy: { from: 'scripts/remote/', to: './scripts/', strict: true, timeout: 500 }
- deploy: { from: 'libraries/', to: './engine/Library/', strict: false }
- deploy: { from: 'legacy_plugins/Community/', to: 'engine/Shopware/Plugins/Community/', strict: false }
- deploy: { from: 'legacy_plugins/Local/', to: 'engine/Shopware/Plugins/Local/', strict: true }
- deploy: { from: 'plugins/', to: 'custom/plugins/', strict: true }
- deploy: { from: 'licenses/', to: 'licenses/', strict: true } # sync files in licenses folder from local to server
- deploy: { from: 'licenses/', to: 'licenses/', strict: true }

# Execute "raw" command on remote host.
- exec: { cmd: './var/cache/clear_cache.sh', desc: 'Clear shopware cache.' }
- shopware/update-plugins # Updates all (>=5.2 system) plugins on server(s).
- shopware/update-legacy-plugins: { sync_sources_folders: true } # Updates all (legacy) plugins on server(s). "Sources" are the Community/Local folders.
- shopware/migrate: { table_suffix: 'bestit', migration_dir: 'sql' } # Executes all SQL migrations on server(s). Both parameters are optional.
- shopware/command: { cmd: 'sw:swaglicense:import ./licenses/licenses_prod.ini' } # Import licenses of license ini file into database, SwagLicense is needed for command
- shopware/command: { cmd: 'sw:theme:cache:generate' } # Warms up the shopware theme cache on server(s).

# Updates all (>=5.2 system) plugins on server(s).
- shopware/update-plugins

# Updates all (legacy) plugins on server(s). "Sources" are the Community/Local folders.
- shopware/update-legacy-plugins: { sync_sources_folders: true }

# Executes all SQL migrations on server(s). Both parameters are optional.
- shopware/migrate: { table_suffix: 'bestit', migration_dir: 'sql' }

# Warms up the shopware theme cache on server(s).
- shopware/command: { cmd: 'sw:theme:cache:generate' }
```
## Installation
Expand Down
35 changes: 33 additions & 2 deletions src/Misc/SetEnvParametersTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function getDescription()
public function execute()
{
$pathToConfig = $this->options['file'];
$pathToTarget = isset($this->options['target']) ? $this->options['target'] : $pathToConfig;
$prefix = $this->options['prefix'];
$encodeForXml = $this->options['encodeForXml'];

if (!is_file($pathToConfig)) {
throw new ErrorException("File not found: {$pathToConfig}");
Expand All @@ -52,11 +55,39 @@ public function execute()
$envVars = array_merge_recursive($_ENV, $_SERVER);

foreach ($envVars as $key => $value) {
$configContent = str_replace("%{$key}%", $value, $configContent);
/*
* Skip all values that are not a string (because we cannot use str_replace on those values).
* And also skip any keys which do not start with the given prefix.
* */
if (!is_string($value) || strpos($key, $prefix) !== 0) {
continue;
}

if ($encodeForXml) {
$value = htmlspecialchars($value, ENT_QUOTES);
}

/**
* Remove the environment prefix from the key, so that it matches up with the
* placeholders in the given file (the prefix should not be included in the placeholder)
*/
$placeholder = str_replace($prefix, '', $key);

$configContent = str_replace("%{$placeholder}%", $value, $configContent);
}

$res = file_put_contents($pathToConfig, $configContent);
$res = file_put_contents($pathToTarget, $configContent);

return $res !== false;
}

/**
* @return array
*/
public function getDefaults()
{
return [
'prefix' => 'ENV'
];
}
}

0 comments on commit 7844e51

Please sign in to comment.