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

View File

@ -0,0 +1,50 @@
<?php namespace Majestic\View\Helpers;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage View
* @since 2010-03-16
*/
class BreadcrumbViewHelper extends ViewHelper
{
protected $separator = ' &gt; ';
public function breadcrumb($text = false, $href = false)
{
if ($text) {
$this->append($text, $href);
}
return $this;
}
public function prepend($text, $href)
{
\Majestic\Registry::set(__CLASS__, array($text => $href) + \Majestic\Registry::get(__CLASS__, array()));
}
public function append($text, $href)
{
\Majestic\Registry::set(__CLASS__, \Majestic\Registry::get(__CLASS__, array()) + array($text => $href));
}
public function setSeparator($sep)
{
$this->separator = $sep;
}
public function __toString()
{
$data = array();
foreach (\Majestic\Registry::get(__CLASS__, array()) as $text => $href) {
if ($href) {
$data[] = '<a href="' . $this->view->escape($href) . '">' . $this->view->escape($text) . '</a>';
} else {
$data[] = $this->view->escape($text);
}
}
return implode($this->separator, $data);
}
}

View File

@ -0,0 +1,55 @@
<?php namespace Majestic\View\Helpers;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage View
* @since 2010-03-09
*/
class GetViewHelper extends ViewHelper
{
protected $get;
public function get($replace)
{
$get = $this->getSanitizedRequest();
if (!is_array($replace)) {
$replace = array($replace);
}
foreach ($replace as $key => $value) {
if (is_int($key)) {
unset($get[$value]);
} else {
$get[$key] = $this->impl($key, $value);
}
}
return '?' . $this->view->escape(implode('&', $get));
}
protected function getSanitizedRequest()
{
if ($this->get === null) {
$get = \Majestic\Env\Env::Get();
foreach ($get as $key => $value) {
$this->get[$key] = $this->impl($key, $value);
}
}
return $this->get;
}
protected function impl($name, $value)
{
if (is_array($value)) {
$result = array();
foreach ($value as $key => $val) {
$result[] = $name . '[' . $key . ']=' . urlencode($val);
}
$result = implode('&', $result);
} else {
$result = $name . '=' . urlencode($value);
}
return $result;
}
}

View File

@ -0,0 +1,27 @@
<?php namespace Majestic\View\Helpers;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage View
* @since 2010-03-16
*/
class HeadViewHelper extends ViewHelper
{
public function head($string = false)
{
if ($string) {
$data = \Majestic\Registry::get(__CLASS__, array());
$data[] = $string;
\Majestic\Registry::set(__CLASS__, $data);
}
return $this;
}
public function __toString()
{
return implode("\n ", \Majestic\Registry::get(__CLASS__, array())) . "\n";
}
}

View File

@ -0,0 +1,95 @@
<?php namespace Majestic\View\Helpers;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage View
* @since 2010-03-09
*/
class MsgViewHelper extends ViewHelper
{
const SUCCESS = 'success';
const ERROR = 'error';
const INFO = 'info';
const WARNING = 'warning';
protected $use_as_html = false;
private static $type_to_class = array(
self::SUCCESS => 'success',
self::ERROR => 'error',
self::INFO => 'info',
self::WARNING => 'warning',
);
protected $css_prefix = '';
public function msg($msg = null, $type = null)
{
if ($msg && $type) {
if (!in_array($type, array(self::SUCCESS, self::ERROR, self::INFO, self::WARNING))) {
throw new \Majestic\Exception\GeneralException('Unknown message type: "' . $type . '"');
}
$this->set($msg, $type);
}
return $this;
}
public function success($msg)
{
$this->set($msg, self::SUCCESS);
}
public function error($msg)
{
$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)
{
\Majestic\Session\Session::set(__CLASS__, array('message' => $msg, 'type' => $type));
}
public static function getType()
{
$msg = \Majestic\Session\Session::get(__CLASS__, false);
return $msg ? $msg['type'] : '';
}
public function withPrefix($css_prefix)
{
$this->css_prefix = $css_prefix;
return $this;
}
public function __toString()
{
$msg = \Majestic\Session\Session::get(__CLASS__, false);
if ($msg) {
\Majestic\Session\Session::del(__CLASS__);
$type_to_class = static::getTypeToClass();
return '<div class="' . $this->css_prefix . $type_to_class[$msg['type']] . '">' . (($this->use_as_html)?$msg['message']:$this->view->escape($msg['message'])) . '</div>';
}
return '';
}
protected static function getTypeToClass()
{
return self::$type_to_class;
}
}

View File

