Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
69.49% |
41 / 59 |
|
37.50% |
3 / 8 |
CRAP | |
0.00% |
0 / 1 |
| PersistenceManager | |
69.49% |
41 / 59 |
|
37.50% |
3 / 8 |
35.74 | |
0.00% |
0 / 1 |
| hasPersistence | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| registerPersistence | |
63.64% |
7 / 11 |
|
0.00% |
0 / 1 |
4.77 | |||
| getPersistenceById | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| createPersistence | |
57.89% |
11 / 19 |
|
0.00% |
0 / 1 |
5.19 | |||
| getSqlSchemas | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| applySchemas | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| applySchemaProvider | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
| getDriverConfigFeeder | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
4.94 | |||
| 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) 2019-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\generis\persistence; |
| 24 | |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use oat\generis\persistence\sql\SchemaCollection; |
| 27 | use common_persistence_SqlPersistence; |
| 28 | use oat\generis\persistence\sql\SchemaProviderInterface; |
| 29 | use oat\oatbox\service\ServiceNotFoundException; |
| 30 | use common_persistence_PhpNeo4jDriver; |
| 31 | |
| 32 | /** |
| 33 | * The PersistenceManager is responsible for initializing all persistences |
| 34 | * |
| 35 | * @author Lionel Lecaque <lionel@taotesting.com> |
| 36 | * @license GPLv2 |
| 37 | */ |
| 38 | class PersistenceManager extends ConfigurableService |
| 39 | { |
| 40 | public const SERVICE_ID = 'generis/persistences'; |
| 41 | |
| 42 | public const OPTION_PERSISTENCES = 'persistences'; |
| 43 | |
| 44 | /** |
| 45 | * Mapping of drivers to implementations. |
| 46 | * All SQL drivers except 'dbal' are deprecated |
| 47 | * @var array |
| 48 | */ |
| 49 | private const DRIVER_MAP = [ |
| 50 | 'dbal' => 'common_persistence_sql_dbal_Driver', |
| 51 | 'dbal_pdo_mysql' => 'common_persistence_sql_dbal_Driver', |
| 52 | 'dbal_pdo_sqlite' => 'common_persistence_sql_dbal_Driver', |
| 53 | 'dbal_pdo_pgsql' => 'common_persistence_sql_dbal_Driver', |
| 54 | 'dbal_pdo_ibm' => 'common_persistence_sql_dbal_Driver', |
| 55 | 'phpneo4j' => 'common_persistence_PhpNeo4jDriver', |
| 56 | 'phpredis' => 'common_persistence_PhpRedisDriver', |
| 57 | 'phpfile' => 'common_persistence_PhpFileDriver', |
| 58 | 'SqlKvWrapper' => 'common_persistence_SqlKvDriver', |
| 59 | 'no_storage' => 'common_persistence_InMemoryKvDriver', |
| 60 | 'no_storage_adv' => 'common_persistence_InMemoryAdvKvDriver' |
| 61 | ]; |
| 62 | |
| 63 | /** |
| 64 | * |
| 65 | * @var array |
| 66 | */ |
| 67 | private $persistences = []; |
| 68 | |
| 69 | /** |
| 70 | * Returns TRUE if the requested persistence exist, otherwise FALSE. |
| 71 | * |
| 72 | * @param string $persistenceId |
| 73 | * @return bool |
| 74 | */ |
| 75 | public function hasPersistence($persistenceId) |
| 76 | { |
| 77 | $persistenceList = $this->getOption(static::OPTION_PERSISTENCES); |
| 78 | return isset($persistenceList[$persistenceId]); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Registers a new persistence. |
| 83 | * |
| 84 | * @param string $persistenceId |
| 85 | * @param array $persistenceConf |
| 86 | */ |
| 87 | public function registerPersistence($persistenceId, array $persistenceConf) |
| 88 | { |
| 89 | // wrap pdo drivers in dbal |
| 90 | if (strpos($persistenceConf['driver'], 'pdo_') === 0) { |
| 91 | $persistenceConf = [ |
| 92 | 'driver' => 'dbal', |
| 93 | 'connection' => $persistenceConf |
| 94 | ]; |
| 95 | } |
| 96 | |
| 97 | if ( |
| 98 | isset($persistenceConf['connection']['driver']) |
| 99 | && $persistenceConf['connection']['driver'] === 'pdo_mysql' |
| 100 | ) { |
| 101 | $persistenceConf['connection']['charset'] = 'utf8'; |
| 102 | } |
| 103 | |
| 104 | $configs = $this->getOption(self::OPTION_PERSISTENCES); |
| 105 | $configs[$persistenceId] = $persistenceConf; |
| 106 | $this->setOption(self::OPTION_PERSISTENCES, $configs); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * |
| 111 | * @return \common_persistence_Persistence |
| 112 | */ |
| 113 | public function getPersistenceById($persistenceId) |
| 114 | { |
| 115 | if (!isset($this->persistences[$persistenceId])) { |
| 116 | $this->persistences[$persistenceId] = $this->createPersistence($persistenceId); |
| 117 | } |
| 118 | return $this->persistences[$persistenceId]; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * |
| 123 | * @param string $persistenceId |
| 124 | * @return \common_persistence_Persistence |
| 125 | * @throws \common_Exception |
| 126 | */ |
| 127 | private function createPersistence($persistenceId) |
| 128 | { |
| 129 | $configs = $this->getOption(self::OPTION_PERSISTENCES); |
| 130 | if (!isset($configs[$persistenceId])) { |
| 131 | throw new \common_Exception('Persistence Configuration for persistence ' . $persistenceId . ' not found'); |
| 132 | } |
| 133 | $config = $configs[$persistenceId]; |
| 134 | $driverString = $config['driver']; |
| 135 | |
| 136 | $driverClassName = self::DRIVER_MAP[$driverString] ?? (string)$driverString; |
| 137 | |
| 138 | if (!class_exists($driverClassName)) { |
| 139 | throw new \common_exception_Error( |
| 140 | sprintf( |
| 141 | 'Driver "%s" not found, check your database configuration for %s.', |
| 142 | $driverString, |
| 143 | $persistenceId |
| 144 | ) |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | $driver = $this->propagate(new $driverClassName()); |
| 149 | |
| 150 | $driverOptionsFeeder = $this->getDriverConfigFeeder(); |
| 151 | |
| 152 | if ($driverOptionsFeeder !== null) { |
| 153 | $config = $driverOptionsFeeder->feed($config); |
| 154 | } |
| 155 | |
| 156 | return $driver->connect($persistenceId, $config); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Return a collection of all SQL schemas |
| 161 | */ |
| 162 | public function getSqlSchemas() |
| 163 | { |
| 164 | $schemas = new SchemaCollection(); |
| 165 | foreach (array_keys($this->getOption(self::OPTION_PERSISTENCES)) as $id) { |
| 166 | $persistence = $this->getPersistenceById($id); |
| 167 | if ($persistence instanceof common_persistence_SqlPersistence) { |
| 168 | $schemas->addSchema($id, $persistence->getSchemaManager()->createSchema()); |
| 169 | } |
| 170 | } |
| 171 | return $schemas; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Adapt the databases to the SQL schemas |
| 176 | */ |
| 177 | public function applySchemas(SchemaCollection $schemaCollection) |
| 178 | { |
| 179 | $this->logInfo('Applying schame changes'); |
| 180 | foreach ($schemaCollection as $id => $schema) { |
| 181 | $persistence = $this->getPersistenceById($id); |
| 182 | $fromSchema = $schemaCollection->getOriginalSchema($id); |
| 183 | $queries = $persistence->getPlatForm()->getMigrateSchemaSql($fromSchema, $schema); |
| 184 | foreach ($queries as $query) { |
| 185 | $persistence->exec($query); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Add the schema of a single service, if needed |
| 192 | */ |
| 193 | public function applySchemaProvider($service): void |
| 194 | { |
| 195 | if ($service instanceof SchemaProviderInterface) { |
| 196 | $schemaCollection = $this->getSqlSchemas(); |
| 197 | $service->provideSchema($schemaCollection); |
| 198 | $this->applySchemas($schemaCollection); |
| 199 | $this->logInfo('Applied schema for ' . get_class($service)); |
| 200 | } else { |
| 201 | $this->logDebug('No schema found for ' . get_class($service)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private function getDriverConfigFeeder(): ?DriverConfigurationFeeder |
| 206 | { |
| 207 | /** |
| 208 | * This class is directly instantiated directly in some installation process, so the ServiceLocator |
| 209 | * might not be available, therefore this if is necessary. |
| 210 | */ |
| 211 | if (!$this->getServiceLocator()) { |
| 212 | return null; |
| 213 | } |
| 214 | |
| 215 | try { |
| 216 | return $this->getServiceLocator()->get(DriverConfigurationFeeder::SERVICE_ID); |
| 217 | } catch (ServiceNotFoundException $exception) { |
| 218 | /** |
| 219 | * This is because PersistenceManager is registered in tao-core and while doing the generis update |
| 220 | * before the update of tao (which had this class in the migrations). |
| 221 | */ |
| 222 | return null; |
| 223 | } |
| 224 | } |
| 225 | } |