view.php 7.1 KB

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