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+ /** @var Action $action */
13+ /** @var Registry $registry */
14+ /** @var Config $config */
15+ /** @var Event $event */
16+ /** @var Log $log */
17+
18+ /** @var Response $response */
19+
20+ use Spindle \System \Engine \Event ;
21+ use Spindle \System \Engine \Action ;
22+ use Spindle \System \Engine \Config ;
23+ use Spindle \System \Engine \registry ;
24+ use Spindle \system \library \loggers \Log ;
25+ use Spindle \system \library \http \Response ;
26+
27+ $ action = '' ;
28+ $ args = [];
29+
30+ // Action error object to execute if any other actions cannot be executed.
31+ $ error = new Action ($ config ->get ('action_error ' ));
32+
33+ // Pre Actions
34+ foreach ($ config ->get ('action_pre_action ' ) as $ pre_actions ) {
35+ $ pre_action = new Action ($ pre_actions );
36+
37+ $ result = $ pre_action ->execute ($ registry , $ args );
38+
39+ if ($ result instanceof Action) {
40+ $ action = $ result ;
41+ break ;
42+ }
43+
44+ // If action cannot be executed, we return an action error object.
45+ if ($ result instanceof Exception) {
46+ $ action = $ error ;
47+ error_log ("---- " );
48+ error_log ($ result ->getMessage ());
49+ error_log ("PreAction: {$ pre_actions } was not found " );
50+ error_log ("---- " );
51+ // In case there is an error we only want to execute once.
52+ $ error = '' ;
53+ break ;
54+ }
55+ }
56+
57+ // Route
58+ if (isset ($ request ->get ['route ' ])) {
59+ $ route = (string )$ request ->get ['route ' ];
60+ } else {
61+ $ route = (string )$ config ->get ('action_default ' );
62+ }
63+
64+ // To block calls to controller methods we want to keep from being accessed directly
65+ if (str_contains ($ route , '._ ' )) {
66+ $ action = new Action ($ config ->get ('action_error ' ));
67+ }
68+
69+ if ($ action ) {
70+ $ route = $ action ->getId ();
71+ }
72+
73+ // Keep the original trigger
74+ $ trigger = $ route ;
75+
76+ $ args = [];
77+
78+ // Trigger the pre events
79+ $ event ->trigger ('controller/ ' . $ trigger . '/before ' , [&$ route , &$ args ]);
80+
81+ // Action to execute
82+ if (!$ action ) {
83+ $ action = new Action ($ route );
84+ }
85+
86+ // Dispatch
87+ while ($ action ) {
88+ // Execute action
89+ $ output = $ action ->execute ($ registry , $ args );
90+
91+ // Make action a non-object so it's not infinitely looping
92+ $ action = '' ;
93+
94+ // Action object returned then we keep the loop going
95+ if ($ output instanceof Action) {
96+ $ action = $ output ;
97+ }
98+
99+ // If action cannot be executed, we return the action error object.
100+ if ($ output instanceof Exception) {
101+ $ action = $ error ;
102+
103+ // In case there is an error we don't want to infinitely keep calling the action error object.
104+ $ error = '' ;
105+ }
106+ }
107+
108+ // Trigger the post events
109+ $ event ->trigger ('controller/ ' . $ trigger . '/after ' , [&$ route , &$ args , &$ output ]);
110+
111+ // Output
112+ $ response ->output ();
0 commit comments