Skip to content

Commit eef63c8

Browse files
committed
added ability to specify file extension and test update accordingly
1 parent 80d2f17 commit eef63c8

6 files changed

+47
-4
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ All notable changes to this project will be documented in this file.
1616
## [1.0.3] - 2021-12-17
1717
- Merged [pull requests](https://github.com/touhidurabir/laravel-stub-generator/pull/3) from [GENL](https://github.com/GENL).
1818
- Added github issue templates.
19+
20+
## [1.0.4] - 2022-02-16
21+
- 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.
22+
- Tests update.
23+
- Readme update.
24+
- Minor fix.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ and then implement as follow
2424
StubGenerator::from('some/path/to/stub/file.stub') // the stub file path
2525
->to('some/path/to/store/generated/file') // the store directory path
2626
->as('TheGeneratingFileNameItself') // the generatable file name without extension
27-
->ext('php') // the generatable file name (optional)
27+
->ext('php') // the file extension(optional, by default to php)
2828
->withReplacers([]) // the stub replacing params
2929
->save(); // save the file
3030
```
@@ -155,7 +155,7 @@ class UserRepository extends BaseRepository {
155155

156156
## Extras
157157

158-
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.
158+
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.
159159

160160
To use the **trait**
161161

src/Concerns/FileHelpers.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ protected function getStoreDirectoryPath(string $path) {
8080
* Get the destination class path.
8181
*
8282
* @param string $name
83+
* @param string $name
84+
* @param string $extension
85+
*
8386
* @return string
8487
*/
8588
protected function getPath(string $path, string $name, string $extension = 'php') {
@@ -93,7 +96,9 @@ protected function getPath(string $path, string $name, string $extension = 'php'
9396
* If not , create the directory in that path
9497
* And return the final directory path in any case
9598
*
96-
* @param string $path
99+
* @param string $path
100+
* @param bool $fullPath
101+
*
97102
* @return string
98103
*/
99104
protected function generateFilePathDirectory(string $path, bool $fullPath = false) {

src/StubFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ protected function buildUseableContentFromStubContent(array $replacers) {
121121
* @return self
122122
*/
123123
protected function replaceInStub(string $key, string $content) {
124+
124125
$pattern = "/\{\{\s*$key\s*\}\}/";
125126

126127
$this->generatedContent = preg_replace($pattern, $content, $this->generatedContent);

src/StubGenerator.php

+2
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ class StubGenerator {
4242
*/
4343
protected $generatingFileName;
4444

45+
4546
/**
4647
* The generateable file extension
4748
*
4849
* @var string
4950
*/
5051
protected $generatingFileExtension = 'php';
52+
5153

5254
/**
5355
* The replaceable data list of the stub file

tests/StubGeneratorTest.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function setUp(): void {
3636

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

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

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

@@ -216,6 +216,35 @@ public function it_will_generate_store_directory_when_not_exists_if_asked_to() {
216216
}
217217

218218

219+
/**
220+
* @test
221+
*/
222+
public function it_can_generate_file_with_different_extensions() {
223+
224+
$stubGenerator = StubGeneratorFacade::from(__DIR__ . '/stubs/repository.stub', true)
225+
->to(__DIR__ . '/App/Repositories/Extras', true, true)
226+
->as('ExtraRepository')
227+
->ext('yaml')
228+
->withReplacers([
229+
'class' => 'ExtraRepository',
230+
'model' => 'Extra',
231+
'modelInstance' => 'extra',
232+
'modelNamespace' => 'App\\Models',
233+
'baseClass' => 'Touhidurabir\\ModelRepository\\BaseRepository',
234+
'baseClassName' => 'BaseRepository',
235+
'classNamespace' => 'App\\Repositories',
236+
])
237+
->replace(true);
238+
239+
$content = $stubGenerator->toString();
240+
$storeFile = $stubGenerator->save();
241+
242+
$this->assertTrue($storeFile);
243+
$this->assertTrue(File::exists(__DIR__ . '/App/Repositories/Extras/ExtraRepository.yaml'));
244+
$this->assertEquals($content, File::get(__DIR__ . '/App/Repositories/Extras/ExtraRepository.yaml'));
245+
}
246+
247+
219248
/**
220249
* @test
221250
*/

0 commit comments

Comments
 (0)