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.

104 lines
2.4 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 Exception('Configuration must have a "path".');
  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 ViewAction $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. public function assign($name, $value = null)
  46. {
  47. $this->variables[$name] = $value;
  48. }
  49. public function fetch($template)
  50. {
  51. $this->template = $this->getTemplatePath($template);
  52. unset($template);
  53. extract($this->variables);
  54. ob_start();
  55. if (!is_readable($this->template)) {
  56. ob_clean();
  57. throw new Exception('Template "' . $this->template .'" not found.');
  58. }
  59. include($this->template);
  60. return ob_get_clean();
  61. }
  62. public function escape($var)
  63. {
  64. return htmlentities($var, ENT_QUOTES, 'UTF-8');
  65. }
  66. /**
  67. * Helpers call
  68. *
  69. * @param mixed $name
  70. * @param mixed $args
  71. * @return ViewHelper
  72. */
  73. public function __call($name, $args)
  74. {
  75. $helper = $this->getHelper($name);
  76. return call_user_func_array(array($helper, $name), $args);
  77. }
  78. protected function getHelper($name)
  79. {
  80. if (!isset($this->helpers[$name])) {
  81. $class = 'ViewHelper' . ucfirst($name);
  82. if (!class_exists($class)) {
  83. throw new GeneralException('View helper "' . $class . '" not found.');
  84. }
  85. $this->helpers[$name] = new $class($this);
  86. }
  87. return $this->helpers[$name];
  88. }
  89. protected function getTemplatePath($template)
  90. {
  91. return $this->path . $template . $this->extension;
  92. }
  93. }