OMUtils.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * @author Open Mind
  4. * @date 26/04/2018
  5. * @time 11:11
  6. */
  7. namespace OpenMind\System;
  8. use FilesystemIterator;
  9. use OpenMind\Web\Web;
  10. use RecursiveDirectoryIterator;
  11. use RecursiveIteratorIterator;
  12. class OMUtils
  13. {
  14. public static function getInstance()
  15. {
  16. static $instance = null;
  17. if ($instance === null) {
  18. $instance = new static(); // PHP 5+ lazy state building
  19. }
  20. return $instance;
  21. }
  22. /**
  23. * Load all modules in <$path> folder
  24. * @param string $path
  25. */
  26. public function PluginLoader(string $path)
  27. {
  28. // <class priority>
  29. //TODO , if is necessary to give a priority to some classes before other, initialize here
  30. Global $System, $Web;
  31. $System = new System(); // we need System Base class first, so will force to load as first
  32. $Web = new Web();
  33. // </class priority>
  34. $Directory = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
  35. $Iterator = new RecursiveIteratorIterator($Directory, RecursiveIteratorIterator::CHILD_FIRST);
  36. foreach($Iterator as $item) {
  37. if (is_file($item->getPathname()))
  38. continue;
  39. $moduleName = $item->getFilename();
  40. $moduleInitPath = "{$item->getPathname()}/{$moduleName}.php";
  41. $className = str_replace('/', '\\', ltrim("{$item->getPathname()}/{$moduleName}", $path));
  42. if (file_exists($moduleInitPath)) { // If already imported, skip
  43. if (!isset($$moduleName)) {
  44. Global $$moduleName; // Declare visibility to Global
  45. $$moduleName = new $className(); // Instantiate the Module, all init procedure must reside in __constructor() method
  46. }
  47. }
  48. }
  49. }
  50. /**
  51. * Use F3 template resolve() to parse string like "{{@BASESITE}}"
  52. *
  53. * @param string $strTemplate the F3 original string
  54. * @return string converted string
  55. */
  56. public function getStringFromTemplate(string $strTemplate)
  57. {
  58. return \Template::instance()->resolve($strTemplate);
  59. }
  60. /**
  61. * Load an encrypted file with the method "base64_encode(gzcompress(<path to config.ini>))", save it in a temporary file in temp folder of system and load with F3::config()
  62. *
  63. * @param string $path
  64. */
  65. public function LoadEncryptedConfig(string $path)
  66. {
  67. $tmpFilepath = tempnam(sys_get_temp_dir(), 'om');
  68. $config = gzuncompress(base64_decode(file_get_contents($path)));
  69. file_put_contents($tmpFilepath, $config);
  70. \F3::config($tmpFilepath, true);
  71. unlink($tmpFilepath);
  72. }
  73. public function DieByOldPhpVersion()
  74. {
  75. if (version_compare(PHP_VERSION, '7.2') != 1)
  76. die('PHP 7.2+ is need to run the project');
  77. }
  78. }