1: <?php
2:
3: namespace LaravelUi5\Core\Commands;
4:
5: use Exception;
6: use Illuminate\Filesystem\Filesystem;
7: use Illuminate\Support\Facades\File;
8: use Illuminate\Support\Str;
9:
10: class GenerateSelfContainedUi5AppCommand extends BaseGenerator
11: {
12:
13: protected $signature = 'ui5:sca
14: {name : The name of the ui5 app}
15: {--package-prefix=pragmatiqu : The composer package namespace prefix}
16: {--php-ns-prefix=Pragmatiqu : Root namespace prefix for PHP classes}
17: {--js-ns-prefix=io.pragmatiqu : Root namespace prefix for JS artifacts}
18: {--title= : The title of the app}
19: {--description= : The description of the app}
20: {--vendor="Pragmatiqu IT GmbH" : The vendor of the module}';
21:
22: protected Filesystem $files;
23:
24: public function __construct(Filesystem $files)
25: {
26: parent::__construct();
27: $this->files = $files;
28: }
29:
30: /**
31: * @throws Exception
32: */
33: public function handle(): int
34: {
35: $name = $this->argument('name');
36: $this->assertCamelCase('Ui5App', $name);
37:
38: $phpPrefix = rtrim($this->option('php-ns-prefix'), '\\');
39: $jsPrefix = rtrim($this->option('js-ns-prefix'), '.');
40: $packagePrefix = rtrim($this->option('package-prefix'), '/');
41: $urlKey = Str::snake($name);
42: $ui5Namespace = implode('.', [$jsPrefix, $urlKey]);
43: $phpNamespace = "{$phpPrefix}\\{$name}";
44: $className = "{$name}App";
45: $moduleName = "{$name}Module";
46: $title = $this->option('title') ?? $name;
47: $description = $this->option('description') ?? 'Ui5App generated via ui5:sca';
48:
49: $targetPath = base_path("ui5/{$name}");
50:
51: $targetFile = "{$targetPath}/src/{$className}.php";
52: if (File::exists($targetFile)) {
53: $this->components->error("Ui5App {$name} already exists.");
54: return self::FAILURE;
55: }
56:
57: File::ensureDirectoryExists("{$targetPath}/src/");
58:
59: $this->files->put("{$targetPath}/src/{$moduleName}.php", $this->compileStub('Ui5ModuleApp.stub', [
60: 'phpNamespace' => $phpNamespace,
61: 'class' => $className,
62: 'moduleClass' => $moduleName,
63: 'srcPath' => "{$targetPath}/resources/app"
64: ]));
65:
66: // Create Ui5App
67: $this->files->put($targetFile, $this->compileStub('Ui5AppSelfContained.stub', [
68: 'namespace' => $phpNamespace,
69: 'class' => $className,
70: 'name' => $name,
71: 'ui5Namespace' => $ui5Namespace,
72: 'urlKey' => $urlKey,
73: 'title' => $title,
74: 'description' => $description,
75: 'component' => str_replace('.', '/', $ui5Namespace)
76: ]));
77:
78: // Manifest
79: $this->files->put("{$targetPath}/src/{$name}Manifest.php", $this->compileStub('Ui5Manifest.stub', [
80: 'phpNamespace' => $phpNamespace,
81: 'class' => $name
82: ]));
83:
84: // composer.json
85: $this->files->put("{$targetPath}/composer.json", $this->compileStub('composer.stub', [
86: 'packagePrefix' => $packagePrefix,
87: 'urlKey' => $urlKey,
88: 'description' => $description,
89: 'namespace' => json_encode($phpNamespace),
90: ]));
91:
92: // ServiceProvider
93: $this->files->put("{$targetPath}/src/{$name}ServiceProvider.php", $this->compileStub('ServiceProvider.stub', [
94: 'namespace' => $phpNamespace,
95: 'name' => $name,
96: ]));
97:
98: File::ensureDirectoryExists("{$targetPath}/resources/app/controller");
99: File::ensureDirectoryExists("{$targetPath}/resources/app/i18n");
100: File::ensureDirectoryExists("{$targetPath}/resources/app/view");
101:
102: $this->files->put("{$targetPath}/resources/app/manifest.json", $this->compileStub('manifest.stub', [
103: 'ui5Namespace' => $ui5Namespace,
104: 'title' => $title,
105: 'description' => $description,
106: 'version' => '1.0.0',
107: ]));
108: $this->files->put("{$targetPath}/resources/app/Component.js", $this->compileStub('Component.stub', [
109: 'ui5Namespace' => $ui5Namespace,
110: ]));
111: $this->files->put("{$targetPath}/resources/app/controller/App.controller.js", $this->compileStub('App.controller.stub', [
112: 'ui5Namespace' => $ui5Namespace,
113: ]));
114: $this->files->put("{$targetPath}/resources/app/view/App.view.xml", $this->compileStub('App.view.stub', [
115: 'ui5Namespace' => $ui5Namespace,
116: ]));
117: $this->files->put("{$targetPath}/resources/app/i18n/i18n.properties", $this->compileStub('i18n.stub', [
118: 'title' => $title,
119: 'description' => $description,
120: ]));
121: $this->files->put("{$targetPath}/resources/app/i18n/i18n_en.properties", $this->compileStub('i18n.stub', [
122: 'title' => $title,
123: 'description' => $description,
124: ]));
125:
126: $this->components->success("UI5App '$name' created successfully.");
127: return self::SUCCESS;
128: }
129: }
130: