|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Spindle CMS |
| 4 | + * Copyright (c) 2025. All rights reserved. |
| 5 | + * |
| 6 | + * This file is part of the Spindle CMS project — a lightweight, modular PHP content framework derived from OpenCart. |
| 7 | + * |
| 8 | + * @license GNU General Public License v3.0 (GPL-3.0-or-later) |
| 9 | + * @link https://github.com/RandomCoderTinker/Spindle |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Spindle\System\Engine; |
| 13 | + |
| 14 | +use Exception; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class Factory |
| 18 | + */ |
| 19 | +class Factory |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var Registry |
| 23 | + */ |
| 24 | + protected Registry $registry; |
| 25 | + |
| 26 | + /** |
| 27 | + * Constructor |
| 28 | + * |
| 29 | + * @param Registry $registry |
| 30 | + */ |
| 31 | + public function __construct (Registry $registry) |
| 32 | + { |
| 33 | + $this->registry = $registry; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Controller |
| 38 | + * |
| 39 | + * @param string $route |
| 40 | + * |
| 41 | + * @return Controller |
| 42 | + */ |
| 43 | + public function controller (string $route): object |
| 44 | + { |
| 45 | + // Sanitize the route |
| 46 | + $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', $route); |
| 47 | + |
| 48 | + // Build primary application class |
| 49 | + $class = 'Spindle\\' . ucfirst($this->registry->get('config')->get('application')) . '\Controller\\' . str_replace(['_', '/'], ['', '\\'], ucwords($route, '_/')); |
| 50 | + |
| 51 | + // Shared fallback class |
| 52 | + $shared = 'Spindle\\Shared\\Controller\\' . str_replace(['_', '/'], ['', '\\'], ucwords($route, '_/')); |
| 53 | + |
| 54 | + if (class_exists($class)) { |
| 55 | + return new $class($this->registry); |
| 56 | + } |
| 57 | + |
| 58 | + if (class_exists($shared)) { |
| 59 | + return new $shared($this->registry); |
| 60 | + } |
| 61 | + |
| 62 | + return new Action('error/not_found'); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @throws Exception |
| 67 | + */ |
| 68 | + public function model (string $route): object |
| 69 | + { |
| 70 | + // Sanitize the route |
| 71 | + $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', $route); |
| 72 | + |
| 73 | + // Build primary application class |
| 74 | + $class = 'Spindle\\' . ucfirst($this->registry->get('config')->get('application')) . '\Model\\' . str_replace(['_', '/'], ['', '\\'], ucwords($route, '_/')); |
| 75 | + |
| 76 | + // Shared fallback class |
| 77 | + $shared = 'Spindle\\Shared\\Model\\' . str_replace(['_', '/'], ['', '\\'], ucwords($route, '_/')); |
| 78 | + |
| 79 | + error_log("----------------------"); |
| 80 | + error_log($class); |
| 81 | + error_log("----------------------"); |
| 82 | + |
| 83 | + if (class_exists($class)) { |
| 84 | + return new $class($this->registry); |
| 85 | + } |
| 86 | + |
| 87 | + if (class_exists($shared)) { |
| 88 | + return new $shared($this->registry); |
| 89 | + } |
| 90 | + |
| 91 | + throw new Exception("Error: Could not load model '$route'!"); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Library |
| 96 | + * |
| 97 | + * @param string $route |
| 98 | + * @param array<mixed> $args |
| 99 | + * |
| 100 | + * @return object |
| 101 | + * @throws Exception |
| 102 | + */ |
| 103 | + public function library (string $route, array $args): object |
| 104 | + { |
| 105 | + // Sanitize the call |
| 106 | + $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', $route); |
| 107 | + |
| 108 | + // Generate the class |
| 109 | + $class = 'Spindle\System\Library\\' . str_replace(['_', '/'], ['', '\\'], ucwords($route, '_/')); |
| 110 | + |
| 111 | + // Check if the requested model is already stored in the registry. |
| 112 | + if (class_exists($class)) { |
| 113 | + return new $class(...$args); |
| 114 | + } else { |
| 115 | + throw new Exception('Error: Could not load library ' . $route . '!'); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments