Skip to content

Commit

Permalink
加入model和theme的mongodb支持
Browse files Browse the repository at this point in the history
加入model和theme的mongodb支持
  • Loading branch information
hisune committed Jun 23, 2015
1 parent 46e443b commit 975e5c0
Show file tree
Hide file tree
Showing 7 changed files with 494 additions and 126 deletions.
9 changes: 4 additions & 5 deletions Tiny/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static function getPurviewCache()
public static function hasPurview($controller = null, $method = null)
{
if($controller && $method){
$ctr = ucfirst(Config::$application) . '\\' . Config::$controller['0'] . '\\' . $controller;
$ctr = ucfirst(Config::$application) . '\\' . Config::$controller['0'] . '\\' . ucfirst($controller);
}else{
$ctr = Request::$controller;
$explode = explode('\\', $ctr);
Expand All @@ -109,11 +109,10 @@ public static function hasPurview($controller = null, $method = null)
$purview = self::getPurviewCache();

$white = property_exists($ctr, 'authWhite') && isset($ctr::$authWhite['purview']) ? $ctr::$authWhite['purview'] : array();

if(
!in_array($method, $white) &&
!in_array($controller . '@' . $method, $purview) &&
!in_array($controller . '@*', $purview)
!in_array(strtolower($method), $white) &&
!in_array(strtolower($controller . '@' . $method), $purview) &&
!in_array(strtolower($controller . '@*'), $purview)
){
return false;
}
Expand Down
43 changes: 8 additions & 35 deletions Tiny/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __call($method, $args)
switch($this->{$action}['type']){
case 'theme':
$theme = '\\Tiny\\Theme\\' . $this->{$action}['name'];
$setting = lcfirst($method) . $this->{$action}['name'] . 'Setting';
$setting = $this->_themeSetting($method, $action);
$option = isset($this->{$action}['option']) ? $this->{$action}['option'] : array();

$builder = new $theme(lcfirst($method), $option);
Expand All @@ -44,45 +44,13 @@ public function __call($method, $args)

switch($this->{$action}['name']){
case 'Delete': // 删除
if(Request::get('id')){
if($model->delete(Request::get('id'))){
if(Request::get($model->key)){
if($model->delete(Request::get($model->key))){
Error::echoJson(1);
}
}
Error::echoJson(-1);
break;
case 'Mod': // 添加修改
if(Request::isPost()){
$post = Request::post();
if($post){
// save前置函数,用来改变post值
$before = lcfirst($method) . 'ModBefore';
if(method_exists($helper, $before)){
$helper::$before($post);
}
$attribute = array_keys($model::attributes());
foreach($post as $k => $v){
if(in_array($k, $attribute)){
if(is_array($v)) $v = json_encode($v);
$model->$k = $v;
}
}

if(isset($model->_data[$model->key]) && $model->_data[$model->key]){
$result = $model->update();
}else{
$result = $model->save();
}
if($result)
Error::echoJson('1', 'success');
else
Error::echoJson('1', 'save error');
}else
Error::echoJson('-1', 'data error');
}else{
Error::echoJson('-1', 'method error');
}
break;
}
break;
default:
Expand Down Expand Up @@ -113,6 +81,11 @@ public function __get($name)
}
}

private function _themeSetting($method, $action)
{
return lcfirst($method) . $this->{$action}['name'] . 'Setting';
}

/**
* Called before the controller method is run
*/
Expand Down
26 changes: 26 additions & 0 deletions Tiny/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static function renderEnum(array $data, $key = 'id', $value = null)
if ($data) {
foreach ($data as $v) {
if(is_object($v)) $v = (array)$v;
if(is_object($v[$key])) $v[$key] = strval($v[$key]);
if(is_string($value))
$option[$v[$key]] = $v[$value];
else{
Expand Down Expand Up @@ -72,4 +73,29 @@ public static function getHumanTree(array $tree = [], $level = 0, $child = 'chil
return $array;
}

public static function mongoType($type, $data)
{
switch($type){
case 'boolean':
$data = $data ? true : false;
break;
case 'int32':
$data = intval($data);
break;
case 'int64':
$data = new \MongoInt64($data);
break;
case 'time':
$data = strtotime($data);
break;
case 'date':
$data = new \MongoDate($data);
break;
default:
$data = strval($data);
}

return $data;
}

}
164 changes: 164 additions & 0 deletions Tiny/Mongo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php
/**
* Created by hisune.com.
* User: [email protected]
* Date: 2015/6/20
* Time: 18:44
*/
namespace Tiny;

class Mongo
{
public $type = 'mongodb';
public static $db; // db对象

public $key = '_id'; // 主键
protected $name; // 配置数组key
public $_data; // 你懂的
protected $table; // 表名
public $collection; // collection

public function __construct($name = 'mongodb')
{
// 设置当前model的表名
$this->_setTableName();
// 连接数据库
$this->_loadDatabase($name);
}

public function __set($key, $value)
{
$this->_data[$key] = $value;
}

private function _loadDatabase($name = 'mongodb')
{
$this->name = $name;

if (!isset(self::$db[$this->name])) {
$config = Config::config()->{$this->name};
if(!is_array($config))
throw new \Exception ($config . ' not a valid db config');
// Load database
$config['option'] = isset($config['option']) ? $config['option'] : array();
try{
$db = new \MongoClient('mongodb://' . $config['host'] . ':' . $config['port'], $config['option']);
}catch (\Exception $e){
throw new \Exception ('connection mongodb failed');
}
if(!isset($config['db'])){
throw new \Exception ('empty mongodb db set');
}
$db = $db->$config['db'];

$this->collection = $db->{$this->table};
self::$db[$this->name] = $db;
}

$this->collection = self::$db[$this->name]->{$this->table};

return $this->getDb();
}

public function getDb()
{
return self::$db[$this->name];
}

private function _setTableName()
{
if (empty($this->table)) {
$name = get_class($this);
if ($pos = strrpos($name, '\\')) { //有命名空间
$this->table = $this->_parseName(substr($name, $pos + 1));
} else {
$this->table = $this->_parseName($name);
}
}
return $this->table;
}

public function getTableName()
{
return $this->table;
}

private function _parseName($name, $type = 0)
{
if ($type) {
return ucfirst(preg_replace_callback('/_([a-zA-Z])/', function ($match) {
return strtoupper($match[1]);
}, $name));
} else {
return strtolower(trim(preg_replace('/[A-Z]/', '_\\0', $name), '_'));
}
}

// 获取主键
public function getKey()
{
return $this->key;
}

public function find(array $condition = array(), array $field = array())
{
$cursor = $this->collection->find($condition, $field);
$rows = array();
while($cursor->hasNext()){
$rows[] = $cursor->getNext();
}

return $rows;
}

public function findOne($condition = array(), array $field = array())
{
if(is_string($condition)){
return $this->collection->findOne(array($this->key => new \MongoId($condition)), $field);
}elseif(is_array($condition)){
return $this->collection->findOne($condition, $field);
}else{
throw new \Exception ('condition must be string or array');
}
}

public function delete($criteria, array $option = array())
{
if(is_array($criteria)){
return $this->collection->remove($criteria, $option);
}elseif(is_string($criteria)){
return $this->collection->remove(array($this->key => new \MongoId($criteria)), $option);
}else{
throw new \Exception ('delete criteria type error');
}
}

public function save($data = array(), array $option = array())
{
if($data){
return $this->collection->save($data, $option);
}elseif($this->_data){
return $this->collection->save($this->_data, $option);
}else{
throw new \Exception ('save data not be empty');
}
}

public function update(array $criteria = array(), array $new = array(), array $option = array())
{
if($criteria && $new){
return $this->collection->update($criteria, $new, $option);
}elseif($this->_data && isset($this->_data[$this->key])){
$data = $this->_data;
unset($data[$this->key]);
if(is_string($this->_data[$this->key])) $this->_data[$this->key] = new \MongoId($this->_data[$this->key]);
return $this->collection->update(array($this->key => $this->_data[$this->key]), array('$set' => $data));
}else{
throw new \Exception ('update data not be empty');
}
}

public static function attributes(){
return array();
}
}
1 change: 1 addition & 0 deletions Tiny/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

class ORM
{
public $type = 'mysql';
public static $db; // db对象
public static $prefix; // 表前缀

Expand Down
Loading

0 comments on commit 975e5c0

Please sign in to comment.