1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Server\Configuration\Extension\StringInjector |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Bernhard Wick <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://www.github.com/appserver-io/server |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Server\Configuration\Extension; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This class will get create a string from a single-column DB table called "virtualHost" |
25
|
|
|
* |
26
|
|
|
* @author Bernhard Wick <[email protected]> |
27
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
29
|
|
|
* @link https://www.github.com/appserver-io/server |
30
|
|
|
* @link http://www.appserver.io |
31
|
|
|
*/ |
32
|
|
|
class StringInjector extends AbstractInjector |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var array $data The data collected from the DB |
36
|
|
|
*/ |
37
|
|
|
protected $data; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Delimiter for the extracted string |
41
|
|
|
* |
42
|
|
|
* @const string STRING_DELIMETER |
43
|
|
|
*/ |
44
|
|
|
const STRING_DELIMETER = ' '; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Will init the injector's datasource |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function init() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
// Init data as an empty array |
54
|
|
|
$this->data = array(); |
55
|
|
|
|
56
|
|
|
// Grab our DB resource |
57
|
|
|
$dbConnection = $this->getDbResource(); |
58
|
|
|
|
59
|
|
|
// Build up the query |
60
|
|
|
$query = 'SELECT * FROM `virtualHost`'; |
61
|
|
|
|
62
|
|
|
// Get the results and fill them into our data |
63
|
|
|
foreach ($dbConnection->query($query) as $row) { |
64
|
|
|
$this->data[] = $row[0]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* We will return a string containing all data entries delimitered by the configured delimiter |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function extract() |
74
|
|
|
{ |
75
|
|
|
// Iterate over all entries and concatenate them |
76
|
|
|
$result = ''; |
77
|
|
|
foreach ($this->data as $dataEntry) { |
78
|
|
|
$result .= $dataEntry . self::STRING_DELIMETER; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Cut the last delimiter |
82
|
|
|
return substr($result, 0, strlen($result) - strlen(self::STRING_DELIMETER)); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.