1+ <?php
2+
3+ namespace Monster \App ;
4+
5+ /*
6+ |--------------------------------------------------------------------------
7+ | API-Monster Framework
8+ |--------------------------------------------------------------------------
9+ |
10+ | API-Monster is a fast, safe, and easy-to-use PHP framework designed for
11+ | building API applications. It provides a wide range of features and
12+ | components to streamline the development process and enhance
13+ | productivity.
14+ |
15+ | Features:
16+ | - Fast: API-Monster is optimized for performance, allowing you to build
17+ | high-performance API applications.
18+ | - Safe: The framework prioritizes security and provides built-in mechanisms
19+ | for handling common security concerns.
20+ | - Easy: API-Monster follows a user-friendly approach, making it easy for
21+ | developers to understand and work with the framework.
22+ |
23+ | Key Components:
24+ | - Routing: API-Monster supports routing similar to Laravel, allowing you to
25+ | define routes and map them to corresponding controller actions.
26+ | - MySQL Class: The framework includes a MySQL class for easy interaction
27+ | with MySQL databases.
28+ | - HTTP Class: API-Monster provides an HTTP class for handling HTTP requests
29+ | and responses, simplifying the communication with external APIs.
30+ | - Cipher Class: The Cipher class offers encoding and decoding functionality,
31+ | allowing you to securely handle sensitive data.
32+ | - Controllers: API-Monster supports controllers, enabling you to organize
33+ | your application's logic into modular and reusable components.
34+ | - Object-Oriented Syntax: The framework utilizes object-oriented programming
35+ | (OOP) syntax, promoting clean and maintainable code.
36+ |
37+ | Getting Started:
38+ | To create a new API-Monster project, you can use Composer by running the
39+ | following command:
40+ | composer create-project darkphp/apimonster myapp
41+ |
42+ | GitHub Repository:
43+ | For more information and to explore the framework's source code, you can
44+ | visit the API-Monster GitHub repository at:
45+ | https://github.com/ReactMVC/API-Monster
46+ |
47+ | Developer Information:
48+ | API-Monster is developed by Hossein Pira. If you have any questions,
49+ | suggestions, or feedback, you can reach out to Hossein via email at:
50+ | - h3dev.pira@gmail.com
51+ | - hosseinpiradev@gmail.com
52+ | Alternatively, you can contact Hossein on Telegram at @h3dev.
53+ |
54+ */
55+
56+ class Core
57+ {
58+ // The HTTP method associated with this route item
59+ private $ method ;
60+
61+ // The path associated with this route item
62+ private $ path ;
63+
64+ // The controller method to execute when this route item is matched
65+ private $ controller ;
66+
67+ // The parameters extracted from the request URI when this route item is matched
68+ private $ params ;
69+
70+ // The regular expression used to match the request URI against the path of this route item
71+ private $ pathRegex ;
72+
73+ public function __construct ($ method , $ path , $ controller )
74+ {
75+ $ this ->method = $ method ;
76+ $ this ->path = $ path ;
77+ $ this ->controller = $ controller ;
78+
79+ // Cache the regular expression created by preg_replace
80+ if (!isset ($ this ->pathRegex [$ path ])) {
81+ $ path_regex = preg_replace ('/\{([a-zA-Z0-9_]+)\}/ ' , '([a-zA-Z0-9_]+) ' , $ path );
82+ $ path_regex = str_replace ('/ ' , '\/ ' , $ path_regex );
83+ $ path_regex .= '( \\?.*)?$ ' ; // Include an optional query parameter at the end of the path
84+ $ this ->pathRegex [$ path ] = '/^ ' . $ path_regex . '$/ ' ;
85+ }
86+ }
87+
88+ // Getters and setters for the private properties
89+
90+ public function getMethod ()
91+ {
92+ return $ this ->method ;
93+ }
94+
95+ public function setMethod ($ method )
96+ {
97+ $ this ->method = $ method ;
98+ }
99+
100+ public function getPath ()
101+ {
102+ return $ this ->path ;
103+ }
104+
105+ public function setPath ($ path )
106+ {
107+ $ this ->path = $ path ;
108+ }
109+
110+ public function getController ()
111+ {
112+ return $ this ->controller ;
113+ }
114+
115+ public function setController ($ controller )
116+ {
117+ $ this ->controller = $ controller ;
118+ }
119+
120+ public function getParams ()
121+ {
122+ return $ this ->params ;
123+ }
124+
125+ public function setParams ($ params )
126+ {
127+ $ this ->params = $ params ;
128+ }
129+
130+ // Match the route item against the request method and URI
131+
132+ public function match ($ request_method , $ request_uri )
133+ {
134+ $ request_uri = rtrim ($ request_uri , '/ ' );
135+ if ($ this ->method === $ request_method && preg_match ($ this ->pathRegex [$ this ->path ], $ request_uri , $ matches )) {
136+ $ this ->params = array_slice ($ matches , 1 );
137+ return true ;
138+ }
139+ return false ;
140+ }
141+
142+ // Execute the controller method associated with this route item
143+
144+ public function execute ()
145+ {
146+ list ($ controller , $ method ) = explode ('@ ' , $ this ->controller );
147+ $ controllerClass = "Monster \\App \\Controllers \\" . $ controller ;
148+
149+ // Cache the value of class_exists and call_user_func_array
150+ static $ classExists = [];
151+ static $ callUserFuncArray = [];
152+
153+ if (!isset ($ classExists [$ controllerClass ])) {
154+ $ classExists [$ controllerClass ] = class_exists ($ controllerClass );
155+ }
156+
157+ if ($ classExists [$ controllerClass ]) {
158+ if (!isset ($ callUserFuncArray [$ controllerClass ])) {
159+ $ callUserFuncArray [$ controllerClass ] = function ($ controllerInstance , $ method , $ params ) {
160+ if ($ params ) {
161+ $ controllerInstance ->$ method (...$ params );
162+ } else {
163+ $ controllerInstance ->$ method ();
164+ }
165+ };
166+ }
167+ $ controllerInstance = new $ controllerClass ;
168+ $ callUserFuncArray [$ controllerClass ]($ controllerInstance , $ method , $ this ->params );
169+ } else {
170+ http_response_code (500 );
171+ echo "Internal Server Error: Controller class not found " ;
172+ }
173+ }
174+ }
0 commit comments