Content.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. namespace Dcat\Admin\Layout;
  3. use Closure;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Exception\RuntimeException;
  6. use Dcat\Admin\Traits\HasBuilderEvents;
  7. use Illuminate\Contracts\Support\Renderable;
  8. use Illuminate\Support\Str;
  9. use Illuminate\Support\Traits\Macroable;
  10. use Illuminate\Support\ViewErrorBag;
  11. class Content implements Renderable
  12. {
  13. use HasBuilderEvents, Macroable;
  14. /**
  15. * @var string
  16. */
  17. protected $view = 'admin::layouts.content';
  18. /**
  19. * @var array
  20. */
  21. protected $variables = [];
  22. /**
  23. * Content title.
  24. *
  25. * @var string
  26. */
  27. protected $title = '';
  28. /**
  29. * Content description.
  30. *
  31. * @var string
  32. */
  33. protected $description = '';
  34. /**
  35. * Page breadcrumb.
  36. *
  37. * @var array
  38. */
  39. protected $breadcrumb = [];
  40. /**
  41. * @var Row[]
  42. */
  43. protected $rows = [];
  44. /**
  45. * @var array
  46. */
  47. protected $config = [];
  48. /**
  49. * Content constructor.
  50. *
  51. * @param Closure|null $callback
  52. */
  53. public function __construct(\Closure $callback = null)
  54. {
  55. $this->callResolving();
  56. if ($callback instanceof Closure) {
  57. $callback($this);
  58. }
  59. }
  60. /**
  61. * Create a content instance.
  62. *
  63. * @param mixed ...$params
  64. *
  65. * @return $this
  66. */
  67. public static function make(...$params)
  68. {
  69. return new static(...$params);
  70. }
  71. /**
  72. * @param string $header
  73. *
  74. * @return $this
  75. */
  76. public function header($header = '')
  77. {
  78. return $this->title($header);
  79. }
  80. /**
  81. * Set title of content.
  82. *
  83. * @param string $title
  84. *
  85. * @return $this
  86. */
  87. public function title($title)
  88. {
  89. $this->title = $title;
  90. return $this;
  91. }
  92. /**
  93. * Set description of content.
  94. *
  95. * @param string $description
  96. *
  97. * @return $this
  98. */
  99. public function description($description = '')
  100. {
  101. $this->description = $description;
  102. return $this;
  103. }
  104. /**
  105. * Build full page.
  106. *
  107. * @return $this
  108. */
  109. public function full()
  110. {
  111. return $this->view('admin::layouts.full-content');
  112. }
  113. /**
  114. * Set breadcrumb of content.
  115. *
  116. * @example
  117. * $this->breadcrumb('Menu', 'auth/menu', 'fa fa-align-justify');
  118. * $this->breadcrumb([
  119. * ['text' => 'Menu', 'url' => 'auth/menu', 'icon' => 'fa fa-align-justify']
  120. * ]);
  121. *
  122. * @param array ...$breadcrumb
  123. *
  124. * @return $this
  125. */
  126. public function breadcrumb(...$breadcrumb)
  127. {
  128. $this->formatBreadcrumb($breadcrumb);
  129. $this->breadcrumb = array_merge($this->breadcrumb, $breadcrumb);
  130. return $this;
  131. }
  132. /**
  133. * @param array $breadcrumb
  134. *
  135. * @throws \Exception
  136. *
  137. * @return void
  138. */
  139. protected function formatBreadcrumb(array &$breadcrumb)
  140. {
  141. if (! $breadcrumb) {
  142. throw new RuntimeException('Breadcrumb format error!');
  143. }
  144. $notArray = false;
  145. foreach ($breadcrumb as &$item) {
  146. $isArray = is_array($item);
  147. if ($isArray && ! isset($item['text'])) {
  148. throw new RuntimeException('Breadcrumb format error!');
  149. }
  150. if (! $isArray && $item) {
  151. $notArray = true;
  152. }
  153. }
  154. if (! $breadcrumb) {
  155. throw new RuntimeException('Breadcrumb format error!');
  156. }
  157. if ($notArray) {
  158. $breadcrumb = [
  159. [
  160. 'text' => $breadcrumb[0] ?? null,
  161. 'url' => $breadcrumb[1] ?? null,
  162. 'icon' => $breadcrumb[2] ?? null,
  163. ],
  164. ];
  165. }
  166. }
  167. /**
  168. * Alias of method row.
  169. *
  170. * @param mixed $content
  171. *
  172. * @return Content
  173. */
  174. public function body($content)
  175. {
  176. return $this->row($content);
  177. }
  178. /**
  179. * Add one row for content body.
  180. *
  181. * @param $content
  182. *
  183. * @return $this
  184. */
  185. public function row($content)
  186. {
  187. if ($content instanceof Closure) {
  188. $row = new Row();
  189. call_user_func($content, $row);
  190. $this->addRow($row);
  191. } else {
  192. $this->addRow(new Row($content));
  193. }
  194. return $this;
  195. }
  196. /**
  197. * @param $content
  198. *
  199. * @return $this
  200. */
  201. public function prepend($content)
  202. {
  203. if ($content instanceof Closure) {
  204. $row = new Row();
  205. call_user_func($content, $row);
  206. $this->prependRow($row);
  207. } else {
  208. $this->prependRow(new Row($content));
  209. }
  210. return $this;
  211. }
  212. protected function prependRow(Row $row)
  213. {
  214. array_unshift($this->rows, $row);
  215. }
  216. /**
  217. * Add Row.
  218. *
  219. * @param Row $row
  220. */
  221. protected function addRow(Row $row)
  222. {
  223. $this->rows[] = $row;
  224. }
  225. /**
  226. * Build html of content.
  227. *
  228. * @return string
  229. */
  230. public function build()
  231. {
  232. try {
  233. $html = '';
  234. foreach ($this->rows as $row) {
  235. $html .= $row->render();
  236. }
  237. return $html;
  238. } catch (\Throwable $e) {
  239. return $this->handleException($e);
  240. }
  241. }
  242. /**
  243. * @param \Throwable $e
  244. *
  245. * @return mixed|string
  246. */
  247. protected function handleException(\Throwable $e)
  248. {
  249. $response = Admin::handleException($e);
  250. if (is_string($response) || $response instanceof Renderable) {
  251. $row = new Row($response);
  252. return $row->render();
  253. }
  254. return $response;
  255. }
  256. /**
  257. * Set success message for content.
  258. *
  259. * @param string $title
  260. * @param string $message
  261. *
  262. * @return $this
  263. */
  264. public function withSuccess($title = '', $message = '')
  265. {
  266. admin_success($title, $message);
  267. return $this;
  268. }
  269. /**
  270. * Set error message for content.
  271. *
  272. * @param string $title
  273. * @param string $message
  274. *
  275. * @return $this
  276. */
  277. public function withError($title = '', $message = '')
  278. {
  279. admin_error($title, $message);
  280. return $this;
  281. }
  282. /**
  283. * Set warning message for content.
  284. *
  285. * @param string $title
  286. * @param string $message
  287. *
  288. * @return $this
  289. */
  290. public function withWarning($title = '', $message = '')
  291. {
  292. admin_warning($title, $message);
  293. return $this;
  294. }
  295. /**
  296. * Set info message for content.
  297. *
  298. * @param string $title
  299. * @param string $message
  300. *
  301. * @return $this
  302. */
  303. public function withInfo($title = '', $message = '')
  304. {
  305. admin_info($title, $message);
  306. return $this;
  307. }
  308. /**
  309. * Set content view.
  310. *
  311. * @param null|string $view
  312. *
  313. * @return $this
  314. */
  315. public function view(?string $view)
  316. {
  317. $this->view = $view;
  318. return $this;
  319. }
  320. /**
  321. * @param string|array $key
  322. * @param mixed $value
  323. *
  324. * @return $this
  325. */
  326. public function with($key, $value = null)
  327. {
  328. if (is_array($key)) {
  329. $this->variables = array_merge($this->variables, $key);
  330. } else {
  331. $this->variables[$key] = $value;
  332. }
  333. return $this;
  334. }
  335. /**
  336. * @param string|array $key
  337. * @param mixed $value
  338. *
  339. * @return $this
  340. */
  341. public function withConfig($key, $value = null)
  342. {
  343. if (is_array($key)) {
  344. $this->config = array_merge($this->config, $key);
  345. } else {
  346. $this->config[$key] = $value;
  347. }
  348. return $this;
  349. }
  350. /**
  351. * @return void
  352. */
  353. protected function shareDefaultErrors()
  354. {
  355. if (! session()->all()) {
  356. view()->share(['errors' => new ViewErrorBag()]);
  357. }
  358. }
  359. /**
  360. * @return array
  361. */
  362. protected function variables()
  363. {
  364. return array_merge([
  365. 'header' => $this->title,
  366. 'description' => $this->description,
  367. 'breadcrumb' => $this->breadcrumb,
  368. 'configData' => $this->applyClasses(),
  369. 'pjaxContainerId' => Admin::getPjaxContainerId(),
  370. ], $this->variables);
  371. }
  372. /**
  373. * @return array
  374. */
  375. protected function applyClasses()
  376. {
  377. // default data array
  378. $defaultData = [
  379. 'theme' => '',
  380. 'sidebar_collapsed' => false,
  381. 'sidebar_style' => 'sidebar-light-primary',
  382. 'navbar_color' => '',
  383. 'navbar_class' => 'sticky',
  384. 'footer_type' => '',
  385. 'body_class' => '',
  386. ];
  387. $data = array_merge(
  388. config('admin.layout') ?: [],
  389. $this->config
  390. );
  391. // 1.0 版本兼容 sidebar_dark 参数
  392. if (empty($data['sidebar_style']) && ! empty($data['sidebar_dark'])) {
  393. $data['sidebar_style'] = 'sidebar-dark-white';
  394. }
  395. $allOptions = [
  396. 'theme' => '',
  397. 'footer_type' => '',
  398. 'body_class' => '',
  399. 'sidebar_style' => ['light' => 'sidebar-light-primary', 'primary' => 'sidebar-primary', 'dark' => 'sidebar-dark-white'],
  400. 'sidebar_collapsed' => [],
  401. 'navbar_color' => [],
  402. 'navbar_class' => ['floating' => 'floating-nav', 'sticky' => 'fixed-top', 'hidden' => 'd-none'],
  403. ];
  404. $maps = [
  405. 'footer_type' => 'footer_type',
  406. ];
  407. foreach ($allOptions as $key => $value) {
  408. if (! array_key_exists($key, $defaultData)) {
  409. continue;
  410. }
  411. if (! isset($data[$key])) {
  412. $data[$key] = $defaultData[$key];
  413. continue;
  414. }
  415. if (
  416. isset($maps[$key])
  417. && ! isset($allOptions[$maps[$key]][$data[$key]])
  418. ) {
  419. $data[$key] = $defaultData[$key];
  420. }
  421. if (! is_array($data[$key]) && isset($value[$data[$key]])) {
  422. $data[$key] = $value[$data[$key]];
  423. }
  424. }
  425. if ($data['body_class'] && Str::contains($data['body_class'], 'dark-mode')) {
  426. $data['sidebar_style'] = 'sidebar-dark-white';
  427. }
  428. return [
  429. 'theme' => $data['theme'],
  430. 'sidebar_collapsed' => $data['sidebar_collapsed'],
  431. 'navbar_color' => $data['navbar_color'],
  432. 'navbar_class' => $allOptions['navbar_class'][$data['navbar_class']],
  433. 'sidebar_class' => $data['sidebar_collapsed'] ? 'sidebar-collapse' : '',
  434. 'body_class' => $data['body_class'],
  435. 'sidebar_style' => $data['sidebar_style'],
  436. ];
  437. }
  438. /**
  439. * Render this content.
  440. *
  441. * @return string
  442. */
  443. public function render()
  444. {
  445. $this->callComposing();
  446. $this->shareDefaultErrors();
  447. $this->variables['content'] = $this->build();
  448. $this->callComposed();
  449. return view($this->view, $this->variables())->render();
  450. }
  451. /**
  452. * Register a composed event.
  453. *
  454. * @param callable $callback
  455. * @param bool $once
  456. */
  457. public static function composed(callable $callback, bool $once = false)
  458. {
  459. static::addBuilderListeners('builder.composed', $callback, $once);
  460. }
  461. /**
  462. * Call the composed callbacks.
  463. *
  464. * @param array ...$params
  465. */
  466. protected function callComposed(...$params)
  467. {
  468. $this->fireBuilderEvent('builder.composed', ...$params);
  469. }
  470. }