Skip to content

Commit 64f6afe

Browse files
authored
Merge pull request #58 from RonasIT/38_change_telescope_middleware
feat: change telescope middleware
2 parents eec24b3 + d7549b4 commit 64f6afe

File tree

7 files changed

+576
-21
lines changed

7 files changed

+576
-21
lines changed

src/Commands/InitCommand.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
use Illuminate\Support\Carbon;
1010
use Illuminate\Support\Facades\Validator;
1111
use Illuminate\Support\Str;
12+
use RonasIT\ProjectInitializator\Enums\AppTypeEnum;
1213
use RonasIT\ProjectInitializator\Enums\AuthTypeEnum;
1314
use RonasIT\ProjectInitializator\Enums\RoleEnum;
14-
use RonasIT\ProjectInitializator\Enums\AppTypeEnum;
15-
use Winter\LaravelConfigWriter\ArrayFile;
15+
use RonasIT\ProjectInitializator\Extensions\ConfigWriter\ArrayFile;
1616
use Winter\LaravelConfigWriter\EnvFile;
1717

1818
class InitCommand extends Command implements Isolatable
@@ -238,6 +238,8 @@ public function handle(): void
238238

239239
$this->setupComposerHooks();
240240

241+
$this->changeMiddlewareForTelescopeAuthorization();
242+
241243
$this->setAutoDocContactEmail($this->codeOwnerEmail);
242244

