|
8 | 8 | switch ($options[xPDOTransport::PACKAGE_ACTION]) { |
9 | 9 | case xPDOTransport::ACTION_INSTALL: |
10 | 10 | case xPDOTransport::ACTION_UPGRADE: |
11 | | - $corePath = MODX_CORE_PATH . 'components/{package_key}/'; |
12 | | - $eb; /* Service class */ |
| 11 | + // Initial variables |
| 12 | + $packageKey = '{package_key}'; |
| 13 | + $keyLower = strtolower($packageKey); |
| 14 | + $corePath = MODX_CORE_PATH . "components/$keyLower/"; |
13 | 15 | $isV3 = $modx->getVersionData()['version'] >= 3; |
| 16 | + |
| 17 | + // Include model based on version |
14 | 18 | if (!$isV3) { |
15 | | - $modx->log(xPDO::LOG_LEVEL_ERROR, "This version of ExtraBuilder is compatible with MODX 3.0 and higher only."); |
| 19 | + // Main class path |
| 20 | + $classFilePath = $corePath . "src/{$packageKey}.php"; |
| 21 | + if (is_file($classFilePath)) { |
| 22 | + // Get the contents |
| 23 | + $contents = file_get_contents($classFilePath); |
| 24 | + |
| 25 | + // For our main class file, remove namespace block |
| 26 | + $key = '//v3 only'; |
| 27 | + $start = strpos($contents, $key); |
| 28 | + if ($start !== false) { |
| 29 | + $end = strpos($contents, $key, $start + strlen($key)); |
| 30 | + $contents = substr_replace($contents, '', $start, $end - $start + strlen($key)); |
| 31 | + } |
| 32 | + |
| 33 | + // Write the file back |
| 34 | + file_put_contents($classFilePath, $contents); |
| 35 | + |
| 36 | + // Now try including and loading the file |
| 37 | + @include_once $classFilePath; |
| 38 | + $myService = new $packageKey($modx, ["install" => true]); |
| 39 | + if ($myService) { |
| 40 | + // Rename classes and remove namespaces in processors |
| 41 | + $myService->replaceOnV2Install($corePath."src/Processors/"); |
| 42 | + |
| 43 | + // Also replace classes on index class file |
| 44 | + $myService->replaceClassesForV2($corePath.'index.class.php'); |
| 45 | + |
| 46 | + // Attempt to add the package |
| 47 | + $result = $modx->addPackage("$keyLower.v2.model", MODX_CORE_PATH.'components/'); |
| 48 | + if (!$result) { |
| 49 | + $modx->log(xPDO::LOG_LEVEL_ERROR, "Unable to add package. Install failed."); |
| 50 | + exit(); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + else { |
| 55 | + // This package has no class file, just add the package |
| 56 | + $result = $modx->addPackage("$keyLower.v2.model", MODX_CORE_PATH.'components/'); |
| 57 | + if (!$result) { |
| 58 | + $modx->log(xPDO::LOG_LEVEL_ERROR, "Unable to add package. Install failed."); |
| 59 | + exit(); |
| 60 | + } |
| 61 | + } |
16 | 62 | } |
17 | 63 | else { |
18 | 64 | // Check for the bootstrap file and include it |
19 | 65 | if (file_exists($corePath.'bootstrap.php')) { |
20 | 66 | // For v3, the bootstrap file needs a namespace object, which doesn't exist yet |
21 | 67 | // Create an object so it can reference it |
22 | | - $namespace = $object->toArray(); |
23 | | - $namespace['path'] = $corePath; |
24 | | - $namespace['assets_path'] = MODX_ASSETS_PATH . 'components/{package_key}/'; |
| 68 | + $namespace = [ |
| 69 | + 'name' => $keyLower, |
| 70 | + 'path' => $corePath, |
| 71 | + 'assets_path' => MODX_ASSETS_PATH . "components/$keyLower/" |
| 72 | + ]; |
25 | 73 | require $corePath.'bootstrap.php'; |
26 | 74 | } |
27 | 75 | else { |
|
32 | 80 | $manager = $modx->getManager(); |
33 | 81 | $objects = []; |
34 | 82 | $classPrefix = ""; |
35 | | - $namespace = "{package_key}"; |
36 | | - $nsLower = strtolower($namespace); |
37 | | - $schemaFile = MODX_CORE_PATH . "components/{package_key}/schema/{$nsLower}.mysql.schema.xml"; |
38 | | - if (is_file($schemaFile)) { |
| 83 | + $schemaFile = MODX_CORE_PATH . "components/{$keyLower}/schema/{$keyLower}.mysql.schema.xml"; |
| 84 | + if (!is_file($schemaFile)) { |
| 85 | + $schemaFile = MODX_CORE_PATH . "components/{$keyLower}/v2/schema/{$keyLower}.mysql.schema.xml"; |
| 86 | + if (!is_file($schemaFile)) { |
| 87 | + $schemaFile = MODX_CORE_PATH . "components/{$keyLower}/model/schema/{$keyLower}.mysql.schema.xml"; |
| 88 | + if (!is_file($schemaFile)) { |
| 89 | + $modx->log(xPDO::LOG_LEVEL_ERROR, "Unable to load schema file..."); |
| 90 | + exit(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // If we have a schema file |
| 96 | + if (is_file($schemaFile)) { |
39 | 97 | $schema = new SimpleXMLElement($schemaFile, 0, true); |
40 | 98 | if (isset($schema->object)) { |
41 | 99 | foreach ($schema->object as $obj) { |
42 | 100 | $objects[] = (string) $obj['class']; |
43 | 101 | } |
44 | 102 | } |
45 | 103 |
|
46 | | - // Store the classPrefix |
47 | | - $classPrefix = $schema[0]['package']; |
| 104 | + // Store the package value |
| 105 | + $classPrefix = trim($schema[0]['package'], '\\').'\\'; |
48 | 106 | unset($schema); |
49 | 107 | } |
50 | 108 |
|
|
0 commit comments