Skip to content

Commit 9b27e01

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

36 files changed

Lines changed: 3154 additions & 162 deletions

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ parameters:
66
- src
77
- test
88
treatPhpDocTypesAsCertain: false
9+
ignoreErrors:
10+
-
11+
message: '#Call to an undefined method org\\bovigo\\vfs\\vfsStreamContent::getChild\(\).#'
12+
path: %currentWorkingDirectory%/test/FileSystem/FileTest.php

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<testsuites>
77
<testsuite name="dot-maker test suite">
88
<directory>./test</directory>
9+
<exclude>./test/Helper.php</exclude>
910
</testsuite>
1011
</testsuites>
1112
<coverage/>

src/FileSystem/File.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ public function create(string $data): void
3636
{
3737
$this->ensureParentDirectoryExists();
3838

39-
$created = file_put_contents($this->path, $data);
40-
41-
if ($created === false) {
39+
if (! $this->write($data)) {
4240
throw new RuntimeException(
4341
sprintf('Could not create file "%s"', $this->path)
4442
);
@@ -90,4 +88,9 @@ public function read(): false|string
9088
{
9189
return file_get_contents($this->path);
9290
}
91+
92+
public function write(string $data): bool
93+
{
94+
return (bool) file_put_contents($this->path, $data);
95+
}
9396
}

src/IO/Input.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Dot\Maker\IO;
66

77
use function fgets;
8+
use function is_resource;
89
use function sprintf;
910
use function strtolower;
1011
use function trim;
@@ -45,6 +46,18 @@ public static function confirm(string $prompt, string $default = 'yes'): bool
4546
}
4647
}
4748

49+
/**
50+
* @return resource
51+
*/
52+
public static function getStream()
53+
{
54+
if (! is_resource(self::$stream)) {
55+
self::$stream = STDIN;
56+
}
57+
58+
return self::$stream;
59+
}
60+
4861
/**
4962
* @param resource $stream
5063
*/

src/IO/Output.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Dot\Maker\ColorEnum;
88

99
use function fwrite;
10+
use function is_resource;
1011

1112
use const PHP_EOL;
1213
use const STDERR;
@@ -66,6 +67,18 @@ public static function writeLine(string $message = '', bool $exit = false): void
6667
$exit && exit(self::SUCCESS);
6768
}
6869

70+
/**
71+
* @return resource
72+
*/
73+
public static function getErrorStream()
74+
{
75+
if (! is_resource(self::$errorStream)) {
76+
self::$errorStream = STDERR;
77+
}
78+
79+
return self::$errorStream;
80+
}
81+
6982
/**
7083
* @param resource $stream
7184
*/
@@ -74,6 +87,18 @@ public static function setErrorStream($stream): void
7487
self::$errorStream = $stream;
7588
}
7689

90+
/**
91+
* @return resource
92+
*/
93+
public static function getOutputStream()
94+
{
95+
if (! is_resource(self::$outputStream)) {
96+
self::$outputStream = STDOUT;
97+
}
98+
99+
return self::$outputStream;
100+
}
101+
77102
/**
78103
* @param resource $stream
79104
*/

src/Maker.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
use const PHP_SAPI;
2424

