Completed
Push — work-fleets ( 070fe2...d6c2ef )
by SuperNova.WS
05:43
created

sn_module::loadModuleRootConfig()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 8
nc 2
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.35# copyright © 2009-2014 Gorlum',
9
10
//    'require' => null,
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
11
    'root_relative' => '',
12
13
    'installed' => true,
14
    'active' => true,
15
16
    // 'constants' array - contents of this array would be instaled into engine
17
    'constants' => array(
18
//      'UNIT_STRUCTURE_NEW' => 999999,
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
19
    ),
20
21
    '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
22
23
    // 'functions' array - this functions would be installed as hooks
24
    // Key: overwritable function name to replace
25
    // Value: which method to use. Format: [*][<object_name>][.]<method>
26
    // '*' means that new function would replace old one
27
    // If object_name is ommited but "." is present - hook linked to global function
28
    // If only "method" present - overwritable linked to appropriate method of current object
29
    // Function/Method should be accessible on module init
30
    'functions' => array(
31
//      'test_object_test_method' => 'test_object.test_method',
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
32
//      'test_function' => '.my_test_function',
33
//      'this_object_test_method' => 'test_method',
34
    ),
35
36
    // 'menu' array - this menu items would be merged into main game menu
37
    // Array element almost identical to $sn_menu with additional param 'LOCATION'.
38
    // '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...
39
    // [-|+][<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...
40
    // <menu_item_id> identifies menu item aginst new menu item would be placed. When ommited new item placed against whole menu
41
    // -/+ 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
42
    // Empty or non-existent LOCATION equivalent to '+' - place item at end of menu
43
    // Non-existent menu_item_id treated as ommited
44
    'menu' => array(),
45
46
    // 'page' array - defines pages which will handle this module and appropriate handlers
47
    'page' => array(),
48
  );
49
50
  protected $config = array();
51
52
  protected $module_full_class_path = __FILE__;
53
54
  /**
55
   * Динамическое назначение переменных
56
   *
57
   * Актуально, когда записываемые данные зависят от статуса игры
58
   * Например - назначаются константы внутри модуля
59
   *
60
   * @return array
61
   */
62
  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...
63
    return array(
64
/*
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...
65
      'sn_data' => array(
66
        UNIT_STRUCTURE_NEW => array( // Will honor new constants
67
          'name' => 'robot_factory',
68
          'type' => UNIT_STRUCTURE,
69
          'location' => LOC_PLANET,
70
          'cost' => array(
71
            RES_METAL     => 400,
72
            RES_CRYSTAL   => 120,
73
            RES_DEUTERIUM => 200,
74
            RES_ENERGY    => 0,
75
            'factor' => 2,
76
          ),
77
          'metal' => 400,
78
          'crystal' => 120,
79
          'deuterium' => 200,
80
          'energy' => 0,
81
          'factor' => 2,
82
        ),
83
      ),
84
*/
85
    );
86
  }
87
88
  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...
89
    static $i;
90
91
    require SN_ROOT_PHYSICAL . 'config.php';
92
93
    $module_config_array = get_class($this) . '_config';
94
    if(!empty($$module_config_array) && is_array($$module_config_array)) {
95
      $this->config = $$module_config_array;
96
      return true;
97
    }
98
99
    return false;
100
  }
101
102
  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...
103
    global $sn_module;
104
105
    // Getting module PHP class name
106
    $class_module_name = get_class($this);
107
108
    // Getting module root relative to SN
109
    $this->manifest['root_relative'] = str_replace(array(SN_ROOT_PHYSICAL, basename($filename)), '', str_replace('\\', '/', $filename));
110
111
    // TODO: Load configuration from DB. Manifest setting
112
    // Trying to load configuration from file
113
    if(!$config_exists = $this->loadModuleRootConfig()) {
114
      // Конфигурация может лежать в config_path в манифеста или в корне модуля
115
      if(isset($this->manifest['config_path']) && file_exists($config_filename = $this->manifest['config_path'] . '/config.php')) {
116
        $config_exists = true;
117
      } elseif(file_exists($config_filename = dirname($filename) . '/config.php')) {
118
        $config_exists = true;
119
      }
120
121
      if($config_exists) {
122
        include($config_filename);
123
        $module_config_array = $class_module_name . '_config';
124
        $this->config = $$module_config_array;
125
      }
126
    }
127
128
    // Registering module
129
    $sn_module[$class_module_name] = $this;
130
  }
131
132
  protected function __patch_menu(&$sn_menu_extra, &$menu_patch) {
133
    if(isset($menu_patch) && is_array($menu_patch) && !empty($menu_patch)) {
134
      foreach($menu_patch as $menu_item_name => $menu_item_data) {
135
        $sn_menu_extra[$menu_item_name] = $menu_item_data;
136
      }
137
    }
138
  }
