apisnetworks / apiscp

ApisCP Issues Tracker
https://apiscp.com
15 stars 1 forks source link

Hooks & actions #31

Closed anatolinicolae closed 4 years ago

anatolinicolae commented 4 years ago

Would be cool if there were hooks and actions à la WordPress kind of stuff, allowing us to add filters and hook pieces of code to the panel during command execution.

An example could be PR #3 which could've been otherwise implemented with an action hook similar to this:

// Apply rewrite structure
add_action('wordpress_install', function ($docroot, ...)
{
    $ret = \Wordpress_Module::execCommand($docroot, "rewrite structure '/%%postname%%/'");

    if (!$ret['success']) {
        return error('failed to set rewrite structure, error: %s', coalesce($ret['stderr'], $ret['stdout']));
    }

    return true;
});
msaladna commented 4 years ago

Surrogates provide the same functionality.

Drop this in lib/modules/surrogates/wordpress.php, then restart the panel:

<?php declare(strict_types=1);

class Wordpress_Module_Surrogate extends Wordpress {
    public function install(string $hostname, string $path = '', array $opts = array()): bool
        if (!parent::install($hostname, $path, $opts)) {
            return false;
        }
        // post-processing
        $docroot = $this->getAppRoot($hostname, $path);
        $this->execCommand($docroot, "rewrite structure '/%%postname%%/'");

        return $ret['success'] ?: error('failed to set rewrite structure, error: %s', coalesce($ret['stderr'], $ret['stdout']));
    }
}

With this approach you're not violating class visibility nor API ACLs and have access to all attributes of the module so long as they're not marked private in the parent class.