Skip to content
Browse files

Merge branch 'master' of github.com:rikh42/band-snb

  • Loading branch information...
2 parents 504a1fd + 63014a8 commit 87559c6f54b64e39fb4bb81f1471d44de1709255 @rikh42 committed Mar 6, 2013
View
4 core/Controller.php
@@ -118,6 +118,7 @@ public function redirectResponse($routeName, array $args=array(), Response $resp
* is also OK (/etc.htm) as we add the current requests http host to it first.
* @param $url
* @param \snb\http\Response $response
+ * @return \snb\http\Response
*/
public function redirectUrlResponse($url, Response $response = null)
{
@@ -127,7 +128,7 @@ public function redirectUrlResponse($url, Response $response = null)
}
// If the URL appears to be a relative URL (/some/path/test.png)
- if (preg_match("/^/[^/]/ui", $url) == 1)
+ if (preg_match('%^/[^/]%iu', $url) == 1)
{
// Add in the protocol and host
/* @var $request \snb\http\Request */
@@ -139,6 +140,7 @@ public function redirectUrlResponse($url, Response $response = null)
// finally, set the redirection
$response->setRedirectToURL($url);
+ return $response;
}
View
12 core/Kernel.php
@@ -121,6 +121,18 @@ public function boot()
$this->booted = true;
}
+
+ /**
+ * Can be called on shutdown to cleanly close various services
+ */
+ public function shutdown()
+ {
+
+ }
+
+
+
+
/**
* Calls the AppKernel to find the list of packages being used
*/
View
1 core/KernelInterface.php
@@ -13,6 +13,7 @@
interface KernelInterface
{
public function boot();
+ public function shutdown();
public function getPackagePath($name);
public function findPath($name);
public function findResource($name, $type);
View
14 errors/ErrorHandler.php
@@ -56,12 +56,11 @@ class ErrorHandler
private $level;
private $exceptionHandler;
+
/**
- * Register the error handler.
- *
- * @param integer $level The level at which the conversion to Exception is done (null to use the error_reporting() value and 0 to disable)
- *
- * @return The registered error handler
+ * @param ExceptionHandler $exceptionHandler
+ * @param null $level - The level at which the conversion to Exception is done (null to use the error_reporting() value and 0 to disable)
+ * @return ErrorHandler
*/
public static function register(ExceptionHandler $exceptionHandler, $level = null)
{
@@ -75,6 +74,11 @@ public static function register(ExceptionHandler $exceptionHandler, $level = nul
return $handler;
}
+
+ /**
+ * Sets the level at which errors become exceptions
+ * @param $level
+ */
public function setLevel($level)
{
$this->level = null === $level ? error_reporting() : $level;
View
45 errors/FlattenExceptionFormatter.php
@@ -41,6 +41,12 @@
*/
class FlattenExceptionFormatter
{
+
+ /**
+ * Turn a flattened Exception into HTML
+ * @param FlattenException $exception
+ * @return string
+ */
public static function formatException(FlattenException $exception)
{
$count = count($exception->getAllPrevious());
@@ -69,6 +75,45 @@ public static function formatException(FlattenException $exception)
}
+
+ /**
+ * Turn a flat exception into plain text
+ * @param FlattenException $exception
+ * @return string
+ */
+ public static function formatExceptionPlain(FlattenException $exception)
+ {
+ $count = count($exception->getAllPrevious());
+ $content = '';
+ foreach ($exception->toArray() as $position => $e)
+ {
+ $ind = $count - $position + 1;
+ $total = $count + 1;
+ $class = $e['class'];
+ $message = nl2br($e['message']);
+ $content .= "$ind/$total: $class: $message\n";
+ foreach ($e['trace'] as $i => $trace)
+ {
+ $content .= '- ';
+ if ($trace['function'])
+ {
+ $content .= sprintf('at %s%s%s()', $trace['class'], $trace['type'], $trace['function']);
+ }
+ if (isset($trace['file']) && isset($trace['line']))
+ {
+ $content .= sprintf('in %s line %s', $trace['file'], $trace['line']);
+ }
+ $content .= "\n";
+ }
+
+ $content .= "\n";
+ }
+
+ return $content;
+
+ }
+
+
public static function abbrClass($class)
{
$parts = explode('\\', $class);
View
34 http/SessionStorage.php
@@ -18,6 +18,7 @@
class SessionStorage extends ContainerAware implements SessionStorageInterface
{
protected $started;
+ protected $writeEnabled;
protected $flashMessages;
const FLASH_KEY = '_flashes';
@@ -30,6 +31,7 @@ public function __construct()
{
// default values
$this->started = false;
+ $this->writeEnabled = true;
}
//==============================
@@ -67,6 +69,38 @@ public function start()
$_SESSION[self::FLASH_KEY] = array();
}
+
+ /**
+ * Enable or disable writing to the session, where supported by the driver (SessionStorageDB only at present)
+ * @param $writeEnable
+ */
+ public function setWriteEnable($writeEnable)
+ {
+ $this->writeEnabled = $writeEnable;
+ }
+
+
+ /**
+ * Close the session before the end of the script.
+ */
+ public function closeAndWrite()
+ {
+ // Allow writing, briefly
+ $old = $this->writeEnabled;
+ $this->setWriteEnable(true);
+
+ // Stop if we were started
+ if ($this->started) {
+ session_write_close();
+ }
+
+ // make sure we are stopped
+ $this->started = false;
+ $this->setWriteEnable($old);
+ }
+
+
+
//==============================
// get
// Gets a value stored in the session
View
89 http/SessionStorageDb.php
@@ -18,9 +18,10 @@ class SessionStorageDb extends SessionStorage
{
protected $database;
- //==============================
- // __construct
- //==============================
+
+ /**
+ * @param \snb\core\DatabaseInterface $database
+ */
public function __construct(DatabaseInterface $database)
{
parent::__construct();
@@ -29,10 +30,11 @@ public function __construct(DatabaseInterface $database)
$this->database = $database;
}
- //==============================
- // start
- // Starts a session
- //==============================
+
+ /**
+ * Starts the session. This sets up all the Session handler functions to
+ * use our overrides, allowing us to write to a database instead
+ */
public function start()
{
// If we have already started the session, don't do it again
@@ -57,10 +59,12 @@ public function start()
- //==============================
- // open
- // Called by PHP when the session is first opened
- //==============================
+ /**
+ * Called by PHP when the session is first opened
+ * @param $path
+ * @param $name
+ * @return bool
+ */
public function open($path, $name)
{
return true;
@@ -69,10 +73,10 @@ public function open($path, $name)
- //==============================
- // close
- // Called by PHP when the session finally closed
- //==============================
+ /**
+ * Called by PHP when the session finally closed
+ * @return bool
+ */
public function close()
{
return true;
@@ -81,10 +85,11 @@ public function close()
- //==============================
- // read
- // called by PHP to read the session data. All the data is read in one go
- //==============================
+ /**
+ * called by PHP to read the session data. All the data is read in one go
+ * @param $sessionID
+ * @return string
+ */
public function read($sessionID)
{
// try and find the session data in the database
@@ -107,28 +112,42 @@ public function read($sessionID)
- //==============================
- // write
- // called by PHP to write all the session data to the database
- //==============================
+
+ /**
+ * called by PHP to write all the session data to the database
+ * @param $sessionID - the PHP session ID (basically a random string of characters)
+ * @param $data - The actual data to be stored
+ * @return bool - returns true
+ */
public function write($sessionID, $data)
{
- // Build the query to update the session in the DB
- $sql = "UPDATE sessions SET sData=:data, iLastTouched=:time WHERE sSessionId=:sessionid LIMIT 1";
+ // If we've been stopped, don't do anything.
+ if (!$this->started) {
+ return true;
+ }
+
+ // If writing has been disabled, don't write to the database
+ if (!$this->writeEnabled) {
+ return true;
+ }
+
+ // prepare the data for the query
$param = array(
- 'text:sessionid' => $sessionID,
- 'text:data' => base64_encode($data),
- 'int:time' => time()
+ 'id' => $sessionID,
+ 'data' => base64_encode($data),
+ 'time' => time()
);
- // try and do it
- $updateCount = $this->database->query($sql, $param);
- if ($updateCount==0) {
- // if we failed, it is because the session was not in the db, so add it
- $this->startNewSession($sessionID, $data);
- }
+ // duplicate some of the data for PDO
+ $param['udata'] = $param['data'];
+ $param['utime'] = $param['time'];
+
+ // Try and insert or update the data
+ $sql = "INSERT INTO sessions (sSessionId, sData, iLastTouched) VALUES (:id, :data, :time) "
+ . "ON DUPLICATE KEY UPDATE sData=:udata, iLastTouched=:utime ";
- // yay
+ // do it.
+ $this->database->query($sql, $param);
return true;
}
View
2 http/SessionStorageInterface.php
@@ -11,6 +11,8 @@
interface SessionStorageInterface
{
public function start();
+ public function setWriteEnable($writeEnable);
+ public function closeAndWrite();
public function get($key, $default=null);
public function set($key, $value);
public function remove($key);
View
4 http/SessionStorageNull.php
@@ -21,6 +21,10 @@ public function start()
{
}
+ public function closeAndWrite()
+ {
+ }
+
public function get($key, $default=null)
{
return $default;
View
12 logger/Logger.php
@@ -11,9 +11,9 @@
class Logger implements LoggerInterface
{
- const DEBUG = 100; // useful to know if you were debugging the app
+ const DEBUG_LOGGER = 100; // useful to know if you were debugging the app (changed to DEBUG_LOGGER to prevent clash with other constants)
const INFO = 200; // Stuff it would be good to know about (eg a login)
- const WARNING = 300; // something stange is going on
+ const WARNING = 300; // something strange is going on
const ERROR = 400; // there was some kind of error
protected $handler;
@@ -79,7 +79,7 @@ public function getHtmlLog()
*/
public function debug($message, $extraData = null)
{
- $this->addRecord(self::DEBUG, $message, $extraData);
+ $this->addRecord(self::DEBUG_LOGGER, $message, $extraData);
}
/**
@@ -129,7 +129,7 @@ public function logMemory($message)
'current' => memory_get_usage(true)
);
- $this->addRecord(self::DEBUG, $message, $data);
+ $this->addRecord(self::DEBUG_LOGGER, $message, $data);
}
/**
@@ -149,7 +149,7 @@ public function logQuery($message, $sql, $args, $queryTime)
'time' => $queryTime
);
- $this->addRecord(self::DEBUG, $message, $data);
+ $this->addRecord(self::DEBUG_LOGGER, $message, $data);
}
/**
@@ -164,6 +164,6 @@ public function logTime($message)
'time' => microtime(true)
);
- $this->addRecord(self::DEBUG, $message, $data);
+ $this->addRecord(self::DEBUG_LOGGER, $message, $data);
}
}
View
10 routing/Route.php
@@ -185,6 +185,16 @@ public function isMatch($path, Request $request)
return true;
}
+
+ /**
+ * Returns the URL that matched the route, or empty string
+ * @return string
+ */
+ public function getMatchedUri()
+ {
+ return $this->matchedUri;
+ }
+
/**
* Gets the name of the class for the controller
* @return string

0 comments on commit 87559c6

Please sign in to comment.
Something went wrong with that request. Please try again.