1: <?php namespace Laravel\CLI\Tasks\Session;
2:
3: use Laravel\IoC;
4: use Laravel\File;
5: use Laravel\Config;
6: use Laravel\Session;
7: use Laravel\CLI\Tasks\Task;
8: use Laravel\Session\Drivers\Sweeper;
9:
10: class Manager extends Task {
11:
12: /**
13: * Generate the session table on the database.
14: *
15: * @param array $arguments
16: * @return void
17: */
18: public function table($arguments = array())
19: {
20: $migrator = IoC::resolve('task: migrate');
21:
22: $key = IoC::resolve('task: key');
23:
24: // Since sessions can't work without an application key, we will go
25: // ahead and set the key if one has not already been set for the
26: // application so the developer doesn't need to set it.
27: $key->generate();
28:
29: // To create the session table, we will actually create a database
30: // migration and then run it. This allows the application to stay
31: // portable through the framework's migrations system.
32: $migration = $migrator->make(array('create_session_table'));
33:
34: $stub = path('sys').'cli/tasks/session/migration'.EXT;
35:
36: File::put($migration, File::get($stub));
37:
38: // By default no session driver is set within the configuration.
39: // Since the developer is requesting that the session table be
40: // created on the database, we'll set it.
41: $this->driver('database');
42:
43: echo PHP_EOL;
44:
45: $migrator->run();
46: }
47:
48: /**
49: * Sweep the expired sessions from storage.
50: *
51: * @param array $arguments
52: * @return void
53: */
54: public function sweep($arguments = array())
55: {
56: $driver = Session::factory(Config::get('session.driver'));
57:
58: // If the driver implements the "Sweeper" interface, we know that it
59: // can sweep expired sessions from storage. Not all drivers need be
60: // sweepers since they do their own.
61: if ($driver instanceof Sweeper)
62: {
63: $lifetime = Config::get('session.lifetime');
64:
65: $driver->sweep(time() - ($lifetime * 60));
66: }
67:
68: echo "The session table has been swept!";
69: }
70:
71: /**
72: * Set the session driver to a given value.
73: *
74: * @param string $driver
75: * @return void
76: */
77: protected function driver($driver)
78: {
79: // By default no session driver is set within the configuration.
80: // This method will replace the empty driver option with the
81: // driver specified in the arguments.
82: $config = File::get(path('app').'config/session'.EXT);
83:
84: $config = str_replace(
85: "'driver' => '',",
86: "'driver' => 'database',",
87: $config
88: );
89:
90: File::put(path('app').'config/session'.EXT, $config);
91: }
92:
93: }