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.

148 lines
4.4 KiB

12 years ago
  1. <?php
  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 BreadcrumbViewHelper breadcrumb() breadcrumb(string $text = false, string $href = false) Append next link to breadcrumb
  11. * @method GetViewHelper get() get(array $replace) Replace some HTTP GET parameters with $replace
  12. * @method HeadViewHelper head() head(string $string = false) Append another string to HEAD section of Layout
  13. * @method MsgViewHelper msg() msg(string $msg = null, string $type = null) Set a message to display for user in Layout
  14. * @method TitleViewHelper title() title(string $string = false) Append another section for TITLE of Layout
  15. * @method FormViewHelper form() form(string $form = null) Get form values from session
  16. * @method 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 ScriptFooterViewHelper scriptFooter() scriptFooter($src = false, $type = 'text/javascript') Append script src (javascript or other) into footer section
  18. * @method GroupedCssViewHelper groupedCss() groupedCss($relative_file_path = false) Append css into one file
  19. * @method GroupedJsViewHelper groupedJs() groupedJs($relative_file_path = false) Append js into one file
  20. * @method DescriptionViewHelper description() description(string $string = false) Set value of meta-tag description
  21. * @method 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 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 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 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 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. throw new GeneralException('View helper "' . $class . '" not found.');
  122. }
  123. $this->helpers[$name] = new $class($this);
  124. }
  125. return $this->helpers[$name];
  126. }
  127. protected function getTemplatePath($template)
  128. {
  129. return $this->path . $template . $this->extension;
  130. }
  131. }