You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php namespace Majestic\View;
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage View
  7. * @since 2010-02-25
  8. */
  9. /**
  10. * @method Helpers\BreadcrumbViewHelper breadcrumb() breadcrumb(string $text = false, string $href = false) Append next link to breadcrumb
  11. * @method Helpers\GetViewHelper get() get(array $replace) Replace some HTTP GET parameters with $replace
  12. * @method Helpers\HeadViewHelper head() head(string $string = false) Append another string to HEAD section of Layout
  13. * @method Helpers\MsgViewHelper msg() msg(string $msg = null, string $type = null) Set a message to display for user in Layout
  14. * @method Helpers\TitleViewHelper title() title(string $string = false) Append another section for TITLE of Layout
  15. * @method Helpers\FormViewHelper form() form(string $form = null) Get form values from session
  16. * @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
  17. * @method Helpers\ScriptFooterViewHelper scriptFooter() scriptFooter($src = false, $type = 'text/javascript') Append script src (javascript or other) into footer section
  18. * @method Helpers\GroupedCssViewHelper groupedCss() groupedCss($relative_file_path = false) Append css into one file
  19. * @method Helpers\GroupedJsViewHelper groupedJs() groupedJs($relative_file_path = false) Append js into one file
  20. * @method Helpers\DescriptionViewHelper description() description(string $string = false) Set value of meta-tag description
  21. * @method Helpers\KeywordsViewHelper keywords() keywords(string $string = false) Set value of meta-tag keywords
  22. *
  23. */
  24. class PHPView implements iView
  25. {
  26. protected $path = '';
  27. protected $variables = array();
  28. protected $helpers = array();
  29. protected $extension = '.phtml';
  30. protected $template = '';
  31. public function __construct($config)
  32. {
  33. if (!isset($config['path'])) {
  34. throw new \Majestic\Exception\InitializationException('Configuration must have a "path" set.');
  35. }
  36. $this->setPath($config['path']);
  37. }
  38. public function getPath()
  39. {
  40. return $this->path;
  41. }
  42. public function setPath($path)
  43. {
  44. $this->path = $path;
  45. }
  46. /**
  47. * @param \Majestic\App\Action $object
  48. */
  49. public function assignObject($object)
  50. {
  51. foreach (get_object_vars($object) as $name => $value) {
  52. $this->assign($name, $value);
  53. }
  54. }
  55. /**
  56. * @param string $name
  57. * @param mixed $value
  58. */
  59. public function assign($name, $value = null)
  60. {
  61. $this->variables[$name] = $value;
  62. }
  63. /**
  64. * @param string $name
  65. * @param mixed $value
  66. */
  67. public function prepend($name, $value)
  68. {
  69. if (isset($this->variables[$name])) {
  70. $this->variables[$name] = $value . $this->variables[$name];
  71. } else {
  72. $this->variables[$name] = $value;
  73. }
  74. }
  75. /**
  76. * @param string $name
  77. * @param mixed $value
  78. */
  79. public function append($name, $value)
  80. {
  81. if (isset($this->variables[$name])) {
  82. $this->variables[$name] .= $value;
  83. } else {
  84. $this->variables[$name] = $value;
  85. }
  86. }
  87. public function fetch($template)
  88. {
  89. $this->template = $this->getTemplatePath($template);
  90. unset($template);
  91. extract($this->variables);
  92. ob_start();
  93. if (!is_readable($this->template)) {
  94. ob_clean();
  95. throw new \Majestic\Exception\GeneralException('Template "' . $this->template . '" not found.');
  96. }
  97. include($this->template);
  98. return ob_get_clean();
  99. }
  100. public function escape($var)
  101. {
  102. return htmlentities($var, ENT_QUOTES, 'UTF-8');
  103. }
  104. /**
  105. * Helpers call
  106. *
  107. * @param mixed $name
  108. * @param mixed $args
  109. * @return Helpers\ViewHelper
  110. */
  111. public function __call($name, $args)
  112. {
  113. $helper = $this->getHelper($name);
  114. return call_user_func_array(array($helper, $name), $args);
  115. }
  116. protected function getHelper($name)
  117. {
  118. if (!isset($this->helpers[$name])) {
  119. $class = '\\' . ucfirst($name) . 'ViewHelper';
  120. if (!class_exists($class)) {
  121. $class = '\Majestic\View\Helpers\\' . ucfirst($name) . 'ViewHelper';
  122. if (!class_exists($class)) {
  123. throw new \Majestic\Exception\GeneralException( 'View helper "' . $class . '" not found.' );
  124. }
  125. }
  126. $this->helpers[$name] = new $class($this);
  127. }
  128. return $this->helpers[$name];
  129. }
  130. protected function getTemplatePath($template)
  131. {
  132. return $this->path . $template . $this->extension;
  133. }
  134. }