Skip to content

Commit a074315

Browse files
committed
WIP
Signed-off-by: alexmerlin <alex.merlin.1985@gmail.com>
1 parent ef182f5 commit a074315

42 files changed

Lines changed: 303 additions & 464 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/ColorEnum.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Dot\Maker;
66

7+
use function sprintf;
8+
79
enum ColorEnum: int
810
{
911
case Default = 0;
@@ -45,12 +47,21 @@ public static function colorize(
4547
self $foregroundColor = self::Default,
4648
self $backgroundColor = self::Default,
4749
): string {
50+
if ($backgroundColor->value !== self::Default->value) {
51+
return sprintf(
52+
"\033[%d;%dm%s\033[%dm",
53+
$foregroundColor->value,
54+
$backgroundColor->value,
55+
$text,
56+
self::Default->value
57+
);
58+
}
59+
4860
return sprintf(
49-
"\033[%d;%dm%s\033[%d;0m",
61+
"\033[%dm%s\033[%dm",
5062
$foregroundColor->value,
51-
$backgroundColor->value,
5263
$text,
53-
self::Default->value,
64+
self::Default->value
5465
);
5566
}
5667
}

src/IO/Input.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
use function strtolower;
1010
use function trim;
1111

12+
use const PHP_EOL;
1213
use const STDIN;
1314

1415
class Input
1516
{
1617
public static function prompt(string $prompt): string
1718
{
18-
echo $prompt;
19+
echo PHP_EOL . $prompt;
1920
return trim(fgets(STDIN));
2021
}
2122

src/IO/Output.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Dot\Maker\IO;
66

7+
use Dot\Maker\ColorEnum;
8+
79
use function fwrite;
810

911
use const PHP_EOL;
@@ -17,11 +19,35 @@ class Output
1719

1820
public static function error(string $message, bool $exit = false): void
1921
{
22+
$message = ColorEnum::colorize($message, ColorEnum::ForegroundBrightRed);
23+
2024
fwrite(STDERR, $message . PHP_EOL);
2125
$exit && exit(self::FAILURE);
2226
}
2327

2428
public static function info(string $message, bool $exit = false): void
29+
{
30+
$message = ColorEnum::colorize($message, ColorEnum::ForegroundBrightBlue);
31+
32+
fwrite(STDOUT, $message . PHP_EOL);
33+
$exit && exit(self::SUCCESS);
34+
}
35+
36+
public static function success(string $message, bool $exit = false): void
37+
{
38+
$message = ColorEnum::colorize($message, ColorEnum::ForegroundBrightGreen);
39+
40+
fwrite(STDOUT, $message . PHP_EOL);
41+
$exit && exit(self::SUCCESS);
42+
}
43+
44+
public static function write(string $message, bool $exit = false): void
45+
{
46+
fwrite(STDOUT, $message . PHP_EOL);
47+
$exit && exit(self::SUCCESS);
48+
}
49+
50+
public static function writeLine(string $message, bool $exit = false): void
2551
{
2652
fwrite(STDOUT, $message . PHP_EOL);
2753
$exit && exit(self::SUCCESS);

src/Type/AbstractType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function hasModule(): bool
5050
return $this->module instanceof Module;
5151
}
5252

53-
public function initComponent(TypeEnum $typeEnum): FileInterface
53+
public function component(TypeEnum $typeEnum): FileInterface
5454
{
5555
return new ($typeEnum->value)(
5656
$this->fileSystem,

src/Type/Collection.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,9 @@ public function create(string $name): File
5757

5858
$content = $this->render($collection->getComponent());
5959

60-
try {
61-
$collection->create($content);
62-
Output::info(sprintf('Created Collection "%s"', $collection->getPath()));
63-
} catch (RuntimeException $exception) {
64-
Output::error($exception->getMessage());
65-
}
60+
$collection->create($content);
61+
62+
Output::success(sprintf('Created Collection "%s"', $collection->getPath()));
6663

6764
return $collection;
6865
}

src/Type/Command.php

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@
2121
use Dot\Maker\VisibilityEnum;
2222
use Throwable;
2323

24-
use function array_shift;
25-
use function count;
26-
use function implode;
27-
use function preg_replace;
28-
use function preg_split;
2924
use function sprintf;
30-
use function strtolower;
25+
use function str_replace;
3126
use function ucfirst;
3227

3328
class Command extends AbstractType implements FileInterface
@@ -73,19 +68,16 @@ public function create(string $name): File
7368
$this->fileSystem->serviceInterface($this->fileSystem->getModuleName())->getComponent(),
7469
);
7570

76-
try {
77-
$command->create($content);
78-
Output::info(sprintf('Created Command "%s"', $command->getPath()));
79-
} catch (RuntimeException $exception) {
80-
Output::error($exception->getMessage());
81-
}
71+
$command->create($content);
72+
73+
Output::success(sprintf('Created Command "%s"', $command->getPath()));
8274

8375
return $command;
8476
}
8577

8678
public function render(Component $command, Component $serviceInterface): string
8779
{
88-
$defaultName = $this->getDefaultName($command->getClassName());
80+
$defaultName = $this->getDefaultName($command);
8981

9082
$class = (new ClassFile($command->getNamespace(), $command->getClassName()))
9183
->setExtends('Command')
@@ -145,17 +137,10 @@ public function render(Component $command, Component $serviceInterface): string
145137
return $class->render();
146138
}
147139

