Add namespace.

This commit is contained in:
2014-06-02 18:58:49 +04:00
parent aec1a60985
commit 1ba341b064
159 changed files with 265 additions and 264 deletions

66
App/Action.php Normal file
View File

@ -0,0 +1,66 @@
<?php namespace Majestic\App;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage app
* @since 2010-02-25
*/
abstract class Action
{
protected $template;
/**
* @var \Majestic\View\PHPView
*/
protected $view;
public function __construct()
{
$this->view = FrontController::getInstance()->getView();
$this->extractParams();
$this->execute();
}
protected function extractParams()
{
foreach (\Majestic\Env::getParam() as $name => $value) {
if (is_string($name)) {
$this->$name = $value;
}
}
}
abstract protected function execute();
/**
* Redirect
*
* @param mixed $url
* @param bool $permanently
*/
protected function redirect($url = null, $permanently = false)
{
if ($permanently) {
header('HTTP/1.1 301 Moved Permanently');
}
header('Location: ' . (($url) ? $url : \Majestic\Env::getRequestUri()));
exit();
}
protected function getTemplate()
{
$class = get_class($this);
$template = ($this->template) ? $this->template : substr($class, 0, -6 /*strlen('Action')*/);
$dir = array_slice(explode('/', \Majestic\Load::getFilePath($class)), -2, 1);
return '/actions/' . array_pop($dir) . '/' . $template;
}
public function fetch()
{
$this->view->assignObject($this);
return $this->view->fetch($this->getTemplate());
}
}

46
App/AjaxAction.php Normal file
View File

@ -0,0 +1,46 @@
<?php namespace Majestic\App;
/**
* AjaxAction
*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage app
* @since 2011-04-27
*/
/**
* Base class for all ajax Actions
*/
abstract class AjaxAction extends Action
{
/**
* Data to output
* @var mixed
*/
public $data = false;
/**
* Use json_encode
* @var bool
*/
protected $json_encode = true;
function __construct()
{
parent::__construct();
$this->template = 'ajax';
}
function fetch()
{
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->json_encode ? json_encode($this->data) : $this->data);
return $this->view->fetch($this->getTemplate());
}
}

99
App/CliController.php Normal file
View File

@ -0,0 +1,99 @@
<?php namespace Majestic\App;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage App
* @since 27.06.12
*
*/
/**
* @desc CliController (run cli_class, end profiler)
* @author Aleksandr Demidov
*/
class CliController
{
/**
* @var CliController
*/
protected static $instance;
protected $error_stream;
protected function __construct()
{
ErrorHandler::init();
$this->error_stream = Config::get('ErrorStream', 'php://stderr');
}
/**
* Refuse cloning
* @codeCoverageIgnoreStart
*/
private function __clone()
{
}
/**
* @codeCoverageIgnoreEnd
*/
/**
* @static
* @return CliController
*/
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new static();
}
return self::$instance;
}
/**
* @param iCli|string $cli
* @throws ErrorException|GeneralException
*/
public function execute($cli)
{
try {
$this->run($cli);
}
catch (Exception $e) {
$this->exception($e);
}
}
protected function run($cli)
{
if (is_string($cli)) {
if (!class_exists($cli)) {
throw new GeneralException('Action class "' . $cli . '" not found.');
}
$cli = new $cli;
}
if (!in_array('iCli', class_implements($cli))) {
throw new ErrorException('Runner "' . get_class($cli) . '" need implement of "iCli" interface.');
}
$cli->run();
if (Config::get('PROFILER')) {
$profile = Profiler::getInstance()->getCli();
if (Config::get('LOGGING')) {
Logger::getInstance()->log($profile);
} else {
echo $profile;
}
}
}
protected function exception($e)
{
$code = $e->getCode();
if ($e instanceof ErrorException) {
$code = $e->getSeverity();
}
file_put_contents($this->error_stream, PHP_EOL . 'Error ' . '#' . $code . ': ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
file_put_contents($this->error_stream, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL, FILE_APPEND);
}
}

84
App/ErrorAction.php Normal file
View File

