1 <?php
2
3 /**
4 * App.php
5 *
6 * @author Elvyrra SAS
7 * @license http://rem.mit-license.org/ MIT
8 */
9
10 namespace Hawk;
11
12 /**
13 * This class is used to generate the application singletons. It is loaded at script start, and initialize the following
14 * singletons : conf, errorHandler, logger, fs (FileSystem), session, router, request, response, cache and db
15 *
16 * @package Core
17 */
18 final class App extends Singleton{
19 /**
20 * The application instance
21 */
22 protected static $instance;
23
24
25 /**
26 * Initialize the application
27 */
28 public function init(){
29 // Load the application configuration
30 $this->singleton('conf', Conf::getInstance());
31
32 // Load the application error Handler
33 $this->singleton('errorHandler', ErrorHandler::getInstance());
34
35 // Load the application logger
36 $this->singleton('logger', Logger::getInstance());
37
38 // Load the filesystem library
39 $this->singleton('fs', FileSystem::getInstance());
40
41 // Load the application session
42 $this->singleton('session', Session::getInstance());
43
44 // Load the application router
45 $this->singleton('router', Router::getInstance());
46
47 // Load the application HTTP request
48 $this->singleton('request', Request::getInstance());
49
50 // Load the application HTTP response
51 $this->singleton('response', Response::getInstance());
52
53 // Load the application cache
54 $this->singleton('cache', Cache::getInstance());
55 }
56
57
58 /**
59 * Create an application singleton
60 *
61 * @param string $name The singleton name, that will running by App::{$name}().
62 * For example, if $name = 'db', the singleton will be accessible by App::db();
63 * @param object $instance The singleton instance
64 */
65 public function singleton($name, $instance){
66 $this->$name = $instance;
67 }
68
69 /**
70 * Call a singleton
71 *
72 * @param string $method The method name, corresponding to the singleton name
73 * @param array $arguments The singleton arguments (not used, but mandatory when overriding __callStatic method)
74 */
75 public static function __callStatic($method, $arguments){
76 if(isset(self::$instance->$method)) {
77 return self::$instance->$method;
78 }
79 else{
80 throw new \Exception('The application singleton "' . $method . '" has not been initiated');
81 }
82 }
83 }
84
85 /**
86 * Throw this exception to force the script to finish properly
87 *
88 * @package Exceptions
89 */
90 class AppStopException extends \Exception{
91
92 }
93