Skip to content

Commit d82f019

Browse files
committed
finish
1 parent 8f32220 commit d82f019

18 files changed

+453
-195
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"prefer-stable": true,
4545
"config": {
4646
"platform": {
47-
"php": "8.1"
47+
"php": "8.2"
4848
}
4949
},
5050
"funding": [

composer.lock

Lines changed: 168 additions & 179 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Mtrgen/Cli/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct()
2323
new ListTemplatesCommand(),
2424
new SaveBundleCommand(),
2525
new ValidateCommand(),
26+
new RepairCommand(),
2627
]);
2728
}
2829
}

src/Mtrgen/Cli/GenerateCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Matronator\Mtrgen\Template;
1010
use Matronator\Mtrgen\Template\ClassicGenerator;
1111
use Matronator\Mtrgen\Template\Generator;
12+
use Matronator\Mtrgen\Template\TemplateNotFoundException;
1213
use Matronator\Parsem\Parser;
1314
use Symfony\Component\Console\Input\InputArgument;
1415
use Symfony\Component\Console\Input\InputInterface;
@@ -56,7 +57,12 @@ public function execute(InputInterface $input, OutputInterface $output): int
5657
$path = $this->askPath($helper);
5758
}
5859

59-
$isLegacy = Template::isLegacy($path);
60+
try {
61+
$isLegacy = Template::isLegacy($path);
62+
} catch (TemplateNotFoundException $e) {
63+
$this->io->error($e->getMessage());
64+
return self::FAILURE;
65+
}
6066
$name = $isLegacy ? ClassicGenerator::getName($path) : Generator::getName($this->getTemplate($path));
6167

