manager.php 2.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?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\Session\Drivers\Sweeper;

class Manager extends Task {

	/**
	 * Generate the session table on the database.
	 *
	 * @param  array  $arguments
	 * @return void
	 */
18
	public function table($arguments = array())
19 20 21
	{
		$migrator = IoC::resolve('task: migrate');

22 23 24 25 26 27 28
		$key = IoC::resolve('task: key');

		// Since sessions can't work without an application key, we will go
		// ahead and set the key if one has not already been set for the
		// application so the developer doesn't need to set it.
		$key->generate();

29 30
		// To create the session table, we will actually create a database
		// migration and then run it. This allows the application to stay
31
		// portable through the framework's migrations system.
32 33
		$migration = $migrator->make(array('create_session_table'));

Taylor Otwell committed
34
		$stub = path('sys').'cli/tasks/session/migration'.EXT;
35 36 37

		File::put($migration, File::get($stub));

38
		// By default no session driver is set within the configuration.
39
		// Since the developer is requesting that the session table be
40 41
		// created on the database, we'll set it.
		$this->driver('database');
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

		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'));

58 59 60
		// If the driver implements the "Sweeper" interface, we know that it
		// can sweep expired sessions from storage. Not all drivers need be
		// sweepers since they do their own.
61 62 63 64 65 66 67 68 69 70
		if ($driver instanceof Sweeper)
		{
			$lifetime = Config::get('session.lifetime');

			$driver->sweep(time() - ($lifetime * 60));
		}

		echo "The session table has been swept!";
	}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	/**
	 * Set the session driver to a given value.
	 *
	 * @param  string  $driver
	 * @return void
	 */
	protected function driver($driver)
	{
		// By default no session driver is set within the configuration.
		// This method will replace the empty driver option with the
		// driver specified in the arguments.
		$config = File::get(path('app').'config/session'.EXT);

		$config = str_replace(
			"'driver' => '',",
			"'driver' => 'database',",
			$config
		);

		File::put(path('app').'config/session'.EXT, $config);
	}

93
}