Ein für Shopware 6 vorbereitet Systemmanagment.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.8 KiB

1 year ago
  1. #!/usr/bin/php
  2. <?php
  3. ### DO NOT EDIT THIS FILE
  4. function usage() {
  5. echo "\n";
  6. echo "Get and Set configuration parameters\n";
  7. echo "\n";
  8. echo "Usage:\n";
  9. echo "\n";
  10. echo "config get [configPath]\n";
  11. echo "config set [configPath] [value]\n";
  12. echo "config remove [configPath]\n";
  13. echo "\n";
  14. echo "configPath: The config path is in the format 'project_manager.workspaces_dir'\n";
  15. echo "\n";
  16. echo "--help Prints this message\n";
  17. echo "\n";
  18. exit;
  19. }
  20. if ($_SERVER['argc'] < 3 || $_SERVER['argv'] == '--help') {
  21. usage();
  22. }
  23. global $project_manager_dir, $shortname, $configuration;
  24. $configuration = [];
  25. $project_manager_dir = dirname(dirname(__DIR__));
  26. $command = $_SERVER['argv'][1];
  27. $configPath = $_SERVER['argv'][2];
  28. $value = $_SERVER['argv'][3] ?? null;
  29. $shortname = substr($configPath, 0, strpos($configPath, '.'));
  30. $configPath = substr($configPath, strpos($configPath, '.')+1);
  31. if (function_exists($command)) {
  32. loadProjectManagerConfig($project_manager_dir);
  33. if ($shortname !== 'project_manager') {
  34. loadProjectConfig();
  35. }
  36. $command();
  37. }
  38. function mergeConfig($file)
  39. {
  40. global $configuration;
  41. $configuration = array_replace_recursive(
  42. $configuration,
  43. json_decode(file_get_contents($file), true)
  44. );
  45. }
  46. function loadProjectManagerConfig()
  47. {
  48. global $configuration, $project_manager_dir;
  49. $config_file = $project_manager_dir . '/etc/config.json';
  50. mergeConfig($config_file);
  51. }
  52. function loadProjectConfig()
  53. {
  54. global $configuration, $project_manager_dir;
  55. $customer = getCustomerFromShortname();
  56. $project = getProjectFromShortname();
  57. $config_file = "$project_manager_dir/data/$customer/$project/etc/config.json";
  58. mergeConfig($config_file);
  59. $plugin_dir = dirname($config_file) . '/plugins';
  60. $dh = opendir($plugin_dir);
  61. if ($dh) {
  62. while (false !== ($file = readdir($dh))) {
  63. if ('.' === $file || '..' === $file || !is_dir($plugin_dir/$file)) {
  64. continue;
  65. }
  66. $config_file = "$plugin_dir/$file/etc/config.json";
  67. mergeConfig($config_file);
  68. }
  69. }
  70. }
  71. function getCustomerFromShortname()
  72. {
  73. global $configuration, $shortname;
  74. return $configuration['project_manager']['projects'][$shortname]['customer'] ?? null;
  75. }
  76. function getProjectFromShortname()
  77. {
  78. global $configuration, $shortname;
  79. return $configuration['project_manager']['projects'][$shortname]['project'] ?? null;
  80. }
  81. function parsePath()
  82. {
  83. global $configPath;
  84. $parsedPath = '';
  85. foreach (explode('.', $configPath) as $part) {
  86. $parsedPath .= "['$part']";
  87. }
  88. return $parsedPath;
  89. }
  90. function is()
  91. {
  92. global $configuration, $shortname;
  93. return eval("return isset(\$configuration['$shortname']".parsePath().");");
  94. }
  95. function get()
  96. {
  97. global $configuration, $shortname;
  98. if (!is()) {
  99. exit(1);
  100. }
  101. $return = eval("return \$configuration['$shortname']".parsePath().";");
  102. if (is_array($return)) {
  103. $echo = '';
  104. foreach (array_keys($return) as $value) {
  105. $echo .= '"'.$value.'" ';
  106. }
  107. $echo = trim($echo, ' ');
  108. echo $echo;
  109. exit;
  110. }
  111. echo $return;
  112. }
  113. function set()
  114. {
  115. global $configuration, $shortname, $value;
  116. if (!is()) {
  117. exit(1);
  118. }
  119. eval("\$configuration['$shortname']".parsePath()." = '$value';");
  120. writeConfig();
  121. }
  122. function remove()
  123. {
  124. global $configuration, $shortname;
  125. if (!is()) {
  126. exit(1);
  127. }
  128. eval("unset \$configuration['$shortname']".parsePath().";");
  129. writeConfig();
  130. }
  131. function writeConfig()
  132. {
  133. global $configuration, $shortname, $project_manager_dir;
  134. $customer = getCustomerFromShortname();
  135. $project = getProjectFromShortname();
  136. foreach($configuration as $shortname => $data) {
  137. if ('project_manager' === $shortname) {
  138. $config_file = "$project_manager_dir/etc/config.json";
  139. } elseif ('project' === $shortname) {
  140. $config_file = "$project_manager_dir/data/$customer/$project/etc/config";
  141. } else {
  142. $config_file = "$project_manager_dir/data/$customer/$project/etc/plugins/$shortname/config";
  143. }
  144. file_put_contents($config_file, json_encode([$shortname => $data], JSON_PRETTY_PRINT));
  145. }
  146. }