forked from illuminate/database
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify database seeding to provide more ovrall flexibility.
- Loading branch information
1 parent
07cb4ef
commit ec69723
Showing
3 changed files
with
39 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,66 @@ | ||
<?php namespace Illuminate\Database; | ||
|
||
use Illuminate\Events\Dispatcher; | ||
use Illuminate\Container\Container; | ||
use Illuminate\Filesystem\Filesystem; | ||
|
||
class Seeder { | ||
|
||
/** | ||
* The filesystem instance. | ||
* The container instance. | ||
* | ||
* @var Illuminate\Filesystem | ||
* @var Illuminate\Container\Container | ||
*/ | ||
protected $files; | ||
protected $container; | ||
|
||
/** | ||
* The event dispatcher instance. | ||
* Run the database seeds. | ||
* | ||
* @var Illuminate\Events\Dispatcher | ||
*/ | ||
protected $events; | ||
|
||
/** | ||
* The database seed file list. | ||
* | ||
* @var array | ||
* @return void | ||
*/ | ||
protected $seeds; | ||
public function run() {} | ||
|
||
/** | ||
* Create a new database seeder instance. | ||
* Seed the given connection from the given path. | ||
* | ||
* @param Illuminate\Filesystem $files | ||
* @param Illuminate\Events\Dispatcher $events | ||
* @param string $class | ||
* @return void | ||
*/ | ||
public function __construct(Filesystem $files, Dispatcher $events = null) | ||
public function call($class) | ||
{ | ||
$this->files = $files; | ||
$this->events = $events; | ||
$this->resolve($class)->run(); | ||
} | ||
|
||
/** | ||
* Seed the given connection from the given path. | ||
* Resolve an instance of the given seeder class. | ||
* | ||
* @param Illuminate\Database\Connection $connection | ||
* @param string $path | ||
* @return int | ||
* @param string $class | ||
* @return Illuminate\Database\Seeder | ||
*/ | ||
public function seed(Connection $connection, $path) | ||
protected function resolve($class) | ||
{ | ||
$total = 0; | ||
|
||
foreach ($this->getFiles($path) as $file) | ||
if (isset($this->container)) | ||
{ | ||
$records = $this->files->getRequire($file); | ||
|
||
// We'll grab the table name here, which could either come from the array or | ||
// from the filename itself. Then, we will simply insert the records into | ||
// the databases via a connection and fire an event noting the seeding. | ||
$table = $this->getTable($records, $file); | ||
|
||
$connection->table($table)->delete(); | ||
|
||
$connection->table($table)->insert($records); | ||
$instance = $this->container->make($class); | ||
|
||
$total += $count = count($records); | ||
|
||
// Once we have seeded the table, we will fire an event to let any listeners | ||
// know the tables have been seeded and how many records were inserted so | ||
// information can be presented to the developer about the seeding run. | ||
if (isset($this->events)) | ||
{ | ||
$payload = compact('table', 'count'); | ||
|
||
$this->events->fire('illuminate.seeding', $payload); | ||
} | ||
return $instance->setContainer($this->container); | ||
} | ||
else | ||
{ | ||
return new $class; | ||
} | ||
|
||
return $total; | ||
} | ||
|
||
/** | ||
* Get all of the files at a given path. | ||
* | ||
* @param string $path | ||
* @return array | ||
*/ | ||
protected function getFiles($path) | ||
{ | ||
if (isset($this->seeds)) return $this->seeds; | ||
|
||
// If the seeds haven't been read before, we will glob the directory and sort | ||
// them alphabetically just in case the developer is using numbers to make | ||
// the seed run in a certain order based on their database design needs. | ||
$files = $this->files->glob($path.'/*.php'); | ||
|
||
sort($files); | ||
|
||
return $this->seeds = $files; | ||
} | ||
|
||
/** | ||
* Get the table from the given records and file. | ||
* Set the IoC container instance. | ||
* | ||
* @param array $records | ||
* @param string $file | ||
* @return string | ||
* @param Illuminate\Container\Container $container | ||
* @return void | ||
*/ | ||
protected function getTable( & $records, $file) | ||
public function setContainer(Container $container) | ||
{ | ||
$table = array_get($records, 'table', basename($file, '.php')); | ||
|
||
unset($records['table']); | ||
$this->container = $container; | ||
|
||
return $table; | ||
return $this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters