@@ -28,8 +28,53 @@ public function setVersion(string $newVersion): bool
2828 return false ;
2929 }
3030
31- return $ this ->updateConfigFile ($ newVersion ) &&
32- $ this ->updateRuntimeConfig ($ newVersion );
31+ if (config ('version-manager.use_environment_variable_for_version ' )) {
32+ return $ this ->updateEnvironmentFile ($ newVersion ) &&
33+ $ this ->updateRuntimeConfig ($ newVersion );
34+ } else {
35+ return $ this ->updateConfigFile ($ newVersion ) &&
36+ $ this ->updateRuntimeConfig ($ newVersion );
37+ }
38+ }
39+
40+ public function updateEnvironmentFile (string $ newVersion ): bool
41+ {
42+ $ env_variable = config ('version-manager.use_environment_variable_for_version ' );
43+
44+ if (empty ($ env_variable )) {
45+ return false ;
46+ }
47+
48+ $ env_path = base_path ('.env ' );
49+
50+ if (! File::exists ($ env_path )) {
51+ return false ;
52+ }
53+
54+ $ content = File::get ($ env_path );
55+
56+ $ key = preg_quote ($ env_variable , '/ ' );
57+ $ pattern = "/^( $ key\s*=\s*)(.*)$/m " ;
58+
59+ if (preg_match ($ pattern , $ content )) {
60+ $ updated_content = preg_replace (
61+ $ pattern ,
62+ '${1} ' .$ newVersion ,
63+ $ content
64+ );
65+ } else {
66+ $ updated_content = $ content ;
67+ if (! str_ends_with ($ updated_content , "\n" ) && strlen ($ updated_content ) > 0 ) {
68+ $ updated_content .= "\n" ;
69+ }
70+ $ updated_content .= "$ env_variable= $ newVersion " ;
71+ }
72+
73+ if ($ updated_content === $ content ) {
74+ return true ;
75+ }
76+
77+ return (bool ) File::put ($ env_path , $ updated_content );
3378 }
3479
3580 /**
0 commit comments