B8Framework fixes
This commit is contained in:
parent
ec0a23cb70
commit
963225382c
6
B8Framework/.gitignore
vendored
6
B8Framework/.gitignore
vendored
|
@ -1,6 +0,0 @@
|
||||||
.DS_Store
|
|
||||||
._.DS_Store
|
|
||||||
Thumbs.db
|
|
||||||
.idea
|
|
||||||
composer.lock
|
|
||||||
vendor
|
|
|
@ -1,11 +0,0 @@
|
||||||
b8 Framework
|
|
||||||
============
|
|
||||||
[![Build Status](https://travis-ci.org/Block8/b8framework.png?branch=master)](https://travis-ci.org/Block8/b8framework)
|
|
||||||
|
|
||||||
|
|
||||||
*b8 framework* is a lightweight, simple framework for high-throughput PHP applications. It does not enforce a specific type of application, nor particularly an application structure, it simply helps you to build things.
|
|
||||||
|
|
||||||
We've used the framework to build web sites, APIs, and web applications, each handling tens of millions of requests a month.
|
|
||||||
|
|
||||||
|
|
||||||
*b8 framework repository is _not quite_ ready for general consumption yet. We have a tool to help you generate all of your project CRUD for you, which still needs to be included. This will be done over the next few days.*
|
|
|
@ -6,6 +6,9 @@ use b8\Config;
|
||||||
use b8\Exception\HttpException\NotFoundException;
|
use b8\Exception\HttpException\NotFoundException;
|
||||||
use b8\Http;
|
use b8\Http;
|
||||||
use b8\View;
|
use b8\View;
|
||||||
|
use b8\Controller;
|
||||||
|
use b8\Http\Response;
|
||||||
|
use b8\Http\Request;
|
||||||
|
|
||||||
class Application
|
class Application
|
||||||
{
|
{
|
||||||
|
@ -15,22 +18,22 @@ class Application
|
||||||
protected $route;
|
protected $route;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \b8\Controller
|
* @var Controller
|
||||||
*/
|
*/
|
||||||
protected $controller;
|
protected $controller;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var b8\Http\Request
|
* @var Request
|
||||||
*/
|
*/
|
||||||
protected $request;
|
protected $request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var b8\Http\Response
|
* @var Response
|
||||||
*/
|
*/
|
||||||
protected $response;
|
protected $response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var b8\Config
|
* @var Config
|
||||||
*/
|
*/
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
|
@ -64,46 +67,62 @@ class Application
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$action = lcfirst($this->toPhpName($this->route['action']));
|
if (!$this->controllerExists($this->route)) {
|
||||||
|
throw new NotFoundException('Controller ' . $this->toPhpName($this->route['controller']) . ' does not exist!');
|
||||||
|
}
|
||||||
|
|
||||||
|
$action = lcfirst($this->toPhpName($this->route['action']));
|
||||||
if (!$this->getController()->hasAction($action)) {
|
if (!$this->getController()->hasAction($action)) {
|
||||||
throw new NotFoundException('Controller ' . $this->toPhpName($this->route['controller']) . ' does not have action ' . $action);
|
throw new NotFoundException('Controller ' . $this->toPhpName($this->route['controller']) . ' does not have action ' . $action . '!');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->getController()->handleAction($action, $this->route['args']);
|
return $this->getController()->handleAction($action, $this->route['args']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \b8\Controller
|
* @return Controller
|
||||||
*/
|
*/
|
||||||
public function getController()
|
public function getController()
|
||||||
{
|
{
|
||||||
if (empty($this->controller)) {
|
if (empty($this->controller)) {
|
||||||
$namespace = $this->toPhpName($this->route['namespace']);
|
$controllerClass = $this->getControllerClass($this->route);
|
||||||
$controller = $this->toPhpName($this->route['controller']);
|
|
||||||
$controllerClass = $this->config->get('b8.app.namespace') . '\\' . $namespace . '\\' . $controller . 'Controller';
|
|
||||||
$this->controller = $this->loadController($controllerClass);
|
$this->controller = $this->loadController($controllerClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->controller;
|
return $this->controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $class
|
||||||
|
*
|
||||||
|
* @return Controller
|
||||||
|
*/
|
||||||
protected function loadController($class)
|
protected function loadController($class)
|
||||||
{
|
{
|
||||||
$controller = new $class($this->config, $this->request, $this->response);
|
$controller = new $class($this->config, $this->request, $this->response);
|
||||||
$controller->init();
|
$controller->init();
|
||||||
|
|
||||||
return $controller;
|
return $controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $route
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
protected function controllerExists($route)
|
protected function controllerExists($route)
|
||||||
|
{
|
||||||
|
return class_exists($this->getControllerClass($route));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $route
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getControllerClass($route)
|
||||||
{
|
{
|
||||||
$namespace = $this->toPhpName($route['namespace']);
|
$namespace = $this->toPhpName($route['namespace']);
|
||||||
$controller = $this->toPhpName($route['controller']);
|
$controller = $this->toPhpName($route['controller']);
|
||||||
|
return $this->config->get('b8.app.namespace') . '\\' . $namespace . '\\' . $controller . 'Controller';
|
||||||
$controllerClass = $this->config->get('b8.app.namespace') . '\\' . $namespace . '\\' . $controller . 'Controller';
|
|
||||||
|
|
||||||
return class_exists($controllerClass);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isValidRoute($route)
|
public function isValidRoute($route)
|
||||||
|
|
|
@ -9,20 +9,11 @@ namespace b8;
|
||||||
|
|
||||||
class Cache
|
class Cache
|
||||||
{
|
{
|
||||||
const TYPE_APC = 'ApcCache';
|
const TYPE_APC = 'ApcCache';
|
||||||
const TYPE_REQUEST = 'RequestCache';
|
const TYPE_REQUEST = 'RequestCache';
|
||||||
|
|
||||||
protected static $instance = array();
|
protected static $instance = array();
|
||||||
|
|
||||||
/**
|
|
||||||
* LEGACY: Older apps will expect an APC cache in return.
|
|
||||||
* @deprecated
|
|
||||||
*/
|
|
||||||
public static function getInstance()
|
|
||||||
{
|
|
||||||
return self::getCache(self::TYPE_APC);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a cache object of a specified type.
|
* Get a cache object of a specified type.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -14,27 +14,27 @@ use b8\View;
|
||||||
abstract class Controller
|
abstract class Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var b8\Http\Request
|
* @var Request
|
||||||
*/
|
*/
|
||||||
protected $request;
|
protected $request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var b8\Http\Response
|
* @var Response
|
||||||
*/
|
*/
|
||||||
protected $response;
|
protected $response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var b8\Config
|
* @var Config
|
||||||
*/
|
*/
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var b8\View
|
* @var View
|
||||||
*/
|
*/
|
||||||
protected $controllerView;
|
protected $controllerView;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var b8\View
|
* @var View
|
||||||
*/
|
*/
|
||||||
protected $view;
|
protected $view;
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ abstract class Controller
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles an action on this controller and returns a Response object.
|
* Handles an action on this controller and returns a Response object.
|
||||||
* @return b8\Http\Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public function handleAction($action, $actionParams)
|
public function handleAction($action, $actionParams)
|
||||||
{
|
{
|
||||||
|
@ -113,4 +113,4 @@ abstract class Controller
|
||||||
{
|
{
|
||||||
return $this->request->unsetParam($key);
|
return $this->request->unsetParam($key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,15 +2,13 @@
|
||||||
|
|
||||||
namespace b8;
|
namespace b8;
|
||||||
|
|
||||||
use b8\Config;
|
|
||||||
|
|
||||||
class Database extends \PDO
|
class Database extends \PDO
|
||||||
{
|
{
|
||||||
protected static $initialised = false;
|
protected static $initialised = false;
|
||||||
protected static $servers = array('read' => array(), 'write' => array());
|
protected static $servers = array('read' => array(), 'write' => array());
|
||||||
protected static $connections = array('read' => null, 'write' => null);
|
protected static $connections = array('read' => null, 'write' => null);
|
||||||
protected static $details = array();
|
protected static $details = array();
|
||||||
protected static $lastUsed = array('read' => null, 'write' => null);
|
protected static $lastUsed = array('read' => null, 'write' => null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated
|
* @deprecated
|
||||||
|
@ -73,21 +71,20 @@ class Database extends \PDO
|
||||||
self::init();
|
self::init();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the connection hasn't been used for 5 minutes, force a reconnection:
|
// If the connection hasn't been used for 5 minutes, force a reconnection:
|
||||||
if (!is_null(self::$lastUsed[$type]) && (time() - self::$lastUsed[$type]) > 300) {
|
if (!is_null(self::$lastUsed[$type]) && (time() - self::$lastUsed[$type]) > 300) {
|
||||||
self::$connections[$type] = null;
|
self::$connections[$type] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(is_null(self::$connections[$type]))
|
if(is_null(self::$connections[$type])) {
|
||||||
{
|
if (is_array(self::$servers[$type])) {
|
||||||
if (is_array(self::$servers[$type])) {
|
// Shuffle, so we pick a random server:
|
||||||
// Shuffle, so we pick a random server:
|
$servers = self::$servers[$type];
|
||||||
$servers = self::$servers[$type];
|
shuffle($servers);
|
||||||
shuffle($servers);
|
} else {
|
||||||
} else {
|
// Only one server was specified
|
||||||
// Only one server was specified
|
$servers = array(self::$servers[$type]);
|
||||||
$servers = array(self::$servers[$type]);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$connection = null;
|
$connection = null;
|
||||||
|
|
||||||
|
@ -97,6 +94,11 @@ class Database extends \PDO
|
||||||
// Pull the next server:
|
// Pull the next server:
|
||||||
$server = array_shift($servers);
|
$server = array_shift($servers);
|
||||||
|
|
||||||
|
if (stristr($server, ':')) {
|
||||||
|
list($host, $port) = explode(':', $server);
|
||||||
|
$server = $host . ';port=' . $port;
|
||||||
|
}
|
||||||
|
|
||||||
// Try to connect:
|
// Try to connect:
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
|
@ -18,19 +18,19 @@ if (!defined('B8_PATH')) {
|
||||||
class Registry
|
class Registry
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var \b8\Registry
|
* @var Registry
|
||||||
*/
|
*/
|
||||||
protected static $instance;
|
protected static $instance;
|
||||||
protected $_data = array();
|
protected $_data = array();
|
||||||
protected $_params = null;
|
protected $_params = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var b8\Config
|
* @var Config
|
||||||
*/
|
*/
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var b8\Http\Request
|
* @var Request
|
||||||
*/
|
*/
|
||||||
protected $request;
|
protected $request;
|
||||||
|
|
||||||
|
@ -84,8 +84,4 @@ class Registry
|
||||||
{
|
{
|
||||||
return $this->request->unsetParam($key);
|
return $this->request->unsetParam($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function parseInput()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,22 +0,0 @@
|
||||||
{
|
|
||||||
"name": "block8/b8framework",
|
|
||||||
"type": "library",
|
|
||||||
"description": "Simple, lightweight framework for high-throughput applications",
|
|
||||||
"keywords": ["php", "framework", "b8", "block8", "lightweight", "mvc"],
|
|
||||||
"homepage": "https://github.com/block8/b8framework",
|
|
||||||
"license": "BSD",
|
|
||||||
"authors": [
|
|
||||||
{
|
|
||||||
"name": "Block 8 Limited",
|
|
||||||
"email": "hello@block8.co.uk",
|
|
||||||
"homepage": "http://www.block8.co.uk"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"require": {
|
|
||||||
"php": ">=5.3.0",
|
|
||||||
"symfony/yaml": "2.*"
|
|
||||||
},
|
|
||||||
"autoload": {
|
|
||||||
"psr-0": { "b8": "" }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
build_settings:
|
|
||||||
ignore:
|
|
||||||
- "vendor"
|
|
||||||
- "tests"
|
|
||||||
irc:
|
|
||||||
server: "irc.freenode.net"
|
|
||||||
port: 6667
|
|
||||||
room: "#phpci"
|
|
||||||
nick: "phpcidev"
|
|
||||||
|
|
||||||
test:
|
|
||||||
lint:
|
|
|
@ -3,32 +3,31 @@
|
||||||
require_once(dirname(__FILE__) . '/../b8/Registry.php');
|
require_once(dirname(__FILE__) . '/../b8/Registry.php');
|
||||||
require_once(dirname(__FILE__) . '/../b8/Cache.php');
|
require_once(dirname(__FILE__) . '/../b8/Cache.php');
|
||||||
|
|
||||||
use b8\Registry,
|
use b8\Registry, b8\Cache;
|
||||||
b8\Cache;
|
|
||||||
|
|
||||||
class CacheTest extends PHPUnit_Framework_TestCase
|
class CacheTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testCreateSingleton()
|
public function testCreateSingleton()
|
||||||
{
|
{
|
||||||
$cache = b8\Cache::getInstance();
|
$cache = Cache::getCache(Cache::TYPE_APC);
|
||||||
$this->assertTrue($cache instanceof Cache);
|
$this->assertTrue($cache instanceof Cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDisableCaching()
|
public function testDisableCaching()
|
||||||
{
|
{
|
||||||
b8\Registry::getInstance()->set('DisableCaching', true);
|
Registry::getInstance()->set('DisableCaching', true);
|
||||||
|
|
||||||
$cache = b8\Cache::getInstance();
|
$cache = Cache::getCache(Cache::TYPE_APC);
|
||||||
$this->assertFalse($cache->isEnabled());
|
$this->assertFalse($cache->isEnabled());
|
||||||
$this->assertFalse($cache->set('anything', 10));
|
$this->assertFalse($cache->set('anything', 10));
|
||||||
$this->assertTrue(is_null($cache->get('anything')));
|
$this->assertTrue(is_null($cache->get('anything')));
|
||||||
|
|
||||||
b8\Registry::getInstance()->set('DisableCaching', false);
|
Registry::getInstance()->set('DisableCaching', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCaching()
|
public function testCaching()
|
||||||
{
|
{
|
||||||
$cache = b8\Cache::getInstance();
|
$cache = Cache::getCache(Cache::TYPE_APC);
|
||||||
|
|
||||||
if($cache->isEnabled())
|
if($cache->isEnabled())
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue