2
2
3
3
namespace Builder ;
4
4
5
- use ByJG \Util \JwtWrapper ;
5
+ use ByJG \AnyDataset \Db \Factory ;
6
+ use ByJG \JwtWrapper \JwtWrapper ;
6
7
use ByJG \Util \Uri ;
7
8
use Composer \Script \Event ;
9
+ use Exception ;
8
10
use RecursiveCallbackFilterIterator ;
9
11
use RecursiveDirectoryIterator ;
10
12
use RecursiveIteratorIterator ;
11
13
12
14
class PostCreateScript
13
15
{
14
- public function execute ($ workdir , $ namespace , $ composerName , $ phpVersion , $ mysqlConnection , $ timezone )
16
+ public function execute ($ workdir , $ namespace , $ composerName , $ phpVersion , $ mysqlConnection , $ timezone ): void
15
17
{
16
18
// ------------------------------------------------
17
- // Defining function to interatively walking through the directories
19
+ // Defining function to interactively walking through the directories
18
20
$ directory = new RecursiveDirectoryIterator ($ workdir );
19
21
$ filter = new RecursiveCallbackFilterIterator ($ directory , function ($ current/*, $key, $iterator*/ ) {
20
22
// Skip hidden files and directories.
@@ -50,8 +52,8 @@ public function execute($workdir, $namespace, $composerName, $phpVersion, $mysql
50
52
foreach ($ files as $ file ) {
51
53
$ contents = file_get_contents ("$ workdir/ $ file " );
52
54
$ contents = str_replace ('ENV TZ=UTC ' , "ENV TZ= $ timezone " , $ contents );
53
- $ contents = str_replace ('php:8.1 -fpm ' , "php: $ phpVersion-fpm " , $ contents );
54
- $ contents = str_replace ('php81 ' , "php $ phpVersionMSimple " , $ contents );
55
+ $ contents = str_replace ('php:8.3 -fpm ' , "php: $ phpVersion-fpm " , $ contents );
56
+ $ contents = str_replace ('php83 ' , "php $ phpVersionMSimple " , $ contents );
55
57
file_put_contents (
56
58
"$ workdir/ $ file " ,
57
59
$ contents
@@ -66,7 +68,6 @@ public function execute($workdir, $namespace, $composerName, $phpVersion, $mysql
66
68
'config/config-prod.php ' ,
67
69
'config/config-test.php ' ,
68
70
'docker-compose-dev.yml ' ,
69
- 'docker-compose-image.yml '
70
71
];
71
72
$ uri = new Uri ($ mysqlConnection );
72
73
foreach ($ files as $ file ) {
@@ -87,12 +88,12 @@ public function execute($workdir, $namespace, $composerName, $phpVersion, $mysql
87
88
$ objects = new RecursiveIteratorIterator ($ filter );
88
89
foreach ($ objects as $ name => $ object ) {
89
90
$ contents = file_get_contents ($ name );
90
- if (strpos ($ contents , 'RestReferenceArchitecture ' ) !== false ) {
91
+ if (str_contains ($ contents , 'RestReferenceArchitecture ' )) {
91
92
echo "$ name \n" ;
92
93
93
94
// Replace inside Quotes
94
95
$ contents = preg_replace (
95
- "/([\ ' \"])RestReferenceArchitecture(.*?[\ ' \"])/ " ,
96
+ "/([' \"])RestReferenceArchitecture(.*?[' \"])/ " ,
96
97
'$1 ' . str_replace ('\\' , '\\\\\\\\' , $ namespace ) . '$2 ' ,
97
98
$ contents
98
99
);
@@ -114,26 +115,77 @@ public function execute($workdir, $namespace, $composerName, $phpVersion, $mysql
114
115
);
115
116
}
116
117
}
118
+
119
+ shell_exec ("composer update " );
120
+ shell_exec ("git init " );
121
+ shell_exec ("git branch -m main " );
122
+ shell_exec ("git add . " );
123
+ shell_exec ("git commit -m 'Initial commit' " );
117
124
}
118
125
126
+ /**
127
+ * @param Event $event
128
+ * @return void
129
+ * @throws Exception
130
+ */
119
131
public static function run (Event $ event )
120
132
{
121
133
$ workdir = realpath (__DIR__ . '/.. ' );
122
134
$ stdIo = $ event ->getIO ();
123
135
124
136
$ currentPhpVersion = PHP_MAJOR_VERSION . ". " .PHP_MINOR_VERSION ;
125
137
138
+ $ validatePHPVersion = function ($ arg ) {
139
+ $ validPHPVersions = ['8.1 ' , '8.2 ' , '8.3 ' ];
140
+ if (in_array ($ arg , $ validPHPVersions )) {
141
+ return $ arg ;
142
+ }
143
+ throw new Exception ('Only the PHP versions ' . implode (', ' , $ validPHPVersions ) . ' are supported ' );
144
+ };
145
+
146
+ $ validateNamespace = function ($ arg ) {
147
+ if (empty ($ arg ) || !preg_match ('/^[A-Z][a-zA-Z0-9]*$/ ' , $ arg )) {
148
+ throw new Exception ('Namespace must be one word in CamelCase ' );
149
+ }
150
+ return $ arg ;
151
+ };
152
+
153
+ $ validateComposer = function ($ arg ) {
154
+ if (empty ($ arg ) || !preg_match ('/^[a-z0-9-]+\/[a-z0-9-]+$/ ' , $ arg )) {
155
+ throw new Exception ('Invalid Composer name ' );
156
+ }
157
+ return $ arg ;
158
+ };
159
+
160
+ $ validateURI = function ($ arg ) {
161
+ $ uri = new Uri ($ arg );
162
+ if (empty ($ uri ->getScheme ())) {
163
+ throw new Exception ('Invalid URI ' );
164
+ }
165
+ Factory::getRegisteredDrivers ($ uri ->getScheme ());
166
+ return $ arg ;
167
+ };
168
+
169
+ $ validateTimeZone = function ($ arg ) {
170
+ if (empty ($ arg ) || !in_array ($ arg , timezone_identifiers_list ())) {
171
+ throw new Exception ('Invalid Timezone ' );
172
+ }
173
+ return $ arg ;
174
+ };
175
+
176
+ $ maxRetries = 5 ;
177
+
126
178
$ stdIo ->write ("======================================================== " );
127
179
$ stdIo ->write (" Setup Project " );
128
180
$ stdIo ->write (" Answer the questions below " );
129
181
$ stdIo ->write ("======================================================== " );
130
182
$ stdIo ->write ("" );
131
183
$ stdIo ->write ("Project Directory: " . $ workdir );
132
- $ phpVersion = $ stdIo ->ask ("PHP Version [ $ currentPhpVersion]: " , $ currentPhpVersion );
133
- $ namespace = $ stdIo ->ask ('Project namespace [MyRest]: ' , 'MyRest ' );
134
- $ composerName = $ stdIo ->ask ('Composer name [me/myrest]: ' , 'me/myrest ' );
135
- $ mysqlConnection = $ stdIo ->ask ('MySQL connection DEV [mysql://root:mysqlp455w0rd@mysql-container/mydb]: ' , 'mysql://root:mysqlp455w0rd@mysql-container/mydb ' );
136
- $ timezone = $ stdIo ->ask ('Timezone [UTC]: ' , 'UTC ' );
184
+ $ phpVersion = $ stdIo ->askAndValidate ("PHP Version [ $ currentPhpVersion]: " , $ validatePHPVersion , $ maxRetries , $ currentPhpVersion );
185
+ $ namespace = $ stdIo ->askAndValidate ('Project namespace [MyRest]: ' , $ validateNamespace , $ maxRetries , 'MyRest ' );
186
+ $ composerName = $ stdIo ->askAndValidate ('Composer name [me/myrest]: ' , $ validateComposer , $ maxRetries , 'me/myrest ' );
187
+ $ mysqlConnection = $ stdIo ->askAndValidate ('MySQL connection DEV [mysql://root:mysqlp455w0rd@mysql-container/mydb]: ' , $ validateURI , $ maxRetries , 'mysql://root:mysqlp455w0rd@mysql-container/mydb ' );
188
+ $ timezone = $ stdIo ->askAndValidate ('Timezone [UTC]: ' , $ validateTimeZone , $ maxRetries , 'UTC ' );
137
189
$ stdIo ->ask ('Press <ENTER> to continue ' );
138
190
139
191
$ script = new PostCreateScript ();
0 commit comments