OMUtils.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. $className = str_replace('/', '\\', ltrim("{$item->getPathname()}/{$moduleName}", $path));
  41. // Load only PHP file with same name of namespace ex.: \Lamborghini\Lamborghini.php
  42. $moduleInitPath = "{$item->getPathname()}/{$moduleName}.php";
  43. if (file_exists($moduleInitPath)) { // If already imported, skip
  44. if (!isset($$moduleName)) {
  45. Global $$moduleName; // Declare visibility to Global
  46. $$moduleName = new $className(); // Instantiate the Module, all init procedure must reside in __constructor() method
  47. }
  48. }
  49. }
  50. }
  51. /**
  52. * Use F3 template resolve() to parse string like "{{@BASESITE}}"
  53. *
  54. * @param string $strTemplate the F3 original string
  55. * @return string converted string
  56. */
  57. public function getStringFromTemplate(string $strTemplate)
  58. {
  59. return \Template::instance()->resolve($strTemplate);
  60. }
  61. /**
  62. * 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()
  63. *
  64. * @param string $path
  65. * @throws \Exception
  66. */
  67. public function LoadEncryptedConfig(string $path)
  68. {
  69. if(!file_exists($path))
  70. throw new \Exception('Config file not found');
  71. $tmpFilepath = tempnam(sys_get_temp_dir(), 'om');
  72. $config = gzuncompress(base64_decode(file_get_contents($path)));
  73. file_put_contents($tmpFilepath, $config);
  74. \F3::config($tmpFilepath, true);
  75. unlink($tmpFilepath);
  76. }
  77. public function DieByOldPhpVersion()
  78. {
  79. if (version_compare(PHP_VERSION, '7.2') != 1)
  80. throw new \Exception('PHP 7.2+ is need to run the project');
  81. }
  82. }