Skip to content

Commit

Permalink
added ability to specify file extension and test update accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Feb 15, 2022
1 parent 80d2f17 commit eef63c8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ All notable changes to this project will be documented in this file.
## [1.0.3] - 2021-12-17
- Merged [pull requests](https://github.com/touhidurabir/laravel-stub-generator/pull/3) from [GENL](https://github.com/GENL).
- Added github issue templates.

## [1.0.4] - 2022-02-16
- Merged [pull request](https://github.com/touhidurabir/laravel-stub-generator/pull/5) from [amirmasoud](https://github.com/amirmasoud) which add the ability to specify the file extension which was only set to php previously.
- Tests update.
- Readme update.
- Minor fix.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ and then implement as follow
StubGenerator::from('some/path/to/stub/file.stub') // the stub file path
->to('some/path/to/store/generated/file') // the store directory path
->as('TheGeneratingFileNameItself') // the generatable file name without extension
->ext('php') // the generatable file name (optional)
->ext('php') // the file extension(optional, by default to php)
->withReplacers([]) // the stub replacing params
->save(); // save the file
```
Expand Down Expand Up @@ -155,7 +155,7 @@ class UserRepository extends BaseRepository {

## Extras

Sometimes what we have is the namespace that follows the **psr-4** standeard and that namespace path is what we intend to use for path. This package can not direcly work with the namespace path and it includes a handy trait that can help up to some extent.
Sometimes what we have is the namespace that follows the **psr-4** standeard and that namespace path is what we intend to use for path. This package can direcly work with the namespace path and it includes a handy trait that can help up to some extent.

To use the **trait**

Expand Down
7 changes: 6 additions & 1 deletion src/Concerns/FileHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ protected function getStoreDirectoryPath(string $path) {
* Get the destination class path.
*
* @param string $name
* @param string $name
* @param string $extension
*
* @return string
*/
protected function getPath(string $path, string $name, string $extension = 'php') {
Expand All @@ -93,7 +96,9 @@ protected function getPath(string $path, string $name, string $extension = 'php'
* If not , create the directory in that path
* And return the final directory path in any case
*
* @param string $path
* @param string $path
* @param bool $fullPath
*
* @return string
*/
protected function generateFilePathDirectory(string $path, bool $fullPath = false) {
Expand Down
1 change: 1 addition & 0 deletions src/StubFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ protected function buildUseableContentFromStubContent(array $replacers) {
* @return self
*/
protected function replaceInStub(string $key, string $content) {

$pattern = "/\{\{\s*$key\s*\}\}/";

$this->generatedContent = preg_replace($pattern, $content, $this->generatedContent);
Expand Down
2 changes: 2 additions & 0 deletions src/StubGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ class StubGenerator {
*/
protected $generatingFileName;


/**
* The generateable file extension
*
* @var string
*/
protected $generatingFileExtension = 'php';


/**
* The replaceable data list of the stub file
Expand Down
31 changes: 30 additions & 1 deletion tests/StubGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function setUp(): void {

$this->beforeApplicationDestroyed(function () use ($self) {

foreach(glob(__DIR__ . '/App/Repositories/*.php') as $fileFullPath) {
foreach(glob(__DIR__ . '/App/Repositories/*.*') as $fileFullPath) {

if ( ! in_array( last(explode('/', $fileFullPath)), $self->cleanUpExcludeFileNames ) ) {

Expand Down Expand Up @@ -216,6 +216,35 @@ public function it_will_generate_store_directory_when_not_exists_if_asked_to() {
}


/**
* @test
*/
public function it_can_generate_file_with_different_extensions() {

$stubGenerator = StubGeneratorFacade::from(__DIR__ . '/stubs/repository.stub', true)
->to(__DIR__ . '/App/Repositories/Extras', true, true)
->as('ExtraRepository')
->ext('yaml')
->withReplacers([
'class' => 'ExtraRepository',
'model' => 'Extra',
'modelInstance' => 'extra',
'modelNamespace' => 'App\\Models',
'baseClass' => 'Touhidurabir\\ModelRepository\\BaseRepository',
'baseClassName' => 'BaseRepository',
'classNamespace' => 'App\\Repositories',
])
->replace(true);

$content = $stubGenerator->toString();
$storeFile = $stubGenerator->save();

$this->assertTrue($storeFile);
$this->assertTrue(File::exists(__DIR__ . '/App/Repositories/Extras/ExtraRepository.yaml'));
$this->assertEquals($content, File::get(__DIR__ . '/App/Repositories/Extras/ExtraRepository.yaml'));
}


/**
* @test
*/
Expand Down

0 comments on commit eef63c8

Please sign in to comment.