@@ -134,7 +134,6 @@ public function moveStub(): static
134
134
* Set conditions.
135
135
*
136
136
* @param array<string, bool|mixed|Closure> $conditions
137
- * @return static
138
137
*/
139
138
public function conditions (array $ conditions ): static
140
139
{
@@ -214,27 +213,34 @@ public function generate(bool $force = false): bool
214
213
}
215
214
216
215
// Process conditions
217
- foreach ($ this ->conditions as $ condition => $ value ) {
218
- if ($ value instanceof Closure) {
219
- $ value = $ value ();
216
+ if (count ($ this ->conditions ) !== 0 ) {
217
+ foreach ($ this ->conditions as $ condition => $ value ) {
218
+ if ($ value instanceof Closure) {
219
+ $ value = $ value ();
220
+ }
221
+
222
+ if ($ value ) {
223
+ // Replace placeholders for conditions that are true
224
+ $ content = preg_replace (
225
+ "/^[ \t]*{{ if $ condition }}\s* \n(.*?)(?=^[ \t]*{{ endif }}\s* \n)/ms " ,
226
+ "$1 " ,
227
+ $ content
228
+ );
229
+ } else {
230
+ // Remove the entire block for conditions that are false
231
+ $ content = preg_replace (
232
+ "/^[ \t]*{{ if $ condition }}\s* \n.*?^[ \t]*{{ endif }}\s* \n/ms " ,
233
+ '' ,
234
+ $ content
235
+ );
236
+ }
220
237
}
221
238
222
- if ($ value ) {
223
- // Remove condition placeholders along with any leading whitespace and newlines
224
- $ content = preg_replace ("/^[ \t]*{{ if $ condition }}\s* \n|^[ \t]*{{ endif }}\s* \n/m " , '' , $ content );
225
- continue ;
226
- }
227
-
228
- // Remove the entire block including any leading whitespace and newlines
229
- $ content = preg_replace ("/^[ \t]*{{ if $ condition }}\s* \n.*?^[ \t]*{{ endif }}\s* \n/ms " , '' , $ content );
239
+ // Finally, clean up any remaining conditional tags and extra newlines
240
+ $ content = preg_replace ("/^[ \t]*{{ if .*? }}\s* \n|^[ \t]*{{ endif }}\s* \n/m " , "\n" , $ content );
241
+ $ content = preg_replace ("/^[ \t]* \n/m " , "\n" , $ content );
230
242
}
231
243
232
- // Remove any remaining conditional tags and their lines
233
- $ content = preg_replace ("/^[ \t]*{{ if .*? }}\s* \n|^[ \t]*{{ endif .*? }}\s* \n/m " , '' , $ content );
234
-
235
- // Remove any remaining empty lines
236
- $ content = preg_replace ("/^[ \t]* \n/m " , '' , $ content );
237
-
238
244
// Get correct path
239
245
$ path = $ this ->getPath ();
240
246
0 commit comments