Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Oct 1, 2024
2 parents a83f903 + 61f7a9d commit 20113e5
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 57 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ on:
PHP_COMMAND:
type: string
required: false

# php command to be used to execute get-php.php
# get-php needs PHP8+ so if the default php command of the operating
# system is not PHP8+ you need to set this variable to a php8+ command
# Example: GET_PHP_COMMAND: "/user/bin/php8.1-cli"
GET_PHP_COMMAND:
type: string
required: false
default: "php"
secrets:
CI_TOKEN:
required: false
Expand Down Expand Up @@ -89,7 +98,7 @@ jobs:
id: get-php
run: |
if [ -z "${{ inputs.PHP_COMMAND }}" ]; then
PHP_COMMAND=$(ssh ${{ inputs.SSH_USER }}@${{ inputs.SSH_HOST }} -p22 "php ${{ env.DEPLOY_FOLDER }}/site/modules/RockMigrations/get-php.php")
PHP_COMMAND=$(ssh ${{ inputs.SSH_USER }}@${{ inputs.SSH_HOST }} -p22 "${{ inputs.GET_PHP_COMMAND }} ${{ env.DEPLOY_FOLDER }}/site/modules/RockMigrations/get-php.php")
else
PHP_COMMAND="${{ inputs.PHP_COMMAND }}"
fi
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ jobs:
- uses: actions/checkout@v3
- name: conventional Changelog Action
id: changelog
uses: TriPSs/conventional-changelog-action@v5.1.0
uses: TriPSs/conventional-changelog-action@v5
with:
preset: "conventionalcommits"
github-token: ${{ secrets.github_token }}
git-user-email: "office@baumrock.com"

- name: create release
uses: actions/create-release@v1
Expand Down
116 changes: 89 additions & 27 deletions RockMigrations.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class RockMigrations extends WireData implements Module, ConfigurableModule
/** @var WireData */
public $fieldSuccessMessages;

private $indent = 0;

/**
* Flag that is set true when migrations are running
* @var bool
Expand Down Expand Up @@ -148,6 +150,7 @@ public function init()
wire()->addHookBefore("InputfieldForm::render", $this, "addRmHints");
wire()->addHookAfter("Modules::refresh", $this, "hookModulesRefresh");
wire()->addHookAfter("Pages::saved", $this, "resetCachesOnSave");
wire()->addHookAfter("Modules::install", $this, "hookModuleInstall");

// core enhancements
wire()->addHookProperty("Pagefile::isImage", $this, "hookIsImage");
Expand All @@ -158,6 +161,13 @@ public function init()
$this->createSnippetfiles();
}

protected function hookModuleInstall(HookEvent $event): void
{
$module = wire()->modules->get($event->arguments(0));
if (!$this->pageClassFiles($module)) return;
$this->migrateModule($module);
}

public function ready()
{
$this->hideFromGuests();
Expand Down Expand Up @@ -1068,15 +1078,15 @@ public function createTemplate($name, $data = false, $migrate = true)
/**
* This makes sure that for every classfile the corresponding template exists
*/
private function createTemplateFromClassfile(string $file, string $namespace)
{
private function createTemplateFromClassfile(
string $file,
string $namespace,
) {
$name = substr(basename($file), 0, -4);
$classname = "\\$namespace\\$name";
$tmp = new $classname();

try {
if ($this->isCLI()) $this->log("Setup Template " . $tmp::tpl);

// if the template already exists we exit early
$tpl = $this->getTemplate($tmp::tpl, true);
if ($tpl) return $tpl;
Expand All @@ -1086,7 +1096,7 @@ private function createTemplateFromClassfile(string $file, string $namespace)
'pageClass' => $classname,
'tags' => $namespace,
'fields' => ['title'],
]);
], false);

return $tpl;
} catch (\Throwable $th) {
Expand Down Expand Up @@ -2178,6 +2188,11 @@ public function hookPagefileResizedUrl(HookEvent $event): void
$event->return = $pagefile->pagefiles->url . $variation_basename;
}

public function indent(int $change = 2): void
{
$this->indent = $this->indent + $change;
}

