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.

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