generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Riley19280/feature/laravel-stub-integration
Add laravel stub implementation
- Loading branch information
Showing
7 changed files
with
203 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace CodeStencil\Laravel; | ||
|
||
use Illuminate\Console\Events\CommandFinished; | ||
use Illuminate\Console\Events\CommandStarting; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\ServiceProvider; | ||
use RecursiveCallbackFilterIterator; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use SplFileInfo; | ||
|
||
class LaravelCodeStencilServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register services. | ||
*/ | ||
public function register(): void | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* Bootstrap services. | ||
*/ | ||
public function boot(): void | ||
{ | ||
$this->publishes([ | ||
__DIR__ . '/code-stencil.php' => config_path('code-stencil.php'), | ||
]); | ||
|
||
if (config('code-stencil.enable-compilation')) { | ||
Event::listen(function(CommandStarting $commandStarting) { | ||
Check failure on line 35 in src/Laravel/LaravelCodeStencilServiceProvider.php GitHub Actions / phpstan
|
||
App::instance('command-file-list', $this->getFiles()); | ||
}); | ||
|
||
Event::listen(function(CommandFinished $commandFinished) { | ||
Check failure on line 39 in src/Laravel/LaravelCodeStencilServiceProvider.php GitHub Actions / phpstan
|
||
$previousFiles = App::make('command-file-list'); | ||
|
||
$currentFiles = $this->getFiles(); | ||
|
||
$newFiles = array_values(array_diff($currentFiles, $previousFiles)); | ||
|
||
$availableArgs = [ | ||
...$commandFinished->input->getArguments(), | ||
...$commandFinished->input->getOptions(), | ||
]; | ||
|
||
$prefixedArgs = []; | ||
|
||
foreach ($availableArgs as $arg => $val) { | ||
$prefixedArgs['i_' . $arg] = $val; | ||
} | ||
|
||
foreach ($newFiles as $newFile) { | ||
(new StencilFileProcessor($newFile, $prefixedArgs))(); | ||
} | ||
|
||
App::forgetInstance('command-file-list'); | ||
}); | ||
} | ||
} | ||
|
||
private function getFiles(): array | ||
{ | ||
$dirIter = new RecursiveDirectoryIterator(base_path()); | ||
$filterIter = new RecursiveCallbackFilterIterator($dirIter, function(SplFileInfo $file) use (&$i) { | ||
if ($file->isDir()) { | ||
$baseDirectoryPath = trim(str_replace(base_path(), '', $file->getPathname()), '/'); | ||
|
||
foreach (config('code-stencil.ignore.directories') as $dir) { | ||
if ($dir === $baseDirectoryPath) { | ||
return false; | ||
} | ||
} | ||
} else { | ||
foreach (config('code-stencil.ignore.files') as $fileName) { | ||
if ($file->getFilename() === $fileName) { | ||
return false; | ||
} | ||
} | ||
|
||
foreach (config('code-stencil.ignore.patterns') as $pattern) { | ||
if (preg_match($pattern, $file->getPath())) { | ||
return false; | ||
} | ||
} | ||
|
||
} | ||
|
||
return true; | ||
}); | ||
|
||
$rii = new RecursiveIteratorIterator($filterIter); | ||
|
||
$files = []; | ||
foreach ($rii as $file) { | ||
if (!$file->isDir()) { | ||
$files[] = $file->getPathname(); | ||
} | ||
} | ||
|
||
return $files; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace CodeStencil\Laravel; | ||
|
||
use CodeStencil\Stencil; | ||
|
||
class StencilFileProcessor | ||
{ | ||
public function __construct(protected string $path, protected array $variables) | ||
{ | ||
} | ||
|
||
public function __invoke() | ||
{ | ||
$contents = file_get_contents($this->path); | ||
|
||
if (!str_starts_with($contents, '<?php')) { | ||
return; | ||
} | ||
|
||
if (!str_contains($contents, 'return Stencil::make()') && !str_contains($contents, 'return \CodeStencil\Stencil::make()')) { | ||
return; | ||
} | ||
|
||
/** @var Stencil $stencil */ | ||
$stencil = require $this->path; | ||
|
||
$stencil->variable($this->variables); | ||
|
||
$stencil->save($this->path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
return [ | ||
/** | ||
* If compilation is enabled, then any new stencil files created by commands will be processed | ||
*/ | ||
'enable-compilation' => true, | ||
|
||
/** | ||
* Ignore specific directories, files, or patterns from being processed | ||
*/ | ||
'ignore' => [ | ||
// directories should be relative to the application root. | ||
'directories' => [ | ||
'vendor', | ||
'node_modules', | ||
'.git', | ||
'storage', | ||
'public', | ||
], | ||
// File names, Ex. foo.txt | ||
'files' => [], | ||
// Patterns are matched against the full file path of files | ||
'patterns' => [], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?php | ||
|
||
it('will not use debugging functions') | ||
->expect(['dd', 'dump', 'ray']) | ||
->each->not->toBeUsed(); | ||
//it('will not use debugging functions') | ||
// ->expect(['dd', 'dump', 'ray']) | ||
// ->each->not->toBeUsed(); |