Skip to content

Commit 80010d4

Browse files
committed
logic file building, cleanup/adjustments
1 parent 15cd545 commit 80010d4

File tree

6 files changed

+175
-23
lines changed

6 files changed

+175
-23
lines changed

src/App/Actions/Create_Controller_Action.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Clyde\Actions\Action_Base;
77
use Clyde\Request\Request;
88
use Clyde\Request\Request_Response;
9-
use GimliDev\Builders\Controller_Builder;
9+
use GimliDev\Builders\File_Builder;
1010

1111
use function GimliDev\load_config;
1212
use function Laravel\Prompts\confirm;
@@ -96,9 +96,9 @@ protected function buildSingleAction(Request $Request): Request_Response {
9696
$extends_path = text(label: 'What is the namespace of the class you would like to extend?', placeholder: $this->controller_config['extends']);
9797
}
9898

99-
$controller_builder = new Controller_Builder($controller_name, $namespace, $extends_path);
100-
$controller_builder->addUseStatements([self::RESPONSE_NAMESPACE, self::LATTE_ENGINE]);
101-
$controller_builder->addMethods([
99+
$File_Builder = new File_Builder($controller_name, $namespace, $extends_path);
100+
$File_Builder->addUseStatements([self::RESPONSE_NAMESPACE, self::LATTE_ENGINE]);
101+
$File_Builder->addMethods([
102102
[
103103
'name' => '__construct',
104104
'return' => null,
@@ -126,7 +126,7 @@ protected function buildSingleAction(Request $Request): Request_Response {
126126
]
127127
]);
128128

129-
$controller = $controller_builder->getClass();
129+
$controller = $File_Builder->getClass();
130130

131131
file_put_contents($controller_path, $controller);
132132

@@ -161,9 +161,9 @@ protected function buildResourceController(Request $Request): Request_Response {
161161
$extends_path = text(label: 'What is the namespace of the class you would like to extend?', placeholder: $this->controller_config['extends']);
162162
}
163163

164-
$controller_builder = new Controller_Builder($controller_name, $namespace, $extends_path);
165-
$controller_builder->addUseStatements([self::RESPONSE_NAMESPACE, self::LATTE_ENGINE, self::REQUEST_NAMESPACE]);
166-
$controller_builder->addMethods([
164+
$File_Builder = new File_Builder($controller_name, $namespace, $extends_path);
165+
$File_Builder->addUseStatements([self::RESPONSE_NAMESPACE, self::LATTE_ENGINE, self::REQUEST_NAMESPACE]);
166+
$File_Builder->addMethods([
167167
[
168168
'name' => '__construct',
169169
'return' => null,
@@ -324,7 +324,7 @@ protected function buildResourceController(Request $Request): Request_Response {
324324
],
325325
]);
326326

327-
$controller = $controller_builder->getClass();
327+
$controller = $File_Builder->getClass();
328328

329329
file_put_contents($controller_path, $controller);
330330

@@ -359,9 +359,9 @@ protected function buildEmptyController(Request $Request): Request_Response {
359359
$extends_path = text(label: 'What is the namespace of the class you would like to extend?', placeholder: $this->controller_config['extends']);
360360
}
361361

362-
$controller_builder = new Controller_Builder($controller_name, $namespace, $extends_path);
363-
$controller_builder->addUseStatements([self::RESPONSE_NAMESPACE, self::LATTE_ENGINE, self::REQUEST_NAMESPACE]);
364-
$controller_builder->addMethods([
362+
$File_Builder = new File_Builder($controller_name, $namespace, $extends_path);
363+
$File_Builder->addUseStatements([self::RESPONSE_NAMESPACE, self::LATTE_ENGINE, self::REQUEST_NAMESPACE]);
364+
$File_Builder->addMethods([
365365
[
366366
'name' => '__construct',
367367
'return' => null,
@@ -376,7 +376,7 @@ protected function buildEmptyController(Request $Request): Request_Response {
376376
]
377377
]);
378378

379-
$controller = $controller_builder->getClass();
379+
$controller = $File_Builder->getClass();
380380

381381
file_put_contents($controller_path, $controller);
382382

src/App/Actions/Create_Init_Action.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ public function execute(Request $Request): Request_Response {
6565
];
6666
}
6767

68+
$logic_defaults = select("Would you like to set logic file defaults?", ["Yes", "No"]);
69+
70+
if ($logic_defaults === "Yes") {
71+
$devtools_config["logic"] = [
72+
'default_save_path' => text(label: 'What is the default save path for logic files?', default: 'App/Logic'),
73+
'namespace' => text(label: 'What is the default namespace for logic files?', default: 'App\Logic'),
74+
'extends' => text(label: 'What is the default class to extend for logic files?') ?? "",
75+
];
76+
}
77+
6878
if ($force_overwrite) {
6979
$continue = confirm("Would you like to continue? Doing so will overwrite your existing configuration file.");
7080
if (!$continue) {
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace GimliDev\Actions;
5+
6+
use Clyde\Actions\Action_Base;
7+
use Clyde\Request\Request;
8+
use Clyde\Request\Request_Response;
9+
use GimliDev\Builders\File_Builder;
10+
11+
use function GimliDev\load_config;
12+
use function Laravel\Prompts\confirm;
13+
use function Laravel\Prompts\text;
14+
15+
class Create_Logic_Action extends Action_Base
16+
{
17+
18+
/**
19+
* @var array
20+
*/
21+
protected array $logic_config;
22+
23+
/**
24+
* Execute the action
25+
*
26+
* @param Request $Request
27+
* @return Request_Response
28+
*/
29+
public function execute(Request $Request): Request_Response {
30+
$config = load_config();
31+
$namespace = $Request->getArgument('namespace') ?? '';
32+
33+
$this->logic_config = $config['logic'] ?? [
34+
'default_save_path' => 'App/Logic',
35+
];
36+
37+
[$logic_name, $logic_path] = $this->formatPath($Request);
38+
39+
if (empty($namespace)) {
40+
$namespace = text('Enter the namespace for the logic file', default: $config['namespace']);
41+
}
42+
43+
44+
if (file_exists($logic_path)) {
45+
$this->Printer->error("The logic file already exists at {$logic_path}");
46+
exit(1);
47+
}
48+
49+
$extends = confirm('Would you like to extend a base logic file or class?');
50+
$extends_path = $config['extends'] ?? '';
51+
if ($extends) {
52+
$extends_path = text(label: 'What is the namespace of the class you would like to extend?', default: $extends_path);
53+
}
54+
55+
$File_Builder = new File_Builder($logic_name, $namespace, $extends_path);
56+
$File_Builder->addUseStatements(["Gimli\Application"]);
57+
$File_Builder->addMethods([
58+
[
59+
'name' => '__construct',
60+
'return' => null,
61+
'params' => [
62+
[
63+
'name' => 'Application',
64+
'type' => 'Gimli\Application',
65+
'type_short' => 'Application',
66+
'comment' => 'Application instance',
67+
]
68+
]
69+
],
70+
]);
71+
$logic_class_output = $File_Builder->getClass();
72+
73+
file_put_contents($logic_path, $logic_class_output);
74+
75+
$this->Printer->success("Logic file created at $logic_path");
76+
77+
return new Request_Response(true);
78+
79+
}
80+
81+
/**
82+
* Format the path for the logic file
83+
*
84+
* @param Request $Request
85+
* @return array
86+
*/
87+
protected function formatPath(Request $Request): array {
88+
$logic_name = $Request->getArgument('logic_name');
89+
$path = $Request->getArgument('save_path') ?? '';
90+
91+
if (empty($path)) {
92+
$path = text(label: 'Where would you like to save the logic file?', default: $this->logic_config['default_save_path']);
93+
}
94+
95+
$path = $path[0] !== '/' ? '/' . $path : $path;
96+
$path = $path[strlen($path) - 1] !== '/' ? $path . '/' : $path;
97+
$logic_path = ROOT . $path . $logic_name . '.php';
98+
99+
return [$logic_name, $logic_path];
100+
}
101+
}

src/App/Builders/Controller_Builder.php renamed to src/App/Builders/File_Builder.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Nette\PhpGenerator\PhpNamespace;
99
use Nette\PhpGenerator\Printer;
1010

11-
class Controller_Builder {
11+
class File_Builder {
1212

1313
protected $Controller_Name;
1414
protected $Namespace;
@@ -95,10 +95,4 @@ public function getClass(): string {
9595
$file->addNamespace($this->Class_File);
9696
return $p->printFile($file);
9797
}
98-
}
99-
100-
// $controller = new Controller_Builder('Some_Class', 'Controllers', 'Shared\Controller_Helper');
101-
// $controller->addUseStatements(['Shared\Controller_Helper', 'Shared\Response', 'Shared\Request']);
102-
// $controller->addMethods(['index', 'create', 'update', 'delete']);
103-
// echo $controller->getClass();
104-
// file_put_contents('Demo.php', $controller->getClass());
98+
}

src/App/Login_Logic.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* Made with Gimli Devtools
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace App\Logic;
10+
11+
use Gimli\Application;
12+
13+
class Login_Logic
14+
{
15+
/**
16+
* __construct
17+
*
18+
* @param Application $Application Application instance
19+
*/
20+
public function __construct(
21+
public Application $Application,
22+
) {
23+
}
24+
}

src/gimli

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use Clyde\Args\Arg_Option;
1515
use Clyde\Commands\Command;
1616
use Clyde\Tools\Emoji;
1717
use GimliDev\Actions\Create_Controller_Action;
18+
use GimliDev\Actions\Create_Logic_Action;
1819

1920
define('ROOT', getcwd());
2021
define('APP_ROOT', __DIR__);
@@ -82,6 +83,30 @@ Application::create('gimli')
8283
->save()
8384
)
8485

86+
// Create a new logic file
87+
88+
->command(
89+
Command::create('logic <logic_name>')
90+
->about('Create a new logic file')
91+
->action(Create_Logic_Action::class)
92+
->arg(
93+
Arg_Option::create('namespace')
94+
->help('Set the namespace for the logic file, remember to double escape \\')
95+
->shortName('n')
96+
->longName('namespace')
97+
->save()
98+
)
99+
->arg(
100+
Arg_Option::create('save_path')
101+
->help('Set the save path for the logic file')
102+
->shortName('p')
103+
->longName('save_path')
104+
->save()
105+
)
106+
->save()
107+
108+
)
109+
85110
// TODO:
86111

87112

@@ -91,8 +116,6 @@ Application::create('gimli')
91116

92117
// Create a new middleware
93118

94-
// Create a new logic
95-
96119
// Create a migration
97120

98121
// Create a route file

0 commit comments

Comments
 (0)