| Conditions | 23 |
| Paths | 52 |
| Total Lines | 128 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 51 | public function register(DiInterface $di): void |
||
| 52 | { |
||
| 53 | $registeredDBServices = []; |
||
| 54 | $config = $di->getShared('config'); |
||
| 55 | $modulesDir = $config->path('core.modulesDir'); |
||
| 56 | |||
| 57 | $results = glob($modulesDir . '/*/module.json', GLOB_NOSORT); |
||
| 58 | |||
| 59 | foreach ($results as $moduleJson) { |
||
| 60 | $jsonString = file_get_contents($moduleJson); |
||
| 61 | if ($jsonString === false){ |
||
| 62 | continue; |
||
| 63 | } |
||
| 64 | $jsonModuleDescription = json_decode($jsonString, true); |
||
| 65 | if ( ! is_array($jsonModuleDescription) |
||
| 66 | || !array_key_exists('moduleUniqueID', $jsonModuleDescription)) { |
||
| 67 | continue; |
||
| 68 | } |
||
| 69 | |||
| 70 | $moduleUniqueId = $jsonModuleDescription['moduleUniqueID']; |
||
| 71 | if ( ! isset($moduleUniqueId)) { |
||
| 72 | continue; |
||
| 73 | } |
||
| 74 | |||
| 75 | $modelsFiles = glob("{$modulesDir}/{$moduleUniqueId}/Models/*.php", GLOB_NOSORT); |
||
| 76 | foreach ($modelsFiles as $file) { |
||
| 77 | $className = pathinfo($file)['filename']; |
||
| 78 | $moduleModelClass = "\\Modules\\{$moduleUniqueId}\\Models\\{$className}"; |
||
| 79 | |||
| 80 | // Test whether this class abstract or not |
||
| 81 | try { |
||
| 82 | $reflection = new ReflectionClass($moduleModelClass); |
||
| 83 | if ($reflection->isAbstract()) { |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | } catch (ReflectionException $exception) { |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | |||
| 90 | if ( |
||
| 91 | ! class_exists($moduleModelClass) |
||
| 92 | || count(get_class_vars($moduleModelClass)) === 0) { |
||
| 93 | continue; |
||
| 94 | } |
||
| 95 | |||
| 96 | $model = new $moduleModelClass(); |
||
| 97 | $connectionServiceName = $model->getReadConnectionService(); |
||
| 98 | if ( ! isset($connectionServiceName)) { |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | $registeredDBServices[] = $connectionServiceName; |
||
| 102 | if ($di->has($connectionServiceName)) { |
||
| 103 | $di->remove($connectionServiceName); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Create and connect database |
||
| 107 | $dbDir = "{$config->path('core.modulesDir')}/{$moduleUniqueId}/db"; |
||
| 108 | if (!file_exists($dbDir)){ |
||
| 109 | Util::mwMkdir($dbDir, true); |
||
| 110 | } |
||
| 111 | $dbFileName = "{$dbDir}/module.db"; |
||
| 112 | $dbFileExistBeforeAttachToConnection = file_exists($dbFileName); |
||
| 113 | |||
| 114 | // Log |
||
| 115 | $logDir = "{$config->path('core.logsDir')}/$moduleUniqueId/db"; |
||
| 116 | $logFileName = "{$logDir}/queries.log"; |
||
| 117 | if (!is_dir($logDir)){ |
||
| 118 | Util::mwMkdir($logDir, true); |
||
| 119 | $touchPath = Util::which('touch'); |
||
| 120 | Processes::mwExec("{$touchPath} {$logFileName}"); |
||
| 121 | Util::addRegularWWWRights($logDir); |
||
| 122 | } |
||
| 123 | |||
| 124 | $params = [ |
||
| 125 | "debugMode" => $config->path('modulesDatabases.debugMode'), |
||
| 126 | "adapter" => "Sqlite", |
||
| 127 | "dbfile" => $dbFileName, |
||
| 128 | "debugLogFile" => $logFileName, |
||
| 129 | ]; |
||
| 130 | |||
| 131 | $this->registerDBService($connectionServiceName, $di, $params); |
||
| 132 | |||
| 133 | // if database was created, we have to apply rules |
||
| 134 | if (!$dbFileExistBeforeAttachToConnection){ |
||
| 135 | Util::addRegularWWWRights($dbDir); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | // Register transactions events |
||
| 141 | $mainConnection = $di->getShared('db'); |
||
| 142 | |||
| 143 | $eventsManager = $mainConnection->getEventsManager(); |
||
| 144 | if ($eventsManager === null) { |
||
| 145 | $eventsManager = new Manager(); |
||
| 146 | } |
||
| 147 | // Attach all created connections to one transaction manager |
||
| 148 | $eventsManager->attach( |
||
| 149 | 'db', |
||
| 150 | function ($event) use ($registeredDBServices, $di) { |
||
| 151 | switch ($event->getType()) { |
||
| 152 | case 'beginTransaction': |
||
| 153 | { |
||
| 154 | foreach ($registeredDBServices as $service) { |
||
| 155 | $di->get($service)->begin(); |
||
| 156 | } |
||
| 157 | break; |
||
| 158 | } |
||
| 159 | case 'commitTransaction': |
||
| 160 | { |
||
| 161 | foreach ($registeredDBServices as $service) { |
||
| 162 | $di->get($service)->commit(); |
||
| 163 | } |
||
| 164 | break; |
||
| 165 | } |
||
| 166 | case 'rollbackTransaction': |
||
| 167 | { |
||
| 168 | foreach ($registeredDBServices as $service) { |
||
| 169 | $di->get($service)->rollback(); |
||
| 170 | } |
||
| 171 | break; |
||
| 172 | } |
||
| 173 | default: |
||
| 174 | } |
||
| 175 | } |
||
| 176 | ); |
||
| 177 | // Назначаем EventsManager экземпляру адаптера базы данных |
||
| 178 | $mainConnection->setEventsManager($eventsManager); |
||
| 179 | } |
||
| 192 |