Skip to content

Commit

Permalink
simplify database seeding to provide more ovrall flexibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 7, 2013
1 parent 07cb4ef commit ec69723
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 126 deletions.
56 changes: 10 additions & 46 deletions Console/SeedCommand.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php namespace Illuminate\Database\Console;

use Illuminate\Console\Command;
use Illuminate\Database\Seeder;
use Illuminate\Events\Dispatcher;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
Expand Down Expand Up @@ -30,46 +28,17 @@ class SeedCommand extends Command {
*/
protected $resolver;

/**
* The database seeder instance.
*
* @var Illuminate\Database\Seeder
*/
protected $seeder;

/**
* The event dispatcher instance.
*
* @var Illuminate\Events\Dispatcher
*/
protected $events;

/**
* The path to the seed files.
*
* @var string
*/
protected $path;

/**
* Create a new database seed command instance.
*
* @param Illuminate\Database\ConnectionResolverInterface $resolver
* @param Illuminate\Database\Seeder $seeder
* @param Illuminate\Events\Dispatcher $events
* @param string $path
* @return void
*/
public function __construct(Resolver $resolver, Seeder $seeder, Dispatcher $events, $path)
public function __construct(Resolver $resolver)
{
parent::__construct();

$this->path = $path;
$this->seeder = $seeder;
$this->events = $events;
$this->resolver = $resolver;

$this->registerSeedEventListener();
}

/**
Expand All @@ -79,28 +48,21 @@ public function __construct(Resolver $resolver, Seeder $seeder, Dispatcher $even
*/
public function fire()
{
$name = $this->input->getOption('database');
$this->resolver->setDefaultConnection($this->input->getOption('database'));

$total = $this->seeder->seed($this->resolver->connection($name), $this->path);
$this->getSeeder()->run();

if ($total == 0) $this->info('Nothing to seed.');
$this->info('Database seeded!');
}

/**
* Register the seeding event listener.
* Get a seeder instance from the container.
*
* @return void
* @return DatabaseSeeder
*/
protected function registerSeedEventListener()
protected function getSeeeder()
{
$me = $this;

$this->events->listen('illuminate.seeding', function($e) use ($me)
{
$message = "<info>Seeded table:</info> {$e->table} ({$e->count} records)";

$me->getOutput()->writeln($message);
});
return $this->laravel->make($this->input->getOption('class'));
}

/**
Expand All @@ -111,6 +73,8 @@ protected function registerSeedEventListener()
protected function getOptions()
{
return array(
array('class', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder', 'DatabaseSeeder'),

array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed'),
);
}
Expand Down
108 changes: 28 additions & 80 deletions Seeder.php
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;
}

}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"require": {
"php": ">=5.3.0",
"illuminate/container": "4.0.x",
"illuminate/events": "4.0.x",
"illuminate/support": "4.0.x"
},
Expand Down

0 comments on commit ec69723

Please sign in to comment.