Completed
Push — work-fleets ( d6c2ef...5197c7 )
by SuperNova.WS
04:51
created

sn_module::check_status()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
class sn_module {
4
  public $manifest = array(
5
    'package'   => 'core',
6
    'name'      => 'sn_module',
7
    'version'   => '1c0',
8
    'copyright' => 'Project "SuperNova.WS" #41a6.37# copyright © 2009-2014 Gorlum',
9
10
    'require'       => array(),
11
    'root_relative' => '',
12
13
    'installed' => true,
14
    'active'    => true,
15
16
    // 'constants' array - contents of this array would be instaled into engine. 'UNIT_STRUCTURE_NEW' => 999999,
17
    'constants' => array(),
18
19
    'vars'      => array(), // Just a placeholder. vars assigned via special method __assign_vars(). Need 'cause of new constants that can be defined within module. See below
20
21
    /**
22
     * 'functions' array - this functions would be installed as hooks
23
     * Key: overwritable function name to replace
24
     * Value: which method to use. Format: [*][<object_name>][.]<method>
25
     * '*' means that new function would replace old one
26
     * If object_name is ommited but "." is present - hook linked to global function
27
     * If only "method" present - overwritable linked to appropriate method of current object
28
     * Examples:
29
     * 'test_object.test_method' - will
30
     * Function/Method should be accessible on module init
31
     * //      'test_object_test_method' => 'test_object.test_method',
32
     * //      'test_function' => '.my_test_function',
33
     * //      'this_object_test_method' => 'test_method',
34
     */
35
    'functions' => array(),
36
37
    // 'menu' array - this menu items would be merged into main game menu
38
    // Array element almost identical to $sn_menu with additional param 'LOCATION'.
39
    // 'LOCATION' => '-news', // Special atrtribute for modules
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40
    // [-|+][<menu_item_id>]
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
    // <menu_item_id> identifies menu item aginst new menu item would be placed. When ommited new item placed against whole menu
42
    // -/+ indicates that new item should be placed before/after identified menu item (or whole menu). If ommited and menu item exists - new item will replace previous one
43
    // Empty or non-existent LOCATION equivalent to '+' - place item at end of menu
44
    // Non-existent menu_item_id treated as ommited
45
    'menu'      => array(),
46
47
    // 'page' array - defines pages which will handle this module and appropriate handlers
48
    'page'      => array(),
49
  );
50
51
  protected $config = array();
52
53
  protected $module_full_class_path = __FILE__;
54
55
  /**
56
   * Динамическое назначение переменных
57
   *
58
   * Актуально, когда записываемые данные зависят от статуса игры
59
   * Например - назначаются константы внутри модуля
60
   *
61
   * @return array
62
   */
63
  function __assign_vars() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
64
    return array();
65
  }
66
67
  function loadModuleRootConfig() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
68
    require SN_ROOT_PHYSICAL . 'config.php';
69
70
    $module_config_array = get_class($this) . '_config';
71
    if(!empty($$module_config_array) && is_array($$module_config_array)) {
72
      $this->config = $$module_config_array;
73
74
      return true;
75
    }
76
77
    return false;
78
  }
79
80
  function __construct($filename = __FILE__) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
81
    global $sn_module;
82
83
    // Getting module PHP class name
84
    $class_module_name = get_class($this);
85
86
    // Getting module root relative to SN
87
    $this->manifest['root_relative'] = str_replace(array(SN_ROOT_PHYSICAL, basename($filename)), '', str_replace('\\', '/', $filename));
88
89
    // TODO: Load configuration from DB. Manifest setting
90
    // Trying to load configuration from file
91
    if(!$config_exists = $this->loadModuleRootConfig()) {
92
      // Конфигурация может лежать в config_path в манифеста или в корне модуля
93
      if(isset($this->manifest['config_path']) && file_exists($config_filename = $this->manifest['config_path'] . '/config.php')) {
94
        $config_exists = true;
95
      } elseif(file_exists($config_filename = dirname($filename) . '/config.php')) {
96
        $config_exists = true;
97
      }
98
99
      if($config_exists) {
100
        include($config_filename);
101
        $module_config_array = $class_module_name . '_config';
102
        $this->config = $$module_config_array;
103
      }
104
    }
105
106
    // Registering module
107
    $sn_module[$class_module_name] = $this;
108
  }
109
110
111
  public function initialize() {
112
    global $sn_menu_extra, $sn_menu_admin_extra;
113
114
    // Checking module status - is it installed and active
115
    $this->check_status();
116
    if(!$this->manifest['active']) {
117
      return;
118
    }
119
120
    $this->setSystemConstants();
121
    $this->setSystemVariables();
122
    $this->addSystemHandlers();
123
    $this->mergeI18N();
124
125
    // Patching game menu - if any
126
    isset($this->manifest['menu']) && $this->mergeMenu($sn_menu_extra, $this->manifest['menu']);
127
    isset($this->manifest['menu_admin']) && $this->mergeMenu($sn_menu_admin_extra, $this->manifest['menu_admin']);
128
129
    $this->mergeJavascript();
130
    $this->mergeCss();
131
    $this->mergeNavbarButton();
132
  }
