summaryrefslogtreecommitdiff
path: root/src/_model.php
blob: ce8dd82afd3824587ebf07c4bf452b4a8a6e9e99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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;
    }

}