@ -0,0 +1,84 @@
<?php namespace Majestic\App;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage app
* @since 2010-02-25
*/
class ErrorAction extends Action
{
/**
* @var \ErrorException|\Majestic\Exception\ErrorHTTPException
*/
public $exception;
protected $ajax_error = false;
public function __construct($exception)
{
$this->exception = $exception;
parent::__construct();
}
protected function execute()
{
if ($this->exception instanceof \Majestic\Exception\Error404Exception) {
$this->template = 404;
} elseif ($this->exception instanceof \Majestic\Exception\ErrorHTTPException) {
$this->template = 'HTTP';
} else {
$this->template = 500;
}
$this->logError();
$this->sendHTTPCode();
}
public function fetch()
{
if ($this->isAjaxActionError()) {
return $this->exception->getMessage();
}
return parent::fetch();
}
protected function getTemplate()
{
return '/actions/' . $this->template;
}
protected function sendHttpCode()
{
if ($this->exception instanceof \Majestic\Exception\ErrorHTTPException) {
header($this->exception->getHTTPHeader());
} else {
header('HTTP/1.0 500 Internal Server Error');
}
}
protected function logError()
{
if (!$this->exception instanceof \Majestic\Exception\Error404Exception) {
\Majestic\Exception\ErrorHandler::logError($this->exception);
}
}
/**
* Check, if exception was thrown from AjaxAction Class
* @return bool
*/
protected function isAjaxActionError()
{
return $this->ajax_error;
}
/**
* Set if exception was thrown from AjaxAction subclass
*/
public function setAjaxError()
{
$this->ajax_error = true;
}
}

163
App/FrontController.php Normal file
View File

@ -0,0 +1,163 @@
<?php namespace Majestic\App;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage app
* @since 2010-02-24
*/
class FrontController
{
/**
* @var Router\Router
*/
protected $router;
/**
* @var string
*/
protected $view = '\Majestic\View\PHPView';
protected $base_url = '';
/**
* @var FrontController
*/
protected static $instance;
private function __construct()
{
// ErrorHandler::init();
$this->router = new Router\Router();
}
/**
* Refuse cloning
* @codeCoverageIgnoreStart
*/
private function __clone()
{
}
/**
* @codeCoverageIgnoreEnd
*/
/**
* @return FrontController
*/
static public function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* @param string $view
* @return FrontController
*/
public function setView($view)
{
$this->view = $view;
return $this;
}
/**
*
* @param null $view
* @return ..\View\PHPView
*/
public function getView($view = null)
{
$view = ($view) ? $view : $this->view;
return new $view(\Majestic\Config::get($view));
}
/**
* @param string $url
* @return FrontController
*/
public function setBaseUrl($url)
{
$this->base_url = rtrim($url, '/');
return $this;
}
public function getBaseUrl()
{
return $this->base_url;
}
/**
* @return Router\Router
*/
public function getRouter()
{
return $this->router;
}
public function execute()
{
try {
$request = \Majestic\Env::getRequestUri(true);
$route = $this->getRouter()->route($request);
if (!$route) {
throw new \Majestic\Exception\Error404Exception('Route for "' . $request . '" not found');
}
$action_class = $route->getAction();
if (!class_exists($action_class)) {
throw new \Majestic\Exception\GeneralException('Action class "' . $action_class . '" not found.');
}
$action = new $action_class();
$layout_class = $route->getLayout();
if (!class_exists($layout_class)) {
throw new \Majestic\Exception\GeneralException('Layout class "' . $layout_class . '" not found.');
}
/**
* @var \Majestic\Layout\Layout $layout
*/
$layout = new $layout_class();
$html = $layout->fetch($action);
if (\Majestic\Config::get('PROFILER')) {
if (is_subclass_of($action, 'AjaxAction')) {
\Majestic\Util\Profiler\Profiler::getInstance()->getJson();
} else {
$html = \Majestic\Util\Profiler\Profiler::getInstance()->end($html);
}
}
return $html;
}
catch (\Exception $e) {
if (\Majestic\Config::get('DEBUG')) {
if (!headers_sent()) {
if ($e instanceof \Majestic\Exception\ErrorHTTPException) {
header($e->getHTTPHeader());
} else {
header('HTTP/1.0 500 Internal Server Error');
}
}
\Majestic\Exception\ErrorHandler::logError($e);
return \Majestic\Exception\ErrorHandler::showDebug($e);
}
$layout_class = $this->getRouter()->getErrorLayout();
/**
* @var \Majestic\Layout\ErrorLayout $layout
*/
$layout = new $layout_class();
$layout->setException($e);
$error_action = new ErrorAction($e);
if (isset($action_class) && is_subclass_of($action_class, 'AjaxAction')) {
$error_action->setAjaxError();
}
return $layout->fetch($error_action);
}
}
}

55
App/PagerAction.php Normal file
View File

@ -0,0 +1,55 @@
<?php namespace Majestic\App;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage app
* @since 2010-03-07
*/
class PagerAction extends Action
{
public $page = 1;
public $last_page = 1;
protected $offset = 0;
protected $limit;
public function __construct($limit = 20)
{
$this->limit = $limit;
parent::__construct();
}
protected function execute() {}
public function setCount($count)
{
$this->last_page = ceil($count / $this->limit);
if (Env::Get('p') == 'last') {
$page = $this->last_page;
} else {
$page = (int) Env::Get('p');
}
$this->page = ($page <= $this->last_page && $page > 0) ? $page : 1;
$this->offset = $this->limit * ($this->page - 1);
}
public function getOffset()
{
return (int) $this->offset;
}
public function getLimit()
{
return (int) $this->limit;
}
protected function getTemplate()
{
$template = ($this->template) ? $this->template : substr(get_class($this), 0, -6 /*strlen('Action')*/);
return '/actions/' . $template;
}
}

