Skip to content

Commit 48219af

Browse files
committed
Fixed CompileCommand
Removed normalizePath, added _pathInfo
1 parent 433dec0 commit 48219af

File tree

6 files changed

+31
-39
lines changed

6 files changed

+31
-39
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/vendor/
22
composer.phar
3+
4+
Data_*.php

app/Command/CompileCommand.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
use PHPDataGen\Compiler;
4444

45-
use function PHPDataGen\Utility\normalizePath;
45+
use function PHPDataGen\Utility\_pathInfo;
4646

4747
/**
4848
* Compile command
@@ -53,7 +53,9 @@ class CompileCommand extends Command {
5353
protected function compileFile(string $file, OutputStyle $io, Compiler $compiler = null) {
5454
$io->title($file);
5555

56-
if (preg_match('/.*\\.pdata/i', $file) === 0) {
56+
$pathinfo = _pathInfo($file);
57+
58+
if ($pathinfo->extension !== 'pdata') {
5759
$io->warning("File \"$file\" extension is not \".pdata\"");
5860
}
5961

@@ -71,13 +73,13 @@ protected function compileFile(string $file, OutputStyle $io, Compiler $compiler
7173
}
7274

7375
$model = $parser->getCurrentState()->getBuilder()->build();
74-
if (count($model->classes) > 1) {
76+
if (count($model->classes) < 1) {
77+
$io->warning("File has no classes");
78+
} elseif (count($model->classes) > 1) {
7579
$io->warning("File has more than one class");
7680
} else {
77-
$namespacePath = str_replace('\\', '/', $model->namespace)."/{$model->classes[0]->name}.pdata";
78-
79-
if (strpos(normalizePath($file), $namespacePath) === false) {
80-
$io->warning("File path is mismatch class name");
81+
if ($pathinfo->filename !== $model->classes[0]->name) {
82+
$io->warning("File name is mismatch class name");
8183
}
8284
}
8385

@@ -89,7 +91,10 @@ protected function compileFile(string $file, OutputStyle $io, Compiler $compiler
8991
return;
9092
}
9193

92-
file_put_contents(dirname($file).'/Data_'.basename($file, '.pdata').'.php', $result);
94+
$resultPath = "{$pathinfo->dirname}/Data_{$pathinfo->filename}.php";
95+
file_put_contents($resultPath, $result);
96+
97+
$io->success("Written in $resultPath");
9398
}
9499

95100
protected function configure() {
File renamed without changes.

app/Utility/normalizePath.php renamed to app/Utility/_pathInfo.php

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,26 @@
2525
namespace PHPDataGen\Utility;
2626

2727
/**
28-
* Path normalize function
28+
* Extracts directory path, filename, file extension from file path
2929
*
30-
* @link http://php.net/manual/function.realpath.php#112367
30+
* Return object contains 3 fields:
31+
* - dirname - Directory path
32+
* - filename - String before first dot in basename
33+
* - extension - String after first dot in basename
3134
*
3235
* @param string $path
3336
*
34-
* @return string Normalized path
37+
* @return \stdClass
3538
*/
36-
function normalizePath(string $path): string {
37-
$parts = array(); // Array to build a new path from the good parts
38-
$path = str_replace('\\', '/', $path); // Replace backslashes with forwardslashes
39-
$path = preg_replace('/\/+/', '/', $path); // Combine multiple slashes into a single slash
40-
$segments = explode('/', $path); // Collect path segments
39+
function _pathInfo(string $path): \stdClass {
40+
$ret = (object) [];
4141

42-
$test = ''; // Initialize testing variable
43-
foreach ($segments as $segment) {
44-
if ($segment != '.') {
45-
$test = array_pop($parts);
42+
$info = pathinfo($path);
43+
$dot = strpos($info['basename'], '.');
4644

47-
if (is_null($test)) {
48-
$parts[] = $segment;
49-
} else if ($segment == '..') {
50-
if ($test == '..') {
51-
$parts[] = $test;
52-
}
45+
$ret->dirname = $info['dirname'];
46+
$ret->extension = substr($info['basename'], $dot + 1);
47+
$ret->filename = substr($info['basename'], 0, $dot);
5348

54-
if ($test == '..' || $test == '') {
55-
$parts[] = $segment;
56-
}
57-
} else {
58-
$parts[] = $test;
59-
$parts[] = $segment;
60-
}
61-
}
62-
}
63-
64-
return implode('/', $parts);
49+
return $ret;
6550
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"PHPDataGen\\": "app"
2222
},
2323
"files": [
24-
"app/Utility/normalizePath.php"
24+
"app/Utility/_pathInfo.php"
2525
]
2626
},
2727
"config": {

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)