Skip to content

Commit

Permalink
Adding option for updating a single child of a controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
elsemieni committed Oct 6, 2020
1 parent 422a658 commit 087ce36
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
41 changes: 28 additions & 13 deletions core/WebGenerator.php
Expand Up @@ -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.
Expand Down Expand Up @@ -58,24 +64,25 @@ 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"));

//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;
}
}
Expand Down Expand Up @@ -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;

Expand All @@ -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]);
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions cron.php
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
14 changes: 13 additions & 1 deletion docs/pages.md
Expand Up @@ -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.

0 comments on commit 087ce36

Please sign in to comment.