Skip to content

Commit

Permalink
Feature/laravel stub integration (#12)
Browse files Browse the repository at this point in the history
* Add ability to override stub location when generating

* Fix styling

* test stencil file processor

* Fix styling

* Disable formatting in tests

* Fix tests

* Fix styling

* Ignore service provider for phpstan

---------

Co-authored-by: Riley19280 <[email protected]>
  • Loading branch information
Riley19280 and Riley19280 authored Jan 14, 2024
1 parent 28f406e commit da929e3
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 2 deletions.
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
4 changes: 4 additions & 0 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 @@ -61,6 +63,8 @@ public function boot(): void
App::forgetInstance('command-file-list');
});
}

$this->registerOverrideStubLocationMacro();
}

private function getFiles(): array
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;
}
}
4 changes: 4 additions & 0 deletions src/Stencil.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,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();
});

0 comments on commit da929e3

Please sign in to comment.