summaryrefslogtreecommitdiff
path: root/src/_model.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/_model.php')
-rw-r--r--src/_model.php63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/_model.php b/src/_model.php
new file mode 100644
index 0000000..ce8dd82
--- /dev/null
+++ b/src/_model.php
@@ -0,0 +1,63 @@
+<?php
+/// CRIMSON --- A simple PHP framework.
+/// Copyright © 2024 Freya Murphy <contact@freyacat.org>
+///
+/// This file is part of CRIMSON.
+///
+/// CRIMSON is free software; you can redistribute it and/or modify it
+/// under the terms of the GNU General Public License as published by
+/// the Free Software Foundation; either version 3 of the License, or (at
+/// your option) any later version.
+///
+/// CRIMSON is distributed in the hope that it will be useful, but
+/// WITHOUT ANY WARRANTY; without even the implied warranty of
+/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+/// GNU General Public License for more details.
+///
+/// You should have received a copy of the GNU General Public License
+/// along with CRIMSON. If not, see <http://www.gnu.org/licenses/>.
+
+abstract class Model extends Base {
+
+ private function flatten_array($arr): array {
+ $fn = fn($e) => is_array($e) ? $e : [$e];
+ return array_merge(...array_map($fn, $arr));
+ }
+
+ protected function get_data(): ?array
+ {
+ /* return barebones base data */
+ $data = array();
+ $data['css'] = array();
+ $data['js'] = array();
+
+ $style = CONFIG['style'];
+ $js = CONFIG['js'];
+
+ $app = NULL;
+ try {
+ // get the class object of the child class, i.e. Blog_model
+ $cls = new ReflectionClass(get_class($this));
+ // the name of the route is the name of the file without .php
+ $path = $cls->getFileName();
+ $app = pathinfo($path, PATHINFO_FILENAME);
+ // sanity check
+ assert(is_string($app));
+ } catch (Exception $_e) {
+ $app = CONTEXT['app'];
+ }
+
+ $data['css'] = $this->flatten_array([
+ $style[''] ?? [],
+ $style[$app] ?? [],
+ ]);
+
+ $data['js'] = $this->flatten_array([
+ $js[''] ?? [],
+ $js[$app] ?? [],
+ ]);
+
+ return $data;
+ }
+
+}