76
App/Router/Route.php Normal file
View File

@ -0,0 +1,76 @@
<?php namespace Majestic\App\Router;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage app
* @since 2010-02-25
*/
class Route
{
protected $route;
protected $action;
protected $params;
protected $layout;
public function __construct($route, $action, $params = array(), $layout = null)
{
$this->route = $route;
$this->action = $action;
$this->params = $params;
$this->layout = $layout;
}
/**
* @param array $request
* @return bool
*/
public function match($request)
{
$parts = explode('/', $this->route);
$cnt = count($parts);
if(count($request) != $cnt) {
return false;
}
for ($i = 0; $i < $cnt; $i++) {
if (substr($parts[$i], 0, 1) == ':') {
$this->params[substr($parts[$i], 1)] = urldecode($request[$i]);
} elseif (substr($parts[$i], 0, 2) == '(?') {
$match = array();
if (!preg_match('#^' . $parts[$i] . '$#iu', $request[$i], $match)) {
return false;
}
$start = strpos($parts[$i], '<') + 1;
$key = substr($parts[$i], $start, strpos($parts[$i], '>', $start) - $start);
$this->params[$key] = urldecode($match[$key]);
} elseif ($parts[$i] != $request[$i]) {
return false;
}
}
return true;
}
public function getUri()
{
return '/' . $this->route;
}
public function getAction()
{
return $this->action . 'Action';
}
public function getLayout()
{
return $this->layout . 'Layout';
}
public function getParams()
{
return $this->params;
}
}

129
App/Router/Router.php Normal file
View File

@ -0,0 +1,129 @@
<?php namespace Majestic\App\Router;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage app
* @since 2010-02-25
*/
class Router
{
/**
* @var Route[]
*/
protected $routes = array();
/**
* @var string
*/
protected $route_name;
protected $default_layout = 'Default';
protected $error_layout = 'Error';
/**
* @var Route
*/
protected $route;
public function add($name, $route, $action, $params = array(), $layout = null)
{
if (!$layout) {
$layout = $this->default_layout;
}
$this->routes[$name] = new Route($route, $action, $params, $layout);
}
/**
* @param $request
* @return bool|Route
*/
public function route($request)
{
$req = explode('/', trim($request, '/'));
foreach ($this->routes as $name => $route) {
if ($route->match($req)) {
$this->route_name = $name;
$this->route = $route;
\Majestic\Env::setParams($route->getParams());
return $this->route;
}
}
return false;
}
/**
* Set default layout name
* @param string $layout
*/
public function setDefaultLayout($layout = 'Default')
{
$this->default_layout = $layout;
}
/**
* Sets the name of error page layout
* @param string $layout
*/
public function setErrorLayout($layout = 'Error')
{
$this->error_layout = $layout;
}
/**
* Returns error layout name
* @return string Error layout name
*/
public function getErrorLayout()
{
return $this->error_layout . 'Layout';
}
/**
* Return current router name
* @return string
*/
public function getRouteName()
{
return $this->route_name;
}
/**
* @param null|string $name
* @return Route
* @throws \ErrorException
*/
public function getRoute($name = null)
{
if (is_null($name)) {
return $this->route;
} else {
if ($this->routeIsExists($name)) {
return $this->getRouteByName($name);
} else {
throw new \ErrorException('Unknown route name: "' . $name . '".');
}
}
}
/**
* @param string $name
* @return bool
*/
public function routeIsExists($name)
{
return array_key_exists($name, $this->routes);
}
/**
* @param string $name
* @return Route
*/
protected function getRouteByName($name)
{
return $this->routes[$name];
}
}

23
App/StaticAction.php Normal file
View File

@ -0,0 +1,23 @@
<?php namespace Majestic\App;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage app
* @since 2010-02-25
*/
abstract class StaticAction extends Action
{
protected function getTemplate()
{
$template = ($this->template) ? $this->template : substr(get_class($this), 0, -6 /*strlen('Action')*/);
return '/static/' . $template;
}
public function fetch()
{
return $this->view->fetch($this->getTemplate());
}
}

18
App/iCli.php Normal file
View File

@ -0,0 +1,18 @@
<?php namespace Majestic\App;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage App
* @since 10.07.12
*
*/
/**
* @desc Starter cli point need implement iCli
* @author Aleksandr Demidov
*/
interface iCli
{
public function run();
}