Skip to content

Commit 1143911

Browse files
committed
feat: Add stub_path config option for custom stub templates
Allow users to customize the seed file template by specifying a custom stub path in config/iseed.php. Falls back to the built-in stub when not configured. Based on PR #222 by @bahri-hirfanoglu, with bug fixes (DIR -> __DIR__) and test compatibility improvements.
1 parent 875df19 commit 1143911

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,21 @@ To (re)seed the database go to the Terminal and run Laravel's `db:seed command`
333333
Please note that some users encountered a problem with large DB table exports ([error when seeding from table with many records](https://github.com/orangehill/iseed/issues/4)). The issue was solved by splitting input data into smaller chunks of elements per insert statement. As you may need to change the chunk size value in some extreme cases where DB table has a large number of rows, the chunk size is configurable in iSeed's `config.php` file:
334334

335335
'chunk_size' => 500 // Maximum number of rows per insert statement
336+
337+
## Custom Stub Template
338+
339+
You can customize the seed file template by creating a custom stub file and configuring iSeed to use it. Create a `config/iseed.php` file in your Laravel application:
340+
341+
```php
342+
<?php
343+
return [
344+
'stub_path' => resource_path('stubs'),
345+
];
346+
```
347+
348+
Then create your custom stub at `resources/stubs/seed.stub`. The stub supports the following placeholders:
349+
- `{{class}}` - The seeder class name
350+
- `{{table}}` - The database table name
351+
- `{{insert_statements}}` - The generated insert statements
352+
- `{{prerun_event}}` - Pre-run event code (if specified)
353+
- `{{postrun_event}}` - Post-run event code (if specified)

src/Orangehill/Iseed/Iseed.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,14 @@ public function generateClassName($table, $prefix = null, $suffix = null)
221221
*/
222222
public function getStubPath()
223223
{
224-
return __DIR__ . DIRECTORY_SEPARATOR . 'stubs';
224+
$customPath = null;
225+
try {
226+
$customPath = config('iseed::config.stub_path');
227+
} catch (\Exception $e) {
228+
// Config not available (e.g., in tests)
229+
}
230+
231+
return $customPath ?: __DIR__ . DIRECTORY_SEPARATOR . 'stubs';
225232
}
226233

227234
/**

src/config/config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
return array(
33
'path' => '/database/seeders',
4-
'chunk_size' => 500 // Maximum number of rows per insert statement
4+
'chunk_size' => 500, // Maximum number of rows per insert statement
5+
'stub_path' => null, // Custom stub path (e.g., resource_path('stubs/seed.stub'))
56
);

0 commit comments

Comments
 (0)