PageRenderTime 35ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Nano/library/Nano/View.php

http://mvh-source.googlecode.com/
PHP | 178 lines | 131 code | 34 blank | 13 comment | 23 complexity | 1ce88eeb9d77803f62d768809f13e428 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * basic attempt at a view abstraction.
  4. * This implementation is far from generic enough but serves the purpose of
  5. * being a placeholder for now!
  6. */
  7. class Nano_View{
  8. protected $_base;
  9. protected $_path;
  10. protected $_layout;
  11. protected $_structure;
  12. protected $_request;
  13. protected $_view;
  14. protected $_content;
  15. protected $_helperPath;
  16. protected $_helpers;
  17. protected $_layoutpath;
  18. public function __construct( $config, $request ){
  19. $config = (array) $config;
  20. $config['request'] = $request;
  21. foreach( $config as $name => $value ){
  22. //@todo not a smart way to do things here
  23. // may overwrite _layout as such, etc.
  24. if( ($property = '_' . $name) && property_exists( $this, $property ) ){
  25. if( ($method = 'set' . ucfirst($name)) && method_exists( $this, $method ) ){
  26. $this->$method( $value );
  27. }
  28. else{
  29. $this->$property = $value;
  30. }
  31. }
  32. }
  33. }
  34. public function __set( $name, $value ){
  35. $this->getContent()->$name = $value;
  36. }
  37. public function __get( $name ){
  38. return $this->getContent()->$name;
  39. }
  40. public function __call( $name, $arguments ){
  41. $helper = $this->getHelper( $name );
  42. return call_user_func_array( array($helper, $name), $arguments );
  43. }
  44. public function getHelperPath(){
  45. if( null == $this->_helperPath ){
  46. $this->_helperPath = array();
  47. }
  48. return $this->_helperPath;
  49. }
  50. public function __toString(){
  51. if( false !== ( $layoutPath = $this->getLayout() ) && ! empty( $layoutPath ) ){
  52. if( false !== ( $path = $this->getViewScript() ) && ! empty($path) ){
  53. ob_start();
  54. require_once( $path );
  55. $this->getContent()->viewScript = ob_get_clean(); // content is now a string!
  56. }
  57. //var_dump( $this );
  58. ob_start();
  59. require_once( $layoutPath );
  60. return ob_get_clean();
  61. }
  62. return '';
  63. }
  64. public function getLayout(){
  65. if( null === $this->_layout && null !== $this->_layoutpath ){
  66. $layout = sprintf('%s/%s/%s', $this->_base, $this->_path, $this->_layoutpath );
  67. $this->setLayout( $layout );
  68. }
  69. return $this->_layout;
  70. }
  71. public function disableLayout(){
  72. $this->_layout = false;
  73. }
  74. public function setLayout( $path ){
  75. $this->_layout = $path;
  76. }
  77. /**
  78. * Gets ViewScript filename
  79. */
  80. public function getViewScript(){
  81. if( null === $this->_view ){
  82. $request = $this->getRequest();
  83. $view = str_replace( array(':controller', ':action')
  84. , array( $request->controller, $request->action )
  85. , $this->_structure
  86. );
  87. $this->setViewScript( $view );
  88. }
  89. return $this->_view;
  90. }
  91. public function setViewScript( $view ){
  92. $path = sprintf( '%s/%s%s', $this->_base, $this->_path, $view );
  93. $this->_view = $path;
  94. }
  95. public function disableViewScript(){
  96. $this->_view = false;
  97. }
  98. private function getContent(){
  99. if( null === $this->_content ){
  100. $this->_content = new Nano_Collection();
  101. }
  102. return $this->_content;
  103. }
  104. public function getRequest(){
  105. return $this->_request;
  106. }
  107. public function headScript(){
  108. return $this->getHelper( 'Script' );
  109. }
  110. public function getHelper( $name ){
  111. if( null == $this->_helpers ){
  112. $this->_helpers = array();
  113. }
  114. if( !key_exists( $name, $this->_helpers ) ){
  115. $base = ucfirst( $name ) . '.php';
  116. $paths = $this->getHelperPath();
  117. $className = 'Helper_' . ucfirst($name);
  118. $helper = null;
  119. foreach( $paths as $path ){
  120. $path = $path . '/' . $base;
  121. if( file_exists( $path ) ){
  122. require_once( $path );
  123. if( class_exists( $className ) ){
  124. $helper = new $className( $this );
  125. }
  126. }
  127. }
  128. if( null == $helper ){
  129. $className = 'Nano_View_Helper_' . ucfirst($name);
  130. // ok this will throw an exception
  131. // but you should know this...
  132. $helper = new $className( $this );
  133. }
  134. $this->_helpers[$name] = $helper;
  135. }
  136. return $this->_helpers[$name];
  137. }
  138. public function setHelperPath( $path ){
  139. if( null == $this->_helperPath ){
  140. $this->_helperPath = array();
  141. }
  142. $this->_helperPath[] = $path;
  143. }
  144. }