Fixes
This commit is contained in:
parent
8e970853f6
commit
ca2f5ed197
30 changed files with 181 additions and 338 deletions
|
|
@ -5,7 +5,6 @@ namespace b8;
|
|||
use b8\Exception\HttpException\NotFoundException;
|
||||
use b8\Http;
|
||||
use b8\View;
|
||||
use b8\Controller;
|
||||
use b8\Http\Response;
|
||||
use b8\Http\Request;
|
||||
|
||||
|
|
|
|||
|
|
@ -18,8 +18,8 @@ class Config
|
|||
}
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
* @var array
|
||||
*/
|
||||
protected $config = [];
|
||||
|
||||
public function __construct($settings = null)
|
||||
|
|
@ -51,11 +51,11 @@ class Config
|
|||
}
|
||||
|
||||
/**
|
||||
* Get a configuration value by key, returning a default value if not set.
|
||||
* @param $key string
|
||||
* @param $default mixed
|
||||
* @return mixed
|
||||
*/
|
||||
* Get a configuration value by key, returning a default value if not set.
|
||||
* @param $key string
|
||||
* @param $default mixed
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
$keyParts = explode('.', $key);
|
||||
|
|
@ -81,52 +81,56 @@ class Config
|
|||
}
|
||||
|
||||
/**
|
||||
* Set a value by key.
|
||||
* @param $key string
|
||||
* @param $value mixed
|
||||
*/
|
||||
* Set a value by key.
|
||||
* @param $key string
|
||||
* @param $value mixed
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function set($key, $value = null)
|
||||
{
|
||||
$this->config[$key] = $value;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an array of values.
|
||||
*/
|
||||
* Set an array of values.
|
||||
*/
|
||||
public function setArray($array)
|
||||
{
|
||||
self::deepMerge($this->config, $array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Short-hand syntax for get()
|
||||
* @see Config::get()
|
||||
*/
|
||||
* Short-hand syntax for get()
|
||||
* @see Config::get()
|
||||
*/
|
||||
public function __get($key)
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Short-hand syntax for set()
|
||||
* @see Config::set()
|
||||
*/
|
||||
* Short-hand syntax for set()
|
||||
* @see Config::set()
|
||||
*/
|
||||
public function __set($key, $value = null)
|
||||
{
|
||||
return $this->set($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is set
|
||||
*/
|
||||
* Is set
|
||||
*/
|
||||
public function __isset($key)
|
||||
{
|
||||
return isset($this->config[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unset
|
||||
*/
|
||||
* Unset
|
||||
*/
|
||||
public function __unset($key)
|
||||
{
|
||||
unset($this->config[$key]);
|
||||
|
|
|
|||
|
|
@ -1,224 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace b8\Controller;
|
||||
|
||||
use b8\Controller,
|
||||
b8\Type\RestUser,
|
||||
b8\Store\Factory,
|
||||
b8\Exception\HttpException;
|
||||
|
||||
class RestController extends Controller
|
||||
{
|
||||
const SEARCHTYPE_AND = 'AND';
|
||||
const SEARCHTYPE_OR = 'OR';
|
||||
|
||||
public $requiresAuthentication = true;
|
||||
public $updateLastAction = true;
|
||||
|
||||
/**
|
||||
* @var \b8\Type\RestUser
|
||||
*/
|
||||
protected $activeUser;
|
||||
protected $where = [];
|
||||
protected $limit = null;
|
||||
protected $offset = null;
|
||||
protected $joins = [];
|
||||
protected $arrayDepth = 2;
|
||||
protected $params = null;
|
||||
protected $order = [];
|
||||
protected $group = null;
|
||||
protected $manualJoins = [];
|
||||
protected $manualWheres = [];
|
||||
protected $searchType = self::SEARCHTYPE_AND;
|
||||
protected $_resourceName = null;
|
||||
protected $_modelName = null;
|
||||
protected $_tableName = null;
|
||||
protected $_modelClass = null;
|
||||
|
||||
public function init()
|
||||
{
|
||||
}
|
||||
|
||||
protected function setControllerView()
|
||||
{
|
||||
}
|
||||
|
||||
protected function setView()
|
||||
{
|
||||
}
|
||||
|
||||
public function handleAction($action, $actionParams)
|
||||
{
|
||||
$response = call_user_func_array([$this, $action], $actionParams);
|
||||
$this->response->setContent($response);
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
public function setActiveUser(RestUser $user)
|
||||
{
|
||||
$this->activeUser = $user;
|
||||
}
|
||||
|
||||
public function getActiveUser()
|
||||
{
|
||||
return $this->activeUser;
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
if (!$this->activeUser->checkPermission('canRead', $this->_resourceName)) {
|
||||
throw new HttpException\ForbiddenException('You do not have permission do this.');
|
||||
}
|
||||
|
||||
$this->where = $this->_parseWhere();
|
||||
$this->limit = is_null($this->limit) ? $this->getParam('limit', 25) : $this->limit;
|
||||
$this->offset = is_null($this->offset) ? $this->getParam('offset', 0) : $this->offset;
|
||||
$this->order = is_null($this->order) || !count($this->order) ? $this->getParam('order', []) : $this->order;
|
||||
$this->group = is_null($this->group) || !count($this->group) ? $this->getParam('group', null) : $this->group;
|
||||
$this->searchType = $this->getParam('searchType', self::SEARCHTYPE_AND);
|
||||
|
||||
$store = Factory::getStore($this->_modelName);
|
||||
$data = $store->getWhere($this->where, $this->limit, $this->offset, $this->joins, $this->order,
|
||||
$this->manualJoins, $this->group, $this->manualWheres, $this->searchType);
|
||||
|
||||
$rtn = [
|
||||
'debug' => [
|
||||
'where' => $this->where,
|
||||
'searchType' => $this->searchType,
|
||||
],
|
||||
'limit' => $this->limit,
|
||||
'offset' => $this->offset,
|
||||
'total' => $data['count'],
|
||||
'items' => []
|
||||
];
|
||||
|
||||
foreach ($data['items'] as $item) {
|
||||
$rtn['items'][] = $item->toArray($this->arrayDepth);
|
||||
}
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function _parseWhere()
|
||||
{
|
||||
$clauses = [
|
||||
'fuzzy' => 'like',
|
||||
'gt' => '>',
|
||||
'gte' => '>=',
|
||||
'lt' => '<',
|
||||
'lte' => '<=',
|
||||
'neq' => '!=',
|
||||
'between' => 'between',
|
||||
];
|
||||
|
||||
$where = $this->getParam('where', []);
|
||||
$where = array_merge($where, $this->where);
|
||||
|
||||
if (count($where)) {
|
||||
foreach ($where as &$value) {
|
||||
if (!is_array($value) || !isset($value['operator'])) {
|
||||
if (is_array($value) && count($value) == 1) {
|
||||
$value = array_shift($value);
|
||||
}
|
||||
|
||||
$value = [
|
||||
'operator' => '=',
|
||||
'value' => $value,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($clauses as $clause => $operator) {
|
||||
$fields = $this->getParam($clause, []);
|
||||
|
||||
if (count($clause)) {
|
||||
if (!is_array($fields)) {
|
||||
$fields = [$fields];
|
||||
}
|
||||
foreach ($fields as $field) {
|
||||
if (isset($where[$field])) {
|
||||
$where[$field]['operator'] = $operator;
|
||||
if ($operator == 'like') {
|
||||
$where[$field]['value'] = str_replace(' ', '%', $where[$field]['value']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $where;
|
||||
}
|
||||
|
||||
public function get($key)
|
||||
{
|
||||
if (!$this->activeUser->checkPermission('canRead', $this->_resourceName)) {
|
||||
throw new HttpException\ForbiddenException('You do not have permission do this.');
|
||||
}
|
||||
|
||||
$rtn = Factory::getStore($this->_modelName)->getByPrimaryKey($key);
|
||||
|
||||
if (is_object($rtn) && method_exists($rtn, 'toArray')) {
|
||||
$rtn = $rtn->toArray($this->arrayDepth);
|
||||
}
|
||||
|
||||
return [strtolower($this->_modelName) => $rtn];
|
||||
}
|
||||
|
||||
public function put($key)
|
||||
{
|
||||
if (!$this->activeUser->checkPermission('canEdit', $this->_resourceName)) {
|
||||
throw new HttpException\ForbiddenException('You do not have permission do this.');
|
||||
}
|
||||
|
||||
$store = Factory::getStore($this->_modelName);
|
||||
|
||||
if ($obj = $store->getByPrimaryKey($key)) {
|
||||
$obj->setValues($this->getParams());
|
||||
$rtn = $store->save($obj);
|
||||
|
||||
return [strtolower($this->_modelName) => $rtn->toArray($this->arrayDepth)];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function post()
|
||||
{
|
||||
if (!$this->activeUser->checkPermission('canCreate', $this->_resourceName)) {
|
||||
throw new HttpException\ForbiddenException('You do not have permission do this.');
|
||||
}
|
||||
|
||||
$store = Factory::getStore($this->_modelName);
|
||||
|
||||
$modelClass = $this->_modelClass;
|
||||
$obj = new $modelClass();
|
||||
$obj->setValues($this->getParams());
|
||||
$rtn = $store->save($obj);
|
||||
|
||||
return [strtolower($this->_modelName) => $rtn->toArray($this->arrayDepth)];
|
||||
}
|
||||
|
||||
public function delete($key)
|
||||
{
|
||||
if (!$this->activeUser->checkPermission('canDelete', $this->_resourceName)) {
|
||||
throw new HttpException\ForbiddenException('You do not have permission do this.');
|
||||
}
|
||||
|
||||
$store = Factory::getStore($this->_modelName);
|
||||
|
||||
try {
|
||||
if ($obj = $store->getByPrimaryKey($key)) {
|
||||
$store->delete($obj);
|
||||
return ['deleted' => true];
|
||||
}
|
||||
} catch (\Exception $ex) {
|
||||
}
|
||||
|
||||
return ['deleted' => false];
|
||||
}
|
||||
}
|
||||
|
|
@ -255,4 +255,4 @@ class Map
|
|||
|
||||
return $fkMethod;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,4 +128,4 @@ class Response
|
|||
{
|
||||
return $this->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,7 @@
|
|||
|
||||
namespace b8\Http;
|
||||
|
||||
use b8\Application;
|
||||
use b8\Config;
|
||||
use b8\Application, b8\Config;
|
||||
|
||||
class Router
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace b8\View;
|
||||
|
||||
class UserView extends Template
|
||||
{
|
||||
public function __construct($string)
|
||||
{
|
||||
trigger_error('Use of UserView is now deprecated. Please use Template instead.', E_USER_NOTICE);
|
||||
parent::__construct($string);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue