Orangehill\Iseed\Iseed::populateStub PHP Method

populateStub() public method

Populate the place-holders in the seed stub.
public populateStub ( string $class, string $stub, string $table, string $data, integer $chunkSize = null, string $prerunEvent = null, $postrunEvent = null ) : string
$class string
$stub string
$table string
$data string
$chunkSize integer
$prerunEvent string
return string
    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;
    }

Usage Example

示例#1
0
 /**
  * Populate the place-holders in the seed stub.
  *
  * @param string $class
  * @param string $stub
  * @param string $table
  * @param string $data
  * @param int $chunkSize
  * @return string 
  * @static 
  */
 public static function populateStub($class, $stub, $table, $data, $chunkSize = null)
 {
     return \Orangehill\Iseed\Iseed::populateStub($class, $stub, $table, $data, $chunkSize);
 }