Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
37.93% |
11 / 29 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| common_ext_ConfigDriver | |
37.93% |
11 / 29 |
|
20.00% |
1 / 5 |
33.91 | |
0.00% |
0 / 1 |
| singleton | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| getContent | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getDefaultHeader | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getHeaderPath | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getPath | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 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) 2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | use oat\oatbox\config\ConfigurationDriver; |
| 23 | |
| 24 | /** |
| 25 | * Short description of class common_ext_Extension |
| 26 | * |
| 27 | * @access public |
| 28 | * @author lionel.lecaque@tudor.lu |
| 29 | * @package generis |
| 30 | * @see @license GNU General Public (GPL) Version 2 http://www.opensource.org/licenses/gpl-2.0.php |
| 31 | |
| 32 | */ |
| 33 | class common_ext_ConfigDriver extends common_persistence_PhpFileDriver implements ConfigurationDriver |
| 34 | { |
| 35 | private static $singleton = null; |
| 36 | |
| 37 | public static function singleton() |
| 38 | { |
| 39 | if (is_null(self::$singleton)) { |
| 40 | $driver = new self(); |
| 41 | return $driver->connect('config', [ |
| 42 | 'dir' => CONFIG_PATH, |
| 43 | 'humanReadable' => true |
| 44 | ]); |
| 45 | } |
| 46 | return self::$singleton; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Override the function to allow an additional header |
| 51 | * |
| 52 | * (non-PHPdoc) |
| 53 | * @see common_persistence_PhpFileDriver::getContent() |
| 54 | */ |
| 55 | protected function getContent($key, $value) |
| 56 | { |
| 57 | $headerPath = $this->getHeaderPath($key); |
| 58 | $header = !is_null($headerPath) && file_exists($headerPath) |
| 59 | ? file_get_contents($headerPath) |
| 60 | : $this->getDefaultHeader($key); |
| 61 | |
| 62 | return $header . PHP_EOL . "return " . common_Utils::toHumanReadablePhpString($value) . ";" . PHP_EOL; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Generates a default header |
| 67 | * |
| 68 | * @param string $key |
| 69 | * @return string |
| 70 | */ |
| 71 | private function getDefaultHeader($key) |
| 72 | { |
| 73 | return '<?php' . PHP_EOL |
| 74 | . '/**' . PHP_EOL |
| 75 | . ' * Default config header' . PHP_EOL |
| 76 | . ' *' . PHP_EOL |
| 77 | . ' * To replace this add a file ' . $this->getHeaderPath($key) . PHP_EOL |
| 78 | . ' */' . PHP_EOL; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the path to the expected header file |
| 83 | * |
| 84 | * @param string $key |
| 85 | * @return string|NULL |
| 86 | */ |
| 87 | private function getHeaderPath($key) |
| 88 | { |
| 89 | $parts = explode('/', $key, 2); |
| 90 | if (count($parts) >= 2) { |
| 91 | list($extId, $configId) = $parts; |
| 92 | $ext = common_ext_ExtensionsManager::singleton()->getExtensionById($extId); |
| 93 | return $ext->getDir() . 'config/header/' . $configId . '.conf.php'; |
| 94 | } else { |
| 95 | return null; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * (non-PHPdoc) |
| 101 | * @see common_persistence_PhpFileDriver::getPath() |
| 102 | */ |
| 103 | protected function getPath($key) |
| 104 | { |
| 105 | $parts = explode('/', $key); |
| 106 | $path = substr(parent::getPath(array_shift($parts)), 0, -4); |
| 107 | foreach ($parts as $part) { |
| 108 | $path .= DIRECTORY_SEPARATOR . $this->sanitizeReadableFileName($part); |
| 109 | } |
| 110 | return $path . '.conf.php'; |
| 111 | } |
| 112 | } |