243245
foreach ($this->shellCommands as $shellCommand) {
@@ -285,7 +287,7 @@ protected function addArrayItemIfMissing(array &$data, string $path, string $val
285287
protected function setAutoDocContactEmail(string $email): void
286288
{
287289
$config = ArrayFile::open(base_path('config/auto-doc.php'));
288-
290+
289291
$config->set('info.contact.email', $email);
290292

291293
$config->write();
@@ -630,4 +632,17 @@ protected function publishWebLogin(): void
630632

631633
file_put_contents(base_path('routes/web.php'), "\nAuth::routes();\n", FILE_APPEND);
632634
}
635+
636+
protected function changeMiddlewareForTelescopeAuthorization(): void
637+
{
638+
$config = ArrayFile::open(base_path('config/telescope.php'));
639+
640+
// TODO: add Authorize::class middleware after inplementing an ability to modify functions in the https://github.com/RonasIT/larabuilder package
641+
$config->set('middleware', [
642+
'web',
643+
'auth:web',
644+
]);
645+
646+
$config->write();
647+
}
633648
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace RonasIT\ProjectInitializator\Extensions\ConfigWriter;
4+
5+
use PhpParser\Error;
6+
use PhpParser\Parser\Php7;
7+
use PhpParser\Parser\Php8;
8+
use PhpParser\PhpVersion;
9+
use Winter\LaravelConfigWriter\ArrayFile as BaseArrayFile;
10+
use Winter\LaravelConfigWriter\Exceptions\ConfigWriterException;
11+
use InvalidArgumentException;
12+
use PhpParser\Lexer\Emulative;
13+
14+
//TODO: remove this class after resolving https://github.com/wintercms/laravel-config-writer/issues/10
15+
class ArrayFile extends BaseArrayFile
16+
{
17+
public static function open(string $filePath, bool $throwIfMissing = false): ArrayFile
18+
{
19+
$exists = file_exists($filePath);
20+
21+
if (!$exists && $throwIfMissing) {
22+
throw new InvalidArgumentException('file not found');
23+
}
24+
25+
$version = PhpVersion::getHostVersion();
26+
27+
$lexer = new Emulative($version);
28+
$parser = ($version->id >= 80000)
29+
? new Php8($lexer, $version)
30+
: new Php7($lexer, $version);
31+
32+
try {
33+
$ast = $parser->parse(
34+
($exists)
35+
? file_get_contents($filePath)
36+
: sprintf('<?php%1$s%1$sreturn [];%1$s', "\n")
37+
);
38+
} catch (Error $e) {
39+
throw new ConfigWriterException($e);
40+
}
41+
42+
return new static($ast, $parser, $filePath, new ArrayPrinter());
43+
}
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace RonasIT\ProjectInitializator\Extensions\ConfigWriter;
4+
5+
use PhpParser\ParserAbstract;
6+
use Winter\LaravelConfigWriter\Printer\ArrayPrinter as BaseArrayPrinter;
7+
use PhpParser\Node\Stmt\InlineHTML;
8+
9+
//TODO: remove this class after resolving https://github.com/wintercms/laravel-config-writer/issues/10
10+
class ArrayPrinter extends BaseArrayPrinter
11+
{
12+
public function render(array $stmts, ParserAbstract $parser): string
13+
{
14+
if (!$stmts) {
15+
return "<?php\n\n";
16+
}
17+
18+
$this->parser = $parser;
19+
20+
$code = "<?php\n\n" . $this->prettyPrint($stmts);
21+
22+
$code = preg_replace('/(;\n?)(return\s*\[)/', ";\n\n$2", $code, 1);
23+
24+
if ($stmts[0] instanceof InlineHTML) {
25+
$code = preg_replace('/^<\?php\s+\?>\n?/', '', $code);
26+
}
27+
28+
if ($stmts[count($stmts) - 1] instanceof InlineHTML) {
29+
$code = preg_replace('/<\?php$/', '', rtrim($code));
30+
}
31+
32+
$this->parser = null;
33+
34+
return $code;
35+
}
36+
}

tests/InitCommandTest.php

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ class InitCommandTest extends TestCase
1010

1111
public function testRunWithoutAdminAndReadmeCreationConvertAppNameToPascalCaseTelescopeAlreadyInstalled()
1212
{
13+
$this->mockNativeFunction(
14+
'\RonasIT\ProjectInitializator\Extensions\ConfigWriter',
15+
$this->callFileExists(base_path('config/telescope.php')),
16+
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope_config.php')),
17+
$this->callFileExists(base_path('config/auto-doc.php')),
18+
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
19+
);
20+
1321
$this->mockNativeFunction(
1422
'\Winter\LaravelConfigWriter',
1523
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_pascal_case.yml'),
1624
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_pascal_case.yml'),
17-
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
25+
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_config_after_initialization.php')),
26+
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
1827
);
1928

2029
$this->mockNativeFunction(
@@ -63,11 +72,20 @@ public function testRunWithoutAdminAndReadmeCreationConvertAppNameToPascalCaseTe
6372

6473
public function testRunWithoutAdminAndReadmeCreation()
6574
{
75+
$this->mockNativeFunction(
76+
'\RonasIT\ProjectInitializator\Extensions\ConfigWriter',
77+
$this->callFileExists(base_path('config/telescope.php')),
78+
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope_config.php')),
79+
$this->callFileExists(base_path('config/auto-doc.php')),
80+
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
81+
);
82+
6683
$this->mockNativeFunction(
6784
'\Winter\LaravelConfigWriter',
6885
$this->changeEnvFileCall('.env', 'env.example.yml', 'env.example_app_name_pascal_case.yml'),
6986
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_pascal_case.yml'),
70-
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
87+
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_config_after_initialization.php')),
88+
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
7189
);
7290

7391
$this->mockNativeFunction(
@@ -115,11 +133,20 @@ public function testRunWithoutAdminAndReadmeCreation()
115133

116134
public function testRunWithAdminAndWithoutReadmeCreation()
117135
{
136+
$this->mockNativeFunction(
137+
'\RonasIT\ProjectInitializator\Extensions\ConfigWriter',
138+
$this->callFileExists(base_path('config/telescope.php')),
139+
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope_config.php')),
140+
$this->callFileExists(base_path('config/auto-doc.php')),
141+
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
142+
);
143+
118144
$this->mockNativeFunction(
119145
'\Winter\LaravelConfigWriter',
120146
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
121147
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
122-
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
148+
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_config_after_initialization.php')),
149+
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
123150
);
124151

125152
$this->mockNativeFunction(
@@ -171,13 +198,22 @@ public function testRunWithAdminAndWithoutReadmeCreation()
171198

172199
public function testRunWithAdminAndDefaultReadmeCreation()
173200
{
201+
$this->mockNativeFunction(
202+
'\RonasIT\ProjectInitializator\Extensions\ConfigWriter',
203+
$this->callFileExists(base_path('config/telescope.php')),
204+
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope_config.php')),
205+
$this->callFileExists(base_path('config/auto-doc.php')),
206+
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
207+
);
208+
174209
$this->mockNativeFunction(
175210
'\Winter\LaravelConfigWriter',
176211
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
177212
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
178213
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_clerk_credentials_added.yml'),
179214
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_clerk_credentials_added.yml'),
180-
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
215+
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_config_after_initialization.php')),
216+
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
181217
);
182218

183219
$this->mockNativeFunction(
@@ -297,11 +333,20 @@ public function testRunWithAdminAndDefaultReadmeCreation()
297333

298334
public function testRunWithAdminAndPartialReadmeCreation()
299335
{
336+
$this->mockNativeFunction(
337+
'\RonasIT\ProjectInitializator\Extensions\ConfigWriter',
338+
$this->callFileExists(base_path('config/telescope.php')),
339+
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope_config.php')),
340+
$this->callFileExists(base_path('config/auto-doc.php')),
341+
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
342+
);
343+
300344
$this->mockNativeFunction(
301345
'\Winter\LaravelConfigWriter',
302346
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
303347
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
304-
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
348+
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_config_after_initialization.php')),
349+
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
305350
);
306351

307352
$this->mockNativeFunction(
@@ -399,11 +444,20 @@ public function testRunWithAdminAndPartialReadmeCreation()
399444

400445
public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorInstallationMedia()
401446
{
447+
$this->mockNativeFunction(
448+
'\RonasIT\ProjectInitializator\Extensions\ConfigWriter',
449+
$this->callFileExists(base_path('config/telescope.php')),
450+
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope_config.php')),
451+
$this->callFileExists(base_path('config/auto-doc.php')),
452+
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
453+
);
454+
402455
$this->mockNativeFunction(
403456
'\Winter\LaravelConfigWriter',
404457
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
405458
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
406-
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
459+
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_config_after_initialization.php')),
460+
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
407461
);
408462

409463
$this->mockNativeFunction(
@@ -517,11 +571,20 @@ public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorIns
517571

518572
public function testRunWithoutAdminAndUsingTelescope()
519573
{
574+
$this->mockNativeFunction(
575+
'\RonasIT\ProjectInitializator\Extensions\ConfigWriter',
576+
$this->callFileExists(base_path('config/telescope.php')),
577+
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope_config.php')),
578+
$this->callFileExists(base_path('config/auto-doc.php')),
579+
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
580+
);
581+
520582
$this->mockNativeFunction(
521583
'\Winter\LaravelConfigWriter',
522584
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
523585
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
524-
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
586+
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_config_after_initialization.php')),
587+
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
525588
);
526589

527590
$this->mockNativeFunction(
@@ -623,15 +686,24 @@ public function testRunWithoutAdminAndUsingTelescope()
623686

624687
public function testRunWithClerkMobileAppWithPintInstalled(): void
625688
{
689+
$this->mockNativeFunction(
690+
'\RonasIT\ProjectInitializator\Extensions\ConfigWriter',
691+
$this->callFileExists(base_path('config/telescope.php')),
692+
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope_config.php')),
693+
$this->callFileExists(base_path('config/auto-doc.php')),
694+
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
695+
);
696+
626697
$this->mockNativeFunction(
627698
'\Winter\LaravelConfigWriter',
628699
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
629700
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
630701
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_clerk_credentials_added_mobile_app.yml'),
631702
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_clerk_credentials_added_mobile_app.yml'),
632-
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
703+
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_config_after_initialization.php')),
704+
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
633705
);
634-
706+
635707
$this->mockNativeFunction(
636708
'RonasIT\ProjectInitializator\Commands',
637709
$this->callFileExists('.env', false),

tests/Support/Traits/InitCommandMockTrait.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,4 @@ protected function changeEnvFileCall(string $fileName, string $sourceFixture, st
4646
$this->callFilePutContent($fileName, $this->getFixture($resultFixture)),
4747
];
4848
}
49-
50-
protected function changeConfigFileCall(string $fileName, string $sourceFixture, string $resultFixture): array
51-
{
52-
return [
53-
$this->callFileExists($fileName),
54-
$this->callFileGetContent($fileName, $this->getFixture($sourceFixture)),
55-
$this->callFilePutContent($fileName, $this->getFixture($resultFixture)),
56-
];
57-
}
5849
}

0 commit comments

Comments
 (0)