-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Separated install process + autoloader. First approach
- Loading branch information
1 parent
526441b
commit d91b736
Showing
14 changed files
with
584 additions
and
429 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Simple autoloader, so we don't need Composer just for this. | ||
*/ | ||
class Autoloader | ||
{ | ||
public static function register() | ||
{ | ||
spl_autoload_register(function ($class) { | ||
$className = str_replace('PrestaShop\\Module\\Doofinder\\', '', $class); | ||
$file = _PS_MODULE_DIR_ . 'doofinder/' . self::uncapitalize(str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php'); | ||
if (file_exists($file)) { | ||
require_once $file; | ||
|
||
return true; | ||
} elseif (file_exists(self::pascalCaseToSnakeCase($file))) { | ||
$file = self::pascalCaseToSnakeCase($file); | ||
require_once $file; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
} | ||
|
||
private static function pascalCaseToSnakeCase($path) | ||
{ | ||
$parts = explode('/', $path); | ||
$filename = array_pop($parts); | ||
// Separate PascalCase words and convert to snake_case without the leading empty element. | ||
$snakeCase = strtolower(ltrim(implode('_', preg_split('/(?=[A-Z])/', basename($filename, '.php'))), '_')); | ||
|
||
return implode('/', $parts) . '/' . $snakeCase . '.php'; | ||
} | ||
|
||
private static function uncapitalize($text) | ||
{ | ||
return strtolower($text[0]) . substr($text, 1); | ||
} | ||
} | ||
Autoloader::register(); |
Oops, something went wrong.