Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a path builder class and fix a bug in the database backup. See #252
- Loading branch information
1 parent
8617284
commit 0242afa
Showing
2 changed files
with
141 additions
and
1 deletion.
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
124 changes: 124 additions & 0 deletions
124
system/modules/syncCto/src/SyncCto/Helper/PathBuilder.php
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,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); | ||
} | ||
} |