Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use RonasIT\ProjectInitializator\Enums\AuthTypeEnum;
use RonasIT\ProjectInitializator\Enums\RoleEnum;
use RonasIT\ProjectInitializator\Enums\AppTypeEnum;
use Winter\LaravelConfigWriter\ArrayFile;
use RonasIT\ProjectInitializator\ConfigWriter\ArrayFile;
use Winter\LaravelConfigWriter\EnvFile;

class InitCommand extends Command implements Isolatable
Expand Down Expand Up @@ -233,6 +233,8 @@ public function handle(): void

$this->setupComposerHooks();

$this->changeMiddlewareForTelescopeAuthorization();

$this->setAutoDocContactEmail($this->codeOwnerEmail);

foreach ($this->shellCommands as $shellCommand) {
Expand Down Expand Up @@ -280,7 +282,7 @@ protected function addArrayItemIfMissing(array &$data, string $path, string $val
protected function setAutoDocContactEmail(string $email): void
{
$config = ArrayFile::open(base_path('config/auto-doc.php'));

$config->set('info.contact.email', $email);

$config->write();
Expand Down Expand Up @@ -611,4 +613,16 @@ protected function publishWebLogin(): void

file_put_contents(base_path('routes/web.php'), "\nAuth::routes();\n", FILE_APPEND);
}

protected function changeMiddlewareForTelescopeAuthorization(): void
{
$config = ArrayFile::open(base_path('config/telescope.php'));

$config->set('middleware', [
'web',
'auth:web',
]);

$config->write();
}
}
42 changes: 42 additions & 0 deletions src/ConfigWriter/ArrayFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace RonasIT\ProjectInitializator\ConfigWriter;

use PhpParser\Error;
use PhpParser\Lexer;
use PhpParser\Parser\Php7;
use PhpParser\Parser\Php8;
use PhpParser\PhpVersion;
use Winter\LaravelConfigWriter\ArrayFile as BaseArrayFile;
use Winter\LaravelConfigWriter\Exceptions\ConfigWriterException;

class ArrayFile extends BaseArrayFile
{
public static function open(string $filePath, bool $throwIfMissing = false)
{
$exists = file_exists($filePath);

if (!$exists && $throwIfMissing) {
throw new \InvalidArgumentException('file not found');
}

$version = PhpVersion::getHostVersion();

$lexer = new Lexer\Emulative($version);
$parser = ($version->id >= 80000)
? new Php8($lexer, $version)
: new Php7($lexer, $version);

try {
$ast = $parser->parse(
$exists
? file_get_contents($filePath)
: sprintf('<?php%1$s%1$sreturn [];%1$s', "\n")
);
} catch (Error $e) {
throw new ConfigWriterException($e);
}

return new static($ast, $parser, $filePath, new ArrayPrinter());
}
}
35 changes: 35 additions & 0 deletions src/ConfigWriter/ArrayPrinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace RonasIT\ProjectInitializator\ConfigWriter;

use PhpParser\Node\Stmt;
use PhpParser\ParserAbstract;
use Winter\LaravelConfigWriter\Printer\ArrayPrinter as BaseArrayPrinter;

class ArrayPrinter extends BaseArrayPrinter
{
public function render(array $stmts, ParserAbstract $parser): string
{
if (!$stmts) {
return "<?php\n\n";
}

$this->parser = $parser;

$code = "<?php\n\n" . $this->prettyPrint($stmts);

$code = preg_replace('/(;\n?)(return\s*\[)/', ";\n\n$2", $code, 1);

if ($stmts[0] instanceof Stmt\InlineHTML) {
$code = preg_replace('/^<\?php\s+\?>\n?/', '', $code);
}

if ($stmts[count($stmts) - 1] instanceof Stmt\InlineHTML) {
$code = preg_replace('/<\?php$/', '', rtrim($code));
}

$this->parser = null;

return $code;
}
}
90 changes: 81 additions & 9 deletions tests/InitCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ class InitCommandTest extends TestCase

public function testRunWithoutAdminAndReadmeCreationConvertAppNameToPascalCaseTelescopeAlreadyInstalled()
{
$this->mockNativeFunction(
'\RonasIT\ProjectInitializator\ConfigWriter',
$this->callFileExists(base_path('config/telescope.php')),
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope.php')),
$this->callFileExists(base_path('config/auto-doc.php')),
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
);

$this->mockNativeFunction(
'\Winter\LaravelConfigWriter',
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_pascal_case.yml'),
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_pascal_case.yml'),
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_after_changes.php')),
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
);

$this->mockNativeFunction(
Expand Down Expand Up @@ -64,11 +73,20 @@ public function testRunWithoutAdminAndReadmeCreationConvertAppNameToPascalCaseTe

public function testRunWithoutAdminAndReadmeCreation()
{
$this->mockNativeFunction(
'\RonasIT\ProjectInitializator\ConfigWriter',
$this->callFileExists(base_path('config/telescope.php')),
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope.php')),
$this->callFileExists(base_path('config/auto-doc.php')),
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
);

$this->mockNativeFunction(
'\Winter\LaravelConfigWriter',
$this->changeEnvFileCall('.env', 'env.example.yml', 'env.example_app_name_pascal_case.yml'),
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_pascal_case.yml'),
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_after_changes.php')),
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
);

$this->mockNativeFunction(
Expand Down Expand Up @@ -116,11 +134,20 @@ public function testRunWithoutAdminAndReadmeCreation()

public function testRunWithAdminAndWithoutReadmeCreation()
{
$this->mockNativeFunction(
'\RonasIT\ProjectInitializator\ConfigWriter',
$this->callFileExists(base_path('config/telescope.php')),
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope.php')),
$this->callFileExists(base_path('config/auto-doc.php')),
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
);

$this->mockNativeFunction(
'\Winter\LaravelConfigWriter',
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_after_changes.php')),
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
);

$this->mockNativeFunction(
Expand Down Expand Up @@ -172,13 +199,22 @@ public function testRunWithAdminAndWithoutReadmeCreation()

public function testRunWithAdminAndDefaultReadmeCreation()
{
$this->mockNativeFunction(
'\RonasIT\ProjectInitializator\ConfigWriter',
$this->callFileExists(base_path('config/telescope.php')),
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope.php')),
$this->callFileExists(base_path('config/auto-doc.php')),
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
);

$this->mockNativeFunction(
'\Winter\LaravelConfigWriter',
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_clerk_credentials_added.yml'),
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_clerk_credentials_added.yml'),
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_after_changes.php')),
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
);

$this->mockNativeFunction(
Expand Down Expand Up @@ -298,11 +334,20 @@ public function testRunWithAdminAndDefaultReadmeCreation()

public function testRunWithAdminAndPartialReadmeCreation()
{
$this->mockNativeFunction(
'\RonasIT\ProjectInitializator\ConfigWriter',
$this->callFileExists(base_path('config/telescope.php')),
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope.php')),
$this->callFileExists(base_path('config/auto-doc.php')),
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
);

$this->mockNativeFunction(
'\Winter\LaravelConfigWriter',
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_after_changes.php')),
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
);

$this->mockNativeFunction(
Expand Down Expand Up @@ -400,11 +445,20 @@ public function testRunWithAdminAndPartialReadmeCreation()

public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorInstallationMedia()
{
$this->mockNativeFunction(
'\RonasIT\ProjectInitializator\ConfigWriter',
$this->callFileExists(base_path('config/telescope.php')),
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope.php')),
$this->callFileExists(base_path('config/auto-doc.php')),
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
);

$this->mockNativeFunction(
'\Winter\LaravelConfigWriter',
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_after_changes.php')),
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
);

$this->mockNativeFunction(
Expand Down Expand Up @@ -518,11 +572,20 @@ public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorIns

public function testRunWithoutAdminAndUsingTelescope()
{
$this->mockNativeFunction(
'\RonasIT\ProjectInitializator\ConfigWriter',
$this->callFileExists(base_path('config/telescope.php')),
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope.php')),
$this->callFileExists(base_path('config/auto-doc.php')),
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
);

$this->mockNativeFunction(
'\Winter\LaravelConfigWriter',
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_after_changes.php')),
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
);

$this->mockNativeFunction(
Expand Down Expand Up @@ -624,15 +687,24 @@ public function testRunWithoutAdminAndUsingTelescope()

public function testRunWithClerkMobileAppWithPintInstalled(): void
{
$this->mockNativeFunction(
'\RonasIT\ProjectInitializator\ConfigWriter',
$this->callFileExists(base_path('config/telescope.php')),
$this->callFileGetContent(base_path('config/telescope.php'), $this->getFixture('telescope.php')),
$this->callFileExists(base_path('config/auto-doc.php')),
$this->callFileGetContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc.php')),
);

$this->mockNativeFunction(
'\Winter\LaravelConfigWriter',
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_clerk_credentials_added_mobile_app.yml'),
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_clerk_credentials_added_mobile_app.yml'),
$this->changeConfigFileCall(base_path('config/auto-doc.php'), 'auto_doc.php', 'auto_doc_after_changes.php'),
$this->callFilePutContent(base_path('config/telescope.php'), $this->getFixture('telescope_after_changes.php')),
$this->callFilePutContent(base_path('config/auto-doc.php'), $this->getFixture('auto_doc_after_changes.php')),
);

$this->mockNativeFunction(
'RonasIT\ProjectInitializator\Commands',
$this->callFileExists('.env', false),
Expand Down
9 changes: 0 additions & 9 deletions tests/Support/Traits/InitCommandMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,4 @@ protected function changeEnvFileCall(string $fileName, string $sourceFixture, st
$this->callFilePutContent($fileName, $this->getFixture($resultFixture)),
];
}

protected function changeConfigFileCall(string $fileName, string $sourceFixture, string $resultFixture): array
{
return [
$this->callFileExists($fileName),
$this->callFileGetContent($fileName, $this->getFixture($sourceFixture)),
$this->callFilePutContent($fileName, $this->getFixture($resultFixture)),
];
}
}
Loading