25-
final class Maker
25+
class Maker
2626
{
2727
public function __construct(
2828
private readonly string $projectPath,
@@ -32,7 +32,7 @@ public function __construct(
3232
public function __invoke(array $arguments): int
3333
{
3434
try {
35-
if (PHP_SAPI !== 'cli') {
35+
if (! $this->isCli()) {
3636
throw new RuntimeException('dot-maker must be run in CLI only');
3737
}
3838

@@ -52,7 +52,7 @@ public function __invoke(array $arguments): int
5252

5353
if ($instance instanceof Help) {
5454
$instance();
55-
exit;
55+
return Output::SUCCESS;
5656
}
5757

5858
Output::info(sprintf('Detected project type: %s', $context->getProjectType()));
@@ -74,4 +74,9 @@ public function __invoke(array $arguments): int
7474
return Output::FAILURE;
7575
}
7676
}
77+
78+
public function isCli(): bool
79+
{
80+
return PHP_SAPI === 'cli';
81+
}
7782
}

src/Type/AbstractType.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,27 @@ public function isValid(string $name): bool
7878
return (bool) preg_match('/^[a-z0-9]+$/i', $name);
7979
}
8080

81+
public function setConfig(Config $config): static
82+
{
83+
$this->config = $config;
84+
85+
return $this;
86+
}
87+
88+
public function setContext(Context $context): static
89+
{
90+
$this->context = $context;
91+
92+
return $this;
93+
}
94+
95+
public function setFileSystem(FileSystem $fileSystem): static
96+
{
97+
$this->fileSystem = $fileSystem;
98+
99+
return $this;
100+
}
101+
81102
public function setModule(?ModuleInterface $module): static
82103
{
83104
$this->module = $module;

src/Type/Form.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,26 @@ public function create(string $name): File
4646
{
4747
$name = preg_replace('/Form$/', '', $name);
4848

49-
if (! $this->context->isApi()) {
50-
$plural = Component::pluralize($name);
51-
if (Input::confirm(sprintf('Allow creating %s?', $plural))) {
52-
$this->component(TypeEnum::FormCreateResource)->create($name);
53-
$this->component(TypeEnum::InputFilterCreateResource)->create($name);
54-
}
55-
if (Input::confirm(sprintf('Allow deleting %s?', $plural))) {
56-
$this->component(TypeEnum::FormDeleteResource)->create($name);
57-
$this->component(TypeEnum::InputFilterDeleteResource)->create($name);
58-
$this->component(TypeEnum::InputConfirmDelete)->create($name);
59-
}
60-
if (Input::confirm(sprintf('Allow editing %s?', $plural))) {
61-
$this->component(TypeEnum::FormEditResource)->create($name);
62-
$this->component(TypeEnum::InputFilterEditResource)->create($name);
63-
}
49+
$form = $this->fileSystem->form($name);
50+
if ($this->context->isApi()) {
51+
return $form;
52+
}
53+
54+
$plural = Component::pluralize($name);
55+
if (Input::confirm(sprintf('Allow creating %s?', $plural))) {
56+
$this->component(TypeEnum::FormCreateResource)->create($name);
57+
$this->component(TypeEnum::InputFilterCreateResource)->create($name);
58+
}
59+
if (Input::confirm(sprintf('Allow deleting %s?', $plural))) {
60+
$this->component(TypeEnum::FormDeleteResource)->create($name);
61+
$this->component(TypeEnum::InputFilterDeleteResource)->create($name);
62+
$this->component(TypeEnum::InputConfirmDelete)->create($name);
63+
}
64+
if (Input::confirm(sprintf('Allow editing %s?', $plural))) {
65+
$this->component(TypeEnum::FormEditResource)->create($name);
66+
$this->component(TypeEnum::InputFilterEditResource)->create($name);
6467
}
6568

66-
return $this->fileSystem->form($name);
69+
return $form;
6770
}
6871
}

src/Type/Form/CreateResourceForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function render(Component $form, Component $entity, Component $inputFilte
102102
// add more form elements
103103
104104
\$this->add(
105-
(new Csrf('{$entity->toCamelCase()}CreateCsrf'))
105+
(new Csrf('create{$entity->getClassName()}Csrf'))
106106
->setOptions([
107107
'csrf_options' => ['timeout' => 3600, 'session' => new Container()],
108108
])

src/Type/Form/DeleteResourceForm.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public function render(Component $form, Component $entity, Component $inputFilte
100100

101101
)
102102
->setBody(<<<BODY
103+
// add more form elements
104+
103105
\$this->add(
104106
(new Checkbox('confirmation'))
105107
->setCheckedValue('yes')
@@ -110,7 +112,7 @@ public function render(Component $form, Component $entity, Component $inputFilte
110112
->setValue('no')
111113
);
112114
\$this->add(
113-
(new Csrf('{$entity->toCamelCase()}DeleteCsrf'))
115+
(new Csrf('delete{$entity->getClassName()}Csrf'))
114116
->setOptions([
115117
'csrf_options' => ['timeout' => 3600, 'session' => new Container()],
116118
])

0 commit comments

Comments
 (0)