From 087ce364382d83f986b033f696a3bf23bb617efa Mon Sep 17 00:00:00 2001 From: elsemieni Date: Mon, 5 Oct 2020 21:45:30 -0300 Subject: [PATCH] Adding option for updating a single child of a controller. --- core/WebGenerator.php | 41 ++++++++++++++++++++++++++++------------- cron.php | 15 ++++++++++++--- docs/pages.md | 14 +++++++++++++- 3 files changed, 53 insertions(+), 17 deletions(-) diff --git a/core/WebGenerator.php b/core/WebGenerator.php index f535b86..2d50134 100644 --- a/core/WebGenerator.php +++ b/core/WebGenerator.php @@ -20,6 +20,12 @@ class WebGenerator static $DYNAMIC = 0; static $STATIC = 1; + /** + * An alias to refer that the user wants to update the default child on a controller + * @var string + */ + static $DEFAULT_CHILD_ALIAS = "//__default__//"; + /** * Generate and/or update all static and dynamic pages, depending of their status. * @param bool $force If true, all pages will be regenerated. @@ -58,10 +64,11 @@ static function generate($force = false) * Search and generate a single controller, if exists. * @param String $controller The controller name, as defined in routes.php * @param bool $force If true, it will be generated regardless if it already exists or not + * @param string $child If defined, it will only update the specified child in controller. * @return bool True if the controller is updated. False if controller does not exist. * @throws Exception */ - static function generateSingle($controller, $force = false) + static function generateSingle($controller, $force = false, $child = NULL) { global $_RPGMAKERES; include_once(RPGMakerES::getRootFolder("routes.php")); @@ -69,13 +76,13 @@ static function generateSingle($controller, $force = false) //seek if the desired controller is in one of the routes path. If so, then generate it. foreach ($_RPGMAKERES["dynamicPages"] as $page) { if ($page == $controller) { - WebGenerator::_processPages(WebGenerator::$DYNAMIC, $page, $force); + WebGenerator::_processPages(WebGenerator::$DYNAMIC, $page, $force, 0, $child); return true; } } foreach ($_RPGMAKERES["staticPages"] as $page => $minutes) { if ($page == $controller) { - WebGenerator::_processPages(WebGenerator::$STATIC, $page, $force, $minutes); + WebGenerator::_processPages(WebGenerator::$STATIC, $page, $force, $minutes, $child); return true; } } @@ -121,10 +128,11 @@ static function deleteControllerPages($controllerName) * @param int $mode WebGenerator::$DYNAMIC or WebGenerator::$STATIC. * @param String $page Controller name, as configured in routes.php * @param bool $force If true, all pages of the controller will be regenerated - * @param null $timeout Optional, Static only. If > 0, pages of the controller will be regenerated after x minutes. + * @param int $timeout Optional, Static only. If > 0, pages of the controller will be regenerated after x minutes. + * @param string $child If defined, it will only update the specified child in controller. * @throws Exception If there's issues with file permissions. */ - static function _processPages($mode, $page, $force, $timeout = null) + static function _processPages($mode, $page, $force, $timeout = null, $childForcing = NULL) { global $_RPGMAKERES; @@ -150,7 +158,7 @@ static function _processPages($mode, $page, $force, $timeout = null) //fine, folders are created. Now do the page procedure for default page $filePath = RPGMakerES::getRootfolder("public/" . $currentPath . "/index." . ($mode == WebGenerator::$DYNAMIC ? "php" : "html")); - if (!@file_exists($filePath) || $force) { + if ((!@file_exists($filePath) || $force ) && !$childForcing || $childForcing == WebGenerator::$DEFAULT_CHILD_ALIAS ) { switch ($mode) { case WebGenerator::$DYNAMIC: WebGenerator::createDyn($filePath, $page, $default_method[0], $default_method[1]); @@ -159,6 +167,8 @@ static function _processPages($mode, $page, $force, $timeout = null) WebGenerator::createStatic($filePath, $page, $default_method[0], $default_method[1]); break; } + //if my intention is just generate the default child then I'm done. + if ($childForcing == WebGenerator::$DEFAULT_CHILD_ALIAS ) return; } else if ($mode == WebGenerator::$STATIC && !!$timeout ) { //I can calculate timeout for this one @@ -186,13 +196,18 @@ static function _processPages($mode, $page, $force, $timeout = null) //create child $filePath = RPGMakerES::getRootfolder("public/" . $currentPath . $child[0] . "/index." . ($mode == WebGenerator::$DYNAMIC ? "php" : "html")); if (!@file_exists($filePath) || $force) { - switch ($mode) { - case WebGenerator::$DYNAMIC: - WebGenerator::createDyn($filePath, $page, $child[1], $child[2]); - break; - case WebGenerator::$STATIC: - WebGenerator::createStatic($filePath, $page, $child[1], $child[2]); - break; + //if there's no child forcing, continue. But if there's child forcing, just continue if it's the chosen one. + if (!$childForcing || (!!$childForcing && $childForcing == $child[0])) { + switch ($mode) { + case WebGenerator::$DYNAMIC: + WebGenerator::createDyn($filePath, $page, $child[1], $child[2]); + break; + case WebGenerator::$STATIC: + WebGenerator::createStatic($filePath, $page, $child[1], $child[2]); + break; + } + //if there's child forcing, then I'm done. + if (!!$child) return; } } else if ($mode == WebGenerator::$STATIC && !!$timeout ) { //I can calculate timeout for this one diff --git a/cron.php b/cron.php index 5399c60..a23438b 100644 --- a/cron.php +++ b/cron.php @@ -9,7 +9,7 @@ if (RPGMakerES::isBrowser()) exit(); global $_RPGMAKERES; -$options = getopt("hvufw:d:m:", ["help", "version", "update", "force", "write:", "delete:", "mailtest:"]); +$options = getopt("hvufw:d:m:c:", ["help", "version", "update", "force", "write:", "delete:", "mailtest:", "child:"]); echo "RPG Maker ES Core version " . $_RPGMAKERES["config"]["version"] . PHP_EOL; @@ -27,7 +27,8 @@ "-f --force If enabled, all sections will be updated, regardless if these are up-to-date" . PHP_EOL . "-w=CONTROLLER --write=CONTROLLER Writes a single section of the site, specified by a CONTROLLER name" . PHP_EOL . "-d=CONTROLLER --delete=CONTROLLER Deletes pages from a controller, specified by a CONTROLLER name" . PHP_EOL . - "-m=EMAIL --mailtest=EMAIL Test email sending, by sending an email to EMAIL address" . PHP_EOL . PHP_EOL . + "-m=EMAIL --mailtest=EMAIL Test email sending, by sending an email to EMAIL address" . PHP_EOL . + "-c=EMAIL --child=CHILDNAME If this option is defined when writing, it will process only this child" . PHP_EOL . PHP_EOL . "We still didn't know what the hell Entidad2 is."; exit(0); @@ -57,8 +58,16 @@ if (array_key_exists("w", $options)) $value = $options["w"]; if (array_key_exists("write", $options)) $value = $options["write"]; + $child = NULL; + if (array_key_exists("c", $options)) $child = $options["c"]; + if (array_key_exists("child", $options)) $child = $options["child"]; + + if ($child) { + echo "Updating only " . $child . PHP_EOL; + } + echo "Updating ... " . PHP_EOL; - if (WebGenerator::generateSingle($value, $force)) echo PHP_EOL . "Done!" . PHP_EOL; + if (WebGenerator::generateSingle($value, $force, $child)) echo PHP_EOL . "Done!" . PHP_EOL; else echo PHP_EOL . "Controller not found" . PHP_EOL; exit(0); } diff --git a/docs/pages.md b/docs/pages.md index 9c22766..1ae202d 100644 --- a/docs/pages.md +++ b/docs/pages.md @@ -82,8 +82,20 @@ Do you remember when I said in **routes.php** part that you can update static pa Where ``controllerName`` is the name of the controller as you defined it on **routes.php** file, and that ``true`` ... leave at it is (It means it will be be updated even if the page already exists on the public www folder). +If you want to update **ONLY one child** of your page, it's not necessary that you need to rebuild the whole controller for this. Instead, use this: + +``WebGenerator::generateSingle(controllerName, true, route);`` + +The same as above, but with an extra parameter: The child route name. + +**NOTE:** If you want to update the default one, use ``//__default__// `` as route name. + Of course, you can update it too from CLI. -``php cron.php --write controllerName --force`` +``php cron.php --write=controllerName --force`` + +or + +``php cron.php --write=controllerName --force --child=childRoute`` Same parameters as above. \ No newline at end of file