Skip to content

Commit

Permalink
Work on session interface
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Dec 17, 2014
1 parent 5ebb079 commit d275f04
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 24 deletions.
9 changes: 8 additions & 1 deletion default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
namespace = App
production = false
encoding = "UTF-8"
session_cs = false

[session]
#Treats session keys as case sensitive
case_sensitive = false
#Session namespace separator
separator = "."
#Session base namespace (defaults to App namespace if false)
base_namespace = false

[request]
api_prefix = "api"
Expand Down
11 changes: 9 additions & 2 deletions src/Core/Start.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use \Gt\Response\Redirect;
use \Gt\Api\ApiFactory;
use \Gt\Dispatcher\DispatcherFactory;
use \Gt\Session\Session;

final class Start {

Expand Down Expand Up @@ -47,12 +48,18 @@ public function __construct($uri) {
$response = new Response($config["response"], $production);
$apiFactory = new ApiFactory($config["api"]);

session_start();
$sessionNs = $config["app"]->namespace;
if($config["session"]->base_namespace != false) {
$sessionNs = $config["session"]->base_namespace;
}
$session = new Session($config["session"], $sessionNs);

$dispatcher = DispatcherFactory::createDispatcher(
$appNamespace,
$request,
$response,
$apiFactory
$apiFactory,
$session
);

// Dispatcher::process returns null on a successful call, only returning
Expand Down
8 changes: 6 additions & 2 deletions src/Dispatcher/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ abstract class Dispatcher {
private $request;
private $response;
private $apiFactory;
private $session;

public function __construct($appNamespace, $request, $response, $apiFactory) {
public function __construct($appNamespace, $request, $response,
$apiFactory, $session) {
$this->appNamespace = $appNamespace;
$this->request = $request;
$this->response = $response;
$this->apiFactory = $apiFactory;
$this->session = $session;
}

/**
Expand Down Expand Up @@ -143,7 +146,8 @@ public function process() {
$this->appNamespace,
$fullUri,
$this->apiFactory,
$content
$content,
$this->session
);

// Call the correct methods on each Logic object:
Expand Down
31 changes: 17 additions & 14 deletions src/Dispatcher/DispatcherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Gt\Request\Request;
use Gt\Response\Response;
use Gt\Api\ApiFactory;
use Gt\Session\Session;

class DispatcherFactory {

Expand All @@ -18,33 +19,35 @@ class DispatcherFactory {
* @param Request $request Representing the HTTP request
* @param Response $response Representing the HTTP response
* @param ApiFactory $apiFactory API Access Layer
* @param Session $session Session manager
*
* @return ApiDispatcher|PageDispatcher The appropriate Dispatcher object
*/
public static function createDispatcher($appNamespace,
Request $request, Response $response, ApiFactory $apiFactory) {
public static function createDispatcher($appNamespace, Request $request,
Response $response, ApiFactory $apiFactory, Session $session) {
$className = "\\Gt\\Dispatcher\\";
$type = $request->getType();

switch($type) {
case Request::TYPE_API:
return new ApiDispatcher(
$appNamespace,
$request,
$response,
$apiFactory
);
$className .= "ApiDispatcher";
break;

case Request::TYPE_PAGE:
return new PageDispatcher(
$appNamespace,
$request,
$response,
$apiFactory
);
$className .= "PageDispatcher";
break;

default:
throw new \Gt\Core\Exception\InvalidAccessException();
}

return new $className(
$appNamespace,
$request,
$response,
$apiFactory,
$session
);
}

}#
4 changes: 3 additions & 1 deletion src/Logic/Logic.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ abstract class Logic {
protected $api;
protected $db;
protected $content;
protected $session;

public function __construct($apiFactory, $content) {
public function __construct($apiFactory, $content, $session) {
$this->api = $apiFactory;
$this->content = $content;
$this->session = $session;
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Logic/LogicFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ class LogicFactory {
* @param string $appNamespace Base namespace containing all application logic
* @param string $uri The current request URI
* @param ApiFactory $apiFactory API Access Layer
* @param Session $session Session manager
*
* @return array An array containing all appropriate Logic objects,
* depending on request type, in execution order
*/
public static function create($appNamespace, $uri, $apiFactory, $content) {
public static function create($appNamespace, $uri, $apiFactory,
$content, $session) {
$objArray = [];
$topPath = Path::get(Path::PAGE);

Expand All @@ -38,7 +40,8 @@ public static function create($appNamespace, $uri, $apiFactory, $content) {

$objArray []= new $className(
$apiFactory,
$content
$content,
$session
);
}

Expand Down
7 changes: 5 additions & 2 deletions src/Logic/PageLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ abstract class PageLogic extends Logic {
/**
* @param ApiFactory $apiFactory API Access Layer
* @param Document $content Dom document representing the response's content
* @param Session $session Session manager
*
* @return void
*/
public function __construct($apiFactory, $content) {
parent::__construct($apiFactory, $content);
public function __construct($apiFactory, $content, $session) {
parent::__construct($apiFactory, $content, $session);

// Synonyms
$this->document = $content;
$this->dom = $this->document;

Expand Down
108 changes: 108 additions & 0 deletions src/Session/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
*
* PHP.Gt (http://php.gt)
* @copyright Copyright Ⓒ 2014 Bright Flair Ltd. (http://brightflair.com)
* @license Apache Version 2.0, January 2004. http://www.apache.org/licenses
*/
namespace Gt\Session;

class Session {

private $config;
private $store;

public function __construct($config) {
$this->config = $config;
session_start();
if(!isset($_SESSION[$this->config->base_namespace])) {
$_SESSION[$this->config->base_namespace] = new Store($this->config);
}

$this->store = $_SESSION[$this->config->base_namespace];
}


/**
*
*/
public function get($key) {
return $this->store[$key];
}

/**
*
*/
public function exists($key) {
return isset($this->store[$key]);
}

/**
*
*/
public function set($key, $value) {
$this->store[$key] = $value;
}

/**
*
*/
public function delete($key) {
unset($this->store[$key]);
}

/**
*
*/
private function fixCase($key) {
if(!$config->case_sensitive) {
return strtoupper($key);
}

return $key;
}

/**
*
*/
private function getNamespaceArray($string) {
$nsArray = array();

if(is_string($ns)) {
$nsArray = explode(".", $ns);
}
else if(is_array($ns)) {
$nsArray = $ns;
}
else {
// TODO: throw exception
die("getNsArray error!!!");
}

return $nsArray;
}

/**
* Initialises a nested array and returns reference to the deepest (the leaf).
*
* @param array $arrayContainer description
*/
private function initLeaf(&$arrayContainer, $nsToInit, &$leaf = null) {
if(empty($nsToInit)) {
return $leaf;
}

$initKey = array_shift($nsToInit);

if(!isset($arrayContainer[$initKey])) {
$arrayContainer[$initKey] = array();
}

return self::initLeaf(
$arrayContainer[$initKey],
$nsToInit,
$arrayContainer[$initKey]
);
}

}#
35 changes: 35 additions & 0 deletions src/Session/Store.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Represents a branch of a session namespace.
* Session storage Shop.Basket.Intl.value has three stores (Shop that contains
* Basket, that in turn contains Intl, that in turn contains the StoreValue
* represented by value).
*
* PHP.Gt (http://php.gt)
* @copyright Copyright Ⓒ 2014 Bright Flair Ltd. (http://brightflair.com)
* @license Apache Version 2.0, January 2004. http://www.apache.org/licenses
*/
namespace Gt\Session;

class Store implements \ArrayAccess {

private $tempStorage = [];

public function __construct($config) {
$this->caseSensitive = $config->case_sensitive;
}

public function offsetExists($key) {
return isset($this->tempStorage[$key]);
}
public function offsetGet($key) {
return $this->tempStorage[$key];
}
public function offsetSet($key, $value) {
$this->tempStorage[$key] = $value;
}
public function offsetUnset($key) {
unset($this->tempStorage[$key]);
}

}#
15 changes: 15 additions & 0 deletions src/Session/StoreValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
*
* PHP.Gt (http://php.gt)
* @copyright Copyright Ⓒ 2014 Bright Flair Ltd. (http://brightflair.com)
* @license Apache Version 2.0, January 2004. http://www.apache.org/licenses
*/
namespace Gt\Session;

class StoreValue {

public function __construct() {
}

}#
15 changes: 15 additions & 0 deletions test/Unit/Api/Api.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* PHP.Gt (http://php.gt)
* @copyright Copyright Ⓒ 2014 Bright Flair Ltd. (http://brightflair.com)
* @license Apache Version 2.0, January 2004. http://www.apache.org/licenses
*/
namespace Gt\Api;

class Api_Test extends \PHPUnit_Framework_TestCase {

public function testTest() {
$this->assertFalse(false);
}

}#

0 comments on commit d275f04

Please sign in to comment.