Merge branch 'ajax_action'
This commit is contained in:
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Model
|
||||
* @since 2010-02-17
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Config extends Registry
|
||||
|
2
Load.php
2
Load.php
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Load
|
||||
* @since 2010-02-24
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Load
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Model
|
||||
* @since 2010-02-17
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Registry extends ArrayObject
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Action
|
||||
|
@ -3,22 +3,28 @@
|
||||
* AjaxAction
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage face
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
* @subpackage app
|
||||
* @since 2011-04-27
|
||||
*/
|
||||
|
||||
/**
|
||||
* базовый класс для всей экшенов выполняющихся по аякс-запросу
|
||||
* Base class for all ajax Actions
|
||||
*/
|
||||
abstract class AjaxAction extends Action
|
||||
{
|
||||
public $data = 1;
|
||||
/**
|
||||
* Data to output
|
||||
* @var mixed
|
||||
*/
|
||||
public $data = false;
|
||||
|
||||
protected $encode = true;
|
||||
/**
|
||||
* Use json_encode
|
||||
* @var bool
|
||||
*/
|
||||
protected $json_encode = true;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
@ -28,10 +34,13 @@ abstract class AjaxAction extends Action
|
||||
|
||||
function fetch()
|
||||
{
|
||||
// header("Content-type: application/json; charset=utf-8");
|
||||
header("Content-type: text/html; charset=utf-8");
|
||||
if ($this->json_encode === true) {
|
||||
header("Content-type: application/json; charset=utf-8");
|
||||
} else {
|
||||
header("Content-type: text/html; charset=utf-8");
|
||||
}
|
||||
header("Cache-Control: no-store, no-cache, must-revalidate");
|
||||
$this->view->assign('data', $this->encode ? json_encode($this->data) : $this->data);
|
||||
$this->view->assign('data', $this->json_encode ? json_encode($this->data) : $this->data);
|
||||
return $this->view->fetch($this->getTemplate());
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,13 @@ class CliController
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuse cloning
|
||||
*/
|
||||
private function __clone()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @static
|
||||
* @return CliController
|
||||
*/
|
||||
@ -58,7 +65,8 @@ class CliController
|
||||
echo $profile;
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
catch (Exception $e) {
|
||||
$code = $e->getCode();
|
||||
if ($e instanceof ErrorException) {
|
||||
$code = $e->getSeverity();
|
||||
|
@ -5,18 +5,18 @@
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class ErrorAction extends Action
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ErrorException
|
||||
* @var ErrorException|ErrorHTTPException
|
||||
*/
|
||||
public $exception;
|
||||
|
||||
protected $ajax_error = false;
|
||||
|
||||
public function __construct($exception)
|
||||
{
|
||||
$this->exception = $exception;
|
||||
@ -27,18 +27,22 @@ class ErrorAction extends Action
|
||||
{
|
||||
$this->template = 500;
|
||||
if ($this->exception instanceof Error404Exception) {
|
||||
if ($this->isAjaxActionError()) {
|
||||
if (!headers_sent()) {
|
||||
header('HTTP/1.0 404 Not Found');
|
||||
die();
|
||||
}
|
||||
}
|
||||
$this->template = 404;
|
||||
} elseif ($this->exception instanceof ErrorHTTPException) {
|
||||
$this->template = 'HTTP';
|
||||
}
|
||||
$this->logError();
|
||||
$this->sendHTTPCode();
|
||||
}
|
||||
|
||||
public function fetch()
|
||||
{
|
||||
if ($this->isAjaxActionError()) {
|
||||
return $this->exception->getMessage();
|
||||
}
|
||||
return parent::fetch();
|
||||
}
|
||||
|
||||
protected function getTemplate()
|
||||
{
|
||||
return '/actions/' . $this->template;
|
||||
@ -46,22 +50,19 @@ class ErrorAction extends Action
|
||||
|
||||
protected function sendHttpCode()
|
||||
{
|
||||
if (headers_sent()) {
|
||||
return;
|
||||
}
|
||||
switch ($this->template) {
|
||||
case 404:
|
||||
header('HTTP/1.0 404 Not Found');
|
||||
break;
|
||||
case 'HTTP':
|
||||
header($this->exception->getHTTPHeader());
|
||||
break;
|
||||
default:
|
||||
header('HTTP/1.0 500 Internal Server Error');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected function logError()
|
||||
{
|
||||
if ($this->template == 500) {
|
||||
|
||||
$error = 0;
|
||||
$ex = $this->exception;
|
||||
if ($ex instanceof ErrorException) {
|
||||
@ -83,7 +84,7 @@ class ErrorAction extends Action
|
||||
break;
|
||||
}
|
||||
$message = 'PHP ' . $error . ': ' . $ex->getMessage() . ' in ' . $ex->getFile()
|
||||
. ' on line ' . $ex->getLine();
|
||||
. ' on line ' . $ex->getLine();
|
||||
error_log($message);
|
||||
}
|
||||
}
|
||||
@ -94,12 +95,14 @@ class ErrorAction extends Action
|
||||
*/
|
||||
protected function isAjaxActionError()
|
||||
{
|
||||
$trace = $this->exception->getTrace();
|
||||
foreach ($trace as $line) {
|
||||
if (isset($line['class']) && $line['class'] === 'AjaxAction') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return $this->ajax_error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if exception was thrown from AjaxAction subclass
|
||||
*/
|
||||
public function setAjaxError()
|
||||
{
|
||||
$this->ajax_error = true;
|
||||
}
|
||||
}
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-24
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class FrontController
|
||||
@ -65,6 +63,7 @@ class FrontController
|
||||
|
||||
/**
|
||||
*
|
||||
* @param null $view
|
||||
* @return iView
|
||||
*/
|
||||
public function getView($view = null)
|
||||
@ -75,6 +74,7 @@ class FrontController
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @return FrontController
|
||||
*/
|
||||
public function setBaseUrl($url)
|
||||
{
|
||||
@ -115,6 +115,9 @@ class FrontController
|
||||
throw new GeneralException('Layout class "' . $layout_class . '" not found.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @var Layout $layout
|
||||
*/
|
||||
$layout = new $layout_class();
|
||||
$html = $layout->fetch($action);
|
||||
if (Config::get('PROFILER')) {
|
||||
@ -128,7 +131,11 @@ class FrontController
|
||||
} catch (Exception $e) {
|
||||
if (Config::get('DEBUG')) {
|
||||
if (!headers_sent()) {
|
||||
header('HTTP/1.0 500 Internal Server Error');
|
||||
if ($e instanceof ErrorHTTPException) {
|
||||
header($e->getHTTPHeader());
|
||||
} else {
|
||||
header('HTTP/1.0 500 Internal Server Error');
|
||||
}
|
||||
}
|
||||
return ErrorHandler::showDebug($e);
|
||||
}
|
||||
@ -139,7 +146,11 @@ class FrontController
|
||||
*/
|
||||
$layout = new $layout_class();
|
||||
$layout->setException($e);
|
||||
return $layout->fetch(new ErrorAction($e));
|
||||
$error_action = new ErrorAction($e);
|
||||
if (isset($action_class) && is_subclass_of($action_class, 'AjaxAction')) {
|
||||
$error_action->setAjaxError();
|
||||
}
|
||||
return $layout->fetch($error_action);
|
||||
}
|
||||
}
|
||||
}
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-03-07
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class PagerAction extends Action
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class StaticAction extends Action
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Route
|
||||
@ -27,6 +25,7 @@ class Route
|
||||
|
||||
/**
|
||||
* @param array $request
|
||||
* @return bool
|
||||
*/
|
||||
public function match($request)
|
||||
{
|
||||
|
@ -5,15 +5,18 @@
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Router
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Route[]
|
||||
*/
|
||||
protected $routes = array();
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $route_name;
|
||||
|
||||
protected $default_layout = 'Default';
|
||||
@ -33,6 +36,10 @@ class Router
|
||||
$this->routes[$name] = new Route($route, $action, $params, $layout);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @return bool|Route
|
||||
*/
|
||||
public function route($request)
|
||||
{
|
||||
$req = explode('/', trim($request, '/'));
|
||||
@ -48,6 +55,10 @@ class Router
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default layout name
|
||||
* @param string $layout
|
||||
*/
|
||||
public function setDefaultLayout($layout = 'Default')
|
||||
{
|
||||
$this->default_layout = $layout;
|
||||
@ -71,6 +82,10 @@ class Router
|
||||
return $this->error_layout . 'Layout';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return current router name
|
||||
* @return string
|
||||
*/
|
||||
public function getRouteName()
|
||||
{
|
||||
return $this->route_name;
|
||||
|
37
cache/Cache.php
vendored
37
cache/Cache.php
vendored
@ -5,78 +5,75 @@
|
||||
* @package Majestic
|
||||
* @subpackage Cache
|
||||
* @since 2010-03-04
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Cache
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Add an item to the cache
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param int $expire
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function add($key, $value, $expire = 0);
|
||||
|
||||
|
||||
/**
|
||||
* Decrement item's value
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $decrement
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function decrement($key, $decrement = 1);
|
||||
|
||||
|
||||
/**
|
||||
* Delete item from the cache
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $value
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function del($key);
|
||||
|
||||
|
||||
/**
|
||||
* Flush all existing items
|
||||
*
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function flush();
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve item from the cache
|
||||
*
|
||||
*
|
||||
* @param mixed $key
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function get($key);
|
||||
|
||||
|
||||
/**
|
||||
* Increment item's value
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $increment
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function increment($key, $increment = 1);
|
||||
|
||||
|
||||
/**
|
||||
* Replace value of the existing item
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $var
|
||||
* @param mixed $value
|
||||
* @param int $expire
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function replace($key, $value, $expire = 0);
|
||||
|
||||
|
||||
/**
|
||||
* Store data in the cache
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param int $expire
|
||||
|
5
cache/CacheKey.php
vendored
5
cache/CacheKey.php
vendored
@ -5,15 +5,13 @@
|
||||
* @package Majestic
|
||||
* @subpackage Cache
|
||||
* @since 2010-03-10
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class CacheKey
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Cacher
|
||||
* @var Cache
|
||||
*/
|
||||
protected $cacher;
|
||||
|
||||
@ -62,6 +60,7 @@ class CacheKey
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function set($value)
|
||||
{
|
||||
|
8
cache/Cacher.php
vendored
8
cache/Cacher.php
vendored
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Cache
|
||||
* @since 2010-03-04
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Cacher
|
||||
@ -19,6 +17,12 @@ class Cacher
|
||||
*/
|
||||
static protected $caches = array();
|
||||
|
||||
/**
|
||||
* @param $cacher
|
||||
* @param null|string $config
|
||||
* @return Cache
|
||||
* @throws InitializationException
|
||||
*/
|
||||
static public function get($cacher, $config = null)
|
||||
{
|
||||
if (!isset(self::$caches[$cacher])) {
|
||||
|
61
cache/MemcacheCache.php
vendored
61
cache/MemcacheCache.php
vendored
@ -5,37 +5,37 @@
|
||||
* @package Majestic
|
||||
* @subpackage Cache
|
||||
* @since 2010-03-04
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class MemcacheCache extends Cache
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var Memcache
|
||||
*/
|
||||
protected $connection = null;
|
||||
|
||||
|
||||
/**
|
||||
* @var null|string
|
||||
*/
|
||||
protected $key_salt = null;
|
||||
|
||||
|
||||
/**
|
||||
* One hour to live default
|
||||
*
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $expire = 3600;
|
||||
|
||||
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->connection = new Memcache();
|
||||
|
||||
|
||||
if (isset($config['key_salt'])) {
|
||||
$this->key_salt = $config['key_salt'];
|
||||
unset($config['key_salt']);
|
||||
}
|
||||
|
||||
|
||||
$required = array('hostname', 'port');
|
||||
foreach ($config as $c) {
|
||||
foreach ($required as $option) {
|
||||
@ -46,10 +46,10 @@ class MemcacheCache extends Cache
|
||||
$this->connection->addServer($c['hostname'], $c['port']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add an item to the cache
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param int $expire
|
||||
@ -59,10 +59,10 @@ class MemcacheCache extends Cache
|
||||
{
|
||||
return $this->connection->add($this->getKey($key), $value, null, $this->getExpire($expire));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decrement item's value
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $decrement
|
||||
* @return bool
|
||||
@ -71,32 +71,32 @@ class MemcacheCache extends Cache
|
||||
{
|
||||
return $this->connection->decrement($this->getKey($key), $decrement);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete item from the cache
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $value
|
||||
* @internal param int $value
|
||||
* @return bool
|
||||
*/
|
||||
public function del($key)
|
||||
{
|
||||
return $this->connection->delete($this->getKey($key), 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Flush all existing items
|
||||
*
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function flush()
|
||||
{
|
||||
return $this->connection->flush();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve item from the cache
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
@ -104,10 +104,10 @@ class MemcacheCache extends Cache
|
||||
{
|
||||
return $this->connection->get($this->getKey($key));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Increment item's value
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $increment
|
||||
* @return bool
|
||||
@ -116,23 +116,24 @@ class MemcacheCache extends Cache
|
||||
{
|
||||
return $this->connection->increment($this->getKey($key), $increment);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Replace value of the existing item
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $var
|
||||
* @param mixed $value
|
||||
* @param int $expire
|
||||
* @internal param mixed $var
|
||||
* @return bool
|
||||
*/
|
||||
public function replace($key, $value, $expire = 0)
|
||||
{
|
||||
return $this->connection->replace($this->getKey($key), $value, null, $this->getExpire($expire));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Store data in the cache
|
||||
*
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param int $expire
|
||||
@ -142,7 +143,7 @@ class MemcacheCache extends Cache
|
||||
{
|
||||
return $this->connection->set($this->getKey($key), $value, null, $this->getExpire($expire));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string
|
||||
@ -151,7 +152,7 @@ class MemcacheCache extends Cache
|
||||
{
|
||||
return md5($this->key_salt . $key);
|
||||
}
|
||||
|
||||
|
||||
public function getExpire($expire)
|
||||
{
|
||||
return ($expire > 0) ? $expire : $this->expire;
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage captcha
|
||||
* @since 2010-04-24
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Captcha
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage captcha
|
||||
* @since 2010-04-24
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class CaptchaImageAction
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-26
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class CaptchaValidator extends Validator
|
||||
|
@ -7,8 +7,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage env
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Env
|
||||
|
@ -7,8 +7,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Core
|
||||
* @since 24.12.2008
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -47,6 +45,7 @@ class Format
|
||||
* @param mixed $int
|
||||
* @param bool $currency - показывать валюту
|
||||
* @param bool $show_decimals - показывать или нет дробную часть
|
||||
* @return string
|
||||
*/
|
||||
static public function int2money($int = 0, $currency = false, $show_decimals = true)
|
||||
{
|
||||
@ -169,7 +168,7 @@ class Format
|
||||
* Преобразует дату в таймстамп.
|
||||
*
|
||||
* @param mixed $time
|
||||
* @return TimeFormat
|
||||
* @return int|bool
|
||||
*/
|
||||
static public function date2int($time)
|
||||
{
|
||||
@ -228,4 +227,3 @@ class Format
|
||||
* Оффсет с учетом летнего/зимнего времени
|
||||
*/
|
||||
Format::setTimezoneOffset(date('Z') - ((date('I') ? 60*60 : 0 )));
|
||||
?>
|
@ -7,8 +7,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Decorator
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
class User
|
||||
{
|
||||
@ -116,4 +114,3 @@ class User
|
||||
return $model->getById($id);
|
||||
}
|
||||
}
|
||||
?>
|
@ -5,8 +5,11 @@
|
||||
* @package Majestic
|
||||
* @subpackage exception
|
||||
* @since 2010-02-26
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Error404Exception extends GeneralException {}
|
||||
class Error404Exception extends ErrorHTTPException {
|
||||
function __construct($message = '', $code = null, Exception $previous = NULL )
|
||||
{
|
||||
parent::__construct($message, 404, $previous);
|
||||
}
|
||||
}
|
39
exception/ErrorHTTPException.php
Normal file
39
exception/ErrorHTTPException.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage exception
|
||||
* @since 2012-11-11
|
||||
*/
|
||||
|
||||
class ErrorHTTPException extends GeneralException
|
||||
{
|
||||
protected $http_headers;
|
||||
|
||||
public function __construct($message = "", $code = 0, Exception $previous = null)
|
||||
{
|
||||
$this->http_headers = array(
|
||||
400 => 'HTTP/1.0 400 Bad Request',
|
||||
402 => 'HTTP/1.0 402 Payment Required',
|
||||
403 => 'HTTP/1.0 403 Forbidden',
|
||||
404 => 'HTTP/1.0 404 Not Found',
|
||||
410 => 'HTTP/1.0 410 Gone',
|
||||
500 => 'HTTP/1.0 500 Internal Server Error',
|
||||
501 => 'HTTP/1.0 501 Not Implemented',
|
||||
503 => 'HTTP/1.0 503 Service Unavailable',
|
||||
);
|
||||
|
||||
if ($code === 200) {
|
||||
throw new GeneralException('Can\'t send 200 via ErrorHTTPException', 0, $this);
|
||||
} elseif (!array_key_exists($code, $this->http_headers)) {
|
||||
throw new GeneralException('Incorrect HTTP code ' . $code . '.', 0, $this);
|
||||
}
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
public function getHTTPHeader()
|
||||
{
|
||||
return $this->http_headers[$this->code];
|
||||
}
|
||||
}
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage exception
|
||||
* @since 2010-02-26
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class ErrorHandler
|
||||
@ -73,6 +71,7 @@ class ErrorHandler
|
||||
|
||||
/**
|
||||
* @param Exception $exception
|
||||
* @return string
|
||||
*/
|
||||
static public function showDebug($exception)
|
||||
{
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage exception
|
||||
* @since 2010-02-26
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class GeneralException extends Exception {}
|
@ -5,29 +5,37 @@
|
||||
* @package Majestic
|
||||
* @subpackage form
|
||||
* @since 2010-04-24
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Form
|
||||
{
|
||||
|
||||
const SUCCESS = 'success';
|
||||
const ERROR = 'error';
|
||||
|
||||
|
||||
const ERROR = 'error';
|
||||
|
||||
/**
|
||||
* @var FormField[]
|
||||
*/
|
||||
protected $fields = array();
|
||||
protected $messages = array(self::SUCCESS => 'Form data valid',
|
||||
self::ERROR => 'Form data invalid');
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $messages = array(
|
||||
self::SUCCESS => 'Form data valid',
|
||||
self::ERROR => 'Form data invalid');
|
||||
|
||||
protected $valid = true;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->init();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param bool|string $message
|
||||
* @return FormField
|
||||
*/
|
||||
protected function addField($name, $message = false)
|
||||
@ -35,13 +43,13 @@ abstract class Form
|
||||
$this->fields[$name] = new FormField($message);
|
||||
return $this->fields[$name];
|
||||
}
|
||||
|
||||
|
||||
public function isValid($data)
|
||||
{
|
||||
if (!is_array($data)) {
|
||||
throw new InitializationException(__CLASS__ . '::' . __METHOD__ . ' expects an array');
|
||||
}
|
||||
|
||||
|
||||
foreach ($this->fields as $field_name => $field) {
|
||||
if (isset($data[$field_name])) {
|
||||
$this->valid &= $field->isValid($data[$field_name], $data);
|
||||
@ -54,7 +62,7 @@ abstract class Form
|
||||
}
|
||||
return $this->valid;
|
||||
}
|
||||
|
||||
|
||||
public function getMessages()
|
||||
{
|
||||
$messages = array();
|
||||
@ -65,7 +73,7 @@ abstract class Form
|
||||
}
|
||||
return $messages;
|
||||
}
|
||||
|
||||
|
||||
public function getValue($key)
|
||||
{
|
||||
if (isset($this->fields[$key])) {
|
||||
@ -73,7 +81,10 @@ abstract class Form
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getValues()
|
||||
{
|
||||
$values = array();
|
||||
@ -84,7 +95,10 @@ abstract class Form
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSourceValues()
|
||||
{
|
||||
$values = array();
|
||||
@ -93,35 +107,49 @@ abstract class Form
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMessageType()
|
||||
{
|
||||
return ($this->valid) ? self::SUCCESS : self::ERROR;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->messages[$this->getMessageType()];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @return Form
|
||||
*/
|
||||
public function setSuccessMessage($message)
|
||||
{
|
||||
$this->messages[self::SUCCESS] = (string) $message;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @return Form
|
||||
*/
|
||||
public function setErrorMessage($message)
|
||||
{
|
||||
$this->messages[self::ERROR] = (string) $message;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
protected function fillHelperData()
|
||||
{
|
||||
$data['messages'] = $this->getMessages();
|
||||
$data['values'] = $this->getSourceValues();
|
||||
Session::set(get_class($this), $data);
|
||||
}
|
||||
|
||||
|
||||
abstract protected function init();
|
||||
}
|
@ -1,61 +1,101 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage form
|
||||
* @since 2010-04-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class FormField
|
||||
{
|
||||
|
||||
/**
|
||||
* @var iValidator[]
|
||||
*/
|
||||
protected $validators = array();
|
||||
|
||||
/**
|
||||
* @var iFilter[]
|
||||
*/
|
||||
protected $filters = array();
|
||||
|
||||
|
||||
/**
|
||||
* Used instead message of validator if defined.
|
||||
*
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $default_message = false;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $message = false;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $value;
|
||||
|
||||
|
||||
/* Flags */
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $required = true;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $ignored = false;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param bool|string $default_message
|
||||
*/
|
||||
public function __construct($default_message = false)
|
||||
{
|
||||
$this->default_message = $default_message;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param bool $flag
|
||||
* @return FormField
|
||||
*/
|
||||
public function setRequired($flag)
|
||||
{
|
||||
$this->required = (bool) $flag;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isRequired()
|
||||
{
|
||||
return $this->required;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param bool $flag
|
||||
* @return FormField
|
||||
*/
|
||||
public function setIgnored($flag)
|
||||
{
|
||||
$this->ignored = (bool) $flag;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isIgnored()
|
||||
{
|
||||
return $this->ignored;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string[]|iValidator[] $validators
|
||||
* @return FormField
|
||||
*/
|
||||
public function addValidators($validators)
|
||||
{
|
||||
foreach ($validators as $validator) {
|
||||
@ -63,7 +103,12 @@ class FormField
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|iValidator $validator
|
||||
* @return FormField
|
||||
* @throws InitializationException
|
||||
*/
|
||||
public function addValidator($validator)
|
||||
{
|
||||
if ($validator instanceof iValidator) {
|
||||
@ -77,7 +122,7 @@ class FormField
|
||||
$this->validators[$name] = $validator;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function addFilters($filters)
|
||||
{
|
||||
foreach ($filters as $filter) {
|
||||
@ -85,10 +130,10 @@ class FormField
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function addFilter($filter)
|
||||
{
|
||||
if ($filter instanceof iFilter) {
|
||||
if ($filter instanceof iFilter) {
|
||||
$name = get_class($filter);
|
||||
} elseif (is_string($filter)) {
|
||||
$name = $filter . 'Filter';
|
||||
@ -99,7 +144,7 @@ class FormField
|
||||
$this->filters[$name] = $filter;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function getValue()
|
||||
{
|
||||
$value = $this->value;
|
||||
@ -110,10 +155,10 @@ class FormField
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* $value & $key for array_walk_recursive
|
||||
*
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param mixed $key
|
||||
*/
|
||||
@ -123,24 +168,24 @@ class FormField
|
||||
$value = $filter->filter($value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function getSourceValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
|
||||
public function isValid($value, $context = null)
|
||||
{
|
||||
$this->value = $value;
|
||||
// filtered value here
|
||||
$value = $this->getValue();
|
||||
|
||||
|
||||
$valid = true;
|
||||
|
||||
|
||||
if ((($value === '') || ($value === null)) && !$this->isRequired()) {
|
||||
return $valid;
|
||||
}
|
||||
|
||||
|
||||
foreach ($this->validators as $validator) {
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $val) {
|
||||
@ -158,14 +203,14 @@ class FormField
|
||||
} elseif ($validator->isValid($value, $context)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
$valid = false;
|
||||
$this->message = ($this->default_message) ? $this->default_message : $validator->getMessage();
|
||||
break;
|
||||
}
|
||||
return $valid;
|
||||
}
|
||||
|
||||
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->message;
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Form
|
||||
* @since 2010-04-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class FormViewHelper extends ViewHelper
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Model
|
||||
* @since 2010-02-23
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class I18N
|
||||
@ -20,10 +18,11 @@ class I18N
|
||||
|
||||
static protected $lang = '';
|
||||
static protected $locale = '';
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $lang default language set
|
||||
* @throws InitializationException
|
||||
* @internal mixed $lang default language set
|
||||
*/
|
||||
static public function init()
|
||||
{
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Layout
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class ErrorLayout extends Layout
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Layout
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Layout
|
||||
@ -56,7 +54,9 @@ abstract class Layout
|
||||
abstract protected function execute();
|
||||
|
||||
/**
|
||||
* Execute Action, insert action's result html into layout template and return Layout html
|
||||
* @param Action $action
|
||||
* @return string
|
||||
*/
|
||||
public function fetch($action)
|
||||
{
|
||||
@ -64,7 +64,11 @@ abstract class Layout
|
||||
$this->execute();
|
||||
return $this->view->fetch($this->getTemplate());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return content of template
|
||||
* @return string
|
||||
*/
|
||||
protected function getTemplate()
|
||||
{
|
||||
$template = ($this->template) ? $this->template : substr(get_class($this), 0, -6/*strlen('Layout')*/);
|
||||
|
@ -13,6 +13,9 @@ class FileLogger extends Logger
|
||||
|
||||
protected $file_path = '';
|
||||
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
protected $handler = null;
|
||||
|
||||
protected function __construct()
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage mail
|
||||
* @since 2010-04-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Mailer
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage db
|
||||
* @since 2010-02-16
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Db
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage db
|
||||
* @since 2010-02-16
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class DbDriver
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage db
|
||||
* @since 2010-02-19
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class DbExpr
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage db
|
||||
* @since 2010-02-19
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class DbStatement
|
||||
|
@ -8,8 +8,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Model
|
||||
* @since 2010-02-16
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Model
|
||||
|
@ -131,6 +131,7 @@ class MongoStatement extends DbStatement
|
||||
|
||||
/**
|
||||
* @param MongoDbCommand $request
|
||||
* @throws GeneralException
|
||||
* @return bool
|
||||
*/
|
||||
protected function driverExecute($request)
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage db
|
||||
* @since 2010-02-17
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage db
|
||||
* @since 2010-02-19
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Redis
|
||||
* @since 2011-07-29
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class RedisDebug
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Redis
|
||||
* @since 2010-07-17
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class RedisManager
|
||||
@ -15,7 +13,7 @@ class RedisManager
|
||||
/**
|
||||
* Redis connections
|
||||
*
|
||||
* @var array
|
||||
* @var Redis[]
|
||||
*/
|
||||
static protected $connections = array();
|
||||
|
||||
@ -25,6 +23,7 @@ class RedisManager
|
||||
* @param string $name connection name. If not set 'default' will be used.
|
||||
* @param array $config Configuration array.
|
||||
*
|
||||
* @throws GeneralException
|
||||
* @return Redis
|
||||
*/
|
||||
static public function connect($name = 'default', $config = null)
|
||||
|
@ -15,7 +15,8 @@
|
||||
* @see https://github.com/nicolasff/phpredis
|
||||
*
|
||||
* @method bool connect() connect(string $host, int $port = 6379, float $timeout = 0) Connect to redis
|
||||
* @method bool pconnect() connect(string $host, int $port = 6379, float $timeout = 0, string $persistent_id) Connect to redis with reusing connection
|
||||
* @method bool pconnect() pconnect(string $host, int $port = 6379, float $timeout = 0, string $persistent_id) Connect to redis with reusing connection
|
||||
* @method bool select() select(int $dbindex) Switches to a given database
|
||||
* @method void close() Close connection to redis
|
||||
* @method bool setOption() setOption(mixed $name, mixed $value) Set client option
|
||||
* @method mixed getOption() getOption(mixed $name) Get client option
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage Session
|
||||
* @since 2010-02-28
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class SessionModel extends SqlModel
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage session
|
||||
* @since 2010-03-14
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Session
|
||||
|
@ -150,12 +150,11 @@ class LoadTest extends PHPUnit_Framework_TestCase
|
||||
$this->setConstants();
|
||||
Load::setAutoloadFrom(self::$file);
|
||||
$autoload = require(self::$file);
|
||||
$this->setExpectedException('PHPUnit_Framework_Error');
|
||||
$this->assertNotEmpty(Load::getFilePath('anton'));
|
||||
$this->setExpectedException('PHPUnit_Framework_Error', 'Undefined index');
|
||||
$this->assertNotEmpty(Load::getFilePath('ClassDontExist'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO: Load::autoload() needs self::$autoload = require(self::$file); after self::buildAutoload();
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testDebugAutoload()
|
||||
|
@ -17,6 +17,7 @@ require_once dirname(__FILE__) . '/../../classes/Env.class.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
|
||||
require_once dirname(__FILE__) . '/../../app/FrontController.php';
|
||||
require_once dirname(__FILE__) . '/../../app/Action.php';
|
||||
require_once dirname(__FILE__) . '/../../view/iView.php';
|
||||
|
||||
class Action_TestCase extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
@ -42,7 +43,7 @@ class Action_TestCase extends PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
class SomeView
|
||||
class SomeView implements iView
|
||||
{
|
||||
private $result = array();
|
||||
public function fetch($template)
|
||||
@ -53,7 +54,17 @@ class SomeView
|
||||
|
||||
public function assignObject() {}
|
||||
|
||||
public function assign($name, $value) {
|
||||
public function assign($name, $value = null) {
|
||||
$this->result[$name] = $value;
|
||||
}
|
||||
|
||||
public function prepend($name, $value)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function append($name, $value)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-1
|
||||
* @since 2011-11-01
|
||||
*
|
||||
* Unit tests for AjaxAction class
|
||||
*/
|
||||
@ -21,10 +21,9 @@ class AjaxActionTest extends Action_TestCase
|
||||
*/
|
||||
public function testConstruct()
|
||||
{
|
||||
|
||||
Config::set('DEBUG', false);
|
||||
Env::setParams(array('ajax' => 'AjaxTemplate', 'param2' => 'value2'));
|
||||
$action = $this->getMockForAbstractClass('AjaxAction' );
|
||||
$action = $this->getMockForAbstractClass('AjaxAction');
|
||||
$this->assertAttributeEquals('ajax', 'template', $action);
|
||||
}
|
||||
|
||||
@ -33,11 +32,10 @@ class AjaxActionTest extends Action_TestCase
|
||||
*/
|
||||
public function testFetchWithEncode()
|
||||
{
|
||||
|
||||
Config::set('DEBUG', false);
|
||||
$controller = FrontController::getInstance();
|
||||
$controller->setView('SomeView');
|
||||
$action = $this->getMockForAbstractClass('AjaxAction' );
|
||||
$action = $this->getMockForAbstractClass('AjaxAction');
|
||||
$action->data = array('var' => 'val');
|
||||
$result = $action->fetch();
|
||||
$this->assertSame('/actions//ajax', $result['template']);
|
||||
@ -49,15 +47,47 @@ class AjaxActionTest extends Action_TestCase
|
||||
*/
|
||||
public function testFetchNoEncode()
|
||||
{
|
||||
|
||||
Config::set('DEBUG', false);
|
||||
Env::setParams(array('encode' => false));
|
||||
Env::setParams(array('json_encode' => false));
|
||||
$controller = FrontController::getInstance();
|
||||
$controller->setView('SomeView');
|
||||
$action = $this->getMockForAbstractClass('AjaxAction' );
|
||||
$action = $this->getMockForAbstractClass('AjaxAction');
|
||||
$action->data = array('var' => 'val');
|
||||
$result = $action->fetch();
|
||||
$this->assertSame('/actions//ajax', $result['template']);
|
||||
$this->assertSame( $action->data, $result['data']);
|
||||
$this->assertSame('Array', (string) $result['data']);
|
||||
$action->data = 'stringvalue';
|
||||
$result = $action->fetch();
|
||||
$this->assertSame('/actions//ajax', $result['template']);
|
||||
$this->assertSame('stringvalue', (string) $result['data']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testFetchWithEncodeDefault()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
$controller = FrontController::getInstance();
|
||||
$controller->setView('SomeView');
|
||||
$action = $this->getMockForAbstractClass('AjaxAction');
|
||||
$result = $action->fetch();
|
||||
$this->assertSame('/actions//ajax', $result['template']);
|
||||
$this->assertSame('false', (string) $result['data']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testFetchNoEncodeDefault()
|
||||
{
|
||||
Config::set('DEBUG', false);
|
||||
Env::setParams(array('json_encode' => false));
|
||||
$controller = FrontController::getInstance();
|
||||
$controller->setView('SomeView');
|
||||
$action = $this->getMockForAbstractClass('AjaxAction');
|
||||
$result = $action->fetch();
|
||||
$this->assertSame('/actions//ajax', $result['template']);
|
||||
$this->assertSame('', (string) $result['data']);
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@
|
||||
|
||||
require_once __DIR__ . '/../../util/profiler/Profiler.php';
|
||||
require_once __DIR__ . '/../../exception/GeneralException.php';
|
||||
require_once __DIR__ . '/../../exception/ErrorHTTPException.php';
|
||||
require_once __DIR__ . '/../../exception/Error404Exception.php';
|
||||
require_once __DIR__ . '/../../exception/ErrorHandler.php';
|
||||
require_once __DIR__ . '/../../app/CliController.php';
|
||||
|
@ -12,6 +12,9 @@
|
||||
|
||||
require_once dirname(__FILE__) . '/Action_TestCase.php';
|
||||
require_once dirname(__FILE__) . '/../../app/ErrorAction.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/ErrorHTTPException.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/Error404Exception.php';
|
||||
|
||||
class ErrorActionTest extends Action_TestCase
|
||||
{
|
||||
@ -24,10 +27,9 @@ class ErrorActionTest extends Action_TestCase
|
||||
|
||||
$this->log = ini_get('error_log');
|
||||
ini_set('error_log', '/dev/null');
|
||||
set_exit_overload(function()
|
||||
{
|
||||
return false;
|
||||
});
|
||||
set_exit_overload(function () {
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,13 +98,15 @@ class ErrorActionTest extends Action_TestCase
|
||||
public function testError404WithAjax()
|
||||
{
|
||||
$this->setConstants(false);
|
||||
$exception = $this->getMock('Error404Exception', array('getMessage', 'getFile', 'getLine', 'getTrace'));
|
||||
$exception->expects($this->once())
|
||||
->method('getTrace')
|
||||
->will($this->returnValue(array('one' => array('class' => 'AjaxAction'))));
|
||||
$exception = new Error404Exception('Some message');
|
||||
|
||||
$controller = FrontController::getInstance();
|
||||
$controller->setView('SomeView');
|
||||
$action = new ErrorAction($exception);
|
||||
$action->setAjaxError();
|
||||
$this->assertSame($exception, $action->exception);
|
||||
$result = $action->fetch();
|
||||
$this->assertSame('Some message', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -111,13 +115,30 @@ class ErrorActionTest extends Action_TestCase
|
||||
public function testError404NoAjax()
|
||||
{
|
||||
$this->setConstants(false);
|
||||
$exception = $this->getMock('Error404Exception', array('getMessage', 'getFile', 'getLine', 'getTrace'));
|
||||
$exception->expects($this->once())
|
||||
->method('getTrace')
|
||||
->will($this->returnValue(array('one' => array('class' => 'Action'))));
|
||||
$exception = new Error404Exception('Some message');
|
||||
|
||||
$controller = FrontController::getInstance();
|
||||
$controller->setView('SomeView');
|
||||
$action = new ErrorAction($exception);
|
||||
$this->assertSame($exception, $action->exception);
|
||||
$result = $action->fetch();
|
||||
$this->assertSame('/actions/404', $result['template']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testErrorHTTP()
|
||||
{
|
||||
$this->setConstants(false);
|
||||
$exception = new ErrorHTTPException('Some message', 410);
|
||||
|
||||
$controller = FrontController::getInstance();
|
||||
$controller->setView('SomeView');
|
||||
$action = new ErrorAction($exception);
|
||||
$this->assertSame($exception, $action->exception);
|
||||
$result = $action->fetch();
|
||||
$this->assertSame('/actions/HTTP', $result['template']);
|
||||
}
|
||||
|
||||
private function setConstants($val = false)
|
||||
|
@ -17,6 +17,7 @@ require_once dirname(__FILE__) . '/../../Config.php';
|
||||
require_once dirname(__FILE__) . '/../../util/FirePHPCore-0.3.2/lib/FirePHPCore/fb.php';
|
||||
require_once dirname(__FILE__) . '/../../util/profiler/Profiler.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/ErrorHTTPException.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/Error404Exception.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
|
||||
require_once dirname(__FILE__) . '/../../app/router/Route.php';
|
||||
@ -25,10 +26,9 @@ require_once dirname(__FILE__) . '/../../app/FrontController.php';
|
||||
require_once dirname(__FILE__) . '/../../app/Action.php';
|
||||
require_once dirname(__FILE__) . '/../../app/AjaxAction.php';
|
||||
|
||||
|
||||
class FrontControllerTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
|
||||
public function run(PHPUnit_Framework_TestResult $result = NULL)
|
||||
{
|
||||
$this->setPreserveGlobalState(false);
|
||||
@ -47,7 +47,7 @@ class FrontControllerTest extends PHPUnit_Framework_TestCase
|
||||
$this->getMock('ErrorLayout', array('fetch', 'setException'), array(), 'ErrorLayoutMock');
|
||||
}
|
||||
if (!class_exists('ErrorActionMock')) {
|
||||
$this->getMock('ErrorAction', array(), array(), 'ErrorActionMock', false);
|
||||
$this->getMock('ErrorAction', array('setAjaxError'), array(), 'ErrorActionMock', false);
|
||||
}
|
||||
set_new_overload(array($this, 'newCallback'));
|
||||
}
|
||||
@ -132,6 +132,8 @@ class FrontControllerTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
$this->setConstants(false);
|
||||
$controller = FrontController::getInstance();
|
||||
$result = $controller->execute();
|
||||
$controller = FrontController::getInstance();
|
||||
$this->assertNull($controller->execute());
|
||||
}
|
||||
|
||||
@ -192,6 +194,24 @@ class FrontControllerTest extends PHPUnit_Framework_TestCase
|
||||
$result = $controller->execute();
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testExecuteWithLayoutProfiler()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
$this->getMock('userAction');
|
||||
$this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
|
||||
$_SERVER['REQUEST_URI'] = '/user/account/213';
|
||||
$this->setConstants(true);
|
||||
$controller = FrontController::getInstance();
|
||||
$router = $controller->getRouter();
|
||||
$router->add('user', 'user/account/:id', 'user');
|
||||
$result = $controller->execute();
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
@ -208,6 +228,38 @@ class FrontControllerTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testExecuteWithAjaxActionError()
|
||||
{
|
||||
$this->getMock('userAction');
|
||||
$_SERVER['REQUEST_URI'] = '/user/account/213';
|
||||
$this->setConstants(false);
|
||||
$controller = FrontController::getInstance();
|
||||
$router = $controller->getRouter();
|
||||
$router->add('user', 'user/account/:id', 'NewAjax');
|
||||
$result = $controller->execute();
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testExecuteWithAjaxActionProfiler()
|
||||
{
|
||||
Config::set('PROFILER', true);
|
||||
$this->getMock('userAction');
|
||||
$this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
|
||||
$_SERVER['REQUEST_URI'] = '/user/account/213';
|
||||
$this->setConstants(true);
|
||||
$controller = FrontController::getInstance();
|
||||
$router = $controller->getRouter();
|
||||
$router->add('user', 'user/account/:id', 'NewAjax');
|
||||
$result = $controller->execute();
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
private function setConstants($val = false)
|
||||
{
|
||||
|
||||
@ -236,6 +288,12 @@ class FrontControllerTest extends PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
class NewAjaxAction extends AjaxAction{
|
||||
protected function execute() {}
|
||||
/**
|
||||
* Used in testExecuteWithAjaxAction
|
||||
*/
|
||||
class NewAjaxAction extends AjaxAction
|
||||
{
|
||||
protected function execute()
|
||||
{
|
||||
}
|
||||
}
|
@ -11,20 +11,27 @@
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/ErrorHTTPException.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/Error404Exception.php';
|
||||
|
||||
class Error404ExceptionTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testError404Exception()
|
||||
{
|
||||
$this->setExpectedException('Error404Exception');
|
||||
$this->setExpectedException('Error404Exception', 404);
|
||||
throw new Error404Exception();
|
||||
}
|
||||
|
||||
public function testError404ExceptionMessage()
|
||||
{
|
||||
$this->setExpectedException('Error404Exception', 'Error404Exception message', 10);
|
||||
$this->setExpectedException('Error404Exception', 'Error404Exception message', 404);
|
||||
throw new Error404Exception('Error404Exception message', 10);
|
||||
}
|
||||
|
||||
public function testError404ExceptionWithPrevious()
|
||||
{
|
||||
$this->setExpectedException('Error404Exception', 'Error404Exception message', 404);
|
||||
throw new Error404Exception('Error404Exception message', 10, new ErrorException('Error message'));
|
||||
}
|
||||
|
||||
}
|
106
tests/exception/ErrorHTTPExceptionTest.php
Normal file
106
tests/exception/ErrorHTTPExceptionTest.php
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-10-11
|
||||
*
|
||||
* Unit tests for Error404Exception class
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/ErrorHTTPException.php';
|
||||
require_once dirname(__FILE__) . '/../../exception/Error404Exception.php';
|
||||
|
||||
class ErrorHTTPExceptionTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testErrorHTTPException()
|
||||
{
|
||||
$this->setExpectedException('ErrorHTTPException', 404);
|
||||
throw new ErrorHTTPException('Some error occurred', 400);
|
||||
}
|
||||
|
||||
public function testErrorHTTPExceptionMessage()
|
||||
{
|
||||
$this->setExpectedException('ErrorHTTPException', 'ErrorHTTPException message', 410);
|
||||
throw new ErrorHTTPException('ErrorHTTPException message', 410);
|
||||
}
|
||||
|
||||
public function testErrorHTTPExceptionWithPrevious()
|
||||
{
|
||||
$this->setExpectedException('ErrorHTTPException', 'ErrorHTTPException message', 500);
|
||||
throw new ErrorHTTPException('ErrorHTTPException message', 500, new ErrorException('Error message'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerHTTPCode
|
||||
*/
|
||||
public function testGetHTTPHeader($message, $code, $header)
|
||||
{
|
||||
try {
|
||||
throw new ErrorHTTPException($message, $code, new ErrorException('Error message'));
|
||||
}
|
||||
catch (ErrorHTTPException $e) {
|
||||
$this->assertSame($header, $e->getHTTPHeader());
|
||||
$this->assertSame($message, $e->getMessage());
|
||||
$this->assertSame($code, $e->getCode());
|
||||
$this->assertEquals(new ErrorException('Error message'), $e->getPrevious());
|
||||
}
|
||||
}
|
||||
|
||||
public function providerHTTPCode()
|
||||
{
|
||||
return array(
|
||||
array('ErrorHTTPException message for 400', 400, 'HTTP/1.0 400 Bad Request'),
|
||||
array('ErrorHTTPException message for 402', 402, 'HTTP/1.0 402 Payment Required'),
|
||||
array('ErrorHTTPException message for 403', 403, 'HTTP/1.0 403 Forbidden'),
|
||||
array('ErrorHTTPException message for 404', 404, 'HTTP/1.0 404 Not Found'),
|
||||
array('ErrorHTTPException message for 410', 410, 'HTTP/1.0 410 Gone'),
|
||||
array('ErrorHTTPException message for 500', 500, 'HTTP/1.0 500 Internal Server Error'),
|
||||
array('ErrorHTTPException message for 501', 501, 'HTTP/1.0 501 Not Implemented'),
|
||||
array('ErrorHTTPException message for 503', 503, 'HTTP/1.0 503 Service Unavailable'),
|
||||
);
|
||||
}
|
||||
|
||||
public function testErrorHTTPException200()
|
||||
{
|
||||
try {
|
||||
throw new ErrorHTTPException('ErrorHTTPException message', 200, new ErrorException('Error message'));
|
||||
}
|
||||
catch (ErrorHTTPException $e) {
|
||||
$this->fail();
|
||||
}
|
||||
catch (GeneralException $e) {
|
||||
$this->assertSame('Can\'t send 200 via ErrorHTTPException', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testErrorHTTPExceptionBadCode()
|
||||
{
|
||||
try {
|
||||
throw new ErrorHTTPException('ErrorHTTPException message', 100, new ErrorException('Error message'));
|
||||
}
|
||||
catch (ErrorHTTPException $e) {
|
||||
$this->fail();
|
||||
}
|
||||
catch (GeneralException $e) {
|
||||
$this->assertSame('Incorrect HTTP code 100.', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testErrorHTTPExceptionGoodCode()
|
||||
{
|
||||
try {
|
||||
throw new ErrorHTTPException('ErrorHTTPException message', 400, new ErrorException('Error message'));
|
||||
}
|
||||
catch (ErrorHTTPException $e) {
|
||||
$this->assertSame(400, $e->getCode());
|
||||
}
|
||||
catch (GeneralException $e) {
|
||||
$this->fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage form
|
||||
* @since 2010-04-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../../validator/iValidator.php';
|
||||
|
@ -16,45 +16,47 @@ require_once dirname(__FILE__) . '/../../../view/helpers/BreadcrumbViewHelper.ph
|
||||
|
||||
class BreadcrumbViewHelperTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var BreadcrumbViewHelper
|
||||
*/
|
||||
public $helper;
|
||||
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
Registry::set('BreadcrumbViewHelper', array());
|
||||
$this->helper = new BreadcrumbViewHelper(new PHPView('any'));
|
||||
}
|
||||
|
||||
|
||||
public function testTitle()
|
||||
{
|
||||
$this->helper->breadcrumb('Guest page', 'guest.php');
|
||||
$this->helper->breadcrumb('Guest page', 'guest.php');
|
||||
$result = Registry::get('BreadcrumbViewHelper');
|
||||
$this->assertSame(array('Guest page' => 'guest.php'), Registry::get('BreadcrumbViewHelper'));
|
||||
|
||||
$this->assertSame(array('Guest page' => 'guest.php'), Registry::get('BreadcrumbViewHelper'));
|
||||
|
||||
$this->helper->prepend('Leave message', 'feedback.php');
|
||||
$this->assertSame(array('Leave message' => 'feedback.php', 'Guest page' => 'guest.php'), Registry::get('BreadcrumbViewHelper'));
|
||||
|
||||
$this->assertSame(array('Leave message' => 'feedback.php', 'Guest page' => 'guest.php'), Registry::get('BreadcrumbViewHelper'));
|
||||
|
||||
$this->helper->append('Home page', 'home.php');
|
||||
$this->assertSame(array('Leave message' => 'feedback.php', 'Guest page' => 'guest.php', 'Home page' => 'home.php'), Registry::get('BreadcrumbViewHelper'));
|
||||
$this->assertSame(array('Leave message' => 'feedback.php', 'Guest page' => 'guest.php', 'Home page' => 'home.php'), Registry::get('BreadcrumbViewHelper'));
|
||||
}
|
||||
|
||||
|
||||
public function testToString()
|
||||
{
|
||||
$this->helper->prepend('Home page', 'home.php');
|
||||
$this->helper->breadcrumb('Guest page', 'guest.php');
|
||||
$this->helper->breadcrumb('Guest page', 'guest.php');
|
||||
$this->helper->append('Leave message', 'feedback.php');
|
||||
$this->assertSame(array('Home page' => 'home.php', 'Guest page' => 'guest.php', 'Leave message'=> 'feedback.php'), Registry::get('BreadcrumbViewHelper'));
|
||||
|
||||
$this->assertSame(array('Home page' => 'home.php', 'Guest page' => 'guest.php', 'Leave message' => 'feedback.php'), Registry::get('BreadcrumbViewHelper'));
|
||||
|
||||
$result = $this->helper->__toString();
|
||||
$this->assertSame('<a href="home.php">Home page</a> > <a href="guest.php">Guest page</a> > <a href="feedback.php">Leave message</a>', $result);
|
||||
|
||||
|
||||
$this->helper->setSeparator('-');
|
||||
$result = $this->helper->__toString();
|
||||
$this->assertSame('<a href="home.php">Home page</a>-<a href="guest.php">Guest page</a>-<a href="feedback.php">Leave message</a>', $result);
|
||||
|
||||
|
||||
$this->helper->append('Last page', '');
|
||||
$result = $this->helper->__toString();
|
||||
$this->assertSame('<a href="home.php">Home page</a>-<a href="guest.php">Guest page</a>-<a href="feedback.php">Leave message</a>-Last page', $result);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -18,14 +18,17 @@ require_once dirname(__FILE__) . '/../../../view/helpers/GetViewHelper.php';
|
||||
|
||||
class GetViewHelperTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var GetViewHelper
|
||||
*/
|
||||
public $helper;
|
||||
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->helper = new GetViewHelper(new PHPView('any'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @TODO: GetViewHelper: initialize GetViewHelper::$get with empty array()
|
||||
*/
|
||||
@ -34,7 +37,7 @@ class GetViewHelperTest extends PHPUnit_Framework_TestCase
|
||||
$this->setExpectedException('PHPUnit_Framework_Error');
|
||||
$this->helper->get(null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @TODO: GetViewHelper: check $_GET not null
|
||||
*/
|
||||
@ -43,7 +46,7 @@ class GetViewHelperTest extends PHPUnit_Framework_TestCase
|
||||
$this->setExpectedException('PHPUnit_Framework_Error');
|
||||
$result = $this->helper->get('param');
|
||||
}
|
||||
|
||||
|
||||
public function testGetWithSingleParam()
|
||||
{
|
||||
$_GET['a'] = 'b';
|
||||
@ -59,7 +62,7 @@ class GetViewHelperTest extends PHPUnit_Framework_TestCase
|
||||
$result = $this->helper->get(array('a'));
|
||||
$this->assertSame('?b=a', $result);
|
||||
}
|
||||
|
||||
|
||||
public function testGetWithArray()
|
||||
{
|
||||
$_GET['a'] = array('one' => 1, 'two' => 2);
|
||||
@ -77,5 +80,4 @@ class GetViewHelperTest extends PHPUnit_Framework_TestCase
|
||||
$result = $this->helper->get(array('b' => 'c d e', 'c' => array('five' => 'six seven')));
|
||||
$this->assertSame('?a[one]=1&a[two]=2&b=c+d+e&c[five]=six+seven', $result);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,32 +16,35 @@ require_once dirname(__FILE__) . '/../../../view/helpers/HeadViewHelper.php';
|
||||
|
||||
class HeadViewHelperTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var HeadViewHelper
|
||||
*/
|
||||
public $helper;
|
||||
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
Registry::set('HeadViewHelper', array());
|
||||
$this->helper = new HeadViewHelper(null);
|
||||
}
|
||||
|
||||
|
||||
public function testHead()
|
||||
{
|
||||
$this->helper->head('<meta />');
|
||||
$this->helper->head('<meta />');
|
||||
$result = Registry::get('HeadViewHelper');
|
||||
$this->assertSame(array('<meta />'), Registry::get('HeadViewHelper'));
|
||||
|
||||
$this->helper->head('<link />');
|
||||
|
||||
$this->helper->head('<link />');
|
||||
$this->assertSame(array('<meta />', '<link />'), Registry::get('HeadViewHelper'));
|
||||
}
|
||||
|
||||
|
||||
public function testToString()
|
||||
{
|
||||
$this->helper->head('<meta />');
|
||||
$this->helper->head('<link />');
|
||||
|
||||
$this->helper->head('<meta />');
|
||||
$this->helper->head('<link />');
|
||||
|
||||
$result = $this->helper->__toString();
|
||||
|
||||
|
||||
$this->assertSame("<meta />\n <link />\n", $result);
|
||||
}
|
||||
}
|
||||
|
@ -19,25 +19,25 @@ require_once dirname(__FILE__) . '/../../../exception/GeneralException.php';
|
||||
|
||||
class MsgViewHelperTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var MsgViewHelper
|
||||
*/
|
||||
public $helper;
|
||||
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
Session::del('MsgViewHelper');
|
||||
$this->helper = new MsgViewHelper(new PHPView(array('path' => 'any')));
|
||||
}
|
||||
|
||||
|
||||
public function testMsg()
|
||||
{
|
||||
|
||||
$this->helper->msg('new message from test', 'success');
|
||||
$this->helper->msg('new message from test', 'success');
|
||||
$this->assertSame(array('message' => 'new message from test', 'type' => 'success'), Session::get('MsgViewHelper'));
|
||||
|
||||
$this->assertSame($this->helper, $this->helper->msg('error message', 'error'));
|
||||
$this->assertSame(array('message' => 'error message', 'type' => 'error'), Session::get('MsgViewHelper'));
|
||||
|
||||
|
||||
$this->assertSame($this->helper, $this->helper->msg('error message', 'error'));
|
||||
$this->assertSame(array('message' => 'error message', 'type' => 'error'), Session::get('MsgViewHelper'));
|
||||
}
|
||||
|
||||
public function testWrongType()
|
||||
@ -45,31 +45,57 @@ class MsgViewHelperTest extends PHPUnit_Framework_TestCase
|
||||
$this->setExpectedException('GeneralException', 'Unknown message type');
|
||||
$this->helper->msg('some message', 'wrong');
|
||||
}
|
||||
|
||||
|
||||
public function testSuccess()
|
||||
{
|
||||
$this->helper->success('success message');
|
||||
$this->assertSame(array('message' => 'success message', 'type' => 'success'), Session::get('MsgViewHelper'));
|
||||
}
|
||||
|
||||
|
||||
public function testError()
|
||||
{
|
||||
$this->helper->error('error message');
|
||||
$this->assertSame(array('message' => 'error message', 'type' => 'error'), Session::get('MsgViewHelper'));
|
||||
$this->assertNull(Session::get('test'));
|
||||
}
|
||||
|
||||
|
||||
public function testInfo()
|
||||
{
|
||||
$this->helper->info('info message');
|
||||
$this->assertSame(array('message' => 'info message', 'type' => 'info'), Session::get('MsgViewHelper'));
|
||||
$this->assertNull(Session::get('test'));
|
||||
}
|
||||
|
||||
public function testWarning()
|
||||
{
|
||||
$this->helper->warning('warning message');
|
||||
$this->assertSame(array('message' => 'warning message', 'type' => 'warning'), Session::get('MsgViewHelper'));
|
||||
$this->assertNull(Session::get('test'));
|
||||
}
|
||||
|
||||
public function testToString()
|
||||
{
|
||||
$this->helper->success('yeah');
|
||||
$result = $this->helper->__toString();
|
||||
$this->assertSame('<div class="success">yeah</div>', $result);
|
||||
}
|
||||
|
||||
|
||||
public function testToStringEmpty()
|
||||
{
|
||||
$result = $this->helper->__toString();
|
||||
$this->assertEmpty($result);
|
||||
}
|
||||
|
||||
|
||||
public function testToStringWithPrefix()
|
||||
{
|
||||
$this->helper->success('yeah');
|
||||
$result = $this->helper->withPrefix('prefix')->__toString();
|
||||
$this->assertSame('<div class="prefixsuccess">yeah</div>', $result);
|
||||
}
|
||||
|
||||
public function testToStringEmptyWithPrefix()
|
||||
{
|
||||
$result = $this->helper->withPrefix('prefix')->__toString();
|
||||
$this->assertEmpty($result);
|
||||
}
|
||||
}
|
||||
|
@ -16,32 +16,34 @@ require_once dirname(__FILE__) . '/../../../view/helpers/TitleViewHelper.php';
|
||||
|
||||
class TitleViewHelperTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var TitleViewHelper
|
||||
*/
|
||||
public $helper;
|
||||
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
Registry::set('TitleViewHelper', array());
|
||||
$this->helper = new TitleViewHelper('view');
|
||||
}
|
||||
|
||||
|
||||
public function testTitle()
|
||||
{
|
||||
$this->helper->title('one');
|
||||
$this->helper->title('one');
|
||||
$result = Registry::get('TitleViewHelper');
|
||||
$this->assertSame(array('one'), Registry::get('TitleViewHelper'));
|
||||
|
||||
$this->helper->title('two');
|
||||
|
||||
$this->helper->title('two');
|
||||
$this->assertSame(array('one', 'two'), Registry::get('TitleViewHelper'));
|
||||
}
|
||||
|
||||
|
||||
public function testSeparator()
|
||||
{
|
||||
$this->helper->title('one');
|
||||
$this->helper->title('two');
|
||||
|
||||
$this->helper->title('one');
|
||||
$this->helper->title('two');
|
||||
|
||||
$this->assertSame('one - two', $this->helper->__toString());
|
||||
$this->helper->setSeparator('=');
|
||||
$this->assertSame('one=two', $this->helper->__toString());
|
||||
$this->assertSame('one=two', $this->helper->__toString());
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage util
|
||||
* @since 2010-02-24
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -24,7 +22,7 @@ class AutoloadBuilder
|
||||
|
||||
protected $handler;
|
||||
|
||||
protected $regex = '/(class|interface) ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/';
|
||||
protected $regex = '/\n(abstract |final )?(class|interface) ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/';
|
||||
|
||||
public function __construct($autoload, $dirs = array(), $exclude = array())
|
||||
{
|
||||
@ -36,7 +34,7 @@ class AutoloadBuilder
|
||||
public function build()
|
||||
{
|
||||
$array_string = "<?php\n// This file is autogenerated by \n// " . __FILE__ . " script.\nreturn array(\n";
|
||||
// for dublicates check
|
||||
// for duplicates check
|
||||
$classes = array();
|
||||
foreach ($this->dirs as $dir) {
|
||||
$iterator = new RecursiveIteratorIterator(
|
||||
@ -45,7 +43,9 @@ class AutoloadBuilder
|
||||
);
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
|
||||
/**
|
||||
* @var SplFileInfo $file
|
||||
*/
|
||||
if ($this->isExcluded($file->getRealPath())) {
|
||||
continue;
|
||||
}
|
||||
@ -62,11 +62,11 @@ class AutoloadBuilder
|
||||
|
||||
if (preg_match_all($this->regex, $content, $matches, PREG_SET_ORDER)) {
|
||||
foreach ($matches as $match) {
|
||||
if (array_key_exists($match[2], $classes)) {
|
||||
if (array_key_exists($match[3], $classes)) {
|
||||
continue;
|
||||
}
|
||||
$array_string .= "'{$match[2]}'=>'" . $relative_path . "',\n";
|
||||
$classes[$match[2]] = $file->getRealPath();
|
||||
$array_string .= "'{$match[3]}'=>'" . $relative_path . "',\n";
|
||||
$classes[$match[3]] = $file->getRealPath();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage util
|
||||
* @since 2010-03-09
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class CommandProfiler
|
||||
|
@ -5,17 +5,24 @@
|
||||
* @package Majestic
|
||||
* @subpackage util
|
||||
* @since 2010-03-09
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Profiler
|
||||
{
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $start = null;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $end = null;
|
||||
|
||||
/**
|
||||
* @var CommandProfiler[]
|
||||
*/
|
||||
protected $queries = array();
|
||||
|
||||
static protected $instance = null;
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-26
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class EmailValidator extends RegexValidator
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-26
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class EqualValidator extends Validator
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-26
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class NotEmptyValidator extends Validator
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-26
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class RegexValidator extends Validator
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-24
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Validator implements iValidator
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
interface iValidator
|
||||
|
@ -5,22 +5,30 @@
|
||||
* @package Majestic
|
||||
* @subpackage View
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @method ViewHelperGet get()
|
||||
* @method BreadcrumbViewHelper breadcrumb() breadcrumb(string $text = false, string $href = false) Append next link to breadcrumb
|
||||
* @method GetViewHelper get() get(array $replace) Replace some HTTP GET parameters with $replace
|
||||
* @method HeadViewHelper head() head(string $string = false) Append another string to HEAD section of Layout
|
||||
* @method MsgViewHelper msg() msg(string $msg = null, string $type = null) Set a message to display for user in Layout
|
||||
* @method TitleViewHelper title() title(string $string = false) Append another section for TITLE of Layout
|
||||
* @method FormViewHelper form() form(string $form = null) Get form values from session
|
||||
*
|
||||
*/
|
||||
class PHPView implements iView
|
||||
{
|
||||
|
||||
protected $path = '';
|
||||
|
||||
protected $path = '';
|
||||
|
||||
protected $variables = array();
|
||||
protected $helpers = array();
|
||||
|
||||
protected $helpers = array();
|
||||
|
||||
protected $extension = '.phtml';
|
||||
protected $template = '';
|
||||
|
||||
|
||||
protected $template = '';
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
if (!isset($config['path'])) {
|
||||
@ -28,17 +36,17 @@ class PHPView implements iView
|
||||
}
|
||||
$this->setPath($config['path']);
|
||||
}
|
||||
|
||||
|
||||
public function getPath()
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
|
||||
public function setPath($path)
|
||||
{
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Action $object
|
||||
*/
|
||||
@ -48,7 +56,7 @@ class PHPView implements iView
|
||||
$this->assign($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
@ -57,7 +65,7 @@ class PHPView implements iView
|
||||
{
|
||||
$this->variables[$name] = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
@ -70,7 +78,7 @@ class PHPView implements iView
|
||||
$this->variables[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
@ -83,7 +91,7 @@ class PHPView implements iView
|
||||
$this->variables[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function fetch($template)
|
||||
{
|
||||
$this->template = $this->getTemplatePath($template);
|
||||
@ -92,20 +100,20 @@ class PHPView implements iView
|
||||
ob_start();
|
||||
if (!is_readable($this->template)) {
|
||||
ob_clean();
|
||||
throw new GeneralException('Template "' . $this->template .'" not found.');
|
||||
throw new GeneralException('Template "' . $this->template . '" not found.');
|
||||
}
|
||||
include($this->template);
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
|
||||
public function escape($var)
|
||||
{
|
||||
return htmlentities($var, ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Helpers call
|
||||
*
|
||||
*
|
||||
* @param mixed $name
|
||||
* @param mixed $args
|
||||
* @return ViewHelper
|
||||
@ -115,7 +123,7 @@ class PHPView implements iView
|
||||
$helper = $this->getHelper($name);
|
||||
return call_user_func_array(array($helper, $name), $args);
|
||||
}
|
||||
|
||||
|
||||
protected function getHelper($name)
|
||||
{
|
||||
if (!isset($this->helpers[$name])) {
|
||||
@ -127,7 +135,7 @@ class PHPView implements iView
|
||||
}
|
||||
return $this->helpers[$name];
|
||||
}
|
||||
|
||||
|
||||
protected function getTemplatePath($template)
|
||||
{
|
||||
return $this->path . $template . $this->extension;
|
||||
|
@ -5,15 +5,13 @@
|
||||
* @package Majestic
|
||||
* @subpackage View
|
||||
* @since 2010-03-16
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class BreadcrumbViewHelper extends ViewHelper
|
||||
{
|
||||
|
||||
|
||||
protected $separator = ' > ';
|
||||
|
||||
|
||||
public function breadcrumb($text = false, $href = false)
|
||||
{
|
||||
if ($text) {
|
||||
@ -21,22 +19,22 @@ class BreadcrumbViewHelper extends ViewHelper
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function prepend($text, $href)
|
||||
{
|
||||
Registry::set(__CLASS__, array($text => $href) + Registry::get(__CLASS__, array()));
|
||||
}
|
||||
|
||||
|
||||
public function append($text, $href)
|
||||
{
|
||||
Registry::set(__CLASS__, Registry::get(__CLASS__, array()) + array($text => $href));
|
||||
}
|
||||
|
||||
|
||||
public function setSeparator($sep)
|
||||
{
|
||||
$this->separator = $sep;
|
||||
}
|
||||
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
$data = array();
|
||||
|
@ -5,15 +5,13 @@
|
||||
* @package Majestic
|
||||
* @subpackage View
|
||||
* @since 2010-03-09
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class GetViewHelper extends ViewHelper
|
||||
{
|
||||
|
||||
|
||||
protected $get;
|
||||
|
||||
|
||||
public function get($replace)
|
||||
{
|
||||
$get = $this->getSanitizedRequest();
|
||||
@ -29,7 +27,7 @@ class GetViewHelper extends ViewHelper
|
||||
}
|
||||
return '?' . $this->view->escape(implode('&', $get));
|
||||
}
|
||||
|
||||
|
||||
protected function getSanitizedRequest()
|
||||
{
|
||||
if ($this->get === null) {
|
||||
@ -40,10 +38,10 @@ class GetViewHelper extends ViewHelper
|
||||
}
|
||||
return $this->get;
|
||||
}
|
||||
|
||||
|
||||
protected function impl($name, $value)
|
||||
{
|
||||
if (is_array($value)){
|
||||
if (is_array($value)) {
|
||||
$result = array();
|
||||
foreach ($value as $key => $val) {
|
||||
$result[] = $name . '[' . $key . ']=' . urlencode($val);
|
||||
|
@ -5,13 +5,11 @@
|
||||
* @package Majestic
|
||||
* @subpackage View
|
||||
* @since 2010-03-16
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class HeadViewHelper extends ViewHelper
|
||||
{
|
||||
|
||||
|
||||
public function head($string = false)
|
||||
{
|
||||
if ($string) {
|
||||
@ -21,7 +19,7 @@ class HeadViewHelper extends ViewHelper
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return implode("\n ", Registry::get(__CLASS__, array())) . "\n";
|
||||
|
@ -5,45 +5,69 @@
|
||||
* @package Majestic
|
||||
* @subpackage View
|
||||
* @since 2010-03-09
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class MsgViewHelper extends ViewHelper
|
||||
{
|
||||
|
||||
|
||||
const SUCCESS = 'success';
|
||||
|
||||
const ERROR = 'error';
|
||||
|
||||
protected $get;
|
||||
|
||||
|
||||
const INFO = 'info';
|
||||
|
||||
const WARNING = 'warning';
|
||||
|
||||
protected $css_prefix = '';
|
||||
|
||||
public function msg($msg = null, $type = null)
|
||||
{
|
||||
if ($msg && $type) {
|
||||
if (!in_array($type, array(self::SUCCESS, self::ERROR))) {
|
||||
if (!in_array($type, array(self::SUCCESS, self::ERROR, self::INFO, self::WARNING))) {
|
||||
throw new GeneralException('Unknown message type: "' . $type . '"');
|
||||
}
|
||||
Session::set(__CLASS__, array('message' => $msg, 'type' => $type));
|
||||
$this->set($msg, $type);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function success($msg)
|
||||
{
|
||||
Session::set(__CLASS__, array('message' => $msg, 'type' => self::SUCCESS));
|
||||
$this->set($msg, self::SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
public function error($msg)
|
||||
{
|
||||
Session::set(__CLASS__, array('message' => $msg, 'type' => self::ERROR));
|
||||
$this->set($msg, self::ERROR);
|
||||
}
|
||||
|
||||
|
||||
public function info($msg)
|
||||
{
|
||||
$this->set($msg, self::INFO);
|
||||
}
|
||||
|
||||
public function warning($msg)
|
||||
{
|
||||
$this->set($msg, self::WARNING);
|
||||
}
|
||||
|
||||
protected function set($msg, $type)
|
||||
{
|
||||
Session::set(__CLASS__, array('message' => $msg, 'type' => $type));
|
||||
}
|
||||
|
||||
public function withPrefix($css_prefix)
|
||||
{
|
||||
$this->css_prefix = $css_prefix;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
$msg = Session::get(__CLASS__, false);
|
||||
if ($msg) {
|
||||
Session::del(__CLASS__);
|
||||
return '<div class="' . $msg['type'] . '">' . $this->view->escape($msg['message']) . '</div>';
|
||||
return '<div class="' . $this->css_prefix . $msg['type'] . '">' . $this->view->escape($msg['message']) . '</div>';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
@ -5,15 +5,13 @@
|
||||
* @package Majestic
|
||||
* @subpackage View
|
||||
* @since 2010-03-16
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class TitleViewHelper extends ViewHelper
|
||||
{
|
||||
|
||||
|
||||
protected $separator = ' - ';
|
||||
|
||||
|
||||
public function title($string = false)
|
||||
{
|
||||
if ($string) {
|
||||
@ -23,12 +21,12 @@ class TitleViewHelper extends ViewHelper
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function setSeparator($sep)
|
||||
{
|
||||
$this->separator = $sep;
|
||||
}
|
||||
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return implode($this->separator, Registry::get(__CLASS__, array()));
|
||||
|
@ -5,18 +5,16 @@
|
||||
* @package Majestic
|
||||
* @subpackage View
|
||||
* @since 2010-03-09
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class ViewHelper
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var PHPView
|
||||
*/
|
||||
protected $view = null;
|
||||
|
||||
|
||||
public function __construct($view)
|
||||
{
|
||||
$this->view = $view;
|
||||
|
@ -5,8 +5,6 @@
|
||||
* @package Majestic
|
||||
* @subpackage View
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
interface iView
|
||||
|
Reference in New Issue
Block a user