Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| tao_install_utils_ConfigWriter | |
0.00% |
0 / 43 |
|
0.00% |
0 / 3 |
380 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| createConfig | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
42 | |||
| writeConstants | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
132 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | use oat\tao\model\configurationMarkers\Secrets\SerializableSecretDto; |
| 28 | |
| 29 | /** |
| 30 | * The ConfigWriter class enables you to create config file from samples |
| 31 | * and to write the constants inside. |
| 32 | * |
| 33 | * @author Bertrand CHEVRIER <bertrand.chevrier@tudor.lu> |
| 34 | * @access public |
| 35 | * @package tao |
| 36 | |
| 37 | * |
| 38 | */ |
| 39 | class tao_install_utils_ConfigWriter |
| 40 | { |
| 41 | /** |
| 42 | * @var string the path to the sample file |
| 43 | */ |
| 44 | protected $sample; |
| 45 | |
| 46 | /** |
| 47 | * @var string the path to the real config file |
| 48 | */ |
| 49 | protected $file; |
| 50 | |
| 51 | /** |
| 52 | * instantiate by config file |
| 53 | * @param string $sample |
| 54 | * @param string $file |
| 55 | * @throws tao_install_utils_Exception |
| 56 | */ |
| 57 | public function __construct($sample, $file) |
| 58 | { |
| 59 | if (!file_exists($sample)) { |
| 60 | throw new tao_install_utils_Exception('Unable to find sample config ' . $sample); |
| 61 | } |
| 62 | $this->sample = $sample; |
| 63 | $this->file = $file; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Create the config file from the sample |
| 68 | * @throws tao_install_utils_Exception |
| 69 | */ |
| 70 | public function createConfig() |
| 71 | { |
| 72 | |
| 73 | //common checks |
| 74 | if (!is_writable(dirname($this->file))) { |
| 75 | throw new tao_install_utils_Exception( |
| 76 | 'Unable to create configuration file. Please set write permission to : ' . dirname($this->file) |
| 77 | ); |
| 78 | } |
| 79 | if (file_exists($this->file) && !is_writable($this->file)) { |
| 80 | throw new tao_install_utils_Exception( |
| 81 | 'Unable to create the configuration file. Please set the write permissions to : ' . $this->file |
| 82 | ); |
| 83 | } |
| 84 | if (!is_readable($this->sample)) { |
| 85 | throw new tao_install_utils_Exception( |
| 86 | 'Unable to read the sample configuration. Please set the read permissions to : ' . $this->sample |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | if (!copy($this->sample, $this->file)) { |
| 91 | throw new tao_install_utils_Exception('Unable to copy the sample configuration to : ' . $this->file); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Write the constants into the config file |
| 97 | * @param array $constants the list of constants to write (the key is the name of the constant) |
| 98 | * @throws tao_install_utils_Exception |
| 99 | */ |
| 100 | public function writeConstants(array $constants) |
| 101 | { |
| 102 | |
| 103 | //common checks |
| 104 | if (!file_exists($this->file)) { |
| 105 | throw new tao_install_utils_Exception("Unable to write constants: $this->file don't exists!"); |
| 106 | } |
| 107 | if (!is_readable($this->file) || !is_writable($this->file)) { |
| 108 | throw new tao_install_utils_Exception( |
| 109 | "Unable to write constants: $this->file must have read and write permissions!" |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | $content = file_get_contents($this->file); |
| 114 | if (!empty($content)) { |
| 115 | foreach ($constants as $name => $val) { |
| 116 | if (is_string($val)) { |
| 117 | $val = addslashes((string)$val); |
| 118 | $content = preg_replace( |
| 119 | '/(\'' . $name . '\')(.*?)$/ms', |
| 120 | '$1,\'' . addslashes($val) . '\');', |
| 121 | $content |
| 122 | ); |
| 123 | } elseif (is_bool($val)) { |
| 124 | ($val === true) ? $val = 'true' : $val = 'false'; |
| 125 | $content = preg_replace('/(\'' . $name . '\')(.*?)$/ms', '$1, ' . $val . ');', $content); |
| 126 | } elseif (is_numeric($val)) { |
| 127 | $content = preg_replace('/(\'' . $name . '\')(.*?)$/ms', '$1, ' . $val . ');', $content); |
| 128 | } elseif ($val instanceof SerializableSecretDto) { |
| 129 | // Operating with .env values |
| 130 | $content = preg_replace('/(\'' . $name . '\')(.*?)$/ms', '$1, ' |
| 131 | . $val->__toPhpCode() . ');', $content); |
| 132 | } |
| 133 | } |
| 134 | file_put_contents($this->file, $content); |
| 135 | } |
| 136 | } |
| 137 | } |