1 <?php
2
3 use Alo\Cache\AbstractCache;
4 use Alo\Controller\AbstractController;
5 use Alo\Controller\Router;
6 use Alo\Db\AbstractDb;
7 use Alo\Session\AbstractSession;
8 use Alo\SFTP;
9 use Alo\Profiler;
10 use Alo\File;
11 use Alo\Email;
12 use Alo\cURL;
13 use Alo\Cron;
14 use Alo\Validators\Form;
15 use Alo\Test\AbstractTester;
16
17 if (!defined('GEN_START')) {
18 http_response_code(404);
19 die();
20 }
21
22 /**
23 * The global framework class
24 *
25 * @author Art <a.molcanovas@gmail.com>
26 */
27 class Alo {
28
29 /**
30 * Defines a session type as SQL
31 *
32 * @var string
33 */
34 const SESS_MYSQL = 'SQLSession';
35
36 /**
37 * Defines a session type as Memcached
38 *
39 * @var string
40 */
41 const SESS_MEMCACHED = 'MemcachedSession';
42
43 /**
44 * SFTP connection manager
45 *
46 * @var SFTP
47 */
48 static $sftp;
49
50 /**
51 * Code profiler
52 *
53 * @var Profiler
54 */
55 static $profiler;
56
57 /**
58 * File manager
59 *
60 * @var File
61 */
62 static $file;
63
64 /**
65 * Email manager
66 *
67 * @var Email
68 */
69 static $email;
70
71 /**
72 * Object-oriented cURL wrapper
73 *
74 * @var cURL
75 */
76 static $curl;
77
78 /**
79 * Crontab manager
80 *
81 * @var Cron
82 */
83 static $cron;
84
85 /**
86 * HTML form validator
87 *
88 * @var Form
89 */
90 static $form_validator;
91
92 /**
93 * Code tester
94 *
95 * @var AbstractTester
96 */
97 static $tester;
98
99 /**
100 * Database connection
101 *
102 * @var AbstractDb
103 */
104 static $db;
105
106 /**
107 * Cache instance
108 *
109 * @var AbstractCache
110 */
111 static $cache;
112
113 /**
114 * The session handler
115 *
116 * @var AbstractSession
117 */
118 static $session;
119
120 /**
121 * The loaded controller
122 *
123 * @var AbstractController
124 */
125 static $controller;
126
127 /**
128 * The routing class
129 *
130 * @var Router
131 */
132 static $router;
133
134 /**
135 * Loads a session
136 *
137 * @param string $type The session class name - see Alo::SESS_* constants
138 * @return AbstractSession
139 * @see self::SESS_MYSQL
140 * @see self::SESS_MEMCACHED
141 */
142 static function loadSession($type = self::SESS_MYSQL) {
143 if (!self::$session) {
144 $sess = '\Alo\Session\\' . $type;
145 self::$session = new $sess();
146 }
147
148 return self::$session;
149 }
150
151 /**
152 * Loads a configuration file based on environment: from DIR_SYS/config during setup & DIR_APP/config during
153 * production/development
154 *
155 * @author Art <a.molcanovas@gmail.com>
156 * @param string $path The config file relative path without the file extension, e.g. to load a file found
157 * in config/db/mysql.php provide db/mysql
158 * @param bool $return_path If set to true it will return the calculated path instead of requiring the file
159 * @return string|bool The path is $return_path is true, TRUE if it is false
160 */
161 static function loadConfig($path, $return_path = false) {
162 $dir = (defined('ENVIRONMENT') && ENVIRONMENT === ENV_SETUP ? DIR_SYS : DIR_APP) . 'config' . DIRECTORY_SEPARATOR;
163 $path = strtolower($path);
164 if (substr($path, -4) == '.php') {
165 $path = substr($path, 0, -4);
166 }
167
168 $final_path = '';
169 if (file_exists($dir . $path . '.php')) {
170 $final_path = $dir . $path . '.php';
171 } else {
172 trigger_error('Configuration file ' . $path . ' not found in the application folder. Attempting to load from sys.', E_USER_WARNING);
173 $final_path = DIR_SYS . 'config' . DIRECTORY_SEPARATOR . $path . '.php';
174 }
175
176 if ($return_path) {
177 return $final_path;
178 } else {
179 include_once $final_path;
180
181 return true;
182 }
183 }
184 }
185
186 \Log::debug('Alo framework class initialised');