139
140
141
  public function initialize() {
142
    // Checking module status - is it installed and active
143
    $this->check_status();
144
    if(!$this->manifest['active']) {
145
      return;
146
    }
147
148
    // Setting constants - if any
149
    if(isset($this->manifest['constants']) && is_array($this->manifest['constants']) && !empty($this->manifest['constants'])) {
150
      foreach($this->manifest['constants'] as $constant_name => $constant_value) {
151
        defined($constant_name) or define($constant_name, $constant_value);
152
      }
153
    }
154
155
    // Adding vars - if any
156
    // Due to possible introduce of new constants in previous step vars is assigned via special method to honor new constants
157
    // Assignation can work with simple variables and with multidimensional arrays - for ex. 'sn_data[groups][test]'
158
    // New values from module variables will overwrite previous values (for root variables) and array elements with corresponding indexes (for arrays)
159
    // Constants as array indexes are honored - it's make valid such declarations as 'sn_data[ques][QUE_STRUCTURES]'
160
    $this->manifest['vars'] = $this->__assign_vars();
161
    if(!empty($this->manifest['vars'])) {
162
      $vars_assigned = array();
163
      foreach($this->manifest['vars'] as $var_name => $var_value) {
164
        $sub_vars = explode('[', str_replace(']', '', $var_name));
165
        $var_name = $sub_vars[0];
166
167
        if(!isset($vars_assigned[$var_name])) {
168
          $vars_assigned[$var_name] = true;
169
          global $$var_name;
170
        }
171
172
        $pointer = &$$var_name;
173
        if(($n = count($sub_vars)) > 1) {
174
          for($i = 1; $i < $n; $i++) {
175
            if(defined($sub_vars[$i])) {
176
              $sub_vars[$i] = constant($sub_vars[$i]);
177
            }
178
179
            if(!isset($pointer[$sub_vars[$i]]) && $i != $n) {
180
              $pointer[$sub_vars[$i]] = array();
181
            }
182
            $pointer = &$pointer[$sub_vars[$i]];
183
          }
184
        }
185
186
        if(!isset($pointer) || !is_array($pointer)) {
187
          $pointer = $var_value;
188
        } elseif(is_array($$var_name)) {
189
          $pointer = array_merge_recursive_numeric($pointer, $var_value);
190
        }
191
      }
192
    }
193
194
    // Overriding function if any
195
    global $functions;
196
    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...
197
198
    // Patching game menu - if any
199
    global $sn_menu_extra, $sn_menu_admin_extra;
200
    isset($this->manifest['menu']) and $this->__patch_menu($sn_menu_extra, $this->manifest['menu']);
201
    isset($this->manifest['menu_admin']) and $this->__patch_menu($sn_menu_admin_extra, $this->manifest['menu_admin']);
202
203
    global $sn_mvc;
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
    if(isset($this->manifest['i18n']) && is_array($this->manifest['i18n']) && !empty($this->manifest['i18n'])) {
209
      foreach($this->manifest['i18n'] as $i18n_page_name => &$i18n_file_list) {
210
        foreach($i18n_file_list as &$i18n_file_data) {
211
          if(is_array($i18n_file_data) && !$i18n_file_data['path']) {
212
            $i18n_file_data['path'] = $this->manifest['root_relative'];
213
          }
214
        }
215
        if(!isset($sn_mvc['i18n'][$i18n_page_name])) {
216
          $sn_mvc['i18n'][$i18n_page_name] = array();
217
        }
218
        $sn_mvc['i18n'][$i18n_page_name] += $i18n_file_list;
219
      }
220
    }
221
222 View Code Duplication
    if(!empty($this->manifest['javascript']) && is_array($this->manifest['javascript'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
      foreach($this->manifest['javascript'] as $javascript_page_name => &$javascript_list) {
224
        !isset($sn_mvc['javascript'][$javascript_page_name]) ? $sn_mvc['javascript'][$javascript_page_name] = array() : false;
225
        foreach($javascript_list as $script_name => &$script_content) {
226
          $sn_mvc['javascript'][$javascript_page_name][$script_name] = $script_content;
227
        }
228
      }
229
    }
230
231 View Code Duplication
    if(!empty($this->manifest['css']) && is_array($this->manifest['css'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
      foreach($this->manifest['css'] as $javascript_page_name => &$javascript_list) {
233
        !isset($sn_mvc['css'][$javascript_page_name]) ? $sn_mvc['css'][$javascript_page_name] = array() : false;
234
        foreach($javascript_list as $script_name => &$script_content) {
235
          $sn_mvc['css'][$javascript_page_name][$script_name] = $script_content;
236
        }
237
      }
238
    }
239
240
    if(!empty($this->manifest['navbar_prefix_button']) && is_array($this->manifest['navbar_prefix_button'])) {
241
      foreach($this->manifest['navbar_prefix_button'] as $button_image => $button_url_relative) {
242
        $sn_mvc['navbar_prefix_button'][$button_image] = $button_url_relative;
243
      }
244
    }
245
  }
246
247
  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...
248
  }
249
}
250
251