Skip to content

Commit 7b87675

Browse files
committed
migrate to Composer autoloader
1 parent c5841a8 commit 7b87675

6 files changed

Lines changed: 41 additions & 30 deletions

File tree

.github/workflows/phpstan.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ jobs:
1818
php-version: 7.4
1919

2020
- name: Install Dependencies
21-
run: composer update --ignore-platform-reqs
21+
run: |
22+
# copy common.config.php
23+
cp core/config/common.config.sample.php core/config/common.config.php
24+
# install composer dependencies
25+
composer update --ignore-platform-reqs
2226
2327
- name: Setup PHP 8.2 for PHPStan
2428
uses: shivammathur/setup-php@v2

composer.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,21 @@
1515
"platform": {
1616
"php": "7.4"
1717
}
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"Jeedom\\plugins\\": "plugins/"
22+
},
23+
"files": [
24+
"core/config/common.config.php",
25+
"core/php/utils.inc.php",
26+
"core/config/jeedom.config.php",
27+
"core/config/compatibility.config.php"
28+
],
29+
"classmap": [
30+
"core/class/",
31+
"core/com/",
32+
"core/repo/"
33+
]
1834
}
1935
}

core/class/translate.class.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,3 @@ public static function setLanguage($_langage) {
215215

216216
/* * *********************Methode d'instance************************* */
217217
}
218-
219-
function __($_content, $_name, $_backslash = false) {
220-
return translate::sentence(str_replace("\'", "'", $_content), $_name, $_backslash);
221-
}

core/class/update.class.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ public function doUpdate() {
360360
if (file_exists($cibDir)) {
361361
rrmdir($cibDir);
362362
}
363+
// re generate composer autoloader
364+
$result = shell_exec(system::getCmdSudo() . ' composer dump-autoload --optimize --working-dir $WEBSERVER_HOME');
365+
log::add(__CLASS__, 'debug', "Composer autoloader dump result:\n$result");
363366
} else {
364367
throw new Exception(__("Impossible de décompresser l'archive zip", __FILE__) . ' : ' . $tmp . ' => ' . ZipErrorMessage($res));
365368
}

core/php/core.inc.php

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,6 @@
1717
*/
1818
date_default_timezone_set('Europe/Brussels');
1919
require_once __DIR__ . '/../../vendor/autoload.php';
20-
require_once __DIR__ . '/../config/common.config.php';
21-
require_once __DIR__ . '/../class/DB.class.php';
22-
require_once __DIR__ . '/../class/config.class.php';
23-
require_once __DIR__ . '/../class/jeedom.class.php';
24-
require_once __DIR__ . '/../class/plugin.class.php';
25-
require_once __DIR__ . '/../class/translate.class.php';
26-
require_once __DIR__ . '/utils.inc.php';
27-
include_file('core', 'jeedom', 'config');
28-
include_file('core', 'compatibility', 'config');
29-
include_file('core', 'utils', 'class');
30-
include_file('core', 'log', 'class');
3120

3221
try {
3322
$configs = config::byKeys(array('timezone', 'log::level'));
@@ -46,18 +35,14 @@
4635
} catch (Error $e) {
4736
}
4837

38+
/**
39+
* Autoload function for specific Jeedom classes
40+
* this function is called after the default Composer autoloader
41+
* it will load specific classes such as Cmd, Real, etc.
42+
* for plugins that do not use namespaces or Composer autoloading
43+
*/
4944
function jeedomAutoload($_classname) {
50-
/* core class always in /core/class : */
51-
$path = __DIR__ . "/../../core/class/$_classname.class.php";
52-
if (file_exists($path)) {
53-
include_file('core', $_classname, 'class');
54-
} else if (substr($_classname, 0, 4) === 'com_') {
55-
/* class com_$1 in /core/com/$1.com.php */
56-
include_file('core', substr($_classname, 4), 'com');
57-
} else if (substr($_classname, 0, 5) === 'repo_') {
58-
/* class repo_$1 in /core/repo/$1.repo.php */
59-
include_file('core', substr($_classname, 5), 'repo');
60-
} else if (strpos($_classname, '\\') === false && strpos($_classname, '/') === false) {
45+
if (strpos($_classname, '\\') === false && strpos($_classname, '/') === false) {
6146
/* autoload for plugins : no namespace */
6247
$classname = str_replace(array('Real', 'Cmd'), '', $_classname);
6348
$plugin_active = config::byKey('active', $classname, null);
@@ -69,12 +54,12 @@ function jeedomAutoload($_classname) {
6954
try {
7055
include_file('core', $classname, 'class', $classname);
7156
} catch (Exception $e) {
72-
57+
log::add('jeedom', 'error', 'Error loading class ' . $classname . ': ' . $e->getMessage());
7358
} catch (Error $e) {
74-
59+
log::add('jeedom', 'error', 'Error loading class ' . $classname . ': ' . $e->getMessage());
7560
}
7661
}
7762
}
7863
}
7964

80-
spl_autoload_register('jeedomAutoload', true, true);
65+
spl_autoload_register('jeedomAutoload', false, false);

core/php/utils.inc.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,3 +1839,10 @@ function implode_recursive($_array, $_separator, $_key = '') {
18391839
}
18401840
return $result;
18411841
}
1842+
1843+
/**
1844+
* alias for translate::sentence
1845+
*/
1846+
function __($_content, $_name, $_backslash = false) {
1847+
return translate::sentence(str_replace("\'", "'", $_content), $_name, $_backslash);
1848+
}

0 commit comments

Comments
 (0)