@ -0,0 +1,34 @@
<?php namespace Majestic\View\Helpers;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage View
* @since 2010-03-16
*/
class TitleViewHelper extends ViewHelper
{
protected $separator = ' - ';
public function title($string = false)
{
if ($string) {
$data = \Majestic\Registry::get(__CLASS__, array());
$data[] = $string;
\Majestic\Registry::set(__CLASS__, $data);
}
return $this;
}
public function setSeparator($sep)
{
$this->separator = $sep;
}
public function __toString()
{
return implode($this->separator, \Majestic\Registry::get(__CLASS__, array()));
}
}

View File

@ -0,0 +1,22 @@
<?php namespace Majestic\View\Helpers;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage View
* @since 2010-03-09
*/
abstract class ViewHelper
{
/**
* @var \Majestic\View\PHPView
*/
protected $view = null;
public function __construct($view)
{
$this->view = $view;
}
}

152
View/PHPView.php Normal file
View File

@ -0,0 +1,152 @@
<?php namespace Majestic\View;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage View
* @since 2010-02-25
*/
/**
* @method Helpers\BreadcrumbViewHelper breadcrumb() breadcrumb(string $text = false, string $href = false) Append next link to breadcrumb
* @method Helpers\GetViewHelper get() get(array $replace) Replace some HTTP GET parameters with $replace
* @method Helpers\HeadViewHelper head() head(string $string = false) Append another string to HEAD section of Layout
* @method Helpers\MsgViewHelper msg() msg(string $msg = null, string $type = null) Set a message to display for user in Layout
* @method Helpers\TitleViewHelper title() title(string $string = false) Append another section for TITLE of Layout
* @method Helpers\FormViewHelper form() form(string $form = null) Get form values from session
* @method Helpers\LinkHeadViewHelper linkHead() linkHead($href = false, $rel = 'stylesheet', $type = 'text/css', $media = 'screen,projection,print') Append link tag (css file or other) into head section
* @method Helpers\ScriptFooterViewHelper scriptFooter() scriptFooter($src = false, $type = 'text/javascript') Append script src (javascript or other) into footer section
* @method Helpers\GroupedCssViewHelper groupedCss() groupedCss($relative_file_path = false) Append css into one file
* @method Helpers\GroupedJsViewHelper groupedJs() groupedJs($relative_file_path = false) Append js into one file
* @method Helpers\DescriptionViewHelper description() description(string $string = false) Set value of meta-tag description
* @method Helpers\KeywordsViewHelper keywords() keywords(string $string = false) Set value of meta-tag keywords
*
*/
class PHPView implements iView
{
protected $path = '';
protected $variables = array();
protected $helpers = array();
protected $extension = '.phtml';
protected $template = '';
public function __construct($config)
{
if (!isset($config['path'])) {
throw new \Majestic\Exception\InitializationException('Configuration must have a "path" set.');
}
$this->setPath($config['path']);
}
public function getPath()
{
return $this->path;
}
public function setPath($path)
{
$this->path = $path;
}
/**
* @param \Majestic\App\Action $object
*/
public function assignObject($object)
{
foreach (get_object_vars($object) as $name => $value) {
$this->assign($name, $value);
}
}
/**
* @param string $name
* @param mixed $value
*/
public function assign($name, $value = null)
{
$this->variables[$name] = $value;
}
/**
* @param string $name
* @param mixed $value
*/
public function prepend($name, $value)
{
if (isset($this->variables[$name])) {
$this->variables[$name] = $value . $this->variables[$name];
} else {
$this->variables[$name] = $value;
}
}
/**
* @param string $name
* @param mixed $value
*/
public function append($name, $value)
{
if (isset($this->variables[$name])) {
$this->variables[$name] .= $value;
} else {
$this->variables[$name] = $value;
}
}
public function fetch($template)
{
$this->template = $this->getTemplatePath($template);
unset($template);
extract($this->variables);
ob_start();
if (!is_readable($this->template)) {
ob_clean();
throw new \Majestic\Exception\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 Helpers\ViewHelper
*/
public function __call($name, $args)
{
$helper = $this->getHelper($name);
return call_user_func_array(array($helper, $name), $args);
}
protected function getHelper($name)
{
if (!isset($this->helpers[$name])) {
$class = '\\' . ucfirst($name) . 'ViewHelper';
if (!class_exists($class)) {
$class = '\Majestic\View\Helpers\\' . ucfirst($name) . 'ViewHelper';
if (!class_exists($class)) {
throw new \Majestic\Exception\GeneralException( 'View helper "' . $class . '" not found.' );
}
}
$this->helpers[$name] = new $class($this);
}
return $this->helpers[$name];
}
protected function getTemplatePath($template)
{
return $this->path . $template . $this->extension;
}
}

16
View/iView.php Normal file
View File

@ -0,0 +1,16 @@
<?php namespace Majestic\View;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage View
* @since 2010-02-25
*/
interface iView
{
public function assign($name, $value = null);
public function prepend($name, $value);
public function append($name, $value);
public function fetch($template);
}