Skip to content

Commit 3afa884

Browse files
committed
Added build and compile commands
1 parent ba69941 commit 3afa884

File tree

10 files changed

+341
-9
lines changed

10 files changed

+341
-9
lines changed
File renamed without changes.
File renamed without changes.

app/Building/FieldBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function setNotFilterDefault(): FieldBuilder {
134134
throw new \RuntimeException('Field is already setted not filters default');
135135
}
136136

137-
$this->filterDefault = $filterDefault;
137+
$this->filterDefault = false;
138138
return $this;
139139
}
140140

app/Command/BuildCommand.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/* MIT License
4+
5+
Copyright (c) 2018 Eridan Domoratskiy
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE. */
24+
25+
namespace PHPDataGen\Command;
26+
27+
use Symfony\Component\Console\Input\InputArgument;
28+
use Symfony\Component\Console\Input\InputInterface;
29+
30+
use Symfony\Component\Console\Output\OutputInterface;
31+
32+
use Symfony\Component\Console\Style\SymfonyStyle;
33+
34+
use Symfony\Component\Finder\Finder;
35+
36+
/**
37+
* Build command
38+
*/
39+
class BuildCommand extends CompileCommand {
40+
41+
protected function configure() {
42+
$this->
43+
setName('build')->
44+
setDescription('Runs full project building')->
45+
setHelp("Reads config file from project-dir (if exists).\nScans project directory for files with extension .pdata and compiles it.")->
46+
47+
addArgument('project-dir', InputArgument::OPTIONAL, 'Path to project directory (default is current directory)');
48+
}
49+
50+
protected function execute(InputInterface $input, OutputInterface $output) {
51+
$io = new SymfonyStyle($input, $output);
52+
53+
$projectDir = $input->getArgument('project-dir');
54+
if (!$projectDir) {
55+
$projectDir = getcwd();
56+
} else {
57+
$projectDir = rtrim($projectDir, '/');
58+
}
59+
$projectDir .= '/';
60+
61+
if (!file_exists($projectDir)) {
62+
$io->error('Project directory is not exists');
63+
64+
return;
65+
}
66+
67+
if (!is_dir($projectDir)) {
68+
$io->error('Project directory is not directory');
69+
$io->note('For single file compiling use "compile" command');
70+
71+
return;
72+
}
73+
74+
// TODO Config file reading
75+
76+
$finder = new Finder();
77+
$finder->
78+
files()->
79+
in($projectDir)->
80+
name('*.pdata');
81+
82+
foreach ($finder as $file) {
83+
$this->compileFile($file, $io);
84+
}
85+
}
86+
}

app/Command/CompileCommand.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
/* MIT License
4+
5+
Copyright (c) 2018 Eridan Domoratskiy
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE. */
24+
25+
namespace PHPDataGen\Command;
26+
27+
use Symfony\Component\Console\Command\Command;
28+
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Input\InputArgument;
31+
use Symfony\Component\Console\Input\InputOption;
32+
33+
use Symfony\Component\Console\Output\OutputInterface;
34+
35+
use Symfony\Component\Console\Style\SymfonyStyle;
36+
use Symfony\Component\Console\Style\OutputStyle;
37+
38+
use PHPDataGen\Exception\ParsingException;
39+
40+
use PHPDataGen\Parsing\Conveyor;
41+
use PHPDataGen\Parsing\Parser;
42+
43+
use PHPDataGen\Compiler;
44+
45+
use function PHPDataGen\Utility\normalizePath;
46+
47+
/**
48+
* Compile command
49+
*/
50+
class CompileCommand extends Command {
51+
52+
// TODO Config
53+
protected function compileFile(string $file, OutputStyle $io, Compiler $compiler = null) {
54+
$io->title($file);
55+
56+
if (preg_match('/.*\\.pdata/i', $file) === 0) {
57+
$io->warning("File \"$file\" extension is not \".pdata\"");
58+
}
59+
60+
$parser = new Parser(new Conveyor(file_get_contents($file)));
61+
62+
try {
63+
$parser->parse();
64+
} catch (ParsingException $e) {
65+
$io->error("Parsing error: {$e->getMessage()}");
66+
return;
67+
}
68+
69+
if (is_null($compiler)) {
70+
$compiler = new Compiler();
71+
}
72+
73+
$model = $parser->getCurrentState()->getBuilder()->build();
74+
if (count($model->classes) > 1) {
75+
$io->warning("File has more than one class");
76+
} 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+
}
82+
}
83+
84+
$result = '';
85+
try {
86+
$result = $compiler->compile($model);
87+
} catch (CompilationException $e) {
88+
$io->error("Compilation error: \"{$e->getMessage()}\"");
89+
return;
90+
}
91+
92+
file_put_contents(dirname($file).'/Data_'.basename($file, '.pdata').'.php', $result);
93+
}
94+
95+
protected function configure() {
96+
$this->
97+
setName('compile')->
98+
setDescription('Compiles specified file')->
99+
setHelp('Compiles specified files')->
100+
101+
addArgument('files', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Paths to files')->
102+
103+
addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Config file', null);
104+
}
105+
106+
protected function execute(InputInterface $input, OutputInterface $output) {
107+
$io = new SymfonyStyle($input, $output);
108+
109+
// TODO Config file reading
110+
111+
var_dump($input->getArgument('files'));
112+
113+
foreach ($input->getArgument('files') as $file) {
114+
if (!file_exists($file)) {
115+
$io->error("File \"$file\" is not exists. Skipping");
116+
continue;
117+
}
118+
119+
if (!is_file($file)) {
120+
$io->error("File \"$file\" is not file. Skipping");
121+
continue;
122+
}
123+
124+
$this->compileFile($file, $io);
125+
}
126+
}
127+
}

