From c9eb7bdf35acfde350e42615d4a6c166fdd88423 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 24 Jan 2012 16:19:28 -0600 Subject: [PATCH] updated session task to run through migration system. --- laravel/cli/artisan.php | 14 +---- laravel/cli/console.php | 55 ++++++++++++++++++ laravel/cli/dependencies.php | 2 +- laravel/cli/tasks/migrate/migrator.php | 22 ++++--- laravel/cli/tasks/migrate/resolver.php | 2 +- laravel/cli/tasks/session.php | 6 +- laravel/cli/tasks/session/manager.php | 77 +++++++++++++++++++++++++ laravel/cli/tasks/session/migration.php | 40 +++++++++++++ laravel/core.php | 3 +- 9 files changed, 195 insertions(+), 26 deletions(-) create mode 100644 laravel/cli/console.php create mode 100644 laravel/cli/tasks/session/manager.php create mode 100644 laravel/cli/tasks/session/migration.php diff --git a/laravel/cli/artisan.php b/laravel/cli/artisan.php index 838e1833..398091bc 100644 --- a/laravel/cli/artisan.php +++ b/laravel/cli/artisan.php @@ -15,17 +15,7 @@ * 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 @@ */ try { - Command::run(array_slice($_SERVER['argv'], 1)); + Command::run(array_slice($arguments, 1)); } catch (\Exception $e) { diff --git a/laravel/cli/console.php b/laravel/cli/console.php new file mode 100644 index 00000000..f909fd7e --- /dev/null +++ b/laravel/cli/console.php @@ -0,0 +1,55 @@ +database->delete($migration['bundle'], $migration['name']); } @@ -176,8 +176,8 @@ public function install() /** * Generate a new migration file. * - * @param array $arguments - * @return void + * @param array $arguments + * @return string */ public function make($arguments = array()) { @@ -188,11 +188,11 @@ public function make($arguments = array()) 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 @@ public function make($arguments = array()) // 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 @@ protected function stub($bundle, $migration) // 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); diff --git a/laravel/cli/tasks/migrate/resolver.php b/laravel/cli/tasks/migrate/resolver.php index 7b7d2537..e4c80cc0 100644 --- a/laravel/cli/tasks/migrate/resolver.php +++ b/laravel/cli/tasks/migrate/resolver.php @@ -116,7 +116,7 @@ protected function resolve($migrations) // 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; diff --git a/laravel/cli/tasks/session.php b/laravel/cli/tasks/session.php index 73b6dfd5..8f9511af 100644 --- a/laravel/cli/tasks/session.php +++ b/laravel/cli/tasks/session.php @@ -1,11 +1,11 @@ -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'); diff --git a/laravel/cli/tasks/session/manager.php b/laravel/cli/tasks/session/manager.php new file mode 100644 index 00000000..55724df1 --- /dev/null +++ b/laravel/cli/tasks/session/manager.php @@ -0,0 +1,77 @@ +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 diff --git a/laravel/cli/tasks/session/migration.php b/laravel/cli/tasks/session/migration.php new file mode 100644 index 00000000..8ef8632e --- /dev/null +++ b/laravel/cli/tasks/session/migration.php @@ -0,0 +1,40 @@ +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 diff --git a/laravel/core.php b/laravel/core.php index fab9b058..373b527a 100644 --- a/laravel/core.php +++ b/laravel/core.php @@ -81,6 +81,7 @@ '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 @@ '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,