Authored by ziy

1、添加缓存

2、修改数据校验
3、修改根方法
4、修改Session
... ... @@ -5,6 +5,7 @@ use Yaf\Controller_Abstract;
use Hood\Helper\View as hoodView;
use Yaf;
use Hood\Validator as hoodValidator;
class Action extends Controller_Abstract
{
private $_viewLink = array();
... ... @@ -74,9 +75,20 @@ class Action extends Controller_Abstract
* @param String $url
* @param String $expression
*/
protected function helpJsRedirect($message = '', $expression = "history.back()")
protected function helpJsRedirect($message = '', $script = "history.back();")
{
$this->helpLocation($message, $expression);
$html = '';
if (!empty($message)) {
header("content-type: text/html; charset=utf-8");
$message = str_replace("\n", "\\n", $message);
$html .= "<script language=\"javascript\">";
$html .= "alert(\"{$message}\");";
$html .= "</script>";
}
$html .= "<script language=\"javascript\">";
$html .= $script;
$html .= "</script>";
die($html);
}
/**
... ...
... ... @@ -8,7 +8,8 @@
namespace Hood;
use Hood\Cache\Memcached as Mc;
use Hood\Cache\Memcached as Mcd;
use Hood\Cache\Memcache as Mc;
use Hood\Cache\CacheRedis;
class Cache
... ... @@ -16,10 +17,22 @@ class Cache
/**
*
* @param null $node
* @return Mc
* @return Mcd
*/
static public function Memcached($node = null, $childNode = 'hosts')
{
$mc = new Mcd();
$mc->setNode($node)->setChildNodes($childNode);
return $mc;
}
/**
* @param null $node
* @param string $childNode
* @return Mc
*/
static public function Memcache($node = null, $childNode = 'hosts')
{
$mc = new Mc();
$mc->setNode($node)->setChildNodes($childNode);
return $mc;
... ...
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 15/4/28
* Time: 下午3:16
*/
namespace Hood\Cache;
use Hood\Core\Root;
class Memcache extends Root implements CacheInterface
{
private $mcInstances = array();
private $persistentIDs = array();
private $section = 'memcached';
private $node = 'servers';
private $tagName = '';
private $prefix = '';
private $persistentID = 'hood.cache';
private $childNodes = 'hosts';
public function __construct($prefix = '', $persistentID = 'hood.cache')
{
parent::__construct();
$this->prefix = $prefix;
$this->persistentIDs[] = $this->persistentID = $persistentID;
}
public function init()
{
if (isset($this->mcInstances[$this->persistentID])) {
$mc = $this->mcInstances[$this->persistentID];
} else {
$instance = new \Memcache();
$server = $this->getServerHost('cache');
$_serverHosts = $server->getServerConfig($this->section, $this->node);
$mcServers = $this->_makeHosts($server->getServer($_serverHosts[$this->childNodes], 2));
foreach ($mcServers as $key => $val) {
$weight = 100;
if (count($val) == 3) {
list($host, $port, $weight) = $val;
} else {
list($host, $port) = $val;
}
$instance->addServer($host, $port, $this->persistentID, $weight);
}
$this->mcInstances[$this->persistentID] = $mc = $instance;
}
return $mc;
}
/**
* 组织host
* @param array $hosts
* @return array
*/
private function _makeHosts(array $hosts)
{
$_server = array();
foreach ($hosts as $key => $val) {
$_server[] = explode(':', $val);
}
return $_server;
}
/**
* 设置mc配置的块节点
* @param $node
* @return $this
*/
public function setNode($node = null)
{
if ($node != null) $this->node = $node;
return $this;
}
/**
* 设置子节点
* @param $childNode
* @return $this
*/
public function setChildNodes($childNode)
{
$this->childNodes = $childNode;
return $this;
}
/**
* 构建tag
* @param bool $mode
* @return string
*/
private function _makeTag($mode = false)
{
if (empty($this->tagName)) return '';
$_tagVal = $this->init()->get($this->tagName);
if (empty($_tagVal) && $mode == true) {
$_tagVal = md5(microtime() . mt_rand() . uniqid());
$this->init()->set($this->tagName, $_tagVal, 0);
}
unset($this->tagName);
return empty($_tagVal) ? '' : $_tagVal . '.';
}
/**
* 检索一个元素
* @param $key
* @param callable $flags
* @return mixed
*/
public function get($key, &$flags = \MEMCACHE_COMPRESSED)
{
return $this->init()->get($this->_makeTag() . $key, $flags);
}
/**
* 向一个新的key下面增加一个元素
* @param $key
* @param $value
* @param $expiration
* @return bool
*/
public function add($key, $value, $expiration = 0)
{
return $this->init()->add($this->_makeTag(true) . $key, $value, $expiration);
}
/**
* 减小数值元素的值
* @param $key
* @param int $offset
* @return int
*/
public function decrement($key, $offset = 1)
{
return $this->init()->decrement($this->_makeTag() . $key, $offset);
}
/**
* @param $key
* @param int $time
* @return bool
*/
public function delete($key, $time = 0)
{
return $this->init()->delete($this->_makeTag() . $key, $time);
}
/**
* 增加数值元素的值
* @param $key
* @param int $offset
* @param int $initialValue
* @param int $expiry
* @return int
*/
public function increment($key, $offset = 1, $initialValue = 0, $expiry = 0)
{
return $this->init()->increment($this->_makeTag() . $key, $offset, $initialValue, $expiry);
}
/**
* 设置
* @param $key
* @param $value
* @param int $expiration
* @return bool
*/
public function set($key, $var, $expire = 0, $flag = \MEMCACHE_COMPRESSED)
{
return $this->init()->set($this->_makeTag(true) . $key, $var, $flag, $expire);
}
/**
* 设置tag
* @param $tagName
* @return $this
*/
public function tag($tagName)
{
$this->tagName = $tagName;
return $this;
}
}
\ No newline at end of file
... ...
... ... @@ -8,7 +8,6 @@
namespace Hood\Core;
use Hood\Debug\DebugError;
use Hood\Core\Server;
class Root
... ... @@ -19,10 +18,6 @@ class Root
*/
protected $applicationEnv;
/**
* @var null
*/
private $debugError = null;
/**
* 缓存时间
... ... @@ -102,16 +97,4 @@ class Root
$serviceFile = APPLICATION_SYSTEM_CONFIG . DIRECTORY_SEPARATOR . implode('.', $serviceFileArray);
return new Server($serviceFile);
}
/**
* 错误处理
* @return DebugError
*/
public function debugError()
{
if ($this->debugError == null) {
$this->debugError = new DebugError();
}
return $this->debugError;
}
}
\ No newline at end of file
... ...
... ... @@ -312,12 +312,10 @@ class Server extends Root
*/
private function _parseIniFile($filename)
{
$this->debugError()->setUserDebug();
$iniArray = parse_ini_file($filename, true);
if ($iniArray == false) {
throw new DebugException(' Ini file ' . $filename . ' not find. ');
}
$this->debugError()->restoreUserDebug();
return $iniArray;
}
... ...
... ... @@ -49,6 +49,14 @@ class CacheSession extends Root implements SessionHandlerInterface
*/
public function __construct()
{
session_set_save_handler(
array(&$this, "open"),
array(&$this, "close"),
array(&$this, "read"),
array(&$this, "write"),
array(&$this, "destroy"),
array(&$this, "gc")
);
if (version_compare(phpversion(), '5.4.0', '>=')) {
register_shutdown_function('session_write_close');
}
... ...
... ... @@ -54,5 +54,6 @@ class Language
],
"active_url" => "{attribute} 不是一个有效的网址.",
"url" => "{attribute} 格式不正确.",
'mobile' => "手机号格式不正确."
];
}
\ No newline at end of file
... ...
... ... @@ -80,10 +80,16 @@ class Validator
*/
protected function explodeRules($rules)
{
$_rules = array();
foreach ($rules as $key => &$rule) {
$rule = (is_string($rule)) ? explode('|', $rule) : $rule;
$_ruleKey = (is_string($key)) ? explode('|', $key) : array($key);
foreach ($_ruleKey as $k) {
$_rules[$k] = $rule;
}
}
return $rules;
unset($rules);
return $_rules;
}
/**
... ... @@ -564,6 +570,27 @@ class Validator
return str_replace(array('{attribute}', '{size}'), array($attribute, $parameters[0]), $message);
}
/**
* 验证手机号
* @param $attribute
* @param $value
* @param $parameters
* @return bool
* @throws \Exception
*/
protected function validateMobile($attribute, $value, $parameters)
{
if (!preg_match('/^1[3|4|5|8|7][0-9]\\d{8}$/si', $value)) {
return false;
}
return true;
}
protected function replaceMobile($message, $attribute, $rule, $parameters)
{
return $message;
}
public function messages()
{
return $this->messages;
... ...
... ... @@ -14,6 +14,8 @@ use Hood\Core\Session\CacheSession;
class Session
{
private static $_session = null;
/**
* 开启Session并设置NameSpace
* @param $namespace
... ... @@ -21,16 +23,9 @@ class Session
*/
static public function start($namespace = 'session_default')
{
if (self::isSessionStarted() == false) {
$_session = new CacheSession();
session_set_save_handler(
array(&$_session, "open"),
array(&$_session, "close"),
array(&$_session, "read"),
array(&$_session, "write"),
array(&$_session, "destroy"),
array(&$_session, "gc")
);
if (self::isSessionStarted() == false || self::$_session == null) {
self::$_session = new CacheSession();
session_start();
}
return new SessionNamespace ($namespace);
... ...