22
33namespace Builder ;
44
5- use ByJG \Util \JwtWrapper ;
5+ use ByJG \AnyDataset \Db \Factory ;
6+ use ByJG \JwtWrapper \JwtWrapper ;
67use ByJG \Util \Uri ;
78use Composer \Script \Event ;
9+ use Exception ;
810use RecursiveCallbackFilterIterator ;
911use RecursiveDirectoryIterator ;
1012use RecursiveIteratorIterator ;
1113
1214class PostCreateScript
1315{
14- public function execute ($ workdir , $ namespace , $ composerName , $ phpVersion , $ mysqlConnection , $ timezone )
16+ public function execute ($ workdir , $ namespace , $ composerName , $ phpVersion , $ mysqlConnection , $ timezone ): void
1517 {
1618 // ------------------------------------------------
17- // Defining function to interatively walking through the directories
19+ // Defining function to interactively walking through the directories
1820 $ directory = new RecursiveDirectoryIterator ($ workdir );
1921 $ filter = new RecursiveCallbackFilterIterator ($ directory , function ($ current/*, $key, $iterator*/ ) {
2022 // Skip hidden files and directories.
@@ -50,8 +52,8 @@ public function execute($workdir, $namespace, $composerName, $phpVersion, $mysql
5052 foreach ($ files as $ file ) {
5153 $ contents = file_get_contents ("$ workdir/ $ file " );
5254 $ 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 );
5557 file_put_contents (
5658 "$ workdir/ $ file " ,
5759 $ contents
@@ -66,7 +68,6 @@ public function execute($workdir, $namespace, $composerName, $phpVersion, $mysql
6668 'config/config-prod.php ' ,
6769 'config/config-test.php ' ,
6870 'docker-compose-dev.yml ' ,
69- 'docker-compose-image.yml '
7071 ];
7172 $ uri = new Uri ($ mysqlConnection );
7273 foreach ($ files as $ file ) {
@@ -87,12 +88,12 @@ public function execute($workdir, $namespace, $composerName, $phpVersion, $mysql
8788 $ objects = new RecursiveIteratorIterator ($ filter );
8889 foreach ($ objects as $ name => $ object ) {
8990 $ contents = file_get_contents ($ name );
90- if (strpos ($ contents , 'RestReferenceArchitecture ' ) !== false ) {
91+ if (str_contains ($ contents , 'RestReferenceArchitecture ' )) {
9192 echo "$ name \n" ;
9293
9394 // Replace inside Quotes
9495 $ contents = preg_replace (
95- "/([\ ' \"])RestReferenceArchitecture(.*?[\ ' \"])/ " ,
96+ "/([' \"])RestReferenceArchitecture(.*?[' \"])/ " ,
9697 '$1 ' . str_replace ('\\' , '\\\\\\\\' , $ namespace ) . '$2 ' ,
9798 $ contents
9899 );
@@ -114,26 +115,77 @@ public function execute($workdir, $namespace, $composerName, $phpVersion, $mysql
114115 );
115116 }
116117 }
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' " );
117124 }
118125
126+ /**
127+ * @param Event $event
128+ * @return void
129+ * @throws Exception
130+ */
119131 public static function run (Event $ event )
120132 {
121133 $ workdir = realpath (__DIR__ . '/.. ' );
122134 $ stdIo = $ event ->getIO ();
123135
124136 $ currentPhpVersion = PHP_MAJOR_VERSION . ". " .PHP_MINOR_VERSION ;
125137
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+
126178 $ stdIo ->write ("======================================================== " );
127179 $ stdIo ->write (" Setup Project " );
128180 $ stdIo ->write (" Answer the questions below " );
129181 $ stdIo ->write ("======================================================== " );
130182 $ stdIo ->write ("" );
131183 $ 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 ' );
137189 $ stdIo ->ask ('Press <ENTER> to continue ' );
138190
139191 $ script = new PostCreateScript ();
0 commit comments