/**
* DEPRECATED
*
Expand Down Expand Up @@ -2270,6 +2285,16 @@ public function installModule($name, $conf = [], $options = [])
if (is_string($options)) $options = ['url' => $options];
if (!$options) $options = [];

// if module is not installed do a refresh
// this is necessary sometimes (don't know why)
$unindent = false;
if (!wire()->modules->isInstalled($name)) {
$this->log("Install module $name");
$unindent = true;
$this->indent(2);
$this->refresh();
} else $this->log("Already installed $name");

$opt = $this->wire(new WireData());
/** @var WireData $opt */
$opt->setArray([
Expand Down Expand Up @@ -2312,6 +2337,8 @@ public function installModule($name, $conf = [], $options = [])
$i = 0;
while ($i++ < 10 && !wire()->modules->isInstalled($name)) $this->refresh();

if ($unindent) $this->indent(-2);

return $module;
}

Expand Down Expand Up @@ -2651,6 +2678,7 @@ public function log($msg, $throwException = true)
// this makes it possible to log a Debug::backtrace for example
// which can be handy for debugging
$msg = $this->str($msg);
$msg = str_repeat(" ", $this->indent) . $msg;

if ($this->isVerbose()) {
try {
Expand Down Expand Up @@ -2883,26 +2911,37 @@ public function migrateAfterModuleInstall(HookEvent $event)
/**
* Migrate a module and all its page classes
*/
private function migrateModule(Module $module): void
public function migrateModule(Module $module): void
{
$this->log("----- Migrate Module $module -----");
if ($module->pageClassPath) {
// if the module has a classloader attached we make sure that we migrate
// pageclasses before migrating the final module.
// that way we can define what happens in the module file - for example
// we could create several templates upfront and then in the module file
// we create pages that use those templates, which is only possible if
// those templates have been created before.
$this->log("Setup all PageClasses of this Module");

$path = $this->pageClassPath($module);

// if the module ships with custom pageclasses
// we first create all templates and in a second step
// we migrate all classes. this makes sure that if a migrate()
// of one pageclass references a template of another pageclass
// it will still work no matter which filenames the classes have.
if ($path) {
// the very first thing to do is to create all templates for pageclasses
$this->wire->classLoader->addNamespace($module->className(), $path);
foreach ($this->pageClassFiles($module) as $file) {
$this->createTemplateFromClassfile($file, $module->className());
}
}

// we trigger migrate() of the module
// this happens before all pageclasses are migrated so that we can
// create fields used by multiple pageclasses in the migrate() method
// of the module.
$this->triggerMigrate($module);

// now that the module migrate() has been triggered we can migrate
// all pageclasses.
if ($path) {
foreach ($this->pageClassFiles($module) as $file) {
$this->migratePageClass($file, $module);
}
}
$this->triggerMigrate($module);
}

/**
Expand Down Expand Up @@ -3007,7 +3046,7 @@ private function migrateWatchfile(WatchFile $file): void

if (!$file->migrate) return;
if (!$this->doMigrate($file)) {
$this->log("--- Skipping {$file->path} (no change)");
$this->log(" - Skipping {$file->path} (no change)");
return;
}

Expand All @@ -3026,7 +3065,9 @@ private function migrateWatchfile(WatchFile $file): void

// if it is a module we call $module->migrate()
if ($module = $file->module) {
$this->indent(2);
$this->migrateModule($module);
$this->indent(-2);
return;
}

Expand All @@ -3041,7 +3082,7 @@ private function migrateWatchfile(WatchFile $file): void
$this->log(" => {$file->pageClass}::migrate()");
$tmp->migrate();
}
} else $this->log("--- Skip {$file->pageClass} (no change)");
} else $this->log(" - Skip {$file->pageClass} (no change)");
return;
}

Expand Down Expand Up @@ -3136,6 +3177,7 @@ private function migrateWatchfiles($force = false)

foreach ($list as $prio => $items) {
if ($this->isCLI()) $this->log("");
$this->indent = 0;
$this->log("### Migrate items with priority $prio ###");

foreach ($items as $path) {
Expand Down Expand Up @@ -3290,10 +3332,11 @@ public function once(
/**
* Get all pageclass files of given module
*/
private function pageClassFiles(Module $module): array
public function pageClassFiles(Module|string $module): array
{
if (!$module->pageClassPath) return [];
return glob($module->pageClassPath . "*.php");
$path = $this->pageClassPath($module);
if (!$path) return [];
return glob($path . "*.php");
}

/**
Expand Down Expand Up @@ -3322,8 +3365,10 @@ public function pageClassLoader(Module $module, $folder = "classes"): void
if ($namespace !== $folder) return;

// make PW autoload all files in given path
$module->pageClassPath = $path;
$this->wire->classLoader->addNamespace($namespace, $path);
$this->wire->classLoader->addNamespace(
$namespace,
$this->pageClassPath($module)
);

// create templates for all files
$files = $this->pageClassFiles($module);
Expand All @@ -3332,6 +3377,17 @@ public function pageClassLoader(Module $module, $folder = "classes"): void
}
}

/**
* Get path to page classes of given module
*/
public function pageClassPath(Module|string $module): string|false
{
$dir = wire()->config->paths->siteModules . $module;
$path = $dir . '/pageClasses/';
if (is_dir($path)) return $path;
return false;
}

/**
* Helper method to add badges to the page list
*/
Expand Down Expand Up @@ -4172,16 +4228,22 @@ public function setOutputLevel($level)
*
* @return void
*/
public function setPageNameFromField($template, $fields = 'title')
public function setPageNameFromField($template, $fields = 'title', $condition = null)
{
if ($template instanceof Page) $template = $template->template;
$template = $this->wire->templates->get((string)$template);
if (!$template) return;
$tpl = "template=$template";
$this->addHookAfter("Pages::saved($tpl,id>0)", function (HookEvent $event) use ($fields) {
$this->addHookAfter("Pages::saved($tpl,id>0)", function (HookEvent $event) use ($fields, $condition) {
/** @var Page $page */
$page = $event->arguments(0);

// if a condition is set, only do this if the condition is met
if ($condition) {
if (!$page->matches($condition)) return;
}

// only do this once
if ($page->rmSetPageName) return;
$page->rmSetPageName = true;

Expand Down Expand Up @@ -4210,9 +4272,9 @@ public function setPageNameFromField($template, $fields = 'title')
*
* @param mixed $object
*/
public function setPageNameFromTitle($template)
public function setPageNameFromTitle($template, $condition = null)
{
return $this->setPageNameFromField($template, 'title');
return $this->setPageNameFromField($template, 'title', $condition);
}

/**
Expand Down
Loading

0 comments on commit 20113e5

Please sign in to comment.