Skip to content

Commit b2ce4f6

Browse files
authored
Merge pull request #20 from binafy/fix/conditions
[1.x] Fix bug generate with conditions
2 parents 2ad57dd + 1ba762a commit b2ce4f6

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

src/LaravelStub.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ public function moveStub(): static
134134
* Set conditions.
135135
*
136136
* @param array<string, bool|mixed|Closure> $conditions
137-
* @return static
138137
*/
139138
public function conditions(array $conditions): static
140139
{
@@ -214,27 +213,34 @@ public function generate(bool $force = false): bool
214213
}
215214

216215
// 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+
}
220237
}
221238

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);
230242
}
231243

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-
238244
// Get correct path
239245
$path = $this->getPath();
240246

tests/Feature/LaravelStubTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Binafy\LaravelStub\Facades\LaravelStub;
44
use Illuminate\Support\Facades\File;
55

6+
use function PHPUnit\Framework\assertFalse;
67
use function PHPUnit\Framework\assertFileDoesNotExist;
78
use function PHPUnit\Framework\assertFileExists;
89
use function PHPUnit\Framework\assertTrue;
@@ -105,14 +106,17 @@
105106
test('generate stub successfully when all conditions are met', function () {
106107
$stub = __DIR__ . '/test.stub';
107108

108-
$testCondition = true;
109-
110109
$generate = LaravelStub::from($stub)
111110
->to(__DIR__ . '/../App')
111+
->replaces([
112+
'CLASS' => 'Milwad',
113+
'NAMESPACE' => 'App\Models'
114+
])
115+
->replace('TRAIT', 'HasFactory')
112116
->conditions([
113117
'CONDITION_ONE' => true,
114-
'CONDITION_TWO' => $testCondition,
115-
'CONDITION_THREE' => fn() => true,
118+
'CONDITION_TWO' => true,
119+
'CONDITION_THREE' => fn () => false,
116120
])
117121
->name('conditional-test')
118122
->ext('php')
@@ -122,9 +126,13 @@
122126
assertFileExists(__DIR__ . '/../App/conditional-test.php');
123127

124128
$content = File::get(__DIR__ . '/../App/conditional-test.php');
125-
expect($content)->toContain('public function handle(): void');
126-
expect($content)->toContain('public function users(): void');
127-
expect($content)->toContain('public function roles(): void');
129+
expect($content)
130+
->toContain('public function handle(): void')
131+
->and($content)
132+
->toContain('public function users(): void')
133+
->and($content)
134+
->not
135+
->toContain('public function roles(): void');
128136
});
129137

130138
test('generate stub successfully when conditions are not met', function () {
@@ -224,7 +232,7 @@
224232
->ext('php')
225233
->generateIf(false);
226234

227-
\PHPUnit\Framework\assertFalse($generate);
235+
assertFalse($generate);
228236
});
229237

230238
test('generate stub successfully with `generateUnless` method', function () {
@@ -259,5 +267,5 @@
259267
->ext('php')
260268
->generateUnless(true);
261269

262-
\PHPUnit\Framework\assertFalse($generate);
270+
assertFalse($generate);
263271
});

0 commit comments

Comments
 (0)