Skip to content

Commit 8943840

Browse files
committed
replace SimpleXML with DOMDocument in AddCommand
1 parent c747a46 commit 8943840

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/Commands/AddCommand.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use DevNet\System\IO\FileMode;
2525
use DevNet\System\IO\FileStream;
2626
use DevNet\System\Runtime\ClassLoader;
27-
use DirectoryIterator;
27+
use DOMDocument;
2828
use ReflectionClass;
2929

3030
class AddCommand extends CommandLine implements ICommandHandler, ICodeGenerator
@@ -96,23 +96,23 @@ public function onExecute(object $sender, CommandEventArgs $args): void
9696
$sourceRoot = getcwd();
9797
$projectPath = $sourceRoot . '/devnet.proj';
9898

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.
100100
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';
111108

109+
$loader = new ClassLoader($sourceRoot);
110+
$loader->map('/');
112111
$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());
116116
}
117117
}
118118
}
@@ -154,7 +154,7 @@ public function generate(array $parameters): array
154154
$name = $parameters['--name'] ?? 'MyClass';
155155
$output = $parameters['--output'] ?? '';
156156
$namespace = $parameters['--prefix'] ?? 'Application';
157-
$namespace = $namespace .'\\' . str_replace('/', '\\', $output);
157+
$namespace = $namespace . '\\' . str_replace('/', '\\', $output);
158158
$namespace = trim($namespace, '\\');
159159

160160
$content = new StringBuilder();

0 commit comments

Comments
 (0)