Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/laravel stub integration #12

Merged
merged 8 commits into from
Jan 14, 2024
Merged
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,15 @@ return Stencil::make()
->use('Illuminate\Database\Eloquent\Model')
->curlyStatement('class i_name extends Model', fn(Stencil $s) => $s
->line('use HasFactory;')
);
)
->overrideStubLocation(base_path('Domain/Other/Path/i_name.php'));
```

### Changing the stub output location

In larger projects, you may not be using the default locations where Laravel generates the file. If you would like to change this,
you can call the `overrideStubLocation` method on the Stencil, and provide a custom location where you would like the stub file to be written to.
Note that this method is implemented via a macro, and code completion will not be available for it.


If you have a formatter installed, such as Pint, PHP CS Fixer, or StyleCI, your stencil will be formatted as well!
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ parameters:
- src
tmpDir: build/phpstan
checkMissingIterableValueType: false
excludePaths:
- src/Laravel/LaravelCodeStencilServiceProvider.php
2 changes: 1 addition & 1 deletion src/CodeStencilHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function multilineComment(string|array $comment): static
return $this->foreach($lines, fn(self $s, string $line) => $s->line($line));
}

public function phpdoc(string|array $summary = null, string|array $description = null, array $tags = null): static
public function phpdoc(string|array|null $summary = null, string|array|null $description = null, ?array $tags = null): static
{
if ($summary === null) {
$summary = [];
Expand Down
16 changes: 10 additions & 6 deletions src/Laravel/LaravelCodeStencilServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class LaravelCodeStencilServiceProvider extends ServiceProvider
{
use RegistersOverrideStubLocationMacro;

/**
* Register services.
*/
Expand Down Expand Up @@ -50,17 +52,19 @@ public function boot(): void

$prefixedArgs = [];

foreach($availableArgs as $arg => $val) {
foreach ($availableArgs as $arg => $val) {
$prefixedArgs['i_' . $arg] = $val;
}

foreach($newFiles as $newFile) {
foreach ($newFiles as $newFile) {
(new StencilFileProcessor($newFile, $prefixedArgs))();
}

App::forgetInstance('command-file-list');
});
}

$this->registerOverrideStubLocationMacro();
}

private function getFiles(): array
Expand All @@ -70,19 +74,19 @@ private function getFiles(): array
if ($file->isDir()) {
$baseDirectoryPath = trim(str_replace(base_path(), '', $file->getPathname()), '/');

foreach(config('code-stencil.ignore.directories') as $dir) {
foreach (config('code-stencil.ignore.directories') as $dir) {
if ($dir === $baseDirectoryPath) {
return false;
}
}
} else {
foreach(config('code-stencil.ignore.files') as $fileName) {
foreach (config('code-stencil.ignore.files') as $fileName) {
if ($file->getFilename() === $fileName) {
return false;
}
}

foreach(config('code-stencil.ignore.patterns') as $pattern) {
foreach (config('code-stencil.ignore.patterns') as $pattern) {
if (preg_match($pattern, $file->getPath())) {
return false;
}
Expand All @@ -96,7 +100,7 @@ private function getFiles(): array
$rii = new RecursiveIteratorIterator($filterIter);

$files = [];
foreach($rii as $file) {
foreach ($rii as $file) {
if (!$file->isDir()) {
$files[] = $file->getPathname();
}
Expand Down
20 changes: 20 additions & 0 deletions src/Laravel/RegistersOverrideStubLocationMacro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace CodeStencil\Laravel;

use CodeStencil\Stencil;

trait RegistersOverrideStubLocationMacro
{
private function registerOverrideStubLocationMacro(): void
{
Stencil::macro('overrideStubLocation', function(string $path) {
$newPath = $this->substituteVariables($path);
$newPath = $this->applyFunctions($newPath);

$this->variable('overrideStubLocation', $newPath);

return $this;
});
}
}
23 changes: 22 additions & 1 deletion src/Laravel/StencilFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ public function __invoke()

$stencil->variable($this->variables);

$stencil->save($this->path);
$savePath = $this->resolveSavePath($stencil);

$stencil->save($savePath);

if ($savePath != $this->path) {
unlink($this->path);
}
}

private function resolveSavePath(Stencil $stencil): string
{
$reflectionClass = new \ReflectionClass($stencil);
$prop = $reflectionClass->getProperty('variables');
$prop->setAccessible(true);

$definedVariables = $prop->getValue($stencil);

if (array_key_exists('overrideStubLocation', $definedVariables)) {
return $definedVariables['overrideStubLocation'];
}

return $this->path;
}
}
11 changes: 8 additions & 3 deletions src/Stencil.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
namespace CodeStencil;

use Closure;
use function CodeStencil\Utility\array_flatten;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;

use function CodeStencil\Utility\array_flatten;

class Stencil
{
use CodeStencilHelpers;
use FileHeaderStencil;
use Macroable;
use Conditionable;
use FileHeaderStencil;
use LaravelStringHelpers;
use Macroable;

protected string $content = '';

Expand Down Expand Up @@ -301,6 +302,10 @@ protected function format(string $path): void
return;
}

if (!$this->formattingEnabled) {
return;
}

$this->getStencilFormatter()($path);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/.pest/snapshots/StencilFileProcessorTest/process_file.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class test extends Model{
use HasFactory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class test extends Model{
use HasFactory;
}
13 changes: 13 additions & 0 deletions tests/Fixtures/LaravelStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use CodeStencil\Stencil;

return Stencil::make()
->php()
->namespace('App\Models')
->use('Illuminate\Database\Eloquent\Factories\HasFactory')
->use('Illuminate\Database\Eloquent\Model')
->curlyStatement('class i_name extends Model', fn(Stencil $s) => $s
->line('use HasFactory;')
)
->disableFormat();
14 changes: 14 additions & 0 deletions tests/Fixtures/LaravelStubCustomLocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use CodeStencil\Stencil;

return Stencil::make()
->php()
->namespace('App\Models')
->use('Illuminate\Database\Eloquent\Factories\HasFactory')
->use('Illuminate\Database\Eloquent\Model')
->curlyStatement('class i_name extends Model', fn(Stencil $s) => $s
->line('use HasFactory;')
)
->disableFormat()
->overrideStubLocation(__DIR__ . DIRECTORY_SEPARATOR . 'Output.php');
35 changes: 35 additions & 0 deletions tests/StencilFileProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use CodeStencil\Laravel\RegistersOverrideStubLocationMacro;
use CodeStencil\Laravel\StencilFileProcessor;

uses(RegistersOverrideStubLocationMacro::class);

test('process file', function() {
$realStubPath = __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'LaravelStub.php';
$tmpPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'LaravelStub.php';
copy($realStubPath, $tmpPath);

(new StencilFileProcessor($tmpPath, ['i_name' => 'test']))();

$contents = file_get_contents($tmpPath);

expect($contents)->toMatchSnapshot();
});

test('process file custom location', function() {
$realStubPath = __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'LaravelStubCustomLocation.php';
$tmpDir = sys_get_temp_dir();
$tmpPath = $tmpDir . DIRECTORY_SEPARATOR . 'LaravelStubCustomLocation.php';
copy($realStubPath, $tmpPath);

$this->registerOverrideStubLocationMacro();

(new StencilFileProcessor($tmpPath, ['i_name' => 'test']))();

$contents = file_get_contents($tmpDir . DIRECTORY_SEPARATOR . 'Output.php');

expect($contents)->toMatchSnapshot();

expect(file_exists($tmpPath))->toBeFalse();
});