public function populateStub($class, $stub, $table, $data, $chunkSize = null, $prerunEvent = null, $postrunEvent = null)
{
$chunkSize = $chunkSize ?: config('iseed::config.chunk_size');
$inserts = '';
$chunks = array_chunk($data, $chunkSize);
foreach ($chunks as $chunk) {
$this->addNewLines($inserts);
$this->addIndent($inserts, 2);
$inserts .= sprintf("\\DB::table('%s')->insert(%s);", $table, $this->prettifyArray($chunk));
}
$stub = str_replace('{{class}}', $class, $stub);
$prerunEventInsert = '';
if ($prerunEvent) {
$prerunEventInsert .= "\$response = Event::until(new {$prerunEvent}());";
$this->addNewLines($prerunEventInsert);
$this->addIndent($prerunEventInsert, 2);
$prerunEventInsert .= 'if ($response === false) {';
$this->addNewLines($prerunEventInsert);
$this->addIndent($prerunEventInsert, 3);
$prerunEventInsert .= 'throw new Exception("Prerun event failed, seed wasn\'t executed!");';
$this->addNewLines($prerunEventInsert);
$this->addIndent($prerunEventInsert, 2);
$prerunEventInsert .= '}';
}
$stub = str_replace('{{prerun_event}}', $prerunEventInsert, $stub);
if (!is_null($table)) {
$stub = str_replace('{{table}}', $table, $stub);
}
$postrunEventInsert = '';
if ($postrunEvent) {
$postrunEventInsert .= "\$response = Event::until(new {$postrunEvent}());";
$this->addNewLines($postrunEventInsert);
$this->addIndent($postrunEventInsert, 2);
$postrunEventInsert .= 'if ($response === false) {';
$this->addNewLines($postrunEventInsert);
$this->addIndent($postrunEventInsert, 3);
$postrunEventInsert .= 'throw new Exception("Seed was executed but the postrun event failed!");';
$this->addNewLines($postrunEventInsert);
$this->addIndent($postrunEventInsert, 2);
$postrunEventInsert .= '}';
}
$stub = str_replace('{{postrun_event}}', $postrunEventInsert, $stub);
$stub = str_replace('{{insert_statements}}', $inserts, $stub);
return $stub;
}