B8Framework fixes

This commit is contained in:
Dmitry Khomutov 2016-04-14 23:10:08 +06:00
parent ec0a23cb70
commit 963225382c
10 changed files with 75 additions and 119 deletions

View file

@ -1,6 +0,0 @@
.DS_Store
._.DS_Store
Thumbs.db
.idea
composer.lock
vendor

View file

@ -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.*

View file

@ -6,6 +6,9 @@ use b8\Config;
use b8\Exception\HttpException\NotFoundException;
use b8\Http;
use b8\View;
use b8\Controller;
use b8\Http\Response;
use b8\Http\Request;
class Application
{
@ -15,22 +18,22 @@ class Application
protected $route;
/**
* @var \b8\Controller
* @var Controller
*/
protected $controller;
/**
* @var b8\Http\Request
* @var Request
*/
protected $request;
/**
* @var b8\Http\Response
* @var Response
*/
protected $response;
/**
* @var b8\Config
* @var 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)) {
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 \b8\Controller
* @return Controller
*/
public function getController()
{
if (empty($this->controller)) {
$namespace = $this->toPhpName($this->route['namespace']);
$controller = $this->toPhpName($this->route['controller']);
$controllerClass = $this->config->get('b8.app.namespace') . '\\' . $namespace . '\\' . $controller . 'Controller';
$controllerClass = $this->getControllerClass($this->route);
$this->controller = $this->loadController($controllerClass);
}
return $this->controller;
}
/**
* @param string $class
*
* @return Controller
*/
protected function loadController($class)
{
$controller = new $class($this->config, $this->request, $this->response);
$controller->init();
return $controller;
}
/**
* @param array $route
*
* @return bool
*/
protected function controllerExists($route)
{
return class_exists($this->getControllerClass($route));
}
/**
* @param array $route
*
* @return string
*/
protected function getControllerClass($route)
{
$namespace = $this->toPhpName($route['namespace']);
$controller = $this->toPhpName($route['controller']);
$controllerClass = $this->config->get('b8.app.namespace') . '\\' . $namespace . '\\' . $controller . 'Controller';
return class_exists($controllerClass);
return $this->config->get('b8.app.namespace') . '\\' . $namespace . '\\' . $controller . 'Controller';
}
public function isValidRoute($route)

View file

@ -9,20 +9,11 @@ namespace b8;
class Cache
{
const TYPE_APC = 'ApcCache';
const TYPE_APC = 'ApcCache';
const TYPE_REQUEST = 'RequestCache';
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.
*/

View file

@ -14,27 +14,27 @@ use b8\View;
abstract class Controller
{
/**
* @var b8\Http\Request
* @var Request
*/
protected $request;
/**
* @var b8\Http\Response
* @var Response
*/
protected $response;
/**
* @var b8\Config
* @var Config
*/
protected $config;
/**
* @var b8\View
* @var View
*/
protected $controllerView;
/**
* @var b8\View
* @var View
*/
protected $view;
@ -60,7 +60,7 @@ abstract class Controller
/**
* Handles an action on this controller and returns a Response object.
* @return b8\Http\Response
* @return Response
*/
public function handleAction($action, $actionParams)
{
@ -113,4 +113,4 @@ abstract class Controller
{
return $this->request->unsetParam($key);
}
}
}

View file

@ -2,15 +2,13 @@
namespace b8;
use b8\Config;
class Database extends \PDO
{
protected static $initialised = false;
protected static $servers = array('read' => array(), 'write' => array());
protected static $connections = array('read' => null, 'write' => null);
protected static $details = array();
protected static $lastUsed = array('read' => null, 'write' => null);
protected static $initialised = false;
protected static $servers = array('read' => array(), 'write' => array());
protected static $connections = array('read' => null, 'write' => null);
protected static $details = array();
protected static $lastUsed = array('read' => null, 'write' => null);
/**
* @deprecated
@ -73,21 +71,20 @@ class Database extends \PDO
self::init();
}
// If the connection hasn't been used for 5 minutes, force a reconnection:
if (!is_null(self::$lastUsed[$type]) && (time() - self::$lastUsed[$type]) > 300) {
self::$connections[$type] = null;
}
// If the connection hasn't been used for 5 minutes, force a reconnection:
if (!is_null(self::$lastUsed[$type]) && (time() - self::$lastUsed[$type]) > 300) {
self::$connections[$type] = null;
}
if(is_null(self::$connections[$type]))
{
if (is_array(self::$servers[$type])) {
// Shuffle, so we pick a random server:
$servers = self::$servers[$type];
shuffle($servers);
} else {
// Only one server was specified
$servers = array(self::$servers[$type]);
}
if(is_null(self::$connections[$type])) {
if (is_array(self::$servers[$type])) {
// Shuffle, so we pick a random server:
$servers = self::$servers[$type];
shuffle($servers);
} else {
// Only one server was specified
$servers = array(self::$servers[$type]);
}
$connection = null;
@ -97,6 +94,11 @@ class Database extends \PDO
// Pull the next server:
$server = array_shift($servers);
if (stristr($server, ':')) {
list($host, $port) = explode(':', $server);
$server = $host . ';port=' . $port;
}
// Try to connect:
try
{

View file

@ -18,19 +18,19 @@ if (!defined('B8_PATH')) {
class Registry
{
/**
* @var \b8\Registry
* @var Registry
*/
protected static $instance;
protected $_data = array();
protected $_params = null;
/**
* @var b8\Config
* @var Config
*/
protected $config;
/**
* @var b8\Http\Request
* @var Request
*/
protected $request;
@ -84,8 +84,4 @@ class Registry
{
return $this->request->unsetParam($key);
}
public function parseInput()
{
}
}

View file

@ -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": "" }
}
}

View file

@ -1,12 +0,0 @@
build_settings:
ignore:
- "vendor"
- "tests"
irc:
server: "irc.freenode.net"
port: 6667
room: "#phpci"
nick: "phpcidev"
test:
lint:

View file

@ -3,32 +3,31 @@
require_once(dirname(__FILE__) . '/../b8/Registry.php');
require_once(dirname(__FILE__) . '/../b8/Cache.php');
use b8\Registry,
b8\Cache;
use b8\Registry, b8\Cache;
class CacheTest extends PHPUnit_Framework_TestCase
{
public function testCreateSingleton()
{
$cache = b8\Cache::getInstance();
$cache = Cache::getCache(Cache::TYPE_APC);
$this->assertTrue($cache instanceof Cache);
}
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->set('anything', 10));
$this->assertTrue(is_null($cache->get('anything')));
b8\Registry::getInstance()->set('DisableCaching', false);
Registry::getInstance()->set('DisableCaching', false);
}
public function testCaching()
{
$cache = b8\Cache::getInstance();
$cache = Cache::getCache(Cache::TYPE_APC);
if($cache->isEnabled())
{