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.

139 lines
3.7 KiB

  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. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. /**
  12. * @method BreadcrumbViewHelper breadcrumb() breadcrumb(string $text = false, string $href = false) Append next link to breadcrumb
  13. * @method GetViewHelper get() get(array $replace) Replace some HTTP GET parameters with $replace
  14. * @method HeadViewHelper head() head(string $string = false) Append another string to HEAD section of Layout
  15. * @method MsgViewHelper msg() msg(string $msg = null, string $type = null) Set a message to display for user in Layout
  16. * @method TitleViewHelper title() title(string $string = false) Append another setcion for TITLE of Layout
  17. *
  18. */
  19. class PHPView implements iView
  20. {
  21. protected $path = '';
  22. protected $variables = array();
  23. protected $helpers = array();
  24. protected $extension = '.phtml';
  25. protected $template = '';
  26. public function __construct($config)
  27. {
  28. if (!isset($config['path'])) {
  29. throw new InitializationException('Configuration must have a "path" set.');
  30. }
  31. $this->setPath($config['path']);
  32. }
  33. public function getPath()
  34. {
  35. return $this->path;
  36. }
  37. public function setPath($path)
  38. {
  39. $this->path = $path;
  40. }
  41. /**
  42. * @param Action $object
  43. */
  44. public function assignObject($object)
  45. {
  46. foreach (get_object_vars($object) as $name => $value) {
  47. $this->assign($name, $value);
  48. }
  49. }
  50. /**
  51. * @param string $name
  52. * @param mixed $value
  53. */
  54. public function assign($name, $value = null)
  55. {
  56. $this->variables[$name] = $value;
  57. }
  58. /**
  59. * @param string $name
  60. * @param mixed $value
  61. */
  62. public function prepend($name, $value)
  63. {
  64. if (isset($this->variables[$name])) {
  65. $this->variables[$name] = $value . $this->variables[$name];
  66. } else {
  67. $this->variables[$name] = $value;
  68. }
  69. }
  70. /**
  71. * @param string $name
  72. * @param mixed $value
  73. */
  74. public function append($name, $value)
  75. {
  76. if (isset($this->variables[$name])) {
  77. $this->variables[$name] .= $value;
  78. } else {
  79. $this->variables[$name] = $value;
  80. }
  81. }
  82. public function fetch($template)
  83. {
  84. $this->template = $this->getTemplatePath($template);
  85. unset($template);
  86. extract($this->variables);
  87. ob_start();
  88. if (!is_readable($this->template)) {
  89. ob_clean();
  90. throw new GeneralException('Template "' . $this->template .'" not found.');
  91. }
  92. include($this->template);
  93. return ob_get_clean();
  94. }
  95. public function escape($var)
  96. {
  97. return htmlentities($var, ENT_QUOTES, 'UTF-8');
  98. }
  99. /**
  100. * Helpers call
  101. *
  102. * @param mixed $name
  103. * @param mixed $args
  104. * @return ViewHelper
  105. */
  106. public function __call($name, $args)
  107. {
  108. $helper = $this->getHelper($name);
  109. return call_user_func_array(array($helper, $name), $args);
  110. }
  111. protected function getHelper($name)
  112. {
  113. if (!isset($this->helpers[$name])) {
  114. $class = ucfirst($name) . 'ViewHelper';
  115. if (!class_exists($class)) {
  116. throw new GeneralException('View helper "' . $class . '" not found.');
  117. }
  118. $this->helpers[$name] = new $class($this);
  119. }
  120. return $this->helpers[$name];
  121. }
  122. protected function getTemplatePath($template)
  123. {
  124. return $this->path . $template . $this->extension;
  125. }
  126. }