Skip to content

Refactor code & implement autoloader#2247

Merged
Novik merged 8 commits intoNovik:masterfrom
stickz:Utility2
Jan 9, 2022
Merged

Refactor code & implement autoloader#2247
Novik merged 8 commits intoNovik:masterfrom
stickz:Utility2

Conversation

@stickz
Copy link
Copy Markdown
Collaborator

@stickz stickz commented Jan 2, 2022

This pull request refactors the ruTorrent utility code to reduce script execution times and improve overall code readability. It moves all of the functions in php/util.php out of the global context and into classes. When the utility file is included, only the required functionality is added is each time. This prevents loading of unnecessary functionality for PHP scripts.

It implements a very basic auto loader function to simplify the including process of utility classes. The only file which needs to be included is php/util.php. When a utility class is used, PHP will automatically go to the /php/utility folder to include the file.

spl_autoload_register(function ($class) {
    require_once 'utility/'. strtolower($class). '.php';
});

Utility class names can have any mixture of lower or upper case letters. However, the files in the utility folder:

  1. Must contain only 1 class per file.
  2. Must end with the file extension .php.
  3. Must have the same file name as the class inside of it.
  4. Must have a file name in lower case letters only.

Other Notes:

  1. The default value of profilePath variable had to be changed from ../share to ../../share. The reason is because it accessed from fileutil.php in /php/utility now instead of util.php in /php. The folder context is different to reach the root folder.

  2. In the sqlite_exists() function in /plugins/geoip/sqlite.php the second argument class_exists had to be changed from true to false. It may be required to update the spl_autoload_register() function, if SQLite3 does not automatically load.

@stickz
Copy link
Copy Markdown
Collaborator Author

stickz commented Jan 3, 2022

I added some instance caching of results for a few static methods as well. I found out preg_replace was being called on $_SERVER['REMOTE_USER'] about a hundred times during the plugin initialization phase.

@Novik
Copy link
Copy Markdown
Owner

Novik commented Jan 3, 2022

Thanks, very massive pull request, I will check it later.

spl_autoload_register(function ($class) {
    require_once 'utility/'. strtolower($class). '.php';
});
  1. I can see 'spl_autoload_register' in the list of changes. Maybe you forget to include some files?
  2. This will not correctly work in such form. We need absolute path here, i.e. require_once __DIR__ . ...
  3. Maybe, you will think about usage of composer instead?

@stickz
Copy link
Copy Markdown
Collaborator Author

stickz commented Jan 3, 2022

spl_autoload_register is there. It's buried behind 760 lines of removed code in util.php. Everything worked when I tested this.

@Novik
Copy link
Copy Markdown
Owner

Novik commented Jan 4, 2022

In the current implementation we may have several user's profiles for the single ruTorrent instllation.
And every user may have his own config.php file with a his own preferences (include profile path, proxy settings and so on).
How this feature will work after your changes?
Will class constants and variables be overridden correctly?

And even if yes, then now we have strange situation, when configuration partially inside global variables and partially in the class declarations. This is not good thing IMHO.
In the ideal situation we should to have only one global variable with something like this - https://github.com/hassankhan/config

@stickz
Copy link
Copy Markdown
Collaborator Author

stickz commented Jan 5, 2022

There is a problem with user specific config.php files. I will fix this issue in a couple of days with anther commit.

Comment thread php/utility/fileutil.php Outdated
@stickz
Copy link
Copy Markdown
Collaborator Author

stickz commented Jan 6, 2022

I reverted the ruTorrentConfig class. A configuration loader can be added in anther pull request. This is beyond the intended scope of this pull request. The purpose here is to remove functions in php/util.php from the global scope.

Comment thread php/utility/json.php
@Novik Novik merged commit 0d44ec9 into Novik:master Jan 9, 2022
@Novik Novik mentioned this pull request Jan 9, 2022
stickz added a commit to stickz/ruTorrent that referenced this pull request Jan 12, 2022
commit 319977d
Merge: e8f0fef 6cc49f4
Author: Novik <Novik@users.noreply.github.com>
Date:   Wed Jan 12 15:52:14 2022 +0300

    Merge pull request Novik#2261 from njutn95/patch-1

    Fix the typo/syntax error in the static method call

commit 6cc49f4
Author: njutn95 <njutn95@yahoo.com>
Date:   Wed Jan 12 13:01:27 2022 +0100

    Fix the typo/syntax error in the static method call

commit e8f0fef
Merge: cc8d4b1 13a75f5
Author: Novik <Novik@users.noreply.github.com>
Date:   Mon Jan 10 14:43:22 2022 +0300

    Merge pull request Novik#2254 from Micdu70/patch-7

    Fix task.php

