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
22 changes: 22 additions & 0 deletions src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use RonasIT\ProjectInitializator\Enums\AppTypeEnum;
use Winter\LaravelConfigWriter\ArrayFile;
use RonasIT\ProjectInitializator\Support\Parser\PhpParser;
use RonasIT\ProjectInitializator\Support\Parser\Arguments\ClassConstFetchArgument;

class InitCommand extends Command implements Isolatable
{
Expand Down Expand Up @@ -553,9 +554,30 @@ protected function enableClerk(): void
path: 'app/Support/Clerk',
);

$this->addClerkRepositoryBind();
$this->modifyUserModel();
}

protected function addClerkRepositoryBind(): void
{
$parser = app(PhpParser::class, ['filePath' => 'app/Providers/AppServiceProvider.php']);

$parser
->appendPartToMethod(
methodName: 'boot',
variableName: 'this',
callMethodName: 'bind',
propertyName: 'app',
firstArgument: new ClassConstFetchArgument('UserRepositoryContract'),
secondArgument: new ClassConstFetchArgument('ClerkUserRepository'),
)
->addImports([
'RonasIT\\Clerk\\Contracts\\UserRepositoryContract',
'App\\Support\Clerk\\ClerkUserRepository',
])
->save();
}

protected function modifyUserModel(): void
{
$parser = app(PhpParser::class, ['filePath' => 'app/Models/User.php']);
Expand Down
19 changes: 19 additions & 0 deletions src/Support/Parser/Arguments/ClassConstFetchArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace RonasIT\ProjectInitializator\Support\Parser\Arguments;

use PhpParser\Node\Arg;
use PhpParser\Node\Name;
use PhpParser\Node\Expr\ClassConstFetch;

class ClassConstFetchArgument extends Arg
{
public function __construct(
protected string $argumentName,
)
{
$this->value = new ClassConstFetch(new Name($this->argumentName), 'class');

return parent::__construct($this->value);
}
}
35 changes: 35 additions & 0 deletions src/Support/Parser/Expressions/AppendMethodCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace RonasIT\ProjectInitializator\Support\Parser\Expressions;

use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;

class AppendMethodCall
{
public static function make(
string $variableName,
string $methodName,
array $arguments,
?string $propertyName = null,
array $attributes = [],
): MethodCall
{
$variable = self::setVariable($variableName, $propertyName);

return new MethodCall(
var: $variable,
name: $methodName,
args: $arguments,
attributes: $attributes
);
}

protected static function setVariable(string $variableName, ?string $propertyName = null): PropertyFetch|Variable
{
return !empty($propertyName)
? new PropertyFetch(new Variable($variableName), $propertyName)
: new Variable($variableName);
}
}
16 changes: 16 additions & 0 deletions src/Support/Parser/PhpParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use PhpParser\Parser;
use PhpParser\PrettyPrinterAbstract;
use PhpParser\NodeTraverserInterface;
use RonasIT\ProjectInitializator\Support\Parser\Visitors\MethodVisitors\AppendPartToMethodVisitor;
use RonasIT\ProjectInitializator\Support\Parser\Visitors\AddImportsVisitor;

class PhpParser
{
Expand Down Expand Up @@ -50,6 +52,20 @@ public function addValueToArrayProperty(array $propertyNames, string $value): se

return $this;
}

public function appendPartToMethod(string $methodName, string $variableName, string $callMethodName, ?string $propertyName, Node\Arg ...$args): self
{
$this->traverser->addVisitor(new AppendPartToMethodVisitor($methodName, $variableName, $callMethodName, $propertyName, ...$args));

return $this;
}

public function addImports(array $fullClassNames): self
{
$this->traverser->addVisitor(new AddImportsVisitor($fullClassNames));

return $this;
}

public function save(): void
{
Expand Down
87 changes: 87 additions & 0 deletions src/Support/Parser/Visitors/AddImportsVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace RonasIT\ProjectInitializator\Support\Parser\Visitors;

use PhpParser\NodeVisitorAbstract;
use PhpParser\Node;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use PhpParser\Node\Name;

class AddImportsVisitor extends NodeVisitorAbstract
{
public function __construct(
protected array $classFullNames,
)
{
}

public function enterNode(Node $node): void
{
if (!$node instanceof Namespace_) {
return;
}

$existingImports = $this->getExistingImports($node);

$importsToAdd = array_diff($this->classFullNames, $existingImports);

if (empty($importsToAdd)) {
return;
}

$newUseStmts = $this->getNewImports($importsToAdd);

$inserted = false;

$this->insertImportsToUseBlock($node, $newUseStmts, $inserted);

if (!$inserted) {
foreach ($newUseStmts as $useStmt) {
$node->stmts[] = $useStmt;
}
}
}

protected function getExistingImports(Node $node): array
{
$existingUses = [];

foreach ($node->stmts as $stmt) {
if ($stmt instanceof Use_) {
foreach ($stmt->uses as $use) {
$existingUses[] = $use->name->toString();
}
}
}

return $existingUses;
}

protected function getNewImports(array $importsToAdd): array
{
$newUseStmts = [];

foreach ($importsToAdd as $classFullName) {
$newUseStmts[] = new Use_([
new UseUse(new Name($classFullName))
]);
}

return $newUseStmts;
}

protected function insertImportsToUseBlock(Node $node, array $newUseStmts, bool &$inserted): void
{
foreach ($node->stmts as $i => $stmt) {
if (!($stmt instanceof Use_)) {
array_splice($node->stmts, $i, 0, $newUseStmts);

$inserted = true;

break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace RonasIT\ProjectInitializator\Support\Parser\Visitors\MethodVisitors;

use PhpParser\NodeVisitorAbstract;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use RonasIT\ProjectInitializator\Support\Parser\Expressions\AppendMethodCall;
use PhpParser\Node\Arg;

class AppendPartToMethodVisitor extends NodeVisitorAbstract
{
protected array $arguments;

public function __construct(
protected string $methodName,
protected string $variableName,
protected string $callMethodName,
protected ?string $propertyName = null,
Arg ...$arguments,
)
{
$this->arguments = $arguments;
}

public function enterNode(Node $node)
{
$stmtToAdd = new Expression(
AppendMethodCall::make(
variableName: $this->variableName,
methodName: $this->callMethodName,
arguments: $this->arguments,
propertyName: $this->propertyName,
)
);

if ($node instanceof ClassMethod
&& $node->name->toString() === $this->methodName
&& !in_array($this->classMethodKeys($stmtToAdd), $this->findMethodKeysInExistClassMethods($node))) {
$node->stmts[] = $stmtToAdd;
}
}

protected function classMethodKeys($stmts): array
{
$expressionAttributes = [];

$expressionAttributes[] = $stmts->expr->name->name;

foreach ($stmts->expr->var as $var) {
$expressionAttributes[] = $var->name;
}

foreach ($stmts->expr->args as $arg) {
$expressionAttributes[] = $arg->value->class->name;
}

return $expressionAttributes;
}

protected function findMethodKeysInExistClassMethods(Node $node): array
{
$nodeStmtsKey = [];

foreach ($node->stmts as $stmt) {
$nodeStmtsKey[] = $this->classMethodKeys($stmt);
}

return $nodeStmtsKey;
}
}
18 changes: 18 additions & 0 deletions tests/InitCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,20 @@ public function testRunWithAdminAndDefaultReadmeCreation()

$this->mockNativeFunction('RonasIT\ProjectInitializator\Support\Parser',
[
$this->functionCall(
name: 'file_get_contents',
arguments: ['app/Providers/AppServiceProvider.php'],
result: $this->getFixture('app_service_provider.php'),
),
$this->functionCall(
name: 'file_get_contents',
arguments: ['app/Models/User.php'],
result: $this->getFixture('user_model.php'),
),
$this->functionCall(
name: 'file_put_contents',
arguments: ['app/Providers/AppServiceProvider.php', $this->getFixture('modified_app_service_provider.php')],
),
$this->functionCall(
name: 'file_put_contents',
arguments: ['app/Models/User.php', $this->getFixture('modified_user_model.php')],
Expand Down Expand Up @@ -807,11 +816,20 @@ public function testRunWithClerkMobileApp(): void

$this->mockNativeFunction('RonasIT\ProjectInitializator\Support\Parser',
[
$this->functionCall(
name: 'file_get_contents',
arguments: ['app/Providers/AppServiceProvider.php'],
result: $this->getFixture('app_service_provider.php'),
),
$this->functionCall(
name: 'file_get_contents',
arguments: ['app/Models/User.php'],
result: $this->getFixture('user_model.php'),
),
$this->functionCall(
name: 'file_put_contents',
arguments: ['app/Providers/AppServiceProvider.php', $this->getFixture('modified_app_service_provider.php')],
),
$this->functionCall(
name: 'file_put_contents',
arguments: ['app/Models/User.php', $this->getFixture('modified_user_model.php')],
Expand Down
25 changes: 25 additions & 0 deletions tests/fixtures/InitCommandTest/app_service_provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* Register any application services.
*
* @return void
*/
public function register(): void
{
}
}
28 changes: 28 additions & 0 deletions tests/fixtures/InitCommandTest/modified_app_service_provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use RonasIT\Clerk\Contracts\UserRepositoryContract;
use App\Support\Clerk\ClerkUserRepository;

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
$this->app->bind(UserRepositoryContract::class, ClerkUserRepository::class);
}
/**
* Register any application services.
*
* @return void
*/
public function register(): void
{
}
}