1212
1313use InvalidArgumentException ;
1414use RuntimeException ;
15+ use Throwable ;
1516
1617class BoardService
1718{
@@ -27,27 +28,40 @@ public function __construct(Project $project, ?Output $output = null)
2728 public function create (string $ name , string $ version = 'latest ' , string $ db = 'mariadb ' , int $ port = 8080 , string $ populate = 'none ' , bool $ debug = false , bool $ replace = false ): array
2829 {
2930 $ this ->project ->init ();
31+ $ this ->project ->assertName ($ name , 'board ' );
3032 $ selection = $ this ->resolveSourceSelection ($ version );
3133 $ boards = $ this ->project ->boards ();
32- if (isset ($ boards [$ name ]))
34+ $ existingName = null ;
35+ foreach (array_keys ($ boards ) as $ registeredName )
3336 {
37+ if ($ this ->project ->namesEqual ((string ) $ registeredName , $ name ))
38+ {
39+ $ existingName = (string ) $ registeredName ;
40+ break ;
41+ }
42+ }
43+ if ($ existingName !== null )
44+ {
45+ if ($ existingName !== $ name )
46+ {
47+ throw new InvalidArgumentException ("Board already exists: $ existingName. Board names are case-insensitive. " );
48+ }
3449 if (!$ replace )
3550 {
3651 throw new InvalidArgumentException ("Board already exists: $ name. Use board:destroy first, or pass --replace to recreate it. " );
3752 }
38-
39- $ this ->createBoardRunner ()->destroy ($ name );
4053 }
4154
4255 foreach ($ this ->project ->boards () as $ board )
4356 {
44- if (( $ board ['name ' ] ?? '' ) !== $ name && (int ) ($ board ['port ' ] ?? 0 ) === $ port )
57+ if (! $ this -> project -> namesEqual (( string ) ( $ board ['name ' ] ?? '' ), $ name) && (int ) ($ board ['port ' ] ?? 0 ) === $ port )
4558 {
4659 throw new InvalidArgumentException ("Port $ port is already used by board: {$ board ['name ' ]}" );
4760 }
4861 }
4962
50- if ($ this ->isPortInUse ($ port ))
63+ $ reusingOwnPort = $ existingName !== null && (int ) ($ boards [$ existingName ]['port ' ] ?? 0 ) === $ port ;
64+ if (!$ reusingOwnPort && $ this ->isPortInUse ($ port ))
5165 {
5266 throw new InvalidArgumentException ("Port $ port is already in use on this host. " );
5367 }
@@ -59,12 +73,6 @@ public function create(string $name, string $version = 'latest', string $db = 'm
5973 throw new RuntimeException ("Unable to determine PHP runtime for source: {$ source ['source_key ' ]}" );
6074 }
6175
62- $ boardDir = $ this ->project ->boardPath ($ name );
63- if (!is_dir ($ boardDir ) && !mkdir ($ boardDir , 0775 , true ) && !is_dir ($ boardDir ))
64- {
65- throw new RuntimeException ("Unable to create board directory: $ boardDir " );
66- }
67-
6876 $ config = [
6977 'phpbb ' => $ source ['version ' ],
7078 'phpbb_source ' => $ source ['source_key ' ],
@@ -80,30 +88,113 @@ public function create(string $name, string $version = 'latest', string $db = 'm
8088 'extensions ' => [],
8189 'styles ' => [],
8290 ];
91+ $ backups = [];
92+ if ($ existingName !== null )
93+ {
94+ $ this ->createBoardRunner ()->prepareReplacement ($ name );
95+ $ backups = $ this ->backupBoardState ($ name );
96+ }
8397
84- $ paths = $ this ->createDockerComposeWriter ()->write ($ name , $ config );
85- $ board = [
86- 'name ' => $ name ,
87- 'phpbb ' => $ source ['version ' ],
88- 'phpbb_source ' => $ source ['source_key ' ],
89- 'phpbb_branch ' => $ source ['phpbb_branch ' ],
90- 'php ' => $ php ,
91- 'db ' => $ db ,
92- 'port ' => $ port ,
93- 'url ' => "http://localhost: $ port/ " ,
94- 'path ' => $ boardDir ,
95- 'populate ' => $ populate ,
96- 'debug ' => $ debug ,
97- 'extensions ' => [],
98- 'styles ' => [],
99- 'created_at ' => gmdate ('c ' ),
100- ];
98+ $ boardDir = $ this ->project ->boardPath ($ name );
99+ try
100+ {
101+ if (!is_dir ($ boardDir ) && !mkdir ($ boardDir , 0775 , true ) && !is_dir ($ boardDir ))
102+ {
103+ throw new RuntimeException ("Unable to create board directory: $ boardDir " );
104+ }
105+
106+ $ paths = $ this ->createDockerComposeWriter ()->write ($ name , $ config );
107+ $ board = [
108+ 'name ' => $ name ,
109+ 'phpbb ' => $ source ['version ' ],
110+ 'phpbb_source ' => $ source ['source_key ' ],
111+ 'phpbb_branch ' => $ source ['phpbb_branch ' ],
112+ 'php ' => $ php ,
113+ 'db ' => $ db ,
114+ 'port ' => $ port ,
115+ 'url ' => "http://localhost: $ port/ " ,
116+ 'path ' => $ boardDir ,
117+ 'populate ' => $ populate ,
118+ 'debug ' => $ debug ,
119+ 'extensions ' => [],
120+ 'styles ' => [],
121+ 'created_at ' => gmdate ('c ' ),
122+ ];
123+
124+ $ this ->project ->appendBoard ($ board );
125+ }
126+ catch (Throwable $ e )
127+ {
128+ $ this ->restoreBoardState ($ backups );
129+ throw $ e ;
130+ }
101131
102- $ this ->project -> appendBoard ( $ board );
132+ $ this ->discardBoardBackups ( $ backups );
103133
104134 return ['board ' => $ board , 'paths ' => $ paths ];
105135 }
106136
137+ private function backupBoardState (string $ name ): array
138+ {
139+ $ token = str_replace ('. ' , '' , uniqid ('replace- ' , true ));
140+ $ backups = [];
141+ foreach ([$ this ->project ->boardPath ($ name ), $ this ->project ->runtimePath ($ name ), $ this ->project ->dbPath ($ name )] as $ path )
142+ {
143+ $ backups [$ path ] = null ;
144+ if (!file_exists ($ path ) && !is_link ($ path ))
145+ {
146+ continue ;
147+ }
148+
149+ $ backup = $ path . '. ' . $ token ;
150+ if (!@rename ($ path , $ backup ))
151+ {
152+ $ this ->restoreBoardState (array_filter ($ backups , 'is_string ' ));
153+ throw new RuntimeException ("Unable to preserve existing board during replacement: $ path " );
154+ }
155+ $ backups [$ path ] = $ backup ;
156+ }
157+
158+ return $ backups ;
159+ }
160+
161+ private function restoreBoardState (array $ backups ): void
162+ {
163+ foreach ($ backups as $ path => $ backup )
164+ {
165+ if (file_exists ($ path ) || is_link ($ path ))
166+ {
167+ $ this ->project ->deleteTree ($ path );
168+ }
169+ if (is_string ($ backup ) && (file_exists ($ backup ) || is_link ($ backup )) && !@rename ($ backup , $ path ))
170+ {
171+ throw new RuntimeException ("Unable to restore board after failed replacement: $ path " );
172+ }
173+ }
174+ }
175+
176+ private function discardBoardBackups (array $ backups ): void
177+ {
178+ foreach ($ backups as $ backup )
179+ {
180+ if (!is_string ($ backup ))
181+ {
182+ continue ;
183+ }
184+ try
185+ {
186+ $ this ->project ->deleteTree ($ backup );
187+ }
188+ catch (RuntimeException $ e )
189+ {
190+ if ($ this ->output !== null )
191+ {
192+ $ this ->output ->error ("Warning: unable to remove replacement backup $ backup: {$ e ->getMessage ()}\n" );
193+ }
194+ }
195+ }
196+ }
197+
107198 private function resolveSourceSelection (string $ version ): array
108199 {
109200 try
0 commit comments