6268
if ($isLegacy) {

src/Mtrgen/Cli/RepairCommand.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Matronator\Mtrgen\Cli;
6+
7+
use Matronator\Mtrgen\Store\Storage;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
// #[AsCommand('generate:entity', 'Generates an Entity file', ['gen:entity'])]
12+
class RepairCommand extends BaseGeneratorCommand
13+
{
14+
protected static $defaultName = 'repair';
15+
protected static $defaultDescription = 'Repairs the local store.';
16+
17+
public function configure(): void
18+
{
19+
$this->setAliases(['r']);
20+
}
21+
22+
public function execute(InputInterface $input, OutputInterface $output): int
23+
{
24+
parent::execute($input, $output);
25+
26+
$storage = new Storage;
27+
$storage->repairStore();
28+
29+
$this->io->success('Local store repaired!');
30+
31+
return self::SUCCESS;
32+
}
33+
}

src/Mtrgen/Cli/SaveTemplateCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace Matronator\Mtrgen\Cli;
66

77
use Matronator\Mtrgen\Store\Storage;
8+
use Matronator\Mtrgen\Template;
89
use Matronator\Mtrgen\Template\ClassicGenerator;
10+
use Matronator\Mtrgen\Template\Generator;
911
use Symfony\Component\Console\Input\InputArgument;
1012
use Symfony\Component\Console\Input\InputInterface;
1113
use Symfony\Component\Console\Input\InputOption;
@@ -38,7 +40,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
3840

3941
$storage = new Storage;
4042
if ($storage->save($path, $alias)) {
41-
$name = $alias ?? ClassicGenerator::getName($path);
43+
$name = $alias ?? (Template::isLegacy($path) ? ClassicGenerator::getName($path) : Generator::getName(path: $path));
4244
$output->writeln("<fg=green>Template '$name' added from '$path'!</>");
4345
$this->io->newLine();
4446

src/Mtrgen/Cli/ValidateCommand.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
namespace Matronator\Mtrgen\Cli;
66

7-
use Matronator\Mtrgen\Store\Storage;
8-
use Matronator\Mtrgen\Template\ClassicGenerator;
97
use Matronator\Parsem\Parser;
108
use Symfony\Component\Console\Input\InputArgument;
119
use Symfony\Component\Console\Input\InputInterface;
12-
use Symfony\Component\Console\Input\InputOption;
1310
use Symfony\Component\Console\Output\OutputInterface;
1411

1512
// #[AsCommand('generate:entity', 'Generates an Entity file', ['gen:entity'])]

src/Mtrgen/Store/Storage.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace Matronator\Mtrgen\Store;
66

7+
use Matronator\Mtrgen\Template\Generator;
78
use InvalidArgumentException;
89
use Matronator\Mtrgen\ClassicFileGenerator;
10+
use Matronator\Mtrgen\Template;
911
use Matronator\Mtrgen\Template\ClassicGenerator;
1012
use Matronator\Parsem\Parser;
1113
use Nette\Neon\Neon;
@@ -53,12 +55,18 @@ public function save(string $filename, ?string $alias = null, ?string $bundle =
5355
if (!file_exists($file))
5456
return false;
5557

58+
if (Template::isLegacy($file)) {
59+
$name = ClassicGenerator::getName($file);
60+
} else {
61+
$name = Generator::getName(path: $file);
62+
}
63+
5664
$basename = $bundle ? $bundle . DIRECTORY_SEPARATOR . basename($file) : basename($file);
5765
if ($bundle && !ClassicFileGenerator::folderExist($this->templateDir . DIRECTORY_SEPARATOR . $bundle) && !mkdir($concurrentDirectory = $this->templateDir . DIRECTORY_SEPARATOR . $bundle, 0777, true) && !is_dir($concurrentDirectory)) {
5866
throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
5967
}
6068

61-
$this->saveEntry($alias ?? ($bundle ? "$bundle:" . ClassicGenerator::getName($file) : ClassicGenerator::getName($file)), $basename);
69+
$this->saveEntry($alias ?? ($bundle ? "$bundle:" . $name : $name), $basename);
6270
copy($file, Path::canonicalize($this->templateDir . DIRECTORY_SEPARATOR . $basename));
6371

6472
return true;
@@ -245,6 +253,19 @@ public function isBundle(string $filename): bool
245253
return (bool) preg_match('/^.+?(\.bundle\.).+?$/', $filename);
246254
}
247255

256+
public function repairStore(): void
257+
{
258+
$store = $this->loadStore();
259+
260+
foreach ($store->templates as $name => $filename) {
261+
if (!file_exists(Path::canonicalize($this->templateDir . DIRECTORY_SEPARATOR . $filename))) {
262+
unset($store->templates->{$name});
263+
}
264+
}
265+
266+
$this->saveStore($store);
267+
}
268+
248269
/**
249270
* Create and save the global store
250271
* @return void

src/Mtrgen/Template.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Matronator\Mtrgen;
66

7+
use Matronator\Mtrgen\Template\TemplateNotFoundException;
78
use Matronator\Parsem\Parser;
89

910
class Template
@@ -12,9 +13,16 @@ public static function isLegacy(string $path): bool
1213
{
1314
$extension = pathinfo($path, PATHINFO_EXTENSION);
1415
if (in_array($extension, ['json', 'yaml', 'neon'])) {
15-
if (Parser::isValid($path, file_get_contents($path))) {
16+
if (!is_readable($path)) {
17+
throw new TemplateNotFoundException($path);
18+
}
19+
$contents = file_get_contents($path);
20+
if (!$contents) {
21+
throw new TemplateNotFoundException($path);
22+
}
23+
if (Parser::isValid($path, $contents)) {
1624
return true;
17-
} else if (Parser::isValidBundle($path, file_get_contents($path))) {
25+
} else if (Parser::isValidBundle($path, $contents)) {
1826
return true;
1927
}
2028
}

src/Mtrgen/Template/ClassicGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
use Nette\PhpGenerator\Property;
1616
use Nette\PhpGenerator\TraitType;
1717

18+
/**
19+
* @deprecated version 2.0.0
20+
* @see \Matronator\Mtrgen\Template\Generator
21+
*/
1822
class ClassicGenerator
1923
{
2024
public const RESERVED_KEYWORDS = ['__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'finally', 'fn', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'match', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'readonly', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor', 'yield', 'yield_from'];

0 commit comments

Comments
 (0)