Skip to content

Instantly share code, notes, and snippets.

@nerdsrescueme
Created November 4, 2011 15:29
Show Gist options
  • Save nerdsrescueme/1339590 to your computer and use it in GitHub Desktop.
Save nerdsrescueme/1339590 to your computer and use it in GitHub Desktop.
Message Implementation
<?php
namespace Atom\Session\Driver;
class File {
protected $path;
public function open($path, $sid)
{
$this->path = LIBRARY_PATH.'/storage/sessions';
return true;
}
public function close()
{
return true;
}
public function read($id)
{
return (string) file_get_contents($this->path.'/sess_'.$id);
}
public function write($id, $data)
{
// Get flash data and write with this...
return file_put_contents($this->path.'/sess_'.$id, $data);
}
public function destroy($id)
{
return unlink($this->path.'/'.$id);
}
public function gc($lifetime)
{
foreach(glob($this->path.'/sess_*') as $file)
{
(filemtime($file) + $lifetime) < time() and unlink($file);
}
return true;
}
}
/** End of file file.php */
<?php
namespace Atom\Session;
class Flash extends \Atom\Message {
public function has($key)
{
return $this->any(function($flash) use ($key)
{
return $key == $flash['key'];
});
}
public function get($key)
{
return $this->find(function($flash) use($key)
{
if($flash['aged'] == true)
{
unset($flash);
return false;
}
if($key == $flash['key'])
{
$flash['aged'] = true;
return true;
}
});
}
public function age($key = null)
{
$this->each(function($flash) use ($key)
{
if($key == $flash['key'] or $key === null)
{
$flash['aged'] = true;
}
});
}
public function delete($key = null)
{
if($key === null)
{
$this->enumerable = array();
return;
}
$this->each(function($flash) use ($key)
{
if($key == $flash['key'])
{
unset($flash);
}
});
}
}
/** End of file flash.php */
<?php
namespace Atom\Message;
/**
* Message Class
*
* The message class is a reusable class meant to make simple
* messaging within other classes simpler, and more powerful.
*
*
*/
class Message extends \Atom\Design\Collection {}
/** End of file message.php */
<?php
namespace Atom;
class Session extends \Atom\Design\Creational\Singleton {
final public static function initialize()
{
// Get session driver from Config.
$handler = 'file';
$handler = new \Atom\Session\File();
session_set_save_handler(
array($handler, 'open'),
array($handler, 'close'),
array($handler, 'read'),
array($handler, 'write'),
array($handler, 'destroy'),
array($handler, 'gc')
);
// Get information from config and set to session_*
// Cookies, name, id?, stuff like that...
session_start();
}
public static function get($name, $default = null)
{
return \Atom\Arr::get($_SESSION, $name, $default);
}
public static function set($name, $data)
{
\Atom\Arr::set($_SESSION, $name, $data);
}
public static function delete($name)
{
unset($_SESSION[$name]);
}
}
/** End of file session.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment