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.

142 lines
3.6 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. *
  17. */
  18. class PHPView implements iView
  19. {
  20. protected $path = '';
  21. protected $variables = array();
  22. protected $helpers = array();
  23. protected $extension = '.phtml';
  24. protected $template = '';
  25. public function __construct($config)
  26. {
  27. if (!isset($config['path'])) {
  28. throw new InitializationException('Configuration must have a "path" set.');
  29. }
  30. $this->setPath($config['path']);
  31. }
  32. public function getPath()
  33. {
  34. return $this->path;
  35. }
  36. public function setPath($path)
  37. {
  38. $this->path = $path;
  39. }
  40. /**
  41. * @param Action $object
  42. */
  43. public function assignObject($object)
  44. {
  45. foreach (get_object_vars($object) as $name => $value) {
  46. $this->assign($name, $value);
  47. }
  48. }
  49. /**
  50. * @param string $name
  51. * @param mixed $value
  52. */
  53. public function assign($name, $value = null)
  54. {
  55. $this->variables[$name] = $value;
  56. }
  57. /**
  58. * @param string $name
  59. * @param mixed $value
  60. */
  61. public function prepend($name, $value)
  62. {
  63. if (isset($this->variables[$name])) {
  64. $this->variables[$name] = $value . $this->variables[$name];
  65. } else {
  66. $this->variables[$name] = $value;
  67. }
  68. }
  69. /**
  70. * @param string $name
  71. * @param mixed $value
  72. */
  73. public function append($name, $value)
  74. {
  75. if (isset($this->variables[$name])) {
  76. $this->variables[$name] .= $value;
  77. } else {
  78. $this->variables[$name] = $value;
  79. }
  80. }
  81. public function fetch($template)
  82. {
  83. $this->template = $this->getTemplatePath($template);
  84. unset($template);
  85. extract($this->variables);
  86. ob_start();
  87. if (!is_readable($this->template)) {
  88. ob_clean();
  89. throw new GeneralException('Template "' . $this->template . '" not found.');
  90. }
  91. include($this->template);
  92. return ob_get_clean();
  93. }
  94. public function escape($var)
  95. {
  96. return htmlentities($var, ENT_QUOTES, 'UTF-8');
  97. }
  98. /**
  99. * Helpers call
  100. *
  101. * @param mixed $name
  102. * @param mixed $args
  103. * @return ViewHelper
  104. */
  105. public function __call($name, $args)
  106. {
  107. $helper = $this->getHelper($name);
  108. return call_user_func_array(array($helper, $name), $args);
  109. }
  110. protected function getHelper($name)
  111. {
  112. if (!isset($this->helpers[$name])) {
  113. $class = ucfirst($name) . 'ViewHelper';
  114. if (!class_exists($class)) {
  115. throw new GeneralException('View helper "' . $class . '" not found.');
  116. }
  117. $this->helpers[$name] = new $class($this);
  118. }
  119. return $this->helpers[$name];
  120. }
  121. protected function getTemplatePath($template)
  122. {
  123. return $this->path . $template . $this->extension;
  124. }
  125. }