Commit c9eb7bdf authored by Taylor Otwell's avatar Taylor Otwell

updated session task to run through migration system.

parent 27fdb1e3
......@@ -15,17 +15,7 @@ Bundle::start(DEFAULT_BUNDLE);
* retrieve them from the various parts of the CLI code. We can use
* the Request class to access them conveniently.
*/
$_SERVER['cli'] = array();
foreach ($_SERVER['argv'] as $key => $value)
{
if (starts_with($value, '--'))
{
$option = array_get($_SERVER['argv'], $key + 1, true);
array_set($_SERVER, 'cli.'.substr($value, 2), $option);
}
}
list($arguments, $_SERVER['cli']) = Console::options($_SERVER['argv']);
/**
* The Laravel environment may be specified on the CLI using the "env"
......@@ -65,7 +55,7 @@ require SYS_PATH.'cli/dependencies'.EXT;
*/
try
{
Command::run(array_slice($_SERVER['argv'], 1));
Command::run(array_slice($arguments, 1));
}
catch (\Exception $e)
{
......
<?php namespace Laravel\CLI;
class Console {
/**
* Parse the command line arguments and return the results.
*
* The returned array contains the arguments and the options.
*
* @param array $argv
* @param array
*/
public static function options($argv)
{
$options = array();
$arguments = array();
for ($i = 0, $count = count($argv); $i < $count; $i++)
{
$argument = $argv[$i];
// If the CLI argument starts with a double hyphen, it is an option,
// so we will extract the value and add it to the array of options
// to be returned by the method.
if (starts_with($argument, '--'))
{
// By default, we will assume the value of the options is true,
// but if the option contains an equals sign, we will take the
// value to the right of the equals sign as the value and
// remove the value from the option key.
list($key, $value) = array(substr($argument, 2), true);
if (($equals = strpos($argument, '=')) !== false)
{
$key = substr($argument, 2, $equals - 2);
$value = substr($argument, $equals + 1);
}
$options[$key] = $value;
}
// If the CLI argument does not start with a double hyphen it is
// simply an argument to be passed to the console task so we'll
// add it to the array of "regular" arguments.
else
{
$arguments[] = $argument;
}
}
return array($arguments, $options);
}
}
\ No newline at end of file
......@@ -43,7 +43,7 @@ IoC::singleton('task: key', function()
*/
IoC::singleton('task: session', function()
{
return new Tasks\Session;
return new Tasks\Session\Manager;
});
/**
......
......@@ -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);
......
......@@ -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;
......
<?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');
......
<?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!";
}
}
\ No newline at end of file
<?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();
});
}
}
\ No newline at end of file
......@@ -81,6 +81,7 @@ Autoloader::$mappings = array(
'Laravel\\Cache\\Drivers\\Redis' => SYS_PATH.'cache/drivers/redis'.EXT,
'Laravel\\Cache\\Drivers\\Database' => SYS_PATH.'cache/drivers/database'.EXT,
'Laravel\\CLI\\Console' => SYS_PATH.'cli/console'.EXT,
'Laravel\\CLI\\Command' => SYS_PATH.'cli/command'.EXT,
'Laravel\\CLI\\Tasks\\Task' => SYS_PATH.'cli/tasks/task'.EXT,
'Laravel\\CLI\\Tasks\\Bundle\\Bundler' => SYS_PATH.'cli/tasks/bundle/bundler'.EXT,
......@@ -92,7 +93,7 @@ Autoloader::$mappings = array(
'Laravel\\CLI\\Tasks\\Migrate\\Resolver' => SYS_PATH.'cli/tasks/migrate/resolver'.EXT,
'Laravel\\CLI\\Tasks\\Migrate\\Database' => SYS_PATH.'cli/tasks/migrate/database'.EXT,
'Laravel\\CLI\\Tasks\\Key' => SYS_PATH.'cli/tasks/key'.EXT,
'Laravel\\CLI\\Tasks\\Session' => SYS_PATH.'cli/tasks/session'.EXT,
'Laravel\\CLI\\Tasks\\Session\\Manager' => SYS_PATH.'cli/tasks/session/manager'.EXT,
'Laravel\\Database\\Connection' => SYS_PATH.'database/connection'.EXT,
'Laravel\\Database\\Expression' => SYS_PATH.'database/expression'.EXT,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment