Skip to content

Commit

Permalink
Update core classes with strict types and modern PHP features
Browse files Browse the repository at this point in the history
The commit modernizes core PHP classes with:
- Add strict type declarations and return type hints
- Convert to match expressions and null coalescing operators
- Add proper type hints for properties and parameters
- Improve error handling and null safety
- Optimize database operations and file handling
- Add namespace declarations and use statements
- Clean up code formatting and documentation
- Remove redundant conditionals and simplify logic
  • Loading branch information
N6REJ committed Dec 17, 2024
1 parent 4229632 commit cef129d
Show file tree
Hide file tree
Showing 28 changed files with 3,708 additions and 4,425 deletions.
Binary file modified bearsampp.exe
Binary file not shown.
39 changes: 27 additions & 12 deletions core/classes/class.autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
* Github: https://github.com/Bearsampp
*/

declare(strict_types=1);

namespace Core\Classes;

/**
* Class Autoloader
*
* This class handles the autoloading of classes within the Bearsampp application.
* Handles the autoloading of classes within the Bearsampp application.
* It registers itself with the SPL autoload stack and loads classes based on naming conventions.
*/
class Autoloader
Expand All @@ -30,31 +34,42 @@ public function __construct()
* @param string $class The name of the class to load.
* @return bool True if the class file was successfully loaded, false otherwise.
*/
public function load($class)
public function load(string $class): bool
{
global $bearsamppRoot;

// Ensure the Util class is loaded
if (!class_exists('Util')) {
$utilFile = $bearsamppRoot->getCorePath() . '/classes/class.util.php';
if (file_exists($utilFile)) {
require_once $utilFile;
} else {
return false;
}
}

$class = strtolower($class);
$rootPath = $bearsamppRoot->getCorePath();

$file = $rootPath . '/classes/class.' . $class . '.php';

if (Util::startWith($class, 'bin')) {
$class = $class != 'bins' ? substr_replace($class, '.', 3, 0) : $class;
$class = ($class !== 'bins') ? substr_replace($class, '.', 3, 0) : $class;
$file = $rootPath . '/classes/bins/class.' . $class . '.php';
} elseif (Util::startWith($class, 'tool')) {
$class = $class != 'tools' ? substr_replace($class, '.', 4, 0) : $class;
$class = ($class !== 'tools') ? substr_replace($class, '.', 4, 0) : $class;
$file = $rootPath . '/classes/tools/class.' . $class . '.php';
} elseif (Util::startWith($class, 'app')) {
$class = $class != 'apps' ? substr_replace($class, '.', 3, 0) : $class;
$class = ($class !== 'apps') ? substr_replace($class, '.', 3, 0) : $class;
$file = $rootPath . '/classes/apps/class.' . $class . '.php';
} elseif (Util::startWith($class, 'action')) {
$class = $class != 'action' ? substr_replace($class, '.', 6, 0) : $class;
$class = ($class !== 'actions') ? substr_replace($class, '.', 6, 0) : $class;
$file = $rootPath . '/classes/actions/class.' . $class . '.php';
} elseif (Util::startWith($class, 'tplapp') && $class != 'tplapp') {
} elseif (Util::startWith($class, 'tplapp') && $class !== 'tplapp') {
$class = substr_replace(substr_replace($class, '.', 3, 0), '.', 7, 0);
$file = $rootPath . '/classes/tpls/app/class.' . $class . '.php';
} elseif (Util::startWith($class, 'tpl')) {
$class = $class != 'tpls' ? substr_replace($class, '.', 3, 0) : $class;
$class = ($class !== 'tpls') ? substr_replace($class, '.', 3, 0) : $class;
$file = $rootPath . '/classes/tpls/class.' . $class . '.php';
}

Expand All @@ -71,18 +86,18 @@ public function load($class)
*
* @return bool True on success, false on failure.
*/
public function register()
public function register(): bool
{
return spl_autoload_register(array($this, 'load'));
return spl_autoload_register([$this, 'load']);
}

/**
* Unregisters the autoloader from the SPL autoload stack.
*
* @return bool True on success, false on failure.
*/
public function unregister()
public function unregister(): bool
{
return spl_autoload_unregister(array($this, 'load'));
return spl_autoload_unregister([$this, 'load']);
}
}
Loading

0 comments on commit cef129d

Please sign in to comment.