view.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php namespace Laravel;
  2. /**
  3. * The view factory class is responsible for the instantiation of Views. It is typically
  4. * access through the application instance from a route or controller, and is managed
  5. * as a singleton by the application IoC container.
  6. */
  7. class View_Factory {
  8. /**
  9. * The view composer instance.
  10. *
  11. * @var View_Composer
  12. */
  13. protected $composer;
  14. /**
  15. * The directory containing the views.
  16. *
  17. * @var string
  18. */
  19. protected $path;
  20. /**
  21. * Create a new view factory instance.
  22. *
  23. * @param View_Composer $composer
  24. * @param string $path
  25. * @return void
  26. */
  27. public function __construct(View_Composer $composer, $path)
  28. {
  29. $this->composer = $composer;
  30. $this->path = $path;
  31. }
  32. /**
  33. * Create a new view instance.
  34. *
  35. * The name of the view given to this method should correspond to a view
  36. * within your application views directory. Dots or slashes may used to
  37. * reference views within sub-directories.
  38. *
  39. * <code>
  40. * // Create a new view instance
  41. * $view = View::make('home.index');
  42. *
  43. * // Create a new view instance with bound data
  44. * $view = View::make('home.index', array('name' => 'Fred'));
  45. * </code>
  46. *
  47. * @param string $view
  48. * @param array $data
  49. * @return View
  50. */
  51. public function make($view, $data = array())
  52. {
  53. return new View($this, $this->composer, $view, $data, $this->path($view));
  54. }
  55. /**
  56. * Create a new view instance from a view name.
  57. *
  58. * View names are defined in the application composers file.
  59. *
  60. * <code>
  61. * // Create a new named view instance
  62. * $view = View::of('layout');
  63. *
  64. * // Create a new named view instance with bound data
  65. * $view = View::of('layout', array('name' => 'Fred'));
  66. * </code>
  67. *
  68. * @param string $name
  69. * @param array $data
  70. * @return View
  71. */
  72. protected function of($name, $data = array())
  73. {
  74. if ( ! is_null($view = $this->composer->name($name)))
  75. {
  76. return $this->make($view, $data);
  77. }
  78. throw new \Exception("Named view [$name] is not defined.");
  79. }
  80. /**
  81. * Get the path to a given view on disk.
  82. *
  83. * @param string $view
  84. * @return string
  85. */
  86. protected function path($view)
  87. {
  88. $view = str_replace('.', '/', $view);
  89. if (file_exists($path = $this->path.$view.'.blade'.EXT))
  90. {
  91. return $path;
  92. }
  93. elseif (file_exists($path = $this->path.$view.EXT))
  94. {
  95. return $path;
  96. }
  97. throw new \Exception('View ['.$view.'] does not exist.');
  98. }
  99. /**
  100. * Magic Method for handling the dynamic creation of named views.
  101. *
  102. * <code>
  103. * // Create an instance of the "layout" named view
  104. * $view = View::of_layout();
  105. *
  106. * // Create an instance of the "layout" named view with bound data
  107. * $view = View::of_layout(array('name' => 'Fred'));
  108. * </code>
  109. */
  110. public function __call($method, $parameters)
  111. {
  112. if (strpos($method, 'of_') === 0)
  113. {
  114. return $this->of(substr($method, 3), Arr::get($parameters, 0, array()));
  115. }
  116. }
  117. }
  118. /**
  119. * The view composer class is responsible for calling the composer on a view and
  120. * searching through the view composers for a given view name. It is injected
  121. * into the View_Factory and View instances themselves, and is managed as a singleton
  122. * by the application IoC container.
  123. */
  124. class View_Composer {
  125. /**
  126. * The view composers.
  127. *
  128. * @var array
  129. */
  130. protected $composers;
  131. /**
  132. * Create a new view composer instance.
  133. *
  134. * @param array $composers
  135. * @return void
  136. */
  137. public function __construct($composers)
  138. {
  139. $this->composers = $composers;
  140. }
  141. /**
  142. * Find the key for a view by name.
  143. *
  144. * @param string $name
  145. * @return string
  146. */
  147. public function name($name)
  148. {
  149. foreach ($this->composers as $key => $value)
  150. {
  151. if ($name === $value or (isset($value['name']) and $name === $value['name'])) { return $key; }
  152. }
  153. }
  154. /**
  155. * Call the composer for the view instance.
  156. *
  157. * @param View $view
  158. * @return void
  159. */
  160. public function compose(View $view)
  161. {
  162. if (isset($this->composers['shared'])) call_user_func($this->composers['shared'], $view);
  163. if (isset($this->composers[$view->view]))
  164. {
  165. foreach ((array) $this->composers[$view->view] as $key => $value)
  166. {
  167. if ($value instanceof \Closure) return call_user_func($value, $view);
  168. }
  169. }
  170. }
  171. }
  172. /**
  173. * The view class is returned by the View Factory "make" method, and is the primary
  174. * class for working with individual views. It provides methods for binding data to
  175. * views as well as evaluating and rendering their contents.
  176. */
  177. class View {
  178. /**
  179. * The name of the view.
  180. *
  181. * @var string
  182. */
  183. public $view;
  184. /**
  185. * The view data.
  186. *
  187. * @var array
  188. */
  189. public $data;
  190. /**
  191. * The path to the view on disk.
  192. *
  193. * @var string
  194. */
  195. protected $path;
  196. /**
  197. * The view composer instance.
  198. *
  199. * @var View_Composer
  200. */
  201. protected $composer;
  202. /**
  203. * The view factory instance, which is used to create sub-views.
  204. *
  205. * @var View_Factory
  206. */
  207. protected $factory;
  208. /**
  209. * Create a new view instance.
  210. *
  211. * @param View_Factory $factory
  212. * @param View_Composer $composer
  213. * @param string $view
  214. * @param array $data
  215. * @param string $path
  216. * @return void
  217. */
  218. public function __construct(View_Factory $factory, View_Composer $composer, $view, $data, $path)
  219. {
  220. $this->view = $view;
  221. $this->data = $data;
  222. $this->path = $path;
  223. $this->factory = $factory;
  224. $this->composer = $composer;
  225. }
  226. /**
  227. * Get the evaluated string content of the view.
  228. *
  229. * If the view has a composer, it will be executed. All sub-views and responses will
  230. * also be evaluated and converted to their string values.
  231. *
  232. * @return string
  233. */
  234. public function render()
  235. {
  236. $this->composer->compose($this);
  237. foreach ($this->data as &$data)
  238. {
  239. if ($data instanceof View or $data instanceof Response) $data = $data->render();
  240. }
  241. ob_start() and extract($this->data, EXTR_SKIP);
  242. $content = ($this->bladed()) ? Blade::parse($this->path) : file_get_contents($this->path);
  243. eval('?>'.$content);
  244. return ob_get_clean();
  245. }
  246. /**
  247. * Determine if the view is using the blade view engine.
  248. *
  249. * @return bool
  250. */
  251. protected function bladed()
  252. {
  253. return (strpos($this->path, '.blade'.EXT) !== false);
  254. }
  255. /**
  256. * Add a view instance to the view data.
  257. *
  258. * <code>
  259. * // Bind a partial view to the view data
  260. * $view->partial('footer', 'partials/footer');
  261. *
  262. * // Bind a partial view to the view data with it's own bound data
  263. * $view->partial('footer', 'partials/footer', array('name' => 'Fred'));
  264. * </code>
  265. *
  266. * @param string $key
  267. * @param string $view
  268. * @param array $data
  269. * @return View
  270. */
  271. public function partial($key, $view, $data = array())
  272. {
  273. return $this->with($key, $this->factory->make($view, $data));
  274. }
  275. /**
  276. * Add a key / value pair to the view data.
  277. *
  278. * Bound data will be available to the view as variables.
  279. *
  280. * <code>
  281. * // Bind a piece of data to a view instance
  282. * $view->with('name', 'Fred');
  283. * </code>
  284. *
  285. * @param string $key
  286. * @param mixed $value
  287. * @return View
  288. */
  289. public function with($key, $value)
  290. {
  291. $this->data[$key] = $value;
  292. return $this;
  293. }
  294. /**
  295. * Magic Method for getting items from the view data.
  296. */
  297. public function __get($key)
  298. {
  299. return $this->data[$key];
  300. }
  301. /**
  302. * Magic Method for setting items in the view data.
  303. */
  304. public function __set($key, $value)
  305. {
  306. $this->with($key, $value);
  307. }
  308. /**
  309. * Magic Method for determining if an item is in the view data.
  310. */
  311. public function __isset($key)
  312. {
  313. return array_key_exists($key, $this->data);
  314. }
  315. /**
  316. * Magic Method for removing an item from the view data.
  317. */
  318. public function __unset($key)
  319. {
  320. unset($this->data[$key]);
  321. }
  322. }