updated session task to run through migration system.

This commit is contained in:
Taylor Otwell
2012-01-24 16:19:28 -06:00
parent 27fdb1e3f5
commit c9eb7bdf35
9 changed files with 195 additions and 26 deletions

View File

@@ -124,7 +124,7 @@ class Migrator extends Task {
// By only removing the migration after it has successfully rolled back,
// we can re-run the rollback command in the event of any errors with
// the migration. When we re-run, only the migrations that have not
// been rolled back for the batch will still be in the database.
// been rolled back will still be in the database.
$this->database->delete($migration['bundle'], $migration['name']);
}
@@ -176,8 +176,8 @@ class Migrator extends Task {
/**
* Generate a new migration file.
*
* @param array $arguments
* @return void
* @param array $arguments
* @return string
*/
public function make($arguments = array())
{
@@ -188,11 +188,11 @@ class Migrator extends Task {
list($bundle, $migration) = Bundle::parse($arguments[0]);
// The migration path is prefixed with the UNIX timestamp, which
// The migration path is prefixed with the date timestamp, which
// is a better way of ordering migrations than a simple integer
// incrementation, since developers may start working on the
// next migration at the same time unknowingly.
$date = date('Y_m_d').'_'.time();
$prefix = date('Y_m_d');
$path = Bundle::path($bundle).'migrations'.DS;
@@ -201,9 +201,16 @@ class Migrator extends Task {
// when we try to write the migration file.
if ( ! is_dir($path)) mkdir($path);
File::put($path.$date.'_'.$migration.EXT, $this->stub($bundle, $migration));
$file = $path.$prefix.'_'.$migration.EXT;
File::put($file, $this->stub($bundle, $migration));
echo "Great! New migration created!";
// Once the migration has been created, we'll return the
// migration file name so it can be used by the task
// consumer if necessary.
return $file;
}
/**
@@ -219,8 +226,7 @@ class Migrator extends Task {
// The class name is formatted simialrly to tasks and controllers,
// where the bundle name is prefixed to the class if it is not in
// the default bundle. However, unlike tasks, there is nothing
// appended to the class name since they're already unique.
// the default bundle.
$class = Bundle::class_prefix($bundle).Str::classify($migration);
return str_replace('{{class}}', $class, $stub);

View File

@@ -116,7 +116,7 @@ class Resolver {
// naming collisions with other bundle's migrations.
$prefix = Bundle::class_prefix($bundle);
$class = $prefix.substr($name, 22);
$class = $prefix.substr($name, 11);
$migration = new $class;

View File

@@ -1,11 +1,11 @@
<?php namespace Laravel\CLI\Tasks;
<?php namespace Laravel\CLI\Tasks\Session;
use Laravel\File;
use Laravel\Config;
use Laravel\Database\Schema;
use Laravel\Session\Drivers\Sweeper;
class Session extends Task {
class Manager extends Task {
/**
* Generate the session table on the database.
@@ -19,7 +19,7 @@ class Session extends Task {
{
$table->create();
// The session table consists simply of a ID, a UNIX timestamp to
// The session table consists simply of an ID, a UNIX timestamp to
// indicate the expiration time, and a blob field which will hold
// the serialized form of the session payload.
$table->string('id')->length(40)->primary('session_primary');

View File

@@ -0,0 +1,77 @@
<?php namespace Laravel\CLI\Tasks\Session;
use Laravel\IoC;
use Laravel\File;
use Laravel\Config;
use Laravel\Session;
use Laravel\CLI\Tasks\Task;
use Laravel\Database\Schema;
use Laravel\Session\Drivers\Sweeper;
use Laravel\CLI\Tasks\Migrate\Migrator;
class Manager extends Task {
/**
* Generate the session table on the database.
*
* @param array $arguments
* @return void
*/
public function table($arguments = array())
{
$migrator = IoC::resolve('task: migrate');
// To create the session table, we will actually create a database
// migration and then run it. This allows the application to stay
// portable through migrations while still having a session table
// generated on the database.
$migration = $migrator->make(array('create_session_table'));
$stub = SYS_PATH.'cli/tasks/session/migration'.EXT;
File::put($migration, File::get($stub));
// By default no session driver is specified in the configuration.
// Since the developer is requesting that the session table be
// created on the database, we'll set the driver to database
// to save an extra step for the developer.
$config = File::get(APP_PATH.'config/session'.EXT);
$config = str_replace(
"'driver' => '',",
"'driver' => 'database',",
$config
);
File::put(APP_PATH.'config/session'.EXT, $config);
echo PHP_EOL;
$migrator->run();
}
/**
* Sweep the expired sessions from storage.
*
* @param array $arguments
* @return void
*/
public function sweep($arguments = array())
{
$driver = Session::factory(Config::get('session.driver'));
// If the driver implements the "Sweeper" interface, we know that
// it can sweep expired sessions from storage. Not all drivers
// need be sweepers, as stores like Memcached and APC will
// perform their own garbage collection.
if ($driver instanceof Sweeper)
{
$lifetime = Config::get('session.lifetime');
$driver->sweep(time() - ($lifetime * 60));
}
echo "The session table has been swept!";
}
}

View File

@@ -0,0 +1,40 @@
<?php
class Create_Session_Table {
/**
* Make changes to the database.
*
* @return void
*/
public function up()
{
Schema::table(Config::get('session.table'), function($table)
{
$table->create();
// The session table consists simply of an ID, a UNIX timestamp to
// indicate the expiration time, and a blob field which will hold
// the serialized form of the session payload.
$table->string('id')->length(40)->primary('session_primary');
$table->integer('last_activity');
$table->text('data');
});
}
/**
* Revert the changes to the database.
*
* @return void
*/
public function down()
{
Schema::table(Config::get('session.table'), function($table)
{
$table->drop();
});
}
}