Passed
Push — 1.0.0-dev ( ea6d41...cf37bc )
by nguereza
03:00
created

Controller   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
c 0
b 0
f 0
dl 0
loc 114
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setModuleNameFromRouter() 0 4 3
A __construct() 0 30 2
A setCacheFromParamOrConfig() 0 9 4
A setLanguages() 0 9 3
A loadRequiredResources() 0 9 1
A get_instance() 0 2 1
1
<?php
2
    defined('ROOT_PATH') || exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the GNU GPL License (GPL)
9
     *
10
     * Copyright (C) 2017 Tony NGUEREZA
11
     *
12
     * This program is free software; you can redistribute it and/or
13
     * modify it under the terms of the GNU General Public License
14
     * as published by the Free Software Foundation; either version 3
15
     * of the License, or (at your option) any later version.
16
     *
17
     * This program is distributed in the hope that it will be useful,
18
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
     * GNU General Public License for more details.
21
     *
22
     * You should have received a copy of the GNU General Public License
23
     * along with this program; if not, write to the Free Software
24
     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
     */
26
27
    class Controller extends BaseClass {
28
		
29
        /**
30
         * The name of the module if this controller belong to an module
31
         * @var string
32
         */
33
        public $moduleName = null;
34
35
        /**
36
         * The singleton of the super object
37
         * @var Controller
38
         */
39
        private static $instance;
40
41
42
        /**
43
         * Class constructor
44
         */
45
        public function __construct() {
46
            parent::__construct();
47
			
48
            //instance of the super object
49
            self::$instance = & $this;
50
51
            //Load the resources loaded during the application bootstrap
52
            $this->logger->debug('Adding the loaded classes to the super instance');
53
            foreach (class_loaded() as $var => $class) {
54
                $this->$var = & class_loader($class);
55
            }
56
			
57
            //set module using the router
58
            $this->setModuleNameFromRouter();
59
60
            //load the required resources
61
            $this->loadRequiredResources();
62
63
            //set application
64
            $this->setLanguages();
65
			
66
            //set the cache using the configuration
67
            $this->setCacheFromParamOrConfig(null);
68
			
69
            //set application session configuration
70
            $this->logger->debug('Setting PHP application session handler');
71
            set_session_config();
72
73
            //dispatch the loaded instance of super controller event
74
            $this->eventdispatcher->dispatch('SUPER_CONTROLLER_CREATED');
75
        }
76
77
78
        /**
79
         * This is a very useful method it's used to get the super object instance
80
         * @return Controller the super object instance
81
         */
82
        public static function &get_instance(){
83
            return self::$instance;
84
        }
85
86
        /**
87
         * This method is used to set the module name
88
         */
89
        protected function setModuleNameFromRouter() {
90
            //set the module using the router instance
91
            if (isset($this->router) && $this->router->getModule()) {
92
                $this->moduleName = $this->router->getModule();
93
            }
94
        }
95
96
        /**
97
         * Set the cache using the argument otherwise will use the configuration
98
         * @param CacheInterface $cache the implementation of CacheInterface if null will use the configured
99
         */
100
        protected function setCacheFromParamOrConfig(CacheInterface $cache = null) {
101
            $this->logger->debug('Setting the cache handler instance');
102
            //set cache handler instance
103
            if (get_config('cache_enable', false)) {
104
                if ($cache !== null) {
105
                    $this->cache = $cache;
0 ignored issues
show
Bug Best Practice introduced by
The property cache does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
106
                } else if (isset($this->{strtolower(get_config('cache_handler'))})) {
107
                    $this->cache = $this->{strtolower(get_config('cache_handler'))};
108
                    unset($this->{strtolower(get_config('cache_handler'))});
109
                } 
110
            }
111
        }
112
113
114
        /**
115
         * This method is used to load the required resources for framework to work
116
         * @return void 
117
         */
118
        private function loadRequiredResources() {
119
            $this->logger->debug('Loading the required classes into super instance');
120
            $this->eventdispatcher = & class_loader('EventDispatcher', 'classes');
0 ignored issues
show
Bug Best Practice introduced by
The property eventdispatcher does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
121
            $this->loader = & class_loader('Loader', 'classes');
122
            $this->lang = & class_loader('Lang', 'classes');
123
            $this->request = & class_loader('Request', 'classes');
124
            //dispatch the request instance created event
125
            $this->eventdispatcher->dispatch('REQUEST_CREATED');
126
            $this->response = & class_loader('Response', 'classes', 'classes');
127
        }
128
129
        /**
130
         * Set the application supported languages
131
         */
132
        private function setLanguages() {
133
            //add the supported languages ('key', 'display name')
134
            $languages = get_config('languages', null);
135
            if (!empty($languages)) {
136
                foreach ($languages as $key => $displayName) {
137
                    $this->lang->addLang($key, $displayName);
138
                }
139
            }
140
            unset($languages);
141
        }
142
143
    }
144