133
134
  protected function setSystemConstants() {
135
    // Setting constants - if any
136
    if(empty($this->manifest['constants']) || !is_array($this->manifest['constants'])) {
137
      return;
138
    }
139
140
    foreach($this->manifest['constants'] as $constant_name => $constant_value) {
141
      defined($constant_name) || define($constant_name, $constant_value);
142
    }
143
  }
144
145
  protected function setSystemVariables() {
146
    // Adding vars - if any
147
    // Due to possible introduce of new constants in previous step vars is assigned via special method to honor new constants
148
    // Assignation can work with simple variables and with multidimensional arrays - for ex. 'sn_data[groups][test]'
149
    // New values from module variables will overwrite previous values (for root variables) and array elements with corresponding indexes (for arrays)
150
    // Constants as array indexes are honored - it's make valid such declarations as 'sn_data[ques][QUE_STRUCTURES]'
151
    $this->manifest['vars'] = $this->__assign_vars();
152
    if(empty($this->manifest['vars']) || !is_array($this->manifest['vars'])) {
153
      return;
154
    }
155
156
    $vars_assigned = array();
157
    foreach($this->manifest['vars'] as $var_name => $var_value) {
158
      $sub_vars = explode('[', str_replace(']', '', $var_name));
159
      $var_name = $sub_vars[0];
160
161
      if(!isset($vars_assigned[$var_name])) {
162
        $vars_assigned[$var_name] = true;
163
        global $$var_name;
164
      }
165
166
      $pointer = &$$var_name;
167
      if(($n = count($sub_vars)) > 1) {
168
        for($i = 1; $i < $n; $i++) {
169
          if(defined($sub_vars[$i])) {
170
            $sub_vars[$i] = constant($sub_vars[$i]);
171
          }
172
173
          if(!isset($pointer[$sub_vars[$i]]) && $i != $n) {
174
            $pointer[$sub_vars[$i]] = array();
175
          }
176
          $pointer = &$pointer[$sub_vars[$i]];
177
        }
178
      }
179
180
      if(!isset($pointer) || !is_array($pointer)) {
181
        $pointer = $var_value;
182
      } elseif(is_array($$var_name)) {
183
        $pointer = array_merge_recursive_numeric($pointer, $var_value);
184
      }
185
    }
186
  }
187
188
  protected function mergeMenu(&$sn_menu_extra, &$menu_patch) {
189
    if(!is_array($menu_patch)) {
190
      return;
191
    }
192
193
    foreach($menu_patch as $menu_item_name => $menu_item_data) {
194
      $sn_menu_extra[$menu_item_name] = $menu_item_data;
195
    }
196
  }
197
198
  protected function addSystemHandlers() {
199
    global $sn_mvc, $functions;
200
201
    // Overriding function if any
202
    sn_sys_handler_add($functions, $this->manifest['functions'], $this);
0 ignored issues
show
Documentation introduced by
$this is of type this<sn_module>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
203
204
    foreach($sn_mvc as $handler_type => &$handler_data) {
205
      sn_sys_handler_add($handler_data, $this->manifest['mvc'][$handler_type], $this, $handler_type);
0 ignored issues
show
Documentation introduced by
$this is of type this<sn_module>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
206
    }
207
  }
208
209
  protected function mergeNavbarButton() {
210
    global $sn_mvc;
211
212
    if(empty($this->manifest['navbar_prefix_button']) || !is_array($this->manifest['navbar_prefix_button'])) {
213
      return;
214
    }
215
216
    foreach($this->manifest['navbar_prefix_button'] as $button_image => $button_url_relative) {
217
      $sn_mvc['navbar_prefix_button'][$button_image] = $button_url_relative;
218
    }
219
  }
220
221
  protected function mergeI18N() {
222
    global $sn_mvc;
223
224
    $arrayName = 'i18n';
225
    if(empty($this->manifest[$arrayName]) || !is_array($this->manifest[$arrayName])) {
226
      return;
227
    }
228
229
    foreach($this->manifest[$arrayName] as $pageName => &$contentList) {
230
      foreach($contentList as &$i18n_file_data) {
231
        if(is_array($i18n_file_data) && !$i18n_file_data['path']) {
232
          $i18n_file_data['path'] = $this->manifest['root_relative'];
233
        }
234
      }
235
      if(!isset($sn_mvc[$arrayName][$pageName])) {
236
        $sn_mvc[$arrayName][$pageName] = array();
237
      }
238
      $sn_mvc[$arrayName][$pageName] += $contentList;
239
    }
240
  }
241
242
  protected function mergeArraySpecial($arrayName) {
243
    global $sn_mvc;
244
245
    if(empty($this->manifest[$arrayName]) || !is_array($this->manifest[$arrayName])) {
246
      return;
247
    }
248
249
    foreach($this->manifest[$arrayName] as $pageName => &$contentList) {
250
      !isset($sn_mvc[$arrayName][$pageName]) ? $sn_mvc[$arrayName][$pageName] = array() : false;
251
      foreach($contentList as $contentName => &$content) {
252
        $sn_mvc[$arrayName][$pageName][$contentName] = $content;
253
      }
254
    }
255
  }
256
257
  protected function mergeCss() { $this->mergeArraySpecial('css'); }
258
259
  protected function mergeJavascript() { $this->mergeArraySpecial('javascript'); }
260
261
  function check_status() { }
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
262
263
}
264