|
24 | 24 | use DevNet\System\IO\FileMode; |
25 | 25 | use DevNet\System\IO\FileStream; |
26 | 26 | use DevNet\System\Runtime\ClassLoader; |
27 | | -use DirectoryIterator; |
| 27 | +use DOMDocument; |
28 | 28 | use ReflectionClass; |
29 | 29 |
|
30 | 30 | class AddCommand extends CommandLine implements ICommandHandler, ICodeGenerator |
@@ -96,23 +96,23 @@ public function onExecute(object $sender, CommandEventArgs $args): void |
96 | 96 | $sourceRoot = getcwd(); |
97 | 97 | $projectPath = $sourceRoot . '/devnet.proj'; |
98 | 98 |
|
99 | | - // gets root namespace from the project file and the source route related to the entrypoint location. |
| 99 | + // Gets root namespace from the project file and the source route related to the entrypoint location. |
100 | 100 | if (is_file($projectPath)) { |
101 | | - $projectFile = simplexml_load_file($projectPath); |
102 | | - if ($projectFile) { |
103 | | - $prefix = $projectFile->Properties->RootNamespace ?? $prefix; |
104 | | - $startupObject = $projectFile->Properties->StartupObject ?? 'Application\\Program'; |
105 | | - $loader = new ClassLoader($sourceRoot); |
106 | | - foreach (new DirectoryIterator($sourceRoot) as $dir) { |
107 | | - if ($dir->isDir() && !str_starts_with($dir->getFilename(), '.')) { |
108 | | - $loader->map($prefix, '/' . $dir); |
109 | | - } |
110 | | - } |
| 101 | + $dom = new DOMDocument(); |
| 102 | + $result = $dom->load($projectPath); |
| 103 | + if ($result) { |
| 104 | + $rootNamespace = $dom->getElementsByTagName('RootNamespace')->item(0); |
| 105 | + $prefix = $rootNamespace ? $rootNamespace->textContent : 'Application'; |
| 106 | + $startupObject = $dom->getElementsByTagName('StartupObject')->item(0); |
| 107 | + $mainClass = $startupObject ? $startupObject->textContent : 'Application\\Program'; |
111 | 108 |
|
| 109 | + $loader = new ClassLoader($sourceRoot); |
| 110 | + $loader->map('/'); |
112 | 111 | $loader->register(); |
113 | | - if (class_exists($startupObject)) { |
114 | | - $entryPointClass = new ReflectionClass((string)$startupObject); |
115 | | - $sourceRoot = dirname($entryPointClass->getFileName()); |
| 112 | + |
| 113 | + if (class_exists($mainClass)) { |
| 114 | + $mainClass = new ReflectionClass($mainClass); |
| 115 | + $sourceRoot = dirname($mainClass->getFileName()); |
116 | 116 | } |
117 | 117 | } |
118 | 118 | } |
@@ -154,7 +154,7 @@ public function generate(array $parameters): array |
154 | 154 | $name = $parameters['--name'] ?? 'MyClass'; |
155 | 155 | $output = $parameters['--output'] ?? ''; |
156 | 156 | $namespace = $parameters['--prefix'] ?? 'Application'; |
157 | | - $namespace = $namespace .'\\' . str_replace('/', '\\', $output); |
| 157 | + $namespace = $namespace . '\\' . str_replace('/', '\\', $output); |
158 | 158 | $namespace = trim($namespace, '\\'); |
159 | 159 |
|
160 | 160 | $content = new StringBuilder(); |
|
0 commit comments