commit 13a75f5
Author: Micdu70 <Micdu70@gmx.fr>
Date:   Mon Jan 10 12:42:10 2022 +0100

    Fix task.php

commit cc8d4b1
Merge: 25580d7 fec4bdd
Author: Novik <Novik@users.noreply.github.com>
Date:   Mon Jan 10 12:36:59 2022 +0300

    Merge pull request Novik#2253 from stickz/Namespace-Fix

    Fix loading utility classes from namespaces

commit fec4bdd
Author: stickz <stickman002@mail.com>
Date:   Mon Jan 10 04:01:07 2022 -0500

    Fix PHP Notice

commit 25580d7
Author: Novik <novik65@gmail.com>
Date:   Mon Jan 10 11:28:42 2022 +0300

    fix context menu error

commit 2897543
Merge: 795bd19 bcea8a6
Author: Novik <Novik@users.noreply.github.com>
Date:   Mon Jan 10 11:07:21 2022 +0300

    Merge pull request Novik#2252 from Micdu70/patch-11

    Add 'copyToClipboard' function in common.js + a few fixes

commit d92e194
Author: stickz <stickman002@mail.com>
Date:   Mon Jan 10 02:53:31 2022 -0500

    Go to end of exploded array instead

commit bcea8a6
Author: Micdu70 <Micdu70@gmx.fr>
Date:   Sat Jan 8 19:04:30 2022 +0100

    Add 'copyToClipboard' function in common.js + a few fixes

commit d12228d
Author: stickz <stickman002@mail.com>
Date:   Mon Jan 10 01:27:00 2022 -0500

    Fix loading utility classes

commit 795bd19
Merge: 0d44ec9 27e4882
Author: Novik <Novik@users.noreply.github.com>
Date:   Sun Jan 9 14:20:14 2022 +0300

    Merge pull request Novik#2241 from stickz/MemoryLeakFixes

     Improve memory management

commit 0d44ec9
Merge: fd35e22 db16d33
Author: Novik <Novik@users.noreply.github.com>
Date:   Sun Jan 9 14:07:37 2022 +0300

    Merge pull request Novik#2247 from stickz/Utility2

    Refactor code & implement autoloader

commit db16d33
Author: stickz <stickman002@mail.com>
Date:   Sat Jan 8 18:49:52 2022 -0500

    Fix json.php

commit fd35e22
Merge: be26ce3 0d91672
Author: Novik <Novik@users.noreply.github.com>
Date:   Fri Jan 7 14:24:08 2022 +0300

    Merge pull request Novik#2251 from Micdu70/patch-11

    Fix 'check_port' and 'extsearch' plugins

commit 0d91672
Author: Micdu70 <Micdu70@gmx.fr>
Date:   Fri Jan 7 10:46:15 2022 +0100

    Fix 'check_port' and 'extsearch' plugins

commit d815e75
Author: stickz <stickman002@mail.com>
Date:   Thu Jan 6 16:53:14 2022 -0500

    Fix third party plugin compatibility

commit 2ec6b86
Author: stickz <stickman002@mail.com>
Date:   Thu Jan 6 16:22:21 2022 -0500

    Remove $user arg for paths

commit 3f60a43
Author: stickz <stickman002@mail.com>
Date:   Thu Jan 6 16:12:12 2022 -0500

    Remove ruTorrentConfig

commit df7c0d0
Author: stickz <stickman002@mail.com>
Date:   Thu Jan 6 16:09:17 2022 -0500

    Fix whitespaces

commit 9f3aee8
Author: stickz <stickman002@mail.com>
Date:   Thu Jan 6 16:06:41 2022 -0500

    Revert ruTorrentConfig class

commit be26ce3
Merge: 9be549f 28fa12c
Author: Novik <Novik@users.noreply.github.com>
Date:   Mon Jan 3 13:20:19 2022 +0300

    Merge pull request Novik#2249 from Micdu70/patch-11

    Fix Novik#2248

commit 28fa12c
Author: Micdu70 <Micdu70@gmx.fr>
Date:   Mon Jan 3 10:25:41 2022 +0100

    Fix Novik#2248

commit c63b61a
Author: stickz <stickman002@mail.com>
Date:   Sun Jan 2 21:55:53 2022 -0500

    Refactor more code

commit 6429984
Author: stickz <stickman002@mail.com>
Date:   Sun Jan 2 13:03:15 2022 -0500

    Refactor code

commit 27e4882
Author: stickz <stickman002@mail.com>
Date:   Thu Dec 30 01:20:31 2021 -0500

     Improve memory management
@stickz stickz deleted the Utility2 branch January 13, 2022 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants