Skip to content

Commit

Permalink
Add a path builder class and fix a bug in the database backup. See #252
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanheimes committed Dec 21, 2015
1 parent 8617284 commit 0242afa
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 1 deletion.
18 changes: 17 additions & 1 deletion system/modules/syncCto/SyncCtoDatabase.php
Expand Up @@ -993,6 +993,9 @@ protected function doRestoreData()
*/
public function runRestore($strRestoreFile, $arrSuffixSQL = null)
{
// Load the path bulder.
$pathBuilder = new \SyncCto\Helper\PathBuilder();

try
{
// Set time out for database. Ticket #2653
Expand All @@ -1015,19 +1018,32 @@ public function runRestore($strRestoreFile, $arrSuffixSQL = null)
// Get structure
if ($objZipRead->getFile($this->strFilenameSyncCto))
{
$objGzFile = new File("system/tmp/$this->strFilenameSyncCto.gz");
$zipPath = $pathBuilder
->addPath('system/tmp')
->addUnknownPath(sprintf('%s.gz', $this->strFilenameSyncCto))
->getPath(false);

$objGzFile = new File($zipPath);
$objGzFile->write($objZipRead->unzip());
$objGzFile->close();

$arrRestoreTables = $this->runRestoreFromXML("system/tmp/$this->strFilenameSyncCto.gz");
}
else
{
$strRestoreFile = $pathBuilder
->addUnknownPath($strRestoreFile)
->getPath(false);

$arrRestoreTables = $this->runRestoreFromSer($strRestoreFile);
}
break;

case "synccto":
$strRestoreFile = $pathBuilder
->addUnknownPath($strRestoreFile)
->getPath(false);

$arrRestoreTables = $this->runRestoreFromXML($strRestoreFile);
break;

Expand Down
124 changes: 124 additions & 0 deletions system/modules/syncCto/src/SyncCto/Helper/PathBuilder.php
@@ -0,0 +1,124 @@
<?php

/**
* Contao Open Source CMS
*
* @copyright MEN AT WORK 2015
* @package syncCto
* @license GNU/LGPL
* @filesource
*/

namespace SyncCto\Helper;

/**
* Class PathBuilder
*
* A helper class to clean the path and add the TL_ROOT if wanted.
*
* @package SyncCto\Helper
*/
class PathBuilder
{
/**
* The current array with all path parts.
*
* @var array
*/
protected $pathParts = array();

/**
* Add a path part to the system.
*
* @param string|array $path
*
* @param string $separator
*
* @return $this
*/
public function addPath($path, $separator = '/')
{
if (is_array($path)) {
$this->addArray($path);
} else {
$this->addString($path, $separator);
}

return $this;
}

/**
* Add a path part to the system, but without the knowing of the directory separator.
*
* @param string|array $path
*
* @return $this
*/
public function addUnknownPath($path)
{
if (is_array($path)) {
$this->addArray($path);
} else {
$wrongSeparator = ((DIRECTORY_SEPARATOR == '/') ? '\\' : '/');
$path = str_replace($wrongSeparator, DIRECTORY_SEPARATOR, $path);
$this->addString($path, DIRECTORY_SEPARATOR);
}

return $this;
}

/**
* Build the whole path with the right directory separator.
*
* @param bool $withTlRoot If true the TL_ROOT will be added.
*
* @return string
*/
public function getPath($withTlRoot = true)
{
// Build the path.
$return = (($withTlRoot) ? TL_ROOT . DIRECTORY_SEPARATOR : '')
. implode(DIRECTORY_SEPARATOR, $this->pathParts);

// Reset the array.
$this->pathParts = array();

// Return the value.
return $return;
}

/**
* Add all elements to the current array.
*
* @param $path
*/
protected function addArray($path)
{
// Trim all values.
$path = array_map(function ($value) {
return trim($value);
}, $path);

// Remove empty.
$path = array_filter($path);

// Add to the array.
$this->pathParts = array_merge($this->pathParts, $path);
}

/**
* Add a string path to the current array.
*
* @param string $path
*
* @param string $separator
*/
protected function addString($path, $separator = '/')
{
// Remove blanks and split.
$parts = trimsplit($separator, $path);

// Add.
$this->addArray($parts);
}
}

0 comments on commit 0242afa

Please sign in to comment.