diff --git a/Application/ApplicationManager.php b/Application/ApplicationManager.php index 7e1d15af1..7ae297594 100644 --- a/Application/ApplicationManager.php +++ b/Application/ApplicationManager.php @@ -1,48 +1,140 @@ moduleManager = $moduleManager; + } + + /** + * Load info of application. + * + * @param string $appPath Application path + * + * @return ApplicationInfo + * + * @since 1.0.0 + */ + private function loadInfo(string $appPath): ApplicationInfo + { + $path = \realpath($appPath); + + if ($path === false) { + throw new PathException($appPath); + } + + $info = new ApplicationInfo($path); + $info->load(); + + return $info; + } + + /** + * Install the application + * + * @param string $source Source of the application + * @param string $destination Destination of the application + * + * @return void + * + * @since 1.0.0 + */ public function install(string $source, string $destination) : void { + if (\file_exists($destination) || !\file_exists($source)) { + return; + } + $app = $this->loadInfo(\rtrim('/\\', $source) . '/info.json'); + $this->applications[$app->getInternalName()] = $app; + + $this->installFiles($source, $destination); + $this->installFromModules($app); } - public function installModules() : void + /** + * Install the files to the destination + * + * @param string $source Source path + * @param string $destination Destination of the application + * + * @return void + * + * @since 1.0.0 + */ + private function installFiles(string $source, string $destination) : void { - + Directory::copy($source, $destination); } - public function installConfig() : void + /** + * Install routes and hooks from modules for application + * + * @param ApplicationInfo $info Application info + * + * @return void + * + * @since 1.0.0 + */ + public function installFromModules(ApplicationInfo $info) : void { - - } - - public function update(string $source, string $destination) : void - { - + $installed = $this->moduleManager->getInstalledModules(); + foreach ($installed as $module => $moduleInfo) { + $this->moduleManager->reInit($module, $info); + } } } diff --git a/Module/InstallerAbstract.php b/Module/InstallerAbstract.php index d049ff6b0..46e489435 100644 --- a/Module/InstallerAbstract.php +++ b/Module/InstallerAbstract.php @@ -204,9 +204,21 @@ abstract class InstallerAbstract foreach ($directories as $child) { if ($child instanceof Directory) { foreach ($child as $file) { + if (!\file_exists(__DIR__ . '/../../' . $child->getName() . '/' . \basename($file->getName(), '.php')) + || ($appInfo !== null && \basename($file->getName(), '.php') !== $appInfo->getInternalName()) + ) { + continue; + } + self::installRoutes(__DIR__ . '/../../' . $child->getName() . '/' . \basename($file->getName(), '.php') . '/Routes.php', $file->getPath()); } } elseif ($child instanceof File) { + if (!\file_exists(__DIR__ . '/../../' . $child->getName()) + || ($appInfo !== null && \basename($child->getName(), '.php') !== $appInfo->getInternalName()) + ) { + continue; + } + self::installRoutes(__DIR__ . '/../../' . $child->getName() . '/Routes.php', $child->getPath()); } } @@ -271,9 +283,21 @@ abstract class InstallerAbstract foreach ($directories as $key => $child) { if ($child instanceof Directory) { foreach ($child as $key2 => $file) { + if (!\file_exists(__DIR__ . '/../../' . $child->getName() . '/' . \basename($file->getName(), '.php')) + || ($appInfo !== null && \basename($file->getName(), '.php') !== $appInfo->getInternalName()) + ) { + continue; + } + self::installHooks(__DIR__ . '/../../' . $child->getName() . '/' . \basename($file->getName(), '.php') . '/Hooks.php', $file->getPath()); } } elseif ($child instanceof File) { + if (!\file_exists(__DIR__ . '/../../' . $child->getName()) + || ($appInfo !== null && \basename($child->getName(), '.php') !== $appInfo->getInternalName()) + ) { + continue; + } + self::installHooks(__DIR__ . '/../../' . $child->getName() . '/Hooks.php', $child->getPath()); } } diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index bcb587e32..0b0458083 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace phpOMS\Module; use phpOMS\Application\ApplicationAbstract; +use phpOMS\Application\ApplicationInfo; use phpOMS\Autoloader; use phpOMS\DataStorage\Database\Query\Builder; use phpOMS\Message\Http\HttpRequest; @@ -446,7 +447,8 @@ final class ModuleManager /** * Re-init module. * - * @param string $module Module name + * @param string $module Module name + * @param ApplicationInfo $appInfo Application info * * @return void * @@ -454,7 +456,7 @@ final class ModuleManager * * @since 1.0.0 */ - public function reInit(string $module) : void + public function reInit(string $module, ApplicationInfo $appInfo = null) : void { $info = $this->loadInfo($module); $class = '\\Modules\\' . $info->getDirectory() . '\\Admin\\Installer'; @@ -464,7 +466,7 @@ final class ModuleManager } /** @var $class InstallerAbstract */ - $class::reInit($info); + $class::reInit($info, $appInfo); } /**