148-
public function getDefaultName(string $className): string
140+
public function getDefaultName(Component $command): string
149141
{
150-
$className = preg_replace('/Command$/', '', $className);
151-
152-
$parts = preg_split('/(?<!^)(?=[A-Z])/', $className);
153-
$module = array_shift($parts);
154-
155-
if (count($parts) === 0) {
156-
$parts[] = 'action';
157-
}
142+
$defaultName = sprintf('%s:command', $command->toKebabCase());
158143

159-
return strtolower(sprintf('%s:%s', $module, implode('-', $parts)));
144+
return str_replace('-command', '', $defaultName);
160145
}
161146
}

src/Type/ConfigProvider.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,9 @@ public function create(string $name): File
8383
);
8484
}
8585

86-
try {
87-
$configProvider->create($content);
88-
Output::info(sprintf('Created ConfigProvider "%s"', $configProvider->getPath()));
89-
} catch (RuntimeException $exception) {
90-
Output::error($exception->getMessage());
91-
}
86+
$configProvider->create($content);
87+
88+
Output::success(sprintf('Created ConfigProvider "%s"', $configProvider->getPath()));
9289

9390
return $configProvider;
9491
}

src/Type/CoreConfigProvider.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@ public function create(string $name): File
4545
$this->fileSystem->entity($name)->getComponent(),
4646
);
4747

48-
try {
49-
$configProvider->create($content);
50-
Output::info(sprintf('Created Core ConfigProvider "%s"', $configProvider->getPath()));
51-
} catch (RuntimeException $exception) {
52-
Output::error($exception->getMessage());
53-
}
48+
$configProvider->create($content);
49+
50+
Output::success(sprintf('Created Core ConfigProvider "%s"', $configProvider->getPath()));
5451

5552
return $configProvider;
5653
}

src/Type/Entity.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __invoke(): void
3434

3535
try {
3636
$this->create($name);
37-
$this->initComponent(TypeEnum::Repository)->create($name);
37+
$this->component(TypeEnum::Repository)->create($name);
3838
} catch (Throwable $exception) {
3939
Output::error($exception->getMessage());
4040
}
@@ -62,12 +62,9 @@ public function create(string $name): File
6262
$this->fileSystem->repository($name)->getComponent(),
6363
);
6464

65-
try {
66-
$entity->create($content);
67-
Output::info(sprintf('Created Entity "%s"', $entity->getPath()));
68-
} catch (RuntimeException $exception) {
69-
Output::error($exception->getMessage());
70-
}
65+
$entity->create($content);
66+
67+
Output::success(sprintf('Created Entity "%s"', $entity->getPath()));
7168

7269
return $entity;
7370
}
@@ -79,6 +76,7 @@ public function render(Component $entity, Component $repository): string
7976
->useClass($this->getAbstractEntityFqcn())
8077
->useClass($this->getTimestampsTraitFqcn())
8178
->useClass($repository->getFqcn())
79+
->useClass(Import::DATETIMEIMMUTABLE)
8280
->useClass(Import::DOCTRINE_ORM_MAPPING, 'ORM')
8381
->addInject(
8482
(new Inject('ORM\Entity'))->addArgument($repository->getClassString(), 'repositoryClass')
@@ -101,6 +99,15 @@ public function render(Component $entity, Component $repository): string
10199

102100
$getArrayCopy = (new Method('getArrayCopy'))
103101
->setReturnType('array')
102+
->setComment(<<<COMM
103+
/**
104+
* @return array{
105+
* uuid: non-empty-string,
106+
* created: DateTimeImmutable,
107+
* updated: DateTimeImmutable|null,
108+
* }
109+
*/
110+
COMM)
104111
->setBody(<<<BODY
105112
return [
106113
'uuid' => \$this->uuid->toString(),

src/Type/Form.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Dot\Maker\FileSystem\File;
99
use Dot\Maker\IO\Input;
1010
use Dot\Maker\IO\Output;
11+
use Throwable;
1112

1213
use function preg_replace;
1314
use function sprintf;
@@ -22,20 +23,15 @@ public function __invoke(): void
2223
return;
2324
}
2425

25-
while (true) {
26-
$name = ucfirst(Input::prompt('Enter new Form name: '));
27-
if ($name === '') {
28-
break;
29-
}
30-
31-
if (! $this->isValid($name)) {
32-
Output::error(sprintf('Invalid Form name: "%s"', $name));
33-
continue;
34-
}
26+
$name = ucfirst(Input::prompt('Enter new Form name: '));
27+
if ($name === '') {
28+
return;
29+
}
3530

31+
try {
3632
$this->create($name);
37-
38-
break;
33+
} catch (Throwable $exception) {
34+
Output::error($exception->getMessage());
3935
}
4036
}
4137

@@ -46,13 +42,13 @@ public function create(string $name): File
4642
if (! $this->context->isApi()) {
4743
$plural = Component::pluralize($name);
4844
if (Input::confirm(sprintf('Allow creating %s?', $plural))) {
49-
$this->initComponent(TypeEnum::FormCreateResource)->create($name);
45+
$this->component(TypeEnum::FormCreateResource)->create($name);
5046
}
5147
if (Input::confirm(sprintf('Allow deleting %s?', $plural))) {
52-
$this->initComponent(TypeEnum::FormDeleteResource)->create($name);
48+
$this->component(TypeEnum::FormDeleteResource)->create($name);
5349
}
5450
if (Input::confirm(sprintf('Allow editing %s?', $plural))) {
55-
$this->initComponent(TypeEnum::FormEditResource)->create($name);
51+
$this->component(TypeEnum::FormEditResource)->create($name);
5652
}
5753
}
5854

0 commit comments

Comments
 (0)