app/Parsing/FieldState.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function step(Conveyor $conveyor): State {
7272
switch ($this->state) {
7373
case 0:
7474
if ($conveyor->readOperator('direct')) {
75-
$this->builder->setDirect(true);
75+
$this->builder->setDirect();
7676

7777
return $this;
7878
}
@@ -88,7 +88,7 @@ public function step(Conveyor $conveyor): State {
8888
}
8989

9090
if ($conveyor->readOperator('var')) {
91-
$this->builder->setEditable(true);
91+
$this->builder->setEditable();
9292

9393
return $this;
9494
}
@@ -108,7 +108,7 @@ public function step(Conveyor $conveyor): State {
108108
}
109109

110110
if ($conveyor->readOperator(':=')) {
111-
$this->builder->setFilterDefault(false);
111+
$this->builder->setNotFilterDefault();
112112

113113
$this->state = 7;
114114
return $this;

app/Utility/normalizePath.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/* MIT License
4+
5+
Copyright (c) 2018 Eridan Domoratskiy
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE. */
24+
25+
namespace PHPDataGen\Utility;
26+
27+
/**
28+
* Path normalize function
29+
*
30+
* @link http://php.net/manual/function.realpath.php#112367
31+
*
32+
* @param string $path
33+
*
34+
* @return string Normalized path
35+
*/
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
41+
42+
$test = ''; // Initialize testing variable
43+
foreach ($segments as $segment) {
44+
if ($segment != '.') {
45+
$test = array_pop($parts);
46+
47+
if (is_null($test)) {
48+
$parts[] = $segment;
49+
} else if ($segment == '..') {
50+
if ($test == '..') {
51+
$parts[] = $test;
52+
}
53+
54+
if ($test == '..' || $test == '') {
55+
$parts[] = $segment;
56+
}
57+
} else {
58+
$parts[] = $test;
59+
$parts[] = $segment;
60+
}
61+
}
62+
}
63+
64+
return implode('/', $parts);
65+
}

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
"minimum-stability": "dev",
1414
"prefer-stable": true,
1515
"require": {
16+
"progminer/php-datagen-lib": "dev-master",
1617
"symfony/console": "^4.0",
17-
"progminer/php-datagen-lib": "dev-master"
18+
"symfony/finder": "^4.1"
1819
},
1920
"repositories": [
2021
{
@@ -25,7 +26,10 @@
2526
"autoload": {
2627
"psr-4": {
2728
"PHPDataGen\\": "app"
28-
}
29+
},
30+
"files": [
31+
"app/Utility/normalizePath.php"
32+
]
2933
},
3034
"config": {
3135
"optimize-autoloader": true,

0 commit comments

Comments
 (0)