ExistingConfigParserController.php (4679B)
1 <?php 2 3 namespace PartKeepr\SetupBundle\Controller; 4 5 use Symfony\Component\HttpFoundation\JsonResponse; 6 use Symfony\Component\HttpFoundation\Request; 7 use Symfony\Component\Routing\Annotation\Route; 8 9 class ExistingConfigParserController extends SetupBaseController 10 { 11 /** 12 * @Route("/setup/parseExistingConfig") 13 * 14 * @param Request $request 15 * 16 * @return JsonResponse 17 */ 18 public function parseExistingConfigAction(Request $request) 19 { 20 if (!$this->ensureAuthKey($request)) { 21 return $this->getAuthKeyErrorResponse(); 22 } 23 24 $response = [ 25 'success' => true, 26 'errors' => [], 27 'message' => 'Existing configuration imported successfully', 28 ]; 29 30 try { 31 $response['config'] = $this->get('partkeepr.setup.config_service')->configParser(); 32 33 if (count($response['config']) == 0) { 34 $response['config'] = $this->getLegacyConfig(); 35 } 36 37 if (count($response['config']) == 0) { 38 $response['message'] = 'No configuration found'; 39 $response['existingConfig'] = false; 40 } else { 41 $response['existingConfig'] = true; 42 } 43 } catch (\Exception $e) { 44 $response['success'] = false; 45 $response['message'] = 'Configuration parse error'; 46 $response['errors'] = [$e->getMessage()]; 47 } 48 49 return new JsonResponse($response); 50 } 51 52 protected function getLegacyConfig() 53 { 54 $config = []; 55 56 $legacyConfig = $this->get('partkeepr.setup.config_service')->legacyConfigParser(); 57 58 if (count($legacyConfig) > 0) { 59 if (array_key_exists('partkeepr.database.driver', $legacyConfig)) { 60 $config['database_driver'] = $legacyConfig['partkeepr.database.driver']; 61 } 62 63 if (array_key_exists('partkeepr.database.hostname', $legacyConfig)) { 64 $config['database_host'] = $legacyConfig['partkeepr.database.hostname']; 65 } 66 67 if (array_key_exists('partkeepr.database.username', $legacyConfig)) { 68 $config['database_user'] = $legacyConfig['partkeepr.database.username']; 69 } 70 71 if (array_key_exists('partkeepr.database.password', $legacyConfig)) { 72 $config['database_password'] = $legacyConfig['partkeepr.database.password']; 73 } 74 75 if (array_key_exists('partkeepr.database.dbname', $legacyConfig)) { 76 $config['database_name'] = $legacyConfig['partkeepr.database.dbname']; 77 } 78 79 if (array_key_exists('partkeepr.database.port', $legacyConfig)) { 80 $config['database_port'] = $legacyConfig['partkeepr.database.port']; 81 } 82 83 $config['authentication_provider'] = 'PartKeepr.Auth.HTTPBasicAuthenticationProvider'; 84 85 if (array_key_exists('partkeepr.frontend.motd', $legacyConfig)) { 86 $config['partkeepr.frontend.motd'] = $legacyConfig['partkeepr.frontend.motd']; 87 } 88 89 if (array_key_exists('partkeepr.frontend.autologin.enabled', $legacyConfig)) { 90 $config['partkeepr.frontend.auto_login.enabled'] = $legacyConfig['partkeepr.frontend.autologin.enabled']; 91 } 92 93 if (array_key_exists('partkeepr.frontend.autologin.username', $legacyConfig)) { 94 $config['partkeepr.frontend.auto_login.username'] = $legacyConfig['partkeepr.frontend.autologin.username']; 95 } 96 97 if (array_key_exists('partkeepr.frontend.autologin.password', $legacyConfig)) { 98 $config['partkeepr.frontend.auto_login.password'] = $legacyConfig['partkeepr.frontend.autologin.password']; 99 } 100 101 if (array_key_exists('partkeepr.cronjobs.disablecheck', $legacyConfig)) { 102 $config['partkeepr.cronjob.check'] = $legacyConfig['partkeepr.cronjobs.disablecheck']; 103 } 104 105 if (array_key_exists('partkeepr.category.path_separator', $legacyConfig)) { 106 $config['partkeepr.category.path_separator'] = $legacyConfig['partkeepr.category.path_separator']; 107 } 108 109 if (array_key_exists('partkeepr.frontend.allow_password_change', $legacyConfig)) { 110 $config['partkeepr.auth.allow_password_change'] = $legacyConfig['partkeepr.frontend.allow_password_change']; 111 } 112 113 if (array_key_exists('partkeepr.files.path', $legacyConfig)) { 114 $config['partkeepr.filesystem.data_directory'] = $legacyConfig['partkeepr.files.path']; 115 } 116 } 117 118 